| 1 |
Finally, we add the em(nested) rule to our bag of rule-tricks. Again, nested |
| 2 |
rules appear frequently: parenthesized expressions and compound statements are |
| 3 |
two very well known examples. These kind of rules are characterized by the |
| 4 |
fact that the nested variant is itself an example of the element appearing in |
| 5 |
the nested variant. The definition of a statement is actually a bit more |
| 6 |
complex than the definition of an expression, since the statement appearing in |
| 7 |
the compound statement is in fact an optional series of elements. Let's first |
| 8 |
have a look at the nested expression rule. Here it is, in a basic form: |
| 9 |
verb( |
| 10 |
expr: |
| 11 |
NUMBER |
| 12 |
| |
| 13 |
ID |
| 14 |
| |
| 15 |
expr '+' expr |
| 16 |
| |
| 17 |
... |
| 18 |
| |
| 19 |
'(' expr ')' |
| 20 |
; |
| 21 |
) |
| 22 |
This definition is simply characterized that the nonterminal tt(expr) |
| 23 |
appears within a set of parentheses, which is not too complex. |
| 24 |
|
| 25 |
The definition of tt(opt_statements), however, is a bit more complex. But |
| 26 |
acknowledging the fact that a tt(statement) contains among other elements a |
| 27 |
compound statement, and that a compound statement, in turn, contains |
| 28 |
tt(opt_statements) an tt(opt_statements) construction can be formulated |
| 29 |
accordingly: |
| 30 |
verb( |
| 31 |
opt_statements: // define an optional series |
| 32 |
// empty |
| 33 |
| |
| 34 |
opt_statements statement |
| 35 |
; |
| 36 |
|
| 37 |
statement: // define alternatives for `statement' |
| 38 |
expr_statement |
| 39 |
| |
| 40 |
if_statement |
| 41 |
| |
| 42 |
... |
| 43 |
| |
| 44 |
compound_statement |
| 45 |
; |
| 46 |
|
| 47 |
compound_statement: // define the compound statement itself |
| 48 |
'{' opt_statements '}' |
| 49 |
; |
| 50 |
) |
| 51 |
|