« Prev 4.10 Specifying parser value types - the ptype statement | Table of Contents | Next » 4.12 Specifying the parser start rule name - the start statement |
Rule statements create parser rules which define the grammar that will be parsed by the generated parser.
Multiple rules with the same name can be specified. Rules with the same name define a rule set for that name and act as alternatives that the parser can accept when attempting to match a reference to that rule.
The default start rule name is Start
.
This can be changed with the start
statement.
The grammar file must define a rule with the name of the start rule name which
will be used as the top-level starting rule that the parser attempts to reduce.
Rule statements are composed of the name of the rule, a ->
token, the fields
defining the rule pattern that must be matched, and a terminating semicolon or
user code block.
Example:
ptype ulong; start Top; token word /[a-z]+/ << $$ = match.length; >> Top -> word << $$ = $1; >>
In the above example the Top
rule is defined to match a single word
token.
Another example:
Start -> E1 << $$ = $1; >> E1 -> E2 << $$ = $1; >> E1 -> E1 plus E2 << $$ = $1 + $3; >> E2 -> E3 << $$ = $1; >> E2 -> E2 times E3 << $$ = $1 * $3; >> E3 -> E4 << $$ = $1; >> E3 -> E3 power E4 << $$ = pow($1, $3); >> E4 -> integer << $$ = $1; >> E4 -> lparen E1 rparen << $$ = $2; >>
This example uses the default start rule name of Start
.
A parser rule has zero or more fields on the right side of its definition.
Each of these fields is either a token name or a rule name.
A field can optionally be followed by a :
and then a field alias name.
If present, the field alias name is used to refer to the field value in user
code blocks, or if AST mode is active, the field alias name is used as the
field name in the generated AST node structure.
A field can be immediately followed by a ?
character to signify that it is
optional.
Another example:
token public; token private; token int; token ident /[a-zA-Z_][a-zA-Z_0-9]*/; token semicolon /;/; IntegerDeclaration -> Visibility? int ident:name semicolon; Visibility -> public; Visibility -> private;
In a parser rule code block, parser values for the right side fields are
accessible as $1
for the first field's parser value, $2
for the second
field's parser value, etc...
For the IntegerDeclaration
rule, the third field value can also be referred
to as ${name}
.
The $$
symbol accesses the output parser value for this rule.
The above examples demonstrate how the parser values for the rule components
can be used to produce the parser value for the accepted rule.
Parser rule code blocks are not allowed and not used when AST generation mode is active.
« Prev 4.10 Specifying parser value types - the ptype statement | Table of Contents | Next » 4.12 Specifying the parser start rule name - the start statement |