1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
Grammar rules have the following characteristics:
itemization(
it() The construction
verb(
exp:
exp '+' exp
;
)
is a recursive rule definition, stating that when two tt(exp) groupings,
with a `+' token in between, have been recognized, these three elements
themselves represent an tt(exp) gouping.
it() Whitespace in rules is only used for separating symbols.
it() em(Action blocks) determining the
semantics of the rule can be inserted between the elements of production
rules. An action block looks like this:
verb(
{
C++ statements
}
)
Usually there is only one action block, defined at the end of production
rules . See section ref(ACTIONS).
it() Multiple production rules for the same grouping can be written
separately or can be separated from each other using the vertical-bar
character `|' as follows:
verb(
result:
first-production-rule
|
second-production-rule
|
...
;
)
it() Alternatively, multiple rules of the same grouping can be
defined. E.g., the previous definition of tt(result:) could also have been
defined as:
verb(
result:
first-production-rule
;
result:
second-production-rule
;
)
However, this is a potentially dangerous practice, since one of the two
tt(result) rules could also have used a misspelled rule-name (maybe the second
tt(result)) should have been tt(results). Therefore, b() generates a warning
if the same nonterminal is used repeatedly when defining production rules.
it() Production rules can be empty. This means that em(result) can match
any token, which then isn't processed by the production rule, although the
rule's nonterminal has been recognized. Such a alternative is called an
em(empty production rule). For example, here is how to define a
comma-separated sequence of zero or more tt(exp) nonterminals:
verb(
expseq:
expseq1
|
// empty
;
expseq1:
expseq1 ',' exp
|
exp
;
)
By convention, to improve the visibility of an empty production rule, it
contains the comment `tt(// empty)'.
Empty production rules may contain action blocks. Statements in
such action blocks are executed when the parser has recognized the empty
production rule. In such cases the tt(// empty) comment is omitted.
)
|