Trivial rules and priority

The following rule does not construct a node in the abstract syntax tree:


 <factor> ::= <unary> ;
    <unary>

but only passes along the result of subsequent instantiation of the non-terminal . The tree building part of the rule performs no additional work since the result of becomes the result of . This type of rule is frequently used to:

For example, production rules that treat all EXP operators at the same ``level:''

 
<exp> ::= <exp> "+" <exp> ;
    plus (<exp>.1, <exp>.2)
<exp> ::= <exp> "-" <exp> ;
    minus (<exp>.1, <exp>.2)
<exp> ::= <exp> "*" <exp> ;
    prod (<exp>.1, <exp>.2)

not only ignore conventional priorities, but render parsing ambiguous for equations such as:

1 - 2 - 3

which, unrestricted by the above equations, may be left associative:

- 3

or right associative:

1 -

To remove this ambiguity, we create a trivial non-terminal named which ensures that the operator prod will be given priority over the plus and minus operators:

 
...
<exp> ::= <exp> "+" <factor> ;
    plus (<exp>, <factor>)
<exp> ::= <exp> "-" <factor> ;
    minus (<exp>, <factor>)
<exp> ::= <factor> ;
    <factor>
<factor> ::= <factor> "*" <unary> ;
    prod (<factor>, <unary>)
...

We will examine other priority issues, including the treatment of parentheses, in the section on pretty printing.


Tutorial