Debian Codegraph @

documentation/manual/grammar/syntax.yo

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