From 388e12ac0f79317d5d8caea84931a900d9d91f7f Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Sat, 29 Feb 2020 09:54:34 +0100 Subject: [PATCH] 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. --- NEWS | 3 +++ examples/c/README.md | 5 +++-- examples/c/bistromathic/README.md | 2 +- examples/c/bistromathic/parse.y | 4 ++-- examples/c/lexcalc/README.md | 2 ++ examples/c/lexcalc/lexcalc.test | 7 ++++++- examples/c/lexcalc/parse.y | 14 +++++++++----- examples/c/lexcalc/scan.l | 25 ++++++++++++++++++++----- 8 files changed, 46 insertions(+), 16 deletions(-) diff --git a/NEWS b/NEWS index 0daa3048..b94ccaf0 100644 --- a/NEWS +++ b/NEWS @@ -101,6 +101,9 @@ GNU Bison NEWS There are now two examples in examples/java: a very simple calculator, and one that tracks locations to provide acurate error messages. + The lexcalc example (a simple example in C based on Flex and Bison) now + also demonstrates location tracking. + A new C example, bistromathic, is a fully featured calculator using many Bison features: pure interface, location tracking, internationalized custom error messages, lookahead-correction, rich debug traces, etc. diff --git a/examples/c/README.md b/examples/c/README.md index 893dfe2f..1a98d462 100644 --- a/examples/c/README.md +++ b/examples/c/README.md @@ -28,7 +28,8 @@ Extracted from the documentation: "Multi-Function Calculator: mfcalc". https://www.gnu.org/software/bison/manual/html_node/Multi_002dfunction-Calc.html ## lexcalc - calculator with Flex and Bison -The calculator, redux. This time using a scanner generated by Flex. +The calculator with precedence directives and location tracking. It uses +Flex to generate the scanner. ## reccalc - recursive calculator with Flex and Bison The example builds on top of the previous one to provide a reentrant parser. @@ -53,7 +54,7 @@ This example demonstrates the best practices when using Bison. - Its interface is pure. - It uses a custom syntax error with location tracking, lookahead correction and token internationalization. -- It enables debug trace support with formatting of semantic values. +- It supports debug traces with semantic values. - It uses named references instead of the traditional $1, $2, etc. It also uses Flex to generate the scanner. diff --git a/examples/c/bistromathic/README.md b/examples/c/bistromathic/README.md index 25411867..9f672d14 100644 --- a/examples/c/bistromathic/README.md +++ b/examples/c/bistromathic/README.md @@ -3,7 +3,7 @@ This example demonstrates the best practices when using Bison. - Its interface is pure. - It uses a custom syntax error with location tracking, lookahead correction and token internationalization. -- It enables debug trace support with formatting of semantic values. +- It supports debug traces with semantic values. - It uses named references instead of the traditional $1, $2, etc. It also uses Flex to generate the scanner. diff --git a/examples/c/bistromathic/parse.y b/examples/c/bistromathic/parse.y index 373bc074..9698161f 100644 --- a/examples/c/bistromathic/parse.y +++ b/examples/c/bistromathic/parse.y @@ -209,10 +209,10 @@ yyreport_syntax_error (const yyparse_context_t *ctx) } // Called by yyparse on error. -void yyerror (YYLTYPE *loc, char const *s) +void yyerror (YYLTYPE *loc, char const *msg) { YY_LOCATION_PRINT (stderr, *loc); - fprintf (stderr, ": %s\n", s); + fprintf (stderr, ": %s\n", msg); } int main (int argc, char const* argv[]) diff --git a/examples/c/lexcalc/README.md b/examples/c/lexcalc/README.md index ddf693c4..58d9d9aa 100644 --- a/examples/c/lexcalc/README.md +++ b/examples/c/lexcalc/README.md @@ -3,6 +3,8 @@ This directory contains lexcalc, the traditional example of using Flex and Bison to build a simple calculator. +It features detailed syntax errors with locations. +