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 ******************************************************************************/
10 * bison language parser
12 * Written by Matyas Forstner using bison's parse-gram.y
18 /*********************************************************************
20 *********************************************************************/
24 #include "../compiler2/string.hh"
29 #include "bison_p.tab.hh"
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;
38 void yyerror(const char *s)
41 Location loc(asn1_infile, plineno);
44 ERROR("parse error: %s at line %d\n", s, bison_lineno);
47 void yywarning(const char *s)
50 Location loc(asn1_infile, plineno);
53 WARNING("%s at line %d\n", s, bison_lineno);
59 /*********************************************************************
61 *********************************************************************/
64 %output="bison_p.tab.cc"
68 /*********************************************************************
70 *********************************************************************/
79 %token GRAM_EOF 0 "end of file"
80 %token STRING "string"
83 %token PERCENT_TOKEN "%token"
84 %token PERCENT_NTERM "%nterm"
86 %token PERCENT_TYPE "%type"
87 %token PERCENT_DESTRUCTOR "%destructor {...}"
88 %token PERCENT_PRINTER "%printer {...}"
90 %token PERCENT_UNION "%union {...}"
92 %token PERCENT_LEFT "%left"
93 %token PERCENT_RIGHT "%right"
94 %token PERCENT_NONASSOC "%nonassoc"
96 %token PERCENT_PREC "%prec"
97 %token PERCENT_DPREC "%dprec"
98 %token PERCENT_MERGE "%merge"
101 /*----------------------.
102 | Global Declarations. |
103 `----------------------*/
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"
137 %token ID "identifier"
138 %token ID_COLON "identifier:"
139 %token PERCENT_PERCENT "%%"
140 %token PROLOGUE "%{...%}"
141 %token EPILOGUE "epilogue"
142 %token BRACED_CODE "{...}"
145 /*********************************************************************
146 * Semantic types of nonterminals
147 *********************************************************************/
149 %type <string_val> STRING ID ID_COLON string_as_id
150 %type <symbol> symbol
152 %type <rules> rhses.1
156 /*********************************************************************
158 *********************************************************************/
161 declarations "%%" {set_firstsymbol=true;} grammar epilogue.opt
164 /*------------------------------------.
165 | Declarations: before the first %%. |
166 `------------------------------------*/
170 | declarations declaration
177 | "%define" string_content string_content
182 | "%file-prefix" "=" string_content
184 | "%initial-action {...}"
187 | "%name-prefix" "=" string_content
189 | "%nondeterministic-parser"
190 | "%output" "=" string_content
191 | "%parse-param {...}"
193 | "%skeleton" string_content
197 | /*FIXME: Err? What is this horror doing here? */ ";"
201 precedence_declaration
204 { grammar->set_startsymbol($2); }
206 | "%destructor {...}" {set_terminal=false;} symbols.1
207 | "%printer {...}" {set_terminal=false;} symbols.1
213 "%nterm" {set_terminal=false;} symbol_defs.1
216 | "%token" {set_terminal=true;} symbol_defs.1
220 | "%type" {set_terminal=false;} TYPE symbols.1
225 precedence_declaration:
226 precedence_declarator {set_terminal=true;} type.opt symbols.1
232 precedence_declarator:
243 /* One or more nonterminals to be %typed. */
247 | symbols.1 symbol { }
250 /* One token definition. */
257 Symbol *s=grammar->get_symbol(*$1);
258 if(set_terminal) s->set_is_terminal();
263 Symbol *s=grammar->get_symbol(*$1);
264 if(set_terminal) s->set_is_terminal();
269 grammar->add_alias(*$1, *$2);
270 delete $1; delete $2;
272 | ID INT string_as_id
274 grammar->add_alias(*$1, *$3);
275 delete $1; delete $3;
279 /* One or more symbol definitions. */
282 | symbol_defs.1 symbol_def
286 /*------------------------------------------.
287 | The grammar section: between the two %%. |
288 `------------------------------------------*/
291 rules_or_grammar_declaration
292 | grammar rules_or_grammar_declaration
295 /* As a Bison extension, one can use the grammar declarations in the
296 body of the grammar. */
297 rules_or_grammar_declaration:
299 | grammar_declaration ";"
309 Symbol *s=grammar->get_symbol(*$1);
310 grammar->add_grouping(new Grouping(s, $2));
312 if(set_firstsymbol) {
313 grammar->set_firstsymbol(s);
314 set_firstsymbol=false;
320 rhs { $$=new Rules(); $$->add_r(new Rule($1)); }
321 | rhses.1 "|" rhs { $$=$1; $$->add_r(new Rule($3)); }
322 | rhses.1 ";" { $$=$1; }
327 { $$=new Symbols(); }
329 { $$=$1; $$->add_s($2); }
343 $$=grammar->get_symbol(*$1);
344 if(set_terminal) $$->set_is_terminal();
349 $$=grammar->get_symbol(*$1);
358 /* A string used as an ID: we have to keep the quotes. */
363 /* A string used for its contents. Strip the quotes. */
380 /*********************************************************************
382 *********************************************************************/
384 int bison_parse_file(const char* filename)
386 bison_in = fopen(filename, "r");
387 if(bison_in == NULL) {
388 FATAL_ERROR("Cannot open input file `%s': %s", filename, strerror(errno));
392 int retval = yyparse();