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