mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-22 18:53:04 +00:00
multistart: toy with it in lexcalc
* examples/c/lexcalc/parse.y: Define several start symbols. * examples/c/lexcalc/lexcalc.test: Check support.
This commit is contained in:
@@ -20,6 +20,15 @@ cat >input <<EOF
|
|||||||
EOF
|
EOF
|
||||||
run 0 7
|
run 0 7
|
||||||
|
|
||||||
|
cat >input <<EOF
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
EOF
|
||||||
|
run 0 '1
|
||||||
|
2
|
||||||
|
3'
|
||||||
|
|
||||||
cat >input <<EOF
|
cat >input <<EOF
|
||||||
(1+2) * 3
|
(1+2) * 3
|
||||||
EOF
|
EOF
|
||||||
@@ -35,3 +44,17 @@ cat >input <<EOF
|
|||||||
1 / (2 - 2)
|
1 / (2 - 2)
|
||||||
EOF
|
EOF
|
||||||
run 1 'err: 1.1-11: error: division by zero'
|
run 1 'err: 1.1-11: error: division by zero'
|
||||||
|
|
||||||
|
|
||||||
|
# Multistart: parse "line" instead of "input".
|
||||||
|
cat >input <<EOF
|
||||||
|
1+2*3
|
||||||
|
EOF
|
||||||
|
run 0 7 -l
|
||||||
|
|
||||||
|
cat >input <<EOF
|
||||||
|
1
|
||||||
|
2
|
||||||
|
EOF
|
||||||
|
run 1 '1
|
||||||
|
err: 2.1: syntax error, unexpected number, expecting end of file' -l
|
||||||
|
|||||||
@@ -36,6 +36,7 @@
|
|||||||
{
|
{
|
||||||
#include <stdio.h> // printf.
|
#include <stdio.h> // printf.
|
||||||
#include <stdlib.h> // getenv.
|
#include <stdlib.h> // getenv.
|
||||||
|
#include <string.h> // strcmp.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Include the header in the implementation rather than duplicating it.
|
// Include the header in the implementation rather than duplicating it.
|
||||||
@@ -78,6 +79,8 @@
|
|||||||
%type <int> exp
|
%type <int> exp
|
||||||
%printer { fprintf (yyo, "%d", $$); } <int>
|
%printer { fprintf (yyo, "%d", $$); } <int>
|
||||||
|
|
||||||
|
%start input line
|
||||||
|
|
||||||
// Precedence (from lowest to highest) and associativity.
|
// Precedence (from lowest to highest) and associativity.
|
||||||
%left "+" "-"
|
%left "+" "-"
|
||||||
%left "*" "/"
|
%left "*" "/"
|
||||||
@@ -121,12 +124,23 @@ void yyerror (YYLTYPE *loc, int *nerrs, const char *msg)
|
|||||||
++*nerrs;
|
++*nerrs;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main (void)
|
int main (int argc, const char *argv[])
|
||||||
{
|
{
|
||||||
int nerrs = 0;
|
int nerrs = 0;
|
||||||
// Possibly enable parser runtime debugging.
|
// Possibly enable parser runtime debugging.
|
||||||
yydebug = !!getenv ("YYDEBUG");
|
yydebug = !!getenv ("YYDEBUG");
|
||||||
yyparse (&nerrs);
|
// Enable parse traces on option -p.
|
||||||
|
int parse_line_p = 0;
|
||||||
|
for (int i = 0; i < argc; ++i)
|
||||||
|
if (1 < argc && strcmp (argv[1], "-p") == 0)
|
||||||
|
yydebug = 1;
|
||||||
|
else if (strcmp (argv[i], "-l") == 0)
|
||||||
|
parse_line_p = 1;
|
||||||
|
|
||||||
|
if (parse_line_p)
|
||||||
|
yyparse_line (&nerrs);
|
||||||
|
else
|
||||||
|
yyparse_input (&nerrs);
|
||||||
// Exit on failure if there were errors.
|
// Exit on failure if there were errors.
|
||||||
return !!nerrs;
|
return !!nerrs;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user