Merge branch 'master' of https://github.com/alovassy/titan.core
[deliverable/titan.core.git] / langviz / bison_p.y
1 /******************************************************************************
2 * Copyright (c) 2000-2015 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 ******************************************************************************/
8
9 /*
10 * bison language parser
11 *
12 * Written by Matyas Forstner using bison's parse-gram.y
13 * 20050908
14 */
15
16 %{
17
18 /*********************************************************************
19 * C(++) declarations
20 *********************************************************************/
21
22 #include <errno.h>
23 #include <stdio.h>
24 #include "../compiler2/string.hh"
25 #include "error.h"
26 #include "Grammar.hh"
27 #include "Rule.hh"
28 #include "Symbol.hh"
29 #include "bison_p.tab.hh"
30
31 extern int bison_lex();
32 extern FILE *bison_in;
33 extern int bison_lineno;
34 extern Grammar *grammar;
35 static bool set_terminal;
36 static bool set_firstsymbol;
37
38 void yyerror(const char *s)
39 {
40 /*
41 Location loc(asn1_infile, plineno);
42 loc.error("%s", s);
43 */
44 ERROR("parse error: %s at line %d\n", s, bison_lineno);
45 }
46
47 void yywarning(const char *s)
48 {
49 /*
50 Location loc(asn1_infile, plineno);
51 loc.warning("%s", s);
52 */
53 WARNING("%s at line %d\n", s, bison_lineno);
54 }
55
56
57 %}
58
59 /*********************************************************************
60 * Bison declarations
61 *********************************************************************/
62
63 %name-prefix="bison_"
64 %output="bison_p.tab.cc"
65 %defines
66 %verbose
67
68 /*********************************************************************
69 * The union-type
70 *********************************************************************/
71
72 %union {
73 string *string_val;
74 Symbol *symbol;
75 Symbols *symbols;
76 Rules *rules;
77 }
78
79 %token GRAM_EOF 0 "end of file"
80 %token STRING "string"
81 %token INT "integer"
82
83 %token PERCENT_TOKEN "%token"
84 %token PERCENT_NTERM "%nterm"
85
86 %token PERCENT_TYPE "%type"
87 %token PERCENT_DESTRUCTOR "%destructor {...}"
88 %token PERCENT_PRINTER "%printer {...}"
89
90 %token PERCENT_UNION "%union {...}"
91
92 %token PERCENT_LEFT "%left"
93 %token PERCENT_RIGHT "%right"
94 %token PERCENT_NONASSOC "%nonassoc"
95
96 %token PERCENT_PREC "%prec"
97 %token PERCENT_DPREC "%dprec"
98 %token PERCENT_MERGE "%merge"
99
100
101 /*----------------------.
102 | Global Declarations. |
103 `----------------------*/
104
105 %token
106 PERCENT_DEBUG "%debug"
107 PERCENT_DEFAULT_PREC "%default-prec"
108 PERCENT_DEFINE "%define"
109 PERCENT_DEFINES "%defines"
110 PERCENT_ERROR_VERBOSE "%error-verbose"
111 PERCENT_EXPECT "%expect"
112 PERCENT_EXPECT_RR "%expect-rr"
113 PERCENT_FILE_PREFIX "%file-prefix"
114 PERCENT_GLR_PARSER "%glr-parser"
115 PERCENT_INITIAL_ACTION "%initial-action {...}"
116 PERCENT_LEX_PARAM "%lex-param {...}"
117 PERCENT_LOCATIONS "%locations"
118 PERCENT_NAME_PREFIX "%name-prefix"
119 PERCENT_NO_DEFAULT_PREC "%no-default-prec"
120 PERCENT_NO_LINES "%no-lines"
121 PERCENT_NONDETERMINISTIC_PARSER
122 "%nondeterministic-parser"
123 PERCENT_OUTPUT "%output"
124 PERCENT_PARSE_PARAM "%parse-param {...}"
125 PERCENT_PURE_PARSER "%pure-parser"
126 PERCENT_SKELETON "%skeleton"
127 PERCENT_START "%start"
128 PERCENT_TOKEN_TABLE "%token-table"
129 PERCENT_VERBOSE "%verbose"
130 PERCENT_YACC "%yacc"
131 ;
132
133 %token TYPE "type"
134 %token EQUAL "="
135 %token SEMICOLON ";"
136 %token PIPE "|"
137 %token ID "identifier"
138 %token ID_COLON "identifier:"
139 %token PERCENT_PERCENT "%%"
140 %token PROLOGUE "%{...%}"
141 %token EPILOGUE "epilogue"
142 %token BRACED_CODE "{...}"
143
144
145 /*********************************************************************
146 * Semantic types of nonterminals
147 *********************************************************************/
148
149 %type <string_val> STRING ID ID_COLON string_as_id
150 %type <symbol> symbol
151 %type <symbols> rhs
152 %type <rules> rhses.1
153
154 %%
155
156 /*********************************************************************
157 * Grammar
158 *********************************************************************/
159
160 input:
161 declarations "%%" {set_firstsymbol=true;} grammar epilogue.opt
162 ;
163
164 /*------------------------------------.
165 | Declarations: before the first %%. |
166 `------------------------------------*/
167
168 declarations:
169 /* Nothing */
170 | declarations declaration
171 ;
172
173 declaration:
174 grammar_declaration
175 | PROLOGUE
176 | "%debug"
177 | "%define" string_content string_content
178 | "%defines"
179 | "%error-verbose"
180 | "%expect" INT
181 | "%expect-rr" INT
182 | "%file-prefix" "=" string_content
183 | "%glr-parser"
184 | "%initial-action {...}"
185 | "%lex-param {...}"
186 | "%locations"
187 | "%name-prefix" "=" string_content
188 | "%no-lines"
189 | "%nondeterministic-parser"
190 | "%output" "=" string_content
191 | "%parse-param {...}"
192 | "%pure-parser"
193 | "%skeleton" string_content
194 | "%token-table"
195 | "%verbose"
196 | "%yacc"
197 | /*FIXME: Err? What is this horror doing here? */ ";"
198 ;
199
200 grammar_declaration:
201 precedence_declaration
202 | symbol_declaration
203 | "%start" symbol
204 { grammar->set_startsymbol($2); }
205 | "%union {...}"
206 | "%destructor {...}" {set_terminal=false;} symbols.1
207 | "%printer {...}" {set_terminal=false;} symbols.1
208 | "%default-prec"
209 | "%no-default-prec"
210 ;
211
212 symbol_declaration:
213 "%nterm" {set_terminal=false;} symbol_defs.1
214 {
215 }
216 | "%token" {set_terminal=true;} symbol_defs.1
217 {
218 set_terminal=false;
219 }
220 | "%type" {set_terminal=false;} TYPE symbols.1
221 {
222 }
223 ;
224
225 precedence_declaration:
226 precedence_declarator {set_terminal=true;} type.opt symbols.1
227 {
228 set_terminal=false;
229 }
230 ;
231
232 precedence_declarator:
233 "%left"
234 | "%right"
235 | "%nonassoc"
236 ;
237
238 type.opt:
239 /* Nothing. */ { }
240 | TYPE { }
241 ;
242
243 /* One or more nonterminals to be %typed. */
244
245 symbols.1:
246 symbol { }
247 | symbols.1 symbol { }
248 ;
249
250 /* One token definition. */
251 symbol_def:
252 TYPE
253 {
254 }
255 | ID
256 {
257 Symbol *s=grammar->get_symbol(*$1);
258 if(set_terminal) s->set_is_terminal();
259 delete $1;
260 }
261 | ID INT
262 {
263 Symbol *s=grammar->get_symbol(*$1);
264 if(set_terminal) s->set_is_terminal();
265 delete $1;
266 }
267 | ID string_as_id
268 {
269 grammar->add_alias(*$1, *$2);
270 delete $1; delete $2;
271 }
272 | ID INT string_as_id
273 {
274 grammar->add_alias(*$1, *$3);
275 delete $1; delete $3;
276 }
277 ;
278
279 /* One or more symbol definitions. */
280 symbol_defs.1:
281 symbol_def
282 | symbol_defs.1 symbol_def
283 ;
284
285
286 /*------------------------------------------.
287 | The grammar section: between the two %%. |
288 `------------------------------------------*/
289
290 grammar:
291 rules_or_grammar_declaration
292 | grammar rules_or_grammar_declaration
293 ;
294
295 /* As a Bison extension, one can use the grammar declarations in the
296 body of the grammar. */
297 rules_or_grammar_declaration:
298 rules
299 | grammar_declaration ";"
300 | error ";"
301 {
302 yyerrok;
303 }
304 ;
305
306 rules:
307 ID_COLON rhses.1
308 {
309 Symbol *s=grammar->get_symbol(*$1);
310 grammar->add_grouping(new Grouping(s, $2));
311 delete $1;
312 if(set_firstsymbol) {
313 grammar->set_firstsymbol(s);
314 set_firstsymbol=false;
315 }
316 }
317 ;
318
319 rhses.1:
320 rhs { $$=new Rules(); $$->add_r(new Rule($1)); }
321 | rhses.1 "|" rhs { $$=$1; $$->add_r(new Rule($3)); }
322 | rhses.1 ";" { $$=$1; }
323 ;
324
325 rhs:
326 /* Nothing. */
327 { $$=new Symbols(); }
328 | rhs symbol
329 { $$=$1; $$->add_s($2); }
330 | rhs action
331 { $$=$1; }
332 | rhs "%prec" symbol
333 { $$=$1; }
334 | rhs "%dprec" INT
335 { $$=$1; }
336 | rhs "%merge" TYPE
337 { $$=$1; }
338 ;
339
340 symbol:
341 ID
342 {
343 $$=grammar->get_symbol(*$1);
344 if(set_terminal) $$->set_is_terminal();
345 delete $1;
346 }
347 | string_as_id
348 {
349 $$=grammar->get_symbol(*$1);
350 delete $1;
351 }
352 ;
353
354 action:
355 BRACED_CODE { }
356 ;
357
358 /* A string used as an ID: we have to keep the quotes. */
359 string_as_id:
360 STRING { $$=$1; }
361 ;
362
363 /* A string used for its contents. Strip the quotes. */
364 string_content:
365 STRING
366 {
367 delete $1;
368 }
369 ;
370
371 epilogue.opt:
372 /* Nothing. */
373 | "%%" EPILOGUE
374 {
375 }
376 ;
377
378 %%
379
380 /*********************************************************************
381 * Interface
382 *********************************************************************/
383
384 int bison_parse_file(const char* filename)
385 {
386 bison_in = fopen(filename, "r");
387 if(bison_in == NULL) {
388 FATAL_ERROR("Cannot open input file `%s': %s", filename, strerror(errno));
389 return -1;
390 }
391
392 int retval = yyparse();
393
394 fclose(bison_in);
395
396 return retval;
397 }
This page took 0.050125 seconds and 5 git commands to generate.