mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-09 12:23:04 +00:00
examples: lexcalc: demonstrate location tracking
The bistromathic example should not use Flex, it makes it too complex. But it was the only example to show location tracking with Flex. * examples/c/lexcalc/lexcalc.test, examples/c/lexcalc/parse.y, * examples/c/lexcalc/scan.l: Demonstrate location tracking as is done in bistromathic.
This commit is contained in:
@@ -9,9 +9,24 @@
|
||||
#include <stdlib.h> /* strtol */
|
||||
|
||||
#include "parse.h"
|
||||
|
||||
// Each time a rule is matched, advance the end cursor/position.
|
||||
#define YY_USER_ACTION \
|
||||
yylloc->last_column += yyleng;
|
||||
|
||||
// Move the first position onto the last.
|
||||
#define LOCATION_STEP() \
|
||||
do { \
|
||||
yylloc->first_line = yylloc->last_line; \
|
||||
yylloc->first_column = yylloc->last_column; \
|
||||
} while (0)
|
||||
%}
|
||||
|
||||
%%
|
||||
%{
|
||||
// Each time yylex is called, move the head position to the end one.
|
||||
LOCATION_STEP ();
|
||||
%}
|
||||
/* Rules. */
|
||||
|
||||
"+" return TOK_PLUS;
|
||||
@@ -27,17 +42,17 @@
|
||||
errno = 0;
|
||||
long n = strtol (yytext, NULL, 10);
|
||||
if (! (INT_MIN <= n && n <= INT_MAX && errno != ERANGE))
|
||||
yyerror (nerrs, "integer is out of range");
|
||||
yyerror (yylloc, nerrs, "integer is out of range");
|
||||
yylval->TOK_NUM = (int) n;
|
||||
return TOK_NUM;
|
||||
}
|
||||
|
||||
"\n" yylloc->last_line++; yylloc->last_column = 1; return TOK_EOL;
|
||||
|
||||
/* Ignore white spaces. */
|
||||
[ \t]+ continue;
|
||||
[ \t]+ LOCATION_STEP (); continue;
|
||||
|
||||
"\n" return TOK_EOL;
|
||||
|
||||
. yyerror (nerrs, "syntax error, invalid character");
|
||||
. yyerror (yylloc, nerrs, "syntax error, invalid character"); continue;
|
||||
|
||||
<<EOF>> return TOK_EOF;
|
||||
%%
|
||||
|
||||
Reference in New Issue
Block a user