1 /* Scanner for lexcalc. -*- C -*-
3 Copyright (C) 2018-2021 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
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/>. */
20 /* Prologue (directives). */
22 /* Disable Flex features we don't need, to avoid warnings. */
23 %option nodefault noinput nounput noyywrap
26 #include <errno.h> /* errno, ERANGE */
27 #include <limits.h> /* INT_MIN */
28 #include <stdlib.h> /* strtol */
32 // Each time a rule is matched, advance the end cursor/position.
33 #define YY_USER_ACTION \
34 yylloc->last_column += (int) yyleng;
36 // Move the first position onto the last.
37 #define LOCATION_STEP() \
39 yylloc->first_line = yylloc->last_line; \
40 yylloc->first_column = yylloc->last_column; \
46 // Each time yylex is called, move the head position to the end one.
56 "(" return TOK_LPAREN;
57 ")" return TOK_RPAREN;
59 /* Scan an integer. */
62 long n = strtol (yytext, NULL, 10);
63 if (! (INT_MIN <= n && n <= INT_MAX && errno != ERANGE))
64 yyerror (yylloc, nerrs, "integer is out of range");
65 yylval->TOK_NUM = (int) n;
69 "\n" yylloc->last_line++; yylloc->last_column = 1; return TOK_EOL;
71 /* Ignore white spaces. */
72 [ \t]+ LOCATION_STEP (); continue;
74 . yyerror (yylloc, nerrs, "syntax error, invalid character"); continue;
76 <<EOF>> return TOK_YYEOF;
78 /* Epilogue (C code). */