1 /******************************************************************************
2 * Copyright (c) 2000-2014 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 lexical analyzer
12 * Written by Matyas Forstner using bison's scan-gram.l
17 %option nodefault nounput noyywrap never-interactive
19 %option prefix="bison_"
21 %{ /* ****************** C declarations ***************** */
23 #include "../compiler2/string.hh"
26 #include "bison_p.tab.hh"
30 #define lval bison_lval
32 static int convert_ucn_to_byte (char const *hex_text);
34 %} /* ***************** definitions ***************** */
36 %x SC_COMMENT SC_LINE_COMMENT SC_YACC_COMMENT
37 %x SC_STRING SC_CHARACTER
38 %x SC_AFTER_IDENTIFIER
39 %x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
40 %x SC_PRE_CODE SC_BRACED_CODE SC_PROLOGUE SC_EPILOGUE
42 letter [.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
43 id {letter}({letter}|[0-9])*
44 directive %{letter}({letter}|[0-9]|-)*
49 /* Zero or more instances of backslash-newline. Following GCC, allow
50 white space between the backslash and the newline. */
51 splice (\\[ \f\t\v]*\n)*
53 %% /* ***************** rules ************************* */
56 /* Nesting level of the current code in braces. */
59 /* Parent context state, when applicable. */
62 /* Token type to return, when applicable. */
67 /*-----------------------.
68 | Scanning white space. |
69 `-----------------------*/
71 <INITIAL,SC_AFTER_IDENTIFIER,SC_PRE_CODE>
73 /* Comments and white space. */
74 "," /*warn_at (*loc, _("stray `,' treated as white space"));*/
78 context_state = YY_START;
79 BEGIN SC_YACC_COMMENT;
82 /* #line directives are not documented, and may be withdrawn or
83 modified in future versions of Bison. */
84 ^"#line "{int}" \"".*"\"\n" {
89 /*----------------------------.
90 | Scanning Bison directives. |
91 `----------------------------*/
94 "%binary" return PERCENT_NONASSOC;
95 "%debug" return PERCENT_DEBUG;
96 "%default"[-_]"prec" return PERCENT_DEFAULT_PREC;
97 "%define" return PERCENT_DEFINE;
98 "%defines" return PERCENT_DEFINES;
99 "%destructor" token_type = PERCENT_DESTRUCTOR; BEGIN SC_PRE_CODE;
100 "%dprec" return PERCENT_DPREC;
101 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
102 "%expect" return PERCENT_EXPECT;
103 "%expect"[-_]"rr" return PERCENT_EXPECT_RR;
104 "%file-prefix" return PERCENT_FILE_PREFIX;
105 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
106 "%initial-action" token_type = PERCENT_INITIAL_ACTION; BEGIN SC_PRE_CODE;
107 "%glr-parser" return PERCENT_GLR_PARSER;
108 "%left" return PERCENT_LEFT;
109 "%lex-param" token_type = PERCENT_LEX_PARAM; BEGIN SC_PRE_CODE;
110 "%locations" return PERCENT_LOCATIONS;
111 "%merge" return PERCENT_MERGE;
112 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
113 "%no"[-_]"default"[-_]"prec" return PERCENT_NO_DEFAULT_PREC;
114 "%no"[-_]"lines" return PERCENT_NO_LINES;
115 "%nonassoc" return PERCENT_NONASSOC;
116 "%nondeterministic-parser" return PERCENT_NONDETERMINISTIC_PARSER;
117 "%nterm" return PERCENT_NTERM;
118 "%output" return PERCENT_OUTPUT;
119 "%parse-param" token_type = PERCENT_PARSE_PARAM; BEGIN SC_PRE_CODE;
120 "%prec" return PERCENT_PREC;
121 "%printer" token_type = PERCENT_PRINTER; BEGIN SC_PRE_CODE;
122 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
123 "%right" return PERCENT_RIGHT;
124 "%skeleton" return PERCENT_SKELETON;
125 "%start" return PERCENT_START;
126 "%term" return PERCENT_TOKEN;
127 "%token" return PERCENT_TOKEN;
128 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
129 "%type" return PERCENT_TYPE;
130 "%union" token_type = PERCENT_UNION; BEGIN SC_PRE_CODE;
131 "%verbose" return PERCENT_VERBOSE;
132 "%yacc" return PERCENT_YACC;
135 /*complain_at (*loc, _("invalid directive: %s"), quote (yytext));*/
140 ";" return SEMICOLON;
143 //val->symbol = symbol_get (yytext, *loc);
144 lval.string_val=new string(yytext);
145 BEGIN SC_AFTER_IDENTIFIER;
149 //lval.integer = scan_integer (yytext, 10);
152 0[xX][0-9abcdefABCDEF]+ {
153 //lval.integer = scan_integer (yytext, 16);
157 /* Characters. We don't check there is only one. */
158 "'" /* +\' */ lval.string_val=new string("'"); BEGIN SC_ESCAPED_CHARACTER;
161 "\"" lval.string_val=new string("\""); BEGIN SC_ESCAPED_STRING;
164 "%{" BEGIN SC_PROLOGUE;
166 /* Code in between braces. */
169 token_type = BRACED_CODE;
171 BEGIN SC_BRACED_CODE;
177 obstack_grow (&obstack_for_string, yytext + 1, yyleng - 2);
179 val->uniqstr = uniqstr_new (last_string);
186 static int percent_percent_count;
187 if (++percent_percent_count == 2)
189 return PERCENT_PERCENT;
193 //complain_at (*loc, _("invalid character: %s"), quote (yytext));
202 /*-----------------------------------------------------------------.
203 | Scanning after an identifier, checking whether a colon is next. |
204 `-----------------------------------------------------------------*/
206 <SC_AFTER_IDENTIFIER>
225 /*---------------------------------------------------------------.
226 | Scanning a Yacc comment. The initial `/ *' is already eaten. |
227 `---------------------------------------------------------------*/
231 "*/" BEGIN context_state;
233 <<EOF>> /*unexpected_eof (token_start, "* /");*/ BEGIN context_state;
237 /*------------------------------------------------------------.
238 | Scanning a C comment. The initial `/ *' is already eaten. |
239 `------------------------------------------------------------*/
243 "*"{splice}"/" /*STRING_GROW;*/ BEGIN context_state;
244 <<EOF>> /*unexpected_eof (token_start, "* /");*/ BEGIN context_state;
248 /*--------------------------------------------------------------.
249 | Scanning a line comment. The initial `//' is already eaten. |
250 `--------------------------------------------------------------*/
254 "\n" /*STRING_GROW;*/ BEGIN context_state;
255 {splice} /*STRING_GROW;*/
256 <<EOF>> BEGIN context_state;
260 /*------------------------------------------------.
261 | Scanning a Bison string, including its escapes. |
262 | The initial quote is already eaten. |
263 `------------------------------------------------*/
271 loc->start = token_start;
272 val->chars = last_string;
275 *lval.string_val+="\"";
280 \n /*unexpected_newline (token_start, "\"");*/ delete lval.string_val; BEGIN INITIAL;
282 <<EOF>> /*unexpected_eof (token_start, "\"");*/ delete lval.string_val; BEGIN INITIAL;
284 . *lval.string_val+=yytext;
289 /*----------------------------------------------------------.
290 | Scanning a Bison character literal, decoding its escapes. |
291 | The initial quote is already eaten. |
292 `----------------------------------------------------------*/
294 <SC_ESCAPED_CHARACTER>
298 unsigned char last_string_1;
301 loc->start = token_start;
302 val->symbol = symbol_get (last_string, *loc);
303 symbol_class_set (val->symbol, token_sym, *loc);
304 last_string_1 = last_string[1];
305 symbol_user_token_number_set (val->symbol, last_string_1, *loc);
309 *lval.string_val+="'";
314 \n /*unexpected_newline (token_start, "'");*/ delete lval.string_val; BEGIN INITIAL;
316 <<EOF>> /*unexpected_eof (token_start, "'");*/ delete lval.string_val; BEGIN INITIAL;
318 . *lval.string_val+=yytext;
321 <SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING>
323 \0 /*complain_at (*loc, _("invalid null character"));*/
327 /*----------------------------.
328 | Decode escaped characters. |
329 `----------------------------*/
331 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
334 unsigned long int c = strtoul (yytext + 1, 0, 8);
336 /*complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext))*/;
338 /*complain_at (*loc, _("invalid null character: %s"), quote (yytext))*/;
340 /*obstack_1grow (&obstack_for_string, c);*/ *lval.string_val+=c;
343 \\x[0-9abcdefABCDEF]+ {
346 c = strtoul (yytext + 2, 0, 16);
347 if (UCHAR_MAX < c || errno)
348 /*complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext))*/;
350 /*complain_at (*loc, _("invalid null character: %s"), quote (yytext))*/;
352 /*obstack_1grow (&obstack_for_string, c);*/ *lval.string_val+=c;
355 \\a /*obstack_1grow (&obstack_for_string, '\a');*/ *lval.string_val+='\a';
356 \\b /*obstack_1grow (&obstack_for_string, '\b');*/ *lval.string_val+='\b';
357 \\f /*obstack_1grow (&obstack_for_string, '\f');*/ *lval.string_val+='\f';
358 \\n /*obstack_1grow (&obstack_for_string, '\n');*/ *lval.string_val+='\n';
359 \\r /*obstack_1grow (&obstack_for_string, '\r');*/ *lval.string_val+='\r';
360 \\t /*obstack_1grow (&obstack_for_string, '\t');*/ *lval.string_val+='\t';
361 \\v /*obstack_1grow (&obstack_for_string, '\v');*/ *lval.string_val+='\v';
363 /* \\[\"\'?\\] would be shorter, but it confuses xgettext. */
364 \\("\""|"'"|"?"|"\\") /*obstack_1grow (&obstack_for_string, yytext[1]);*/ *lval.string_val+=yytext[1];
366 \\(u|U[0-9abcdefABCDEF]{4})[0-9abcdefABCDEF]{4} {
367 int c = convert_ucn_to_byte (yytext);
369 /*complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext))*/;
371 /*complain_at (*loc, _("invalid null character: %s"), quote (yytext))*/;
373 /*obstack_1grow (&obstack_for_string, c);*/ *lval.string_val+=c;
377 complain_at (*loc, _("unrecognized escape sequence: %s"), quote (yytext));
380 *lval.string_val+=yytext;
385 /*--------------------------------------------.
386 | Scanning user-code characters and strings. |
387 `--------------------------------------------*/
389 <SC_CHARACTER,SC_STRING>
391 {splice}|\\{splice}[^\n$@\[\]] /*STRING_GROW;*/
396 "'" /*STRING_GROW;*/ BEGIN context_state;
397 \n /*unexpected_newline (token_start, "'");*/ BEGIN context_state;
398 <<EOF>> /*unexpected_eof (token_start, "'");*/ BEGIN context_state;
403 "\"" /*STRING_GROW;*/ BEGIN context_state;
404 \n /*unexpected_newline (token_start, "\"");*/ BEGIN context_state;
405 <<EOF>> /*unexpected_eof (token_start, "\"");*/ BEGIN context_state;
409 /*---------------------------------------------------.
410 | Strings, comments etc. can be found in user code. |
411 `---------------------------------------------------*/
413 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
418 token_start = loc->start;
420 context_state = YY_START;
426 token_start = loc->start;
428 context_state = YY_START;
434 token_start = loc->start;
436 context_state = YY_START;
441 context_state = YY_START;
442 BEGIN SC_LINE_COMMENT;
447 /*---------------------------------------------------------------.
448 | Scanning after %union etc., possibly followed by white space. |
449 | For %union only, allow arbitrary C code to appear before the |
450 | following brace, as an extension to POSIX. |
451 `---------------------------------------------------------------*/
457 bool valid = yytext[0] == '{' || token_type == PERCENT_UNION;
458 scanner_cursor.column -= mbsnwidth (yytext, yyleng, 0);
466 //code_start = loc->start;
467 BEGIN SC_BRACED_CODE;
472 complain_at (*loc, _("missing `{' in `%s'"),
473 token_name (token_type));
474 obstack_sgrow (&obstack_for_string, "{}");
476 val->chars = last_string;
483 <<EOF>> /*unexpected_eof (scanner_cursor, "{}");*/ BEGIN INITIAL;
487 /*---------------------------------------------------------------.
488 | Scanning some code in braces (%union and actions). The initial |
489 | "{" is already eaten. |
490 `---------------------------------------------------------------*/
494 "{"|"<"{splice}"%" /*STRING_GROW;*/ braces_level++;
495 "%"{splice}">" /*STRING_GROW;*/ braces_level--;
497 bool outer_brace = --braces_level < 0;
499 /* As an undocumented Bison extension, append `;' before the last
500 brace in braced code, so that the user code can omit trailing
501 `;'. But do not append `;' if emulating Yacc, since Yacc does
504 FIXME: Bison should warn if a semicolon seems to be necessary
505 here, and should omit the semicolon if it seems unnecessary
506 (e.g., after ';', '{', or '}', each followed by comments or
507 white space). Such a warning shouldn't depend on --yacc; it
508 should depend on a new --pedantic option, which would cause
509 Bison to warn if it detects an extension to POSIX. --pedantic
510 should also diagnose other Bison extensions like %yacc.
511 Perhaps there should also be a GCC-style --pedantic-errors
512 option, so that such warnings are diagnosed as errors. */
515 if (outer_brace && token_type == BRACED_CODE && ! yacc_flag)
516 obstack_1grow (&obstack_for_string, ';');
518 obstack_1grow (&obstack_for_string, '}');
526 loc->start = code_start;
527 val->chars = last_string;
534 /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
536 "<"{splice}"<" /*STRING_GROW;*/
537 "$"("<"{tag}">")?(-?[0-9]+|"$") /*handle_dollar (token_type, yytext, *loc);*/
538 "@"(-?[0-9]+|"$") /* handle_at (token_type, yytext, *loc);*/
540 <<EOF>> /*unexpected_eof (code_start, "}");*/ BEGIN INITIAL;
544 /*--------------------------------------------------------------.
545 | Scanning some prologue: from "%{" (already scanned) to "%}". |
546 `--------------------------------------------------------------*/
553 loc->start = code_start;
554 val->chars = last_string;
560 <<EOF>> /*unexpected_eof (code_start, "%}");*/ BEGIN INITIAL;
564 /*---------------------------------------------------------------.
565 | Scanning the epilogue (everything after the second "%%", which |
566 | has already been eaten). |
567 `---------------------------------------------------------------*/
574 loc->start = code_start;
575 val->chars = last_string;
583 /*-----------------------------------------------------.
584 | By default, grow the string obstack with the input. |
585 `-----------------------------------------------------*/
587 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER>. |
588 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>\n /*STRING_GROW;*/
593 /*------------------------------------------------------------------.
594 | Convert universal character name UCN to a single-byte character, |
595 | and return that character. Return -1 if UCN does not correspond |
596 | to a single-byte character. |
597 `------------------------------------------------------------------*/
600 convert_ucn_to_byte (char const *ucn)
602 unsigned long int code = strtoul (ucn + 2, 0, 16);
604 /* FIXME: Currently we assume Unicode-compatible unibyte characters
605 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
606 non-ASCII hosts we support only the portable C character set.
607 These limitations should be removed once we add support for
608 multibyte characters. */
610 if (UCHAR_MAX < code)
613 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
615 /* A non-ASCII host. Use CODE to index into a table of the C
616 basic execution character set, which is guaranteed to exist on
617 all Standard C platforms. This table also includes '$', '@',
618 and '`', which are not in the basic execution character set but
619 which are unibyte characters on all the platforms that we know
621 static signed char const table[] =
623 '\0', -1, -1, -1, -1, -1, -1, '\a',
624 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
625 -1, -1, -1, -1, -1, -1, -1, -1,
626 -1, -1, -1, -1, -1, -1, -1, -1,
627 ' ', '!', '"', '#', '$', '%', '&', '\'',
628 '(', ')', '*', '+', ',', '-', '.', '/',
629 '0', '1', '2', '3', '4', '5', '6', '7',
630 '8', '9', ':', ';', '<', '=', '>', '?',
631 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
632 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
633 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
634 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
635 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
636 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
637 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
638 'x', 'y', 'z', '{', '|', '}', '~'
641 code = code < sizeof table ? table[code] : -1;