Debian Codegraph @

examples/c++/glr/c++-types.yy

raw
1 /* -*- C++ -*-
2 Copyright (C) 2020-2021 Free Software Foundation, Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18 /* Simplified C++ Type and Expression Grammar.
19 Written by Paul Hilfinger for Bison's test suite. */
20
21 %require "3.8"
22 %glr-parser
23 %skeleton "glr2.cc"
24 %define parse.assert
25 %define api.token.constructor
26 %header
27 %locations
28 %debug
29
30 // Custom error messages.
31 %define parse.error custom
32
33 %code requires
34 {
35 #include "ast.hh"
36 }
37
38 %define api.value.type variant
39
40 %code
41 {
42 #include <cassert>
43 #include <cctype>
44 #include <fstream>
45 #include <cstring>
46
47 // Merge two semantic values.
48 static Node
49 stmt_merge (const Node& x0, const Node& x1);
50
51 // Fetch a token.
52 static yy::parser::symbol_type
53 yylex ();
54 }
55
56 %expect-rr 1
57
58 %type <Node> TYPENAME ID stmt expr decl declarator
59 %printer { yyo << $$; } <Node>
60
61 %token
62 TYPENAME "typename"
63 ID "identifier"
64 SEMICOLON ";"
65 EQUAL "="
66 PLUS "+"
67 LPAREN "("
68 RPAREN ")"
69
70 %right "="
71 %left "+"
72
73 %%
74
75 prog : %empty
76 | prog stmt { std::cout << @2 << ": " << $2 << '\n'; }
77 ;
78
79 stmt : expr ";" %merge <stmt_merge> { $$ = $1; }
80 | decl %merge <stmt_merge>
81 | error ";" { $$ = Nterm ("<error>"); }
82 ;
83
84 expr : ID
85 | TYPENAME "(" expr ")" { $$ = Nterm ("<cast>", $3, $1); }
86 | expr "+" expr { $$ = Nterm ("+", $1, $3); }
87 | expr "=" expr { $$ = Nterm ("=", $1, $3); }
88 ;
89
90 decl : TYPENAME declarator ";"
91 { $$ = Nterm ("<declare>", $1, $2); }
92 | TYPENAME declarator "=" expr ";"
93 { $$ = Nterm ("<init-declare>", $1, $2, $4); }
94 ;
95
96 declarator
97 : ID
98 | "(" declarator ")" { $$ = $2; }
99 ;
100
101 %%
102 std::istream* input = nullptr;
103 yy::parser::location_type loc;
104
105
106 /*---------.
107 | Parser. |
108 `---------*/
109
110 // Generate a custom error message.
111 void
112 yy::parser::report_syntax_error (const context& ctx) const
113 {
114 std::cerr << ctx.location () << ": syntax error";
115 if (!ctx.lookahead ().empty ())
116 std::cerr << " on token " << ctx.lookahead ().name ();
117 {
118 enum { TOKENMAX = 10 };
119 symbol_kind_type expected[TOKENMAX];
120 int n = ctx.expected_tokens (expected, TOKENMAX);
121 if (0 < n)
122 {
123 for (int i = 0; i < n; ++i)
124 std::cerr << (i == 0 ? " (expected " : " or ")
125 << symbol_name (expected[i]);
126 std::cerr << ')';
127 }
128 }
129 std::cerr << '\n';
130 }
131
132 // Report the error to the user.
133 void
134 yy::parser::error (const location_type& l, const std::string& m)
135 {
136 std::cerr << l << ": " << m << '\n';
137 }
138
139 // Fetch the next token.
140 static yy::parser::symbol_type
141 yylex ()
142 {
143 while (true)
144 {
145 loc.step ();
146 loc += 1;
147 assert (!input->eof ());
148 switch (int c = input->get ())
149 {
150 case EOF:
151 return yy::parser::make_YYEOF (loc);
152 case '\t':
153 loc.end.column = (loc.end.column + 7) & ~7;
154 loc.step ();
155 break;
156 case ' ': case '\f':
157 loc.step ();
158 break;
159 case '\n':
160 loc.lines (1);
161 loc.end.column = 0;
162 loc.step ();
163 break;
164 case '+':
165 return yy::parser::make_PLUS (loc);
166 case '=':
167 return yy::parser::make_EQUAL (loc);
168 case '(':
169 return yy::parser::make_LPAREN (loc);
170 case ')':
171 return yy::parser::make_RPAREN (loc);
172 case ';':
173 return yy::parser::make_SEMICOLON (loc);
174 default:
175 if (isalpha (c))
176 {
177 std::string form;
178 do
179 {
180 form += static_cast<char> (c);
181 loc += 1;
182 c = input->get ();
183 }
184 while (isalnum (c) || c == '_');
185 input->unget ();
186 loc -= 1;
187 if (isupper (static_cast <unsigned char> (form[0])))
188 return yy::parser::make_TYPENAME (Term (form), loc);
189 else
190 return yy::parser::make_ID (Term (form), loc);
191 }
192 else
193 {
194 auto msg = "invalid character: " + std::string(1, static_cast<char> (c));
195 throw yy::parser::syntax_error (loc, msg);
196 }
197 }
198 }
199 }
200
201 // Merge two semantic values as an AST including both alternatives.
202 static Node
203 stmt_merge (const Node& x0, const Node& x1)
204 {
205 return Nterm ("<OR>", x0, x1);
206 }
207
208
209 /*-------.
210 | Main. |
211 `-------*/
212
213 // Parse `file` using parser `parse`.
214 int
215 process (yy::parser& parse, const std::string& file)
216 {
217 bool is_stdin = file == "-" || file.empty ();
218 if (is_stdin)
219 input = &std::cin;
220 else
221 input = new std::ifstream (file.c_str ());
222 loc.initialize (nullptr, 1, 0);
223 int status = parse ();
224 if (!is_stdin)
225 delete input;
226 return status;
227 }
228
229 int
230 main (int argc, char **argv)
231 {
232 yy::parser parse;
233
234 if (getenv ("YYDEBUG"))
235 parse.set_debug_level (1);
236
237 bool ran = false;
238 for (int i = 1; i < argc; ++i)
239 // Enable parse traces on option -p.
240 if (strcmp (argv[i], "-p") == 0)
241 parse.set_debug_level (1);
242 else
243 {
244 int status = process (parse, argv[i]);
245 ran = true;
246 if (!status)
247 return status;
248 }
249
250 if (!ran)
251 {
252 int status = process (parse, "");
253 if (!status)
254 return status;
255 }
256 return 0;
257 }