| 1 |
/* Scanner for calc++. -*- C++ -*- |
| 2 |
|
| 3 |
Copyright (C) 2005-2015, 2018-2021 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
This file is part of Bison, the GNU Compiler Compiler. |
| 6 |
|
| 7 |
This program is free software: you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation, either version 3 of the License, or |
| 10 |
(at your option) any later version. |
| 11 |
|
| 12 |
This program is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
%{ /* -*- C++ -*- */ |
| 21 |
# include <cerrno> |
| 22 |
# include <climits> |
| 23 |
# include <cstdlib> |
| 24 |
# include <cstring> // strerror |
| 25 |
# include <string> |
| 26 |
# include "driver.hh" |
| 27 |
# include "parser.hh" |
| 28 |
%} |
| 29 |
|
| 30 |
%{ |
| 31 |
#if defined __clang__ |
| 32 |
# define CLANG_VERSION (__clang_major__ * 100 + __clang_minor__) |
| 33 |
#endif |
| 34 |
|
| 35 |
// Clang and ICC like to pretend they are GCC. |
| 36 |
#if defined __GNUC__ && !defined __clang__ && !defined __ICC |
| 37 |
# define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) |
| 38 |
#endif |
| 39 |
|
| 40 |
// Pacify warnings in yy_init_buffer (observed with Flex 2.6.4) |
| 41 |
// and GCC 6.4.0, 7.3.0 with -O3. |
| 42 |
#if defined GCC_VERSION && 600 <= GCC_VERSION |
| 43 |
# pragma GCC diagnostic ignored "-Wnull-dereference" |
| 44 |
#endif |
| 45 |
|
| 46 |
// This example uses Flex's C back end, yet compiles it as C++. |
| 47 |
// So expect warnings about C style casts and NULL. |
| 48 |
#if defined CLANG_VERSION && 500 <= CLANG_VERSION |
| 49 |
# pragma clang diagnostic ignored "-Wold-style-cast" |
| 50 |
# pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" |
| 51 |
#elif defined GCC_VERSION && 407 <= GCC_VERSION |
| 52 |
# pragma GCC diagnostic ignored "-Wold-style-cast" |
| 53 |
# pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant" |
| 54 |
#endif |
| 55 |
|
| 56 |
#define FLEX_VERSION (YY_FLEX_MAJOR_VERSION * 100 + YY_FLEX_MINOR_VERSION) |
| 57 |
|
| 58 |
// Old versions of Flex (2.5.35) generate an incomplete documentation comment. |
| 59 |
// |
| 60 |
// In file included from src/scan-code-c.c:3: |
| 61 |
// src/scan-code.c:2198:21: error: empty paragraph passed to '@param' command |
| 62 |
// [-Werror,-Wdocumentation] |
| 63 |
// * @param line_number |
| 64 |
// ~~~~~~~~~~~~~~~~~^ |
| 65 |
// 1 error generated. |
| 66 |
#if FLEX_VERSION < 206 && defined CLANG_VERSION |
| 67 |
# pragma clang diagnostic ignored "-Wdocumentation" |
| 68 |
#endif |
| 69 |
|
| 70 |
// Old versions of Flex (2.5.35) use 'register'. Warnings introduced in |
| 71 |
// GCC 7 and Clang 6. |
| 72 |
#if FLEX_VERSION < 206 |
| 73 |
# if defined CLANG_VERSION && 600 <= CLANG_VERSION |
| 74 |
# pragma clang diagnostic ignored "-Wdeprecated-register" |
| 75 |
# elif defined GCC_VERSION && 700 <= GCC_VERSION |
| 76 |
# pragma GCC diagnostic ignored "-Wregister" |
| 77 |
# endif |
| 78 |
#endif |
| 79 |
|
| 80 |
#if FLEX_VERSION < 206 |
| 81 |
# if defined CLANG_VERSION |
| 82 |
# pragma clang diagnostic ignored "-Wconversion" |
| 83 |
# pragma clang diagnostic ignored "-Wdocumentation" |
| 84 |
# pragma clang diagnostic ignored "-Wshorten-64-to-32" |
| 85 |
# pragma clang diagnostic ignored "-Wsign-conversion" |
| 86 |
# elif defined GCC_VERSION |
| 87 |
# pragma GCC diagnostic ignored "-Wconversion" |
| 88 |
# pragma GCC diagnostic ignored "-Wsign-conversion" |
| 89 |
# endif |
| 90 |
#endif |
| 91 |
|
| 92 |
// Flex 2.6.4, GCC 9 |
| 93 |
// warning: useless cast to type 'int' [-Wuseless-cast] |
| 94 |
// 1361 | YY_CURRENT_BUFFER_LVALUE->yy_buf_size = (int) (new_size - 2); |
| 95 |
// | ^ |
| 96 |
#if defined GCC_VERSION && 900 <= GCC_VERSION |
| 97 |
# pragma GCC diagnostic ignored "-Wuseless-cast" |
| 98 |
#endif |
| 99 |
%} |
| 100 |
|
| 101 |
%option noyywrap nounput noinput batch debug |
| 102 |
|
| 103 |
%{ |
| 104 |
// A number symbol corresponding to the value in S. |
| 105 |
yy::parser::symbol_type |
| 106 |
make_NUMBER (const std::string &s, const yy::parser::location_type& loc); |
| 107 |
%} |
| 108 |
|
| 109 |
id [a-zA-Z][a-zA-Z_0-9]* |
| 110 |
int [0-9]+ |
| 111 |
blank [ \t\r] |
| 112 |
|
| 113 |
%{ |
| 114 |
// Code run each time a pattern is matched. |
| 115 |
# define YY_USER_ACTION loc.columns (yyleng); |
| 116 |
%} |
| 117 |
%% |
| 118 |
%{ |
| 119 |
// A handy shortcut to the location held by the driver. |
| 120 |
yy::location& loc = drv.location; |
| 121 |
// Code run each time yylex is called. |
| 122 |
loc.step (); |
| 123 |
%} |
| 124 |
{blank}+ loc.step (); |
| 125 |
\n+ loc.lines (yyleng); loc.step (); |
| 126 |
|
| 127 |
"-" return yy::parser::make_MINUS (loc); |
| 128 |
"+" return yy::parser::make_PLUS (loc); |
| 129 |
"*" return yy::parser::make_STAR (loc); |
| 130 |
"/" return yy::parser::make_SLASH (loc); |
| 131 |
"(" return yy::parser::make_LPAREN (loc); |
| 132 |
")" return yy::parser::make_RPAREN (loc); |
| 133 |
":=" return yy::parser::make_ASSIGN (loc); |
| 134 |
|
| 135 |
{int} return make_NUMBER (yytext, loc); |
| 136 |
{id} return yy::parser::make_IDENTIFIER (yytext, loc); |
| 137 |
. { |
| 138 |
throw yy::parser::syntax_error |
| 139 |
(loc, "invalid character: " + std::string(yytext)); |
| 140 |
} |
| 141 |
<<EOF>> return yy::parser::make_YYEOF (loc); |
| 142 |
%% |
| 143 |
|
| 144 |
yy::parser::symbol_type |
| 145 |
make_NUMBER (const std::string &s, const yy::parser::location_type& loc) |
| 146 |
{ |
| 147 |
errno = 0; |
| 148 |
long n = strtol (s.c_str(), NULL, 10); |
| 149 |
if (! (INT_MIN <= n && n <= INT_MAX && errno != ERANGE)) |
| 150 |
throw yy::parser::syntax_error (loc, "integer is out of range: " + s); |
| 151 |
return yy::parser::make_NUMBER ((int) n, loc); |
| 152 |
} |
| 153 |
|
| 154 |
void |
| 155 |
driver::scan_begin () |
| 156 |
{ |
| 157 |
yy_flex_debug = trace_scanning; |
| 158 |
if (file.empty () || file == "-") |
| 159 |
yyin = stdin; |
| 160 |
else if (!(yyin = fopen (file.c_str (), "r"))) |
| 161 |
{ |
| 162 |
std::cerr << "cannot open " << file << ": " << strerror (errno) << '\n'; |
| 163 |
exit (EXIT_FAILURE); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
void |
| 168 |
driver::scan_end () |
| 169 |
{ |
| 170 |
fclose (yyin); |
| 171 |
} |