Skip to content

Commit a70e75b

Browse files
committed
examples: don't demonstrate multistart, which is not part of 3.8
Besides, for mysterious reasons, this fails on some environment. Reported by Dagobert Michelsen. <https://lists.gnu.org/archive/html/bug-bison/2021-08/msg00008.html> * examples/c/lexcalc/lexcalc.test, examples/c/lexcalc/parse.y, * examples/c/lexcalc/scan.l: Revert to a single-start example.
1 parent 3afa975 commit a70e75b

File tree

4 files changed

+18
-58
lines changed

4 files changed

+18
-58
lines changed

Diff for: TODO

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
* 3.8
2-
Don't showcase multi start.
3-
41
* Soon
52
** scan-code
63
The default case is scanning char-per-char.

Diff for: examples/c/lexcalc/lexcalc.test

+2-19
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,9 @@ run -noerr 0 9 -p
3838
cat >input <<EOF
3939
(1+2) *
4040
EOF
41-
run 1 'err: 1.8-2.0: syntax error, unexpected end of line, expecting ( or number
42-
err: errors: 1'
41+
run 1 'err: 1.8-2.0: syntax error, unexpected end of line, expecting ( or number'
4342

4443
cat >input <<EOF
4544
1 / (2 - 2)
4645
EOF
47-
run 1 'err: 1.1-11: error: division by zero
48-
err: errors: 1'
49-
50-
51-
# Multistart: parse "expression" instead of "input".
52-
cat >input <<EOF
53-
1+2*3
54-
EOF
55-
run 0 'expression: 7' -e
56-
57-
cat >input <<EOF
58-
1
59-
2
60-
EOF
61-
run 1 'expression: failure
62-
err: 2.1: syntax error, unexpected number, expecting end of file
63-
err: errors: 1' -e
46+
run 1 'err: 1.1-11: error: division by zero'

Diff for: examples/c/lexcalc/parse.y

+14-34
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
{
2626
// Tell Flex the expected prototype of yylex.
2727
#define YY_DECL \
28-
yytoken_kind_t yylex (YYSTYPE* yylval, YYLTYPE *yylloc)
28+
yytoken_kind_t yylex (YYSTYPE* yylval, YYLTYPE *yylloc, int *nerrs)
2929
YY_DECL;
3030

31-
void yyerror (const YYLTYPE *loc, const char *msg);
31+
void yyerror (const YYLTYPE *loc, int *nerrs, const char *msg);
3232
}
3333

3434
// Emitted on top of the implementation file.
@@ -62,6 +62,9 @@
6262
// Enable debug traces (see yydebug in main).
6363
%define parse.trace
6464

65+
// Error count, exchanged between main, yyparse and yylex.
66+
%param {int *nerrs}
67+
6568
%token
6669
PLUS "+"
6770
MINUS "-"
@@ -73,11 +76,9 @@
7376
;
7477

7578
%token <int> NUM "number"
76-
%type <int> exp expression line
79+
%type <int> exp
7780
%printer { fprintf (yyo, "%d", $$); } <int>
7881

79-
%start input expression NUM
80-
8182
// Precedence (from lowest to highest) and associativity.
8283
%left "+" "-"
8384
%left "*" "/"
@@ -90,12 +91,8 @@ input:
9091
;
9192

9293
line:
93-
exp EOL { $$ = $exp; printf ("%d\n", $$); }
94-
| error EOL { $$ = 0; yyerrok; }
95-
;
96-
97-
expression:
98-
exp EOL { $$ = $exp; }
94+
exp EOL { printf ("%d\n", $exp); }
95+
| error EOL { yyerrok; }
9996
;
10097

10198
exp:
@@ -106,7 +103,7 @@ exp:
106103
{
107104
if ($3 == 0)
108105
{
109-
yyerror (&@$, "error: division by zero");
106+
yyerror (&@$, nerrs, "error: division by zero");
110107
YYERROR;
111108
}
112109
else
@@ -118,41 +115,24 @@ exp:
118115
%%
119116
// Epilogue (C code).
120117

121-
void yyerror (const YYLTYPE *loc, const char *msg)
118+
void yyerror (const YYLTYPE *loc, int *nerrs, const char *msg)
122119
{
123120
YYLOCATION_PRINT (stderr, loc);
124121
fprintf (stderr, ": %s\n", msg);
122+
++*nerrs;
125123
}
126124

127125
int main (int argc, const char *argv[])
128126
{
129127
// Possibly enable parser runtime debugging.
130128
yydebug = !!getenv ("YYDEBUG");
131-
int parse_expression_p = 0;
132-
int nerrs = 0;
133-
134129
// Enable parse traces on option -p.
135130
for (int i = 1; i < argc; ++i)
136-
if (strcmp (argv[i], "-e") == 0)
137-
parse_expression_p = 1;
138-
else if (strcmp (argv[i], "-p") == 0)
131+
if (strcmp (argv[i], "-p") == 0)
139132
yydebug = 1;
140133

141-
if (parse_expression_p)
142-
{
143-
yyparse_expression_t res = yyparse_expression ();
144-
nerrs = res.yynerrs;
145-
if (res.yystatus == 0)
146-
printf ("expression: %d\n", res.yyvalue);
147-
else
148-
printf ("expression: failure\n");
149-
}
150-
else
151-
nerrs = yyparse_input ().yynerrs;
152-
153-
if (nerrs)
154-
fprintf (stderr, "errors: %d\n", nerrs);
155-
134+
int nerrs = 0;
135+
yyparse (&nerrs);
156136
// Exit on failure if there were errors.
157137
return !!nerrs;
158138
}

Diff for: examples/c/lexcalc/scan.l

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
errno = 0;
6262
long n = strtol (yytext, NULL, 10);
6363
if (! (INT_MIN <= n && n <= INT_MAX && errno != ERANGE))
64-
yyerror (yylloc, "integer is out of range");
64+
yyerror (yylloc, nerrs, "integer is out of range");
6565
yylval->TOK_NUM = (int) n;
6666
return TOK_NUM;
6767
}
@@ -71,7 +71,7 @@
7171
/* Ignore white spaces. */
7272
[ \t]+ LOCATION_STEP (); continue;
7373

74-
. yyerror (yylloc, "syntax error, invalid character"); continue;
74+
. yyerror (yylloc, nerrs, "syntax error, invalid character"); continue;
7575

7676
<<EOF>> return TOK_YYEOF;
7777
%%

0 commit comments

Comments
 (0)