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 lexical analyzer
17 * Written by Matyas Forstner using bison's scan-gram.l
22 %option nodefault nounput noyywrap never-interactive
24 %option prefix="bison_"
26 %{ /* ****************** C declarations ***************** */
28 #include "../compiler2/string.hh"
31 #include "bison_p.tab.hh"
35 #define lval bison_lval
37 static int convert_ucn_to_byte (char const *hex_text);
39 %} /* ***************** definitions ***************** */
41 %x SC_COMMENT SC_LINE_COMMENT SC_YACC_COMMENT
42 %x SC_STRING SC_CHARACTER
43 %x SC_AFTER_IDENTIFIER
44 %x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
45 %x SC_PRE_CODE SC_BRACED_CODE SC_PROLOGUE SC_EPILOGUE
47 letter [.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
48 id {letter}({letter}|[0-9])*
49 directive %{letter}({letter}|[0-9]|-)*
54 /* Zero or more instances of backslash-newline. Following GCC, allow
55 white space between the backslash and the newline. */
56 splice (\\[ \f\t\v]*\n)*
58 %% /* ***************** rules ************************* */
61 /* Nesting level of the current code in braces. */
64 /* Parent context state, when applicable. */
67 /* Token type to return, when applicable. */
72 /*-----------------------.
73 | Scanning white space. |
74 `-----------------------*/
76 <INITIAL,SC_AFTER_IDENTIFIER,SC_PRE_CODE>
78 /* Comments and white space. */
79 "," /*warn_at (*loc, _("stray `,' treated as white space"));*/
83 context_state = YY_START;
84 BEGIN SC_YACC_COMMENT;
87 /* #line directives are not documented, and may be withdrawn or
88 modified in future versions of Bison. */
89 ^"#line "{int}" \"".*"\"\n" {
94 /*----------------------------.
95 | Scanning Bison directives. |
96 `----------------------------*/
99 "%binary" return PERCENT_NONASSOC;
100 "%debug" return PERCENT_DEBUG;
101 "%default"[-_]"prec" return PERCENT_DEFAULT_PREC;
102 "%define" return PERCENT_DEFINE;
103 "%defines" return PERCENT_DEFINES;
104 "%destructor" token_type = PERCENT_DESTRUCTOR; BEGIN SC_PRE_CODE;
105 "%dprec" return PERCENT_DPREC;
106 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
107 "%expect" return PERCENT_EXPECT;
108 "%expect"[-_]"rr" return PERCENT_EXPECT_RR;
109 "%file-prefix" return PERCENT_FILE_PREFIX;
110 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
111 "%initial-action" token_type = PERCENT_INITIAL_ACTION; BEGIN SC_PRE_CODE;
112 "%glr-parser" return PERCENT_GLR_PARSER;
113 "%left" return PERCENT_LEFT;
114 "%lex-param" token_type = PERCENT_LEX_PARAM; BEGIN SC_PRE_CODE;
115 "%locations" return PERCENT_LOCATIONS;
116 "%merge" return PERCENT_MERGE;
117 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
118 "%no"[-_]"default"[-_]"prec" return PERCENT_NO_DEFAULT_PREC;
119 "%no"[-_]"lines" return PERCENT_NO_LINES;
120 "%nonassoc" return PERCENT_NONASSOC;
121 "%nondeterministic-parser" return PERCENT_NONDETERMINISTIC_PARSER;
122 "%nterm" return PERCENT_NTERM;
123 "%output" return PERCENT_OUTPUT;
124 "%parse-param" token_type = PERCENT_PARSE_PARAM; BEGIN SC_PRE_CODE;
125 "%prec" return PERCENT_PREC;
126 "%printer" token_type = PERCENT_PRINTER; BEGIN SC_PRE_CODE;
127 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
128 "%right" return PERCENT_RIGHT;
129 "%skeleton" return PERCENT_SKELETON;
130 "%start" return PERCENT_START;
131 "%term" return PERCENT_TOKEN;
132 "%token" return PERCENT_TOKEN;
133 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
134 "%type" return PERCENT_TYPE;
135 "%union" token_type = PERCENT_UNION; BEGIN SC_PRE_CODE;
136 "%verbose" return PERCENT_VERBOSE;
137 "%yacc" return PERCENT_YACC;
140 /*complain_at (*loc, _("invalid directive: %s"), quote (yytext));*/
145 ";" return SEMICOLON;
148 //val->symbol = symbol_get (yytext, *loc);
149 lval.string_val=new string(yytext);
150 BEGIN SC_AFTER_IDENTIFIER;
154 //lval.integer = scan_integer (yytext, 10);
157 0[xX][0-9abcdefABCDEF]+ {
158 //lval.integer = scan_integer (yytext, 16);
162 /* Characters. We don't check there is only one. */
163 "'" /* +\' */ lval.string_val=new string("'"); BEGIN SC_ESCAPED_CHARACTER;
166 "\"" lval.string_val=new string("\""); BEGIN SC_ESCAPED_STRING;
169 "%{" BEGIN SC_PROLOGUE;
171 /* Code in between braces. */
174 token_type = BRACED_CODE;
176 BEGIN SC_BRACED_CODE;
182 obstack_grow (&obstack_for_string, yytext + 1, yyleng - 2);
184 val->uniqstr = uniqstr_new (last_string);
191 static int percent_percent_count;
192 if (++percent_percent_count == 2)
194 return PERCENT_PERCENT;
198 //complain_at (*loc, _("invalid character: %s"), quote (yytext));
207 /*-----------------------------------------------------------------.
208 | Scanning after an identifier, checking whether a colon is next. |
209 `-----------------------------------------------------------------*/
211 <SC_AFTER_IDENTIFIER>
230 /*---------------------------------------------------------------.
231 | Scanning a Yacc comment. The initial `/ *' is already eaten. |
232 `---------------------------------------------------------------*/
236 "*/" BEGIN context_state;
238 <<EOF>> /*unexpected_eof (token_start, "* /");*/ BEGIN context_state;
242 /*------------------------------------------------------------.
243 | Scanning a C comment. The initial `/ *' is already eaten. |
244 `------------------------------------------------------------*/
248 "*"{splice}"/" /*STRING_GROW;*/ BEGIN context_state;
249 <<EOF>> /*unexpected_eof (token_start, "* /");*/ BEGIN context_state;
253 /*--------------------------------------------------------------.
254 | Scanning a line comment. The initial `//' is already eaten. |
255 `--------------------------------------------------------------*/
259 "\n" /*STRING_GROW;*/ BEGIN context_state;
260 {splice} /*STRING_GROW;*/
261 <<EOF>> BEGIN context_state;
265 /*------------------------------------------------.
266 | Scanning a Bison string, including its escapes. |
267 | The initial quote is already eaten. |
268 `------------------------------------------------*/
276 loc->start = token_start;
277 val->chars = last_string;
280 *lval.string_val+="\"";
285 \n /*unexpected_newline (token_start, "\"");*/ delete lval.string_val; BEGIN INITIAL;
287 <<EOF>> /*unexpected_eof (token_start, "\"");*/ delete lval.string_val; BEGIN INITIAL;
289 . *lval.string_val+=yytext;
294 /*----------------------------------------------------------.
295 | Scanning a Bison character literal, decoding its escapes. |
296 | The initial quote is already eaten. |
297 `----------------------------------------------------------*/
299 <SC_ESCAPED_CHARACTER>
303 unsigned char last_string_1;
306 loc->start = token_start;
307 val->symbol = symbol_get (last_string, *loc);
308 symbol_class_set (val->symbol, token_sym, *loc);
309 last_string_1 = last_string[1];
310 symbol_user_token_number_set (val->symbol, last_string_1, *loc);
314 *lval.string_val+="'";
319 \n /*unexpected_newline (token_start, "'");*/ delete lval.string_val; BEGIN INITIAL;
321 <<EOF>> /*unexpected_eof (token_start, "'");*/ delete lval.string_val; BEGIN INITIAL;
323 . *lval.string_val+=yytext;
326 <SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING>
328 \0 /*complain_at (*loc, _("invalid null character"));*/
332 /*----------------------------.
333 | Decode escaped characters. |
334 `----------------------------*/
336 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
339 unsigned long int c = strtoul (yytext + 1, 0, 8);
341 /*complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext))*/;
343 /*complain_at (*loc, _("invalid null character: %s"), quote (yytext))*/;
345 /*obstack_1grow (&obstack_for_string, c);*/ *lval.string_val+=c;
348 \\x[0-9abcdefABCDEF]+ {
351 c = strtoul (yytext + 2, 0, 16);
352 if (UCHAR_MAX < c || errno)
353 /*complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext))*/;
355 /*complain_at (*loc, _("invalid null character: %s"), quote (yytext))*/;
357 /*obstack_1grow (&obstack_for_string, c);*/ *lval.string_val+=c;
360 \\a /*obstack_1grow (&obstack_for_string, '\a');*/ *lval.string_val+='\a';
361 \\b /*obstack_1grow (&obstack_for_string, '\b');*/ *lval.string_val+='\b';
362 \\f /*obstack_1grow (&obstack_for_string, '\f');*/ *lval.string_val+='\f';
363 \\n /*obstack_1grow (&obstack_for_string, '\n');*/ *lval.string_val+='\n';
364 \\r /*obstack_1grow (&obstack_for_string, '\r');*/ *lval.string_val+='\r';
365 \\t /*obstack_1grow (&obstack_for_string, '\t');*/ *lval.string_val+='\t';
366 \\v /*obstack_1grow (&obstack_for_string, '\v');*/ *lval.string_val+='\v';
368 /* \\[\"\'?\\] would be shorter, but it confuses xgettext. */
369 \\("\""|"'"|"?"|"\\") /*obstack_1grow (&obstack_for_string, yytext[1]);*/ *lval.string_val+=yytext[1];
371 \\(u|U[0-9abcdefABCDEF]{4})[0-9abcdefABCDEF]{4} {
372 int c = convert_ucn_to_byte (yytext);
374 /*complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext))*/;
376 /*complain_at (*loc, _("invalid null character: %s"), quote (yytext))*/;
378 /*obstack_1grow (&obstack_for_string, c);*/ *lval.string_val+=c;
382 complain_at (*loc, _("unrecognized escape sequence: %s"), quote (yytext));
385 *lval.string_val+=yytext;
390 /*--------------------------------------------.
391 | Scanning user-code characters and strings. |
392 `--------------------------------------------*/
394 <SC_CHARACTER,SC_STRING>
396 {splice}|\\{splice}[^\n$@\[\]] /*STRING_GROW;*/
401 "'" /*STRING_GROW;*/ BEGIN context_state;
402 \n /*unexpected_newline (token_start, "'");*/ BEGIN context_state;
403 <<EOF>> /*unexpected_eof (token_start, "'");*/ BEGIN context_state;
408 "\"" /*STRING_GROW;*/ BEGIN context_state;
409 \n /*unexpected_newline (token_start, "\"");*/ BEGIN context_state;
410 <<EOF>> /*unexpected_eof (token_start, "\"");*/ BEGIN context_state;
414 /*---------------------------------------------------.
415 | Strings, comments etc. can be found in user code. |
416 `---------------------------------------------------*/
418 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
423 token_start = loc->start;
425 context_state = YY_START;
431 token_start = loc->start;
433 context_state = YY_START;
439 token_start = loc->start;
441 context_state = YY_START;
446 context_state = YY_START;
447 BEGIN SC_LINE_COMMENT;
452 /*---------------------------------------------------------------.
453 | Scanning after %union etc., possibly followed by white space. |
454 | For %union only, allow arbitrary C code to appear before the |
455 | following brace, as an extension to POSIX. |
456 `---------------------------------------------------------------*/
462 bool valid = yytext[0] == '{' || token_type == PERCENT_UNION;
463 scanner_cursor.column -= mbsnwidth (yytext, yyleng, 0);
471 //code_start = loc->start;
472 BEGIN SC_BRACED_CODE;
477 complain_at (*loc, _("missing `{' in `%s'"),
478 token_name (token_type));
479 obstack_sgrow (&obstack_for_string, "{}");
481 val->chars = last_string;
488 <<EOF>> /*unexpected_eof (scanner_cursor, "{}");*/ BEGIN INITIAL;
492 /*---------------------------------------------------------------.
493 | Scanning some code in braces (%union and actions). The initial |
494 | "{" is already eaten. |
495 `---------------------------------------------------------------*/
499 "{"|"<"{splice}"%" /*STRING_GROW;*/ braces_level++;
500 "%"{splice}">" /*STRING_GROW;*/ braces_level--;
502 bool outer_brace = --braces_level < 0;
504 /* As an undocumented Bison extension, append `;' before the last
505 brace in braced code, so that the user code can omit trailing
506 `;'. But do not append `;' if emulating Yacc, since Yacc does
509 FIXME: Bison should warn if a semicolon seems to be necessary
510 here, and should omit the semicolon if it seems unnecessary
511 (e.g., after ';', '{', or '}', each followed by comments or
512 white space). Such a warning shouldn't depend on --yacc; it
513 should depend on a new --pedantic option, which would cause
514 Bison to warn if it detects an extension to POSIX. --pedantic
515 should also diagnose other Bison extensions like %yacc.
516 Perhaps there should also be a GCC-style --pedantic-errors
517 option, so that such warnings are diagnosed as errors. */
520 if (outer_brace && token_type == BRACED_CODE && ! yacc_flag)
521 obstack_1grow (&obstack_for_string, ';');
523 obstack_1grow (&obstack_for_string, '}');
531 loc->start = code_start;
532 val->chars = last_string;
539 /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
541 "<"{splice}"<" /*STRING_GROW;*/
542 "$"("<"{tag}">")?(-?[0-9]+|"$") /*handle_dollar (token_type, yytext, *loc);*/
543 "@"(-?[0-9]+|"$") /* handle_at (token_type, yytext, *loc);*/
545 <<EOF>> /*unexpected_eof (code_start, "}");*/ BEGIN INITIAL;
549 /*--------------------------------------------------------------.
550 | Scanning some prologue: from "%{" (already scanned) to "%}". |
551 `--------------------------------------------------------------*/
558 loc->start = code_start;
559 val->chars = last_string;
565 <<EOF>> /*unexpected_eof (code_start, "%}");*/ BEGIN INITIAL;
569 /*---------------------------------------------------------------.
570 | Scanning the epilogue (everything after the second "%%", which |
571 | has already been eaten). |
572 `---------------------------------------------------------------*/
579 loc->start = code_start;
580 val->chars = last_string;
588 /*-----------------------------------------------------.
589 | By default, grow the string obstack with the input. |
590 `-----------------------------------------------------*/
592 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER>. |
593 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>\n /*STRING_GROW;*/
598 /*------------------------------------------------------------------.
599 | Convert universal character name UCN to a single-byte character, |
600 | and return that character. Return -1 if UCN does not correspond |
601 | to a single-byte character. |
602 `------------------------------------------------------------------*/
605 convert_ucn_to_byte (char const *ucn)
607 unsigned long int code = strtoul (ucn + 2, 0, 16);
609 /* FIXME: Currently we assume Unicode-compatible unibyte characters
610 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
611 non-ASCII hosts we support only the portable C character set.
612 These limitations should be removed once we add support for
613 multibyte characters. */
615 if (UCHAR_MAX < code)
618 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
620 /* A non-ASCII host. Use CODE to index into a table of the C
621 basic execution character set, which is guaranteed to exist on
622 all Standard C platforms. This table also includes '$', '@',
623 and '`', which are not in the basic execution character set but
624 which are unibyte characters on all the platforms that we know
626 static signed char const table[] =
628 '\0', -1, -1, -1, -1, -1, -1, '\a',
629 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
630 -1, -1, -1, -1, -1, -1, -1, -1,
631 -1, -1, -1, -1, -1, -1, -1, -1,
632 ' ', '!', '"', '#', '$', '%', '&', '\'',
633 '(', ')', '*', '+', ',', '-', '.', '/',
634 '0', '1', '2', '3', '4', '5', '6', '7',
635 '8', '9', ':', ';', '<', '=', '>', '?',
636 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
637 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
638 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
639 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
640 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
641 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
642 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
643 'x', 'y', 'z', '{', '|', '}', '~'
646 code = code < sizeof table ? table[code] : -1;