4.11 Specifying a parser rule - the rule 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.

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 terms on the right side of its definition. Each of these terms is either a token name or a rule name. A term 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 semicolon;
Visibility -> public;
Visibility -> private;

In a parser rule code block, parser values for the right side terms are accessible as $1 for the first term's parser value, $2 for the second term's parser value, etc... 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.