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
12 ******************************************************************************/
15 * bison language parser
17 * Written by Matyas Forstner using bison's parse-gram.y
23 /*********************************************************************
25 *********************************************************************/
29 #include "../compiler2/string.hh"
34 #include "bison_p.tab.hh"
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;
43 void yyerror(const char *s)
46 Location loc(asn1_infile, plineno);
49 ERROR("parse error: %s at line %d\n", s, bison_lineno);
52 void yywarning(const char *s)
55 Location loc(asn1_infile, plineno);
58 WARNING("%s at line %d\n", s, bison_lineno);
64 /*********************************************************************
66 *********************************************************************/
69 %output="bison_p.tab.cc"
73 /*********************************************************************
75 *********************************************************************/
84 %token GRAM_EOF 0 "end of file"
85 %token STRING "string"
88 %token PERCENT_TOKEN "%token"
89 %token PERCENT_NTERM "%nterm"
91 %token PERCENT_TYPE "%type"
92 %token PERCENT_DESTRUCTOR "%destructor {...}"
93 %token PERCENT_PRINTER "%printer {...}"
95 %token PERCENT_UNION "%union {...}"
97 %token PERCENT_LEFT "%left"
98 %token PERCENT_RIGHT "%right"
99 %token PERCENT_NONASSOC "%nonassoc"
101 %token PERCENT_PREC "%prec"
102 %token PERCENT_DPREC "%dprec"
103 %token PERCENT_MERGE "%merge"
106 /*----------------------.
107 | Global Declarations. |
108 `----------------------*/
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"
142 %token ID "identifier"
143 %token ID_COLON "identifier:"
144 %token PERCENT_PERCENT "%%"
145 %token PROLOGUE "%{...%}"
146 %token EPILOGUE "epilogue"
147 %token BRACED_CODE "{...}"
150 /*********************************************************************
151 * Semantic types of nonterminals
152 *********************************************************************/
154 %type <string_val> STRING ID ID_COLON string_as_id
155 %type <symbol> symbol
157 %type <rules> rhses.1
161 /*********************************************************************
163 *********************************************************************/
166 declarations "%%" {set_firstsymbol=true;} grammar epilogue.opt
169 /*------------------------------------.
170 | Declarations: before the first %%. |
171 `------------------------------------*/
175 | declarations declaration
182 | "%define" string_content string_content
187 | "%file-prefix" "=" string_content
189 | "%initial-action {...}"
192 | "%name-prefix" "=" string_content
194 | "%nondeterministic-parser"
195 | "%output" "=" string_content
196 | "%parse-param {...}"
198 | "%skeleton" string_content
202 | /*FIXME: Err? What is this horror doing here? */ ";"
206 precedence_declaration
209 { grammar->set_startsymbol($2); }
211 | "%destructor {...}" {set_terminal=false;} symbols.1
212 | "%printer {...}" {set_terminal=false;} symbols.1
218 "%nterm" {set_terminal=false;} symbol_defs.1
221 | "%token" {set_terminal=true;} symbol_defs.1
225 | "%type" {set_terminal=false;} TYPE symbols.1
230 precedence_declaration:
231 precedence_declarator {set_terminal=true;} type.opt symbols.1
237 precedence_declarator:
248 /* One or more nonterminals to be %typed. */
252 | symbols.1 symbol { }
255 /* One token definition. */
262 Symbol *s=grammar->get_symbol(*$1);
263 if(set_terminal) s->set_is_terminal();
268 Symbol *s=grammar->get_symbol(*$1);
269 if(set_terminal) s->set_is_terminal();
274 grammar->add_alias(*$1, *$2);
275 delete $1; delete $2;
277 | ID INT string_as_id
279 grammar->add_alias(*$1, *$3);
280 delete $1; delete $3;
284 /* One or more symbol definitions. */
287 | symbol_defs.1 symbol_def
291 /*------------------------------------------.
292 | The grammar section: between the two %%. |
293 `------------------------------------------*/
296 rules_or_grammar_declaration
297 | grammar rules_or_grammar_declaration
300 /* As a Bison extension, one can use the grammar declarations in the
301 body of the grammar. */
302 rules_or_grammar_declaration:
304 | grammar_declaration ";"
314 Symbol *s=grammar->get_symbol(*$1);
315 grammar->add_grouping(new Grouping(s, $2));
317 if(set_firstsymbol) {
318 grammar->set_firstsymbol(s);
319 set_firstsymbol=false;
325 rhs { $$=new Rules(); $$->add_r(new Rule($1)); }
326 | rhses.1 "|" rhs { $$=$1; $$->add_r(new Rule($3)); }
327 | rhses.1 ";" { $$=$1; }
332 { $$=new Symbols(); }
334 { $$=$1; $$->add_s($2); }
348 $$=grammar->get_symbol(*$1);
349 if(set_terminal) $$->set_is_terminal();
354 $$=grammar->get_symbol(*$1);
363 /* A string used as an ID: we have to keep the quotes. */
368 /* A string used for its contents. Strip the quotes. */
385 /*********************************************************************
387 *********************************************************************/
389 int bison_parse_file(const char* filename)
391 bison_in = fopen(filename, "r");
392 if(bison_in == NULL) {
393 FATAL_ERROR("Cannot open input file `%s': %s", filename, strerror(errno));
397 int retval = yyparse();