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:
Akim Demaille
2020-02-29 09:54:34 +01:00
parent b870d5fee4
commit 388e12ac0f
8 changed files with 46 additions and 16 deletions

View File

@@ -6,10 +6,10 @@
{
// Tell Flex the expected prototype of yylex.
#define YY_DECL \
enum yytokentype yylex (YYSTYPE* yylval, int *nerrs)
enum yytokentype yylex (YYSTYPE* yylval, YYLTYPE *yylloc, int *nerrs)
YY_DECL;
void yyerror (int *nerrs, const char *msg);
void yyerror (YYLTYPE *loc, int *nerrs, const char *msg);
}
// Emitted on top of the implementation file.
@@ -33,6 +33,9 @@
// Generate detailed error messages.
%define parse.error detailed
// with locations.
%locations
// Enable debug traces (see yydebug in main).
%define parse.trace
@@ -78,7 +81,7 @@ exp:
{
if ($3 == 0)
{
yyerror (nerrs, "invalid division by zero");
yyerror (&@$, nerrs, "error: division by zero");
YYERROR;
}
else
@@ -90,9 +93,10 @@ exp:
%%
// Epilogue (C code).
void yyerror (int *nerrs, const char *msg)
void yyerror (YYLTYPE *loc, int *nerrs, const char *msg)
{
fprintf (stderr, "%s\n", msg);
YY_LOCATION_PRINT (stderr, *loc);
fprintf (stderr, ": %s\n", msg);
++*nerrs;
}