Debian Codegraph @

documentation/manual/grammar/parser.yo

raw
1 In this section a small demo-parser is developed using polymorphic semantic
2 values. Its tt(%polymorphic) directive looks like this:
3 verb(
4 %polymorphic INT: int; TEXT: std::string;
5 )
6 Furthermore, the grammar declares tokens tt(INT) and tt(IDENTIFIER), and
7 pre-associates the tt(TEXT) tag with the tt(identifier) nonterminal,
8 associates the tt(INT) tag with the tt(int) nonterminal. The tt(combi)
9 nonterminal is associated with the generic tt(STYPE_) semantic value type:
10 verb(
11 %type <TEXT> identifier
12 %type <INT> int
13 %type <STYPE_> combi
14 )
15
16 The parser's grammar is simple, expecting input lines, formatted
17 according to the following (tt(rule)) production rule:
18 verb(
19 rule:
20 identifier '(' identifier ')' '\n'
21 |
22 identifier '=' int '\n'
23 |
24 combi '\n'
25 ;
26 )
27
28 The rules for tt(identifier) and tt(int) return, respectively, text and an
29 tt(int) value:
30 verb(
31 identifier:
32 IDENTIFIER
33 {
34 $$ = d_scanner.matched();
35 }
36 ;
37 int:
38 INT
39 {
40 $$ = d_scanner.intValue();
41 }
42 ;
43 )
44
45 These simple assignments can be used as tt(int) is pre-associated with the
46 tt(INT) tag and tt(identifier) is asociated with the tt(TEXT) tag.
47
48 The tt(combi) rule, which is used in one of the production rules of
49 `tt(rule)', accepts a single tt(int) value, as well as an identifier. So it
50 cannot be associated with a single polymorphic type. But as it is associated
51 with the generic tt(STYPE_) type, it can pass on any polymorphic value. In
52 tt(rule's) production rule the generic semantic value is then simply passed on
53 to tt(process), expecting a plain tt(STYPE_ const &). The function
54 tt(process) has to inspect the semantic value's tag to learn what
55 kind of value is stored inside the received semantic value. Here are the
56 definition of the tt(combi) nonterminal and action blocks for the tt(rule)
57 nonterminal:
58 verb(
59 combi:
60 int
61 |
62 identifier
63 ;
64
65 rule:
66 identifier '(' identifier ')' '\n'
67 {
68 cout << $1 << " " << $3 << '\n';
69 }
70 |
71 identifier '=' int '\n'
72 {
73 cout << $1 << " " << $3 << '\n';
74 }
75 |
76 combi '\n'
77 {
78 process($1);
79 }
80 ;
81 )
82
83 Note that tt(combi's) production rules do not define action blocks. The
84 standard way to handle these situations is to add tt($$ = $1) action blocks to
85 non-empty production rules not defining final action blocks. This works well
86 in the current example, but a tt(default-actions quiet) (or tt(warn)) option
87 or directive can also be used.
88
89 The function tt(process), called from tt(combi's) action block, inspects the
90 semantic value's tag to select the proper way of handling the received
91 semantic value. Here is its implementation:
92 verb(
93 void Parser::process(STYPE_ &semVal) const
94 {
95 if (semVal.tag() == Tag_::INT)
96 cout << "Saw an int-value: " << semVal.get<Tag_::INT>() << '\n';
97 else
98 cout << "Saw text: " << semVal.get<Tag_::TEXT>() << '\n';
99 }
100 )
101
102
103
104
105
106