« Prev 4.8 Specifying parser value types - the ptype statement | Table of Contents | Next » 4.10 Specifying the parser module name - the module 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 grammar file must define a rule with the name Start
which will be used as
the top-level starting rule that the parser attempts to reduce.
Example:
ptype ulong; token word /[a-z]+/ << $$ = match.length; >> Start -> word << $$ = $1; >>
In the above example the Start
rule is defined to match a single word
token.
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; >>
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.
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.
« Prev 4.8 Specifying parser value types - the ptype statement | Table of Contents | Next » 4.10 Specifying the parser module name - the module statement |