From: Akim Demaille Date: Mon, 2 Oct 2000 10:20:59 +0000 (+0000) Subject: The location tracking mechanism is precious for parse error X-Git-Tag: BISON-1_28b~101 X-Git-Url: https://git.saurik.com/bison.git/commitdiff_plain/89cab50df11c5f02d719db0239eabc0fb6095ec8?hp=d6c2cba06d7c3c86f98713c498c0d142d45b86be The location tracking mechanism is precious for parse error messages. Nevertheless, it is enabled only when `@n' is used in the grammar, which is a different issue (you can use it in error message, but not in the grammar per se). Therefore, there should be another means to enable it. * src/getargs.c (getargs): Support `--locations'. (usage): Report it. * src/getargs.h (locationsflag): Export it. * src/lex.c (percent_table): Support `%locations'. * src/reader.c (yylsp_needed): Remove this variable, now replaced with `locationsflag'. * doc/bison.texinfo: Document `--locations' and `%locations'. Sort the options. * tests/calc.m4: Test it. For regularity of the names, replace each (nolineflag, toknumflag, rawtokenumflag, noparserflag): with... (no_lineflag, token_tableflag, rawflag, no_parserflag): this. In addition replace each `flag' with `_flag'. --- diff --git a/ChangeLog b/ChangeLog index fef6a3fe..4ddc4903 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,27 @@ +2000-10-02 Akim Demaille + + The location tracking mechanism is precious for parse error + messages. Nevertheless, it is enabled only when `@n' is used in + the grammar, which is a different issue (you can use it in error + message, but not in the grammar per se). Therefore, there should + be another means to enable it. + + * src/getargs.c (getargs): Support `--locations'. + (usage): Report it. + * src/getargs.h (locationsflag): Export it. + * src/lex.c (percent_table): Support `%locations'. + * src/reader.c (yylsp_needed): Remove this variable, now replaced + with `locationsflag'. + * doc/bison.texinfo: Document `--locations' and `%locations'. + Sort the options. + * tests/calc.m4: Test it. + + For regularity of the names, replace each + (nolineflag, toknumflag, rawtokenumflag, noparserflag): with... + (no_lineflag, token_tableflag, rawflag, no_parserflag): this. + In addition replace each `flag' with `_flag'. + + 2000-10-02 Akim Demaille Also test parse error messages, including with YYERROR_VERBOSE. diff --git a/doc/bison.texinfo b/doc/bison.texinfo index 5ec63108..3cf61b01 100644 --- a/doc/bison.texinfo +++ b/doc/bison.texinfo @@ -3180,14 +3180,23 @@ Declare the type of semantic values for a nonterminal symbol (@pxref{Type Decl, ,Nonterminal Symbols}). @item %start -Specify the grammar's start symbol (@pxref{Start Decl, ,The Start-Symbol}). +Specify the grammar's start symbol (@pxref{Start Decl, ,The +Start-Symbol}). @item %expect Declare the expected number of shift-reduce conflicts (@pxref{Expect Decl, ,Suppressing Conflict Warnings}). +@item %locations +Generate the code processing the locations (@pxref{Action Features, +,Special Features for Use in Actions}). This mode is enabled as soon as +the grammar uses the special @samp{@@@var{n}} tokens, but if your +grammar does not use it, using @samp{%locations} allows for more +accurate parse error messages. + @item %pure_parser -Request a pure (reentrant) parser program (@pxref{Pure Decl, ,A Pure (Reentrant) Parser}). +Request a pure (reentrant) parser program (@pxref{Pure Decl, ,A Pure +(Reentrant) Parser}). @item %no_lines Don't generate any @code{#line} preprocessor commands in the parser @@ -3474,16 +3483,17 @@ then the code in @code{yylex} might look like this: @subsection Textual Positions of Tokens @vindex yylloc -If you are using the @samp{@@@var{n}}-feature (@pxref{Action Features, ,Special Features for Use in Actions}) in -actions to keep track of the textual locations of tokens and groupings, -then you must provide this information in @code{yylex}. The function -@code{yyparse} expects to find the textual location of a token just parsed -in the global variable @code{yylloc}. So @code{yylex} must store the -proper data in that variable. The value of @code{yylloc} is a structure -and you need only initialize the members that are going to be used by the -actions. The four members are called @code{first_line}, -@code{first_column}, @code{last_line} and @code{last_column}. Note that -the use of this feature makes the parser noticeably slower. +If you are using the @samp{@@@var{n}}-feature (@pxref{Action Features, +,Special Features for Use in Actions}) in actions to keep track of the +textual locations of tokens and groupings, then you must provide this +information in @code{yylex}. The function @code{yyparse} expects to +find the textual location of a token just parsed in the global variable +@code{yylloc}. So @code{yylex} must store the proper data in that +variable. The value of @code{yylloc} is a structure and you need only +initialize the members that are going to be used by the actions. The +four members are called @code{first_line}, @code{first_column}, +@code{last_line} and @code{last_column}. Note that the use of this +feature makes the parser noticeably slower. @tindex YYLTYPE The data type of @code{yylloc} has the name @code{YYLTYPE}. @@ -4894,25 +4904,56 @@ Here is a list of options that can be used with Bison, alphabetized by short option. It is followed by a cross key alphabetized by long option. -@table @samp -@item -b @var{file-prefix} -@itemx --file-prefix=@var{prefix} -Specify a prefix to use for all Bison output file names. The names are -chosen as if the input file were named @file{@var{prefix}.c}. +@c Please, keep this ordered as in `bison --help'. +@noindent +Operations modes: +@table @option +@item -h +@itemx --help +Print a summary of the command-line options to Bison and exit. -@item -d -@itemx --defines -Write an extra output file containing macro definitions for the token -type names defined in the grammar and the semantic value type -@code{YYSTYPE}, as well as a few @code{extern} variable declarations. +@item -V +@itemx --version +Print the version number of Bison and exit. -If the parser output file is named @file{@var{name}.c} then this file -is named @file{@var{name}.h}.@refill +@need 1750 +@item -y +@itemx --yacc +@itemx --fixed-output-files +Equivalent to @samp{-o y.tab.c}; the parser output file is called +@file{y.tab.c}, and the other outputs are called @file{y.output} and +@file{y.tab.h}. The purpose of this option is to imitate Yacc's output +file name conventions. Thus, the following shell script can substitute +for Yacc:@refill -This output file is essential if you wish to put the definition of -@code{yylex} in a separate source file, because @code{yylex} needs to -be able to refer to token type codes and the variable -@code{yylval}. @xref{Token Values, ,Semantic Values of Tokens}.@refill +@example +bison -y $* +@end example +@end table + +@noindent +Tuning the parser: + +@table @option +@item -t +@itemx --debug +Output a definition of the macro @code{YYDEBUG} into the parser file, +so that the debugging facilities are compiled. @xref{Debugging, ,Debugging Your Parser}. + +@item --locations +Pretend that @code{%locactions} was specified. @xref{Decl Summary}. + +@item -p @var{prefix} +@itemx --name-prefix=@var{prefix} +Rename the external symbols used in the parser so that they start with +@var{prefix} instead of @samp{yy}. The precise list of symbols renamed +is @code{yyparse}, @code{yylex}, @code{yyerror}, @code{yynerrs}, +@code{yylval}, @code{yychar} and @code{yydebug}. + +For example, if you use @samp{-p c}, the names become @code{cparse}, +@code{clex}, and so on. + +@xref{Multiple Parsers, ,Multiple Parsers in the Same Program}. @item -l @itemx --no-lines @@ -4932,33 +4973,37 @@ This option also tells Bison to write the C code for the grammar actions into a file named @file{@var{filename}.act}, in the form of a brace-surrounded body fit for a @code{switch} statement. -@item -o @var{outfile} -@itemx --output-file=@var{outfile} -Specify the name @var{outfile} for the parser file. +@item -r +@itemx --raw +Pretend that @code{%raw} was specified. @xref{Decl Summary}. -The other output files' names are constructed from @var{outfile} -as described under the @samp{-v} and @samp{-d} options. +@item -k +@itemx --token-table +Pretend that @code{%token_table} was specified. @xref{Decl Summary}. +@end table -@item -p @var{prefix} -@itemx --name-prefix=@var{prefix} -Rename the external symbols used in the parser so that they start with -@var{prefix} instead of @samp{yy}. The precise list of symbols renamed -is @code{yyparse}, @code{yylex}, @code{yyerror}, @code{yynerrs}, -@code{yylval}, @code{yychar} and @code{yydebug}. +@noindent +Adjust the output: -For example, if you use @samp{-p c}, the names become @code{cparse}, -@code{clex}, and so on. +@table @option +@item -d +@itemx --defines +Write an extra output file containing macro definitions for the token +type names defined in the grammar and the semantic value type +@code{YYSTYPE}, as well as a few @code{extern} variable declarations. -@xref{Multiple Parsers, ,Multiple Parsers in the Same Program}. +If the parser output file is named @file{@var{name}.c} then this file +is named @file{@var{name}.h}.@refill -@item -r -@itemx --raw -Pretend that @code{%raw} was specified. @xref{Decl Summary}. +This output file is essential if you wish to put the definition of +@code{yylex} in a separate source file, because @code{yylex} needs to +be able to refer to token type codes and the variable +@code{yylval}. @xref{Token Values, ,Semantic Values of Tokens}.@refill -@item -t -@itemx --debug -Output a definition of the macro @code{YYDEBUG} into the parser file, -so that the debugging facilities are compiled. @xref{Debugging, ,Debugging Your Parser}. +@item -b @var{file-prefix} +@itemx --file-prefix=@var{prefix} +Specify a prefix to use for all Bison output file names. The names are +chosen as if the input file were named @file{@var{prefix}.c}. @item -v @itemx --verbose @@ -4976,27 +5021,12 @@ Therefore, if the input file is @file{foo.y}, then the parser file is called @file{foo.tab.c} by default. As a consequence, the verbose output file is called @file{foo.output}.@refill -@item -V -@itemx --version -Print the version number of Bison and exit. - -@item -h -@itemx --help -Print a summary of the command-line options to Bison and exit. - -@need 1750 -@item -y -@itemx --yacc -@itemx --fixed-output-files -Equivalent to @samp{-o y.tab.c}; the parser output file is called -@file{y.tab.c}, and the other outputs are called @file{y.output} and -@file{y.tab.h}. The purpose of this option is to imitate Yacc's output -file name conventions. Thus, the following shell script can substitute -for Yacc:@refill +@item -o @var{outfile} +@itemx --output-file=@var{outfile} +Specify the name @var{outfile} for the parser file. -@example -bison -y $* -@end example +The other output files' names are constructed from @var{outfile} +as described under the @samp{-v} and @samp{-d} options. @end table @node Environment Variables, Option Cross Key, Bison Options, Invocation @@ -5280,15 +5310,17 @@ Bison declarations section or the additional C code section. @xref{Grammar Layout, ,The Overall Layout of a Bison Grammar}. @item %@{ %@} -All code listed between @samp{%@{} and @samp{%@}} is copied directly -to the output file uninterpreted. Such code forms the ``C -declarations'' section of the input file. @xref{Grammar Outline, ,Outline of a Bison Grammar}. +All code listed between @samp{%@{} and @samp{%@}} is copied directly to +the output file uninterpreted. Such code forms the ``C declarations'' +section of the input file. @xref{Grammar Outline, ,Outline of a Bison +Grammar}. @item /*@dots{}*/ Comment delimiters, as in C. @item : -Separates a rule's result from its components. @xref{Rules, ,Syntax of Grammar Rules}. +Separates a rule's result from its components. @xref{Rules, ,Syntax of +Grammar Rules}. @item ; Terminates a rule. @xref{Rules, ,Syntax of Grammar Rules}. @@ -5305,13 +5337,15 @@ Separates alternate rules for the same result nonterminal. @table @asis @item Backus-Naur Form (BNF) Formal method of specifying context-free grammars. BNF was first used -in the @cite{ALGOL-60} report, 1963. @xref{Language and Grammar, ,Languages and Context-Free Grammars}. +in the @cite{ALGOL-60} report, 1963. @xref{Language and Grammar, +,Languages and Context-Free Grammars}. @item Context-free grammars Grammars specified as rules that can be applied regardless of context. Thus, if there is a rule which says that an integer can be used as an expression, integers are allowed @emph{anywhere} an expression is -permitted. @xref{Language and Grammar, ,Languages and Context-Free Grammars}. +permitted. @xref{Language and Grammar, ,Languages and Context-Free +Grammars}. @item Dynamic allocation Allocation of memory that occurs during execution, rather than at @@ -5352,8 +5386,9 @@ Operators having left associativity are analyzed from left to right: @samp{c}. @xref{Precedence, ,Operator Precedence}. @item Left recursion -A rule whose result symbol is also its first component symbol; -for example, @samp{expseq1 : expseq1 ',' exp;}. @xref{Recursion, ,Recursive Rules}. +A rule whose result symbol is also its first component symbol; for +example, @samp{expseq1 : expseq1 ',' exp;}. @xref{Recursion, ,Recursive +Rules}. @item Left-to-right parsing Parsing a sentence of a language by analyzing it token by token from @@ -5372,7 +5407,8 @@ A token which consists of two or more fixed characters. @xref{Symbols}. @item Look-ahead token -A token already read but not yet shifted. @xref{Look-Ahead, ,Look-Ahead Tokens}. +A token already read but not yet shifted. @xref{Look-Ahead, ,Look-Ahead +Tokens}. @item LALR(1) The class of context-free grammars that Bison (like most other parser @@ -5403,7 +5439,8 @@ performs some operation. @item Reduction Replacing a string of nonterminals and/or terminals with a single -nonterminal, according to a grammar rule. @xref{Algorithm, ,The Bison Parser Algorithm }. +nonterminal, according to a grammar rule. @xref{Algorithm, ,The Bison +Parser Algorithm }. @item Reentrant A reentrant subprogram is a subprogram which can be in invoked any @@ -5414,8 +5451,9 @@ invocations. @xref{Pure Decl, ,A Pure (Reentrant) Parser}. A language in which all operators are postfix operators. @item Right recursion -A rule whose result symbol is also its last component symbol; -for example, @samp{expseq1: exp ',' expseq1;}. @xref{Recursion, ,Recursive Rules}. +A rule whose result symbol is also its last component symbol; for +example, @samp{expseq1: exp ',' expseq1;}. @xref{Recursion, ,Recursive +Rules}. @item Semantics In computer languages, the semantics are specified by the actions @@ -5449,9 +5487,9 @@ The input of the Bison parser is a stream of tokens which comes from the lexical analyzer. @xref{Symbols}. @item Terminal symbol -A grammar symbol that has no rules in the grammar and therefore -is grammatically indivisible. The piece of text it represents -is a token. @xref{Language and Grammar, ,Languages and Context-Free Grammars}. +A grammar symbol that has no rules in the grammar and therefore is +grammatically indivisible. The piece of text it represents is a token. +@xref{Language and Grammar, ,Languages and Context-Free Grammars}. @end table @node Index, , Glossary, Top diff --git a/src/conflicts.c b/src/conflicts.c index c694f830..f89c3a38 100644 --- a/src/conflicts.c +++ b/src/conflicts.c @@ -45,7 +45,7 @@ static int rrc_count; static inline void log_resolution (int state, int LAno, int token, char *resolution) { - if (verboseflag) + if (verbose_flag) fprintf (foutput, _("\ Conflict in state %d between rule %d and token %s resolved as %s.\n"), @@ -456,7 +456,7 @@ print_conflicts (void) src_total += src_count; rrc_total += rrc_count; - if (verboseflag) + if (verbose_flag) { fprintf (foutput, _("State %d contains"), i); conflict_report (foutput, src_count, rrc_count); @@ -465,7 +465,7 @@ print_conflicts (void) } /* Report the total number of conflicts on STDERR. */ - if (yaccflag) + if (yacc_flag) { /* If invoked with `--yacc', use the output format specified by POSIX. */ diff --git a/src/files.c b/src/files.c index 1d23b887..939b1731 100644 --- a/src/files.c +++ b/src/files.c @@ -232,7 +232,7 @@ open_files (void) static char FIXED_NAME_BASE[] = "y.y"; - name_base = yaccflag ? FIXED_NAME_BASE : infile; + name_base = yacc_flag ? FIXED_NAME_BASE : infile; /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */ @@ -255,7 +255,7 @@ open_files (void) finput = xfopen (infile, "r"); - if (!noparserflag) + if (!no_parser_flag) { filename = getenv ("BISON_SIMPLE"); #ifdef MSDOS @@ -273,7 +273,7 @@ open_files (void) fparser = xfopen (filename ? filename : PFILE, "r"); } - if (verboseflag) + if (verbose_flag) { #ifdef MSDOS outfile = stringappend (name_base, short_base_length, ".out"); @@ -285,7 +285,7 @@ open_files (void) foutput = xfopen (outfile, "w"); } - if (noparserflag) + if (no_parser_flag) { /* use permanent name for actions file */ actfile = stringappend (name_base, short_base_length, ".act"); @@ -293,32 +293,32 @@ open_files (void) } #ifdef MSDOS - if (!noparserflag) + if (!no_parser_flag) actfile = mktemp (stringappend (tmp_base, tmp_len, "acXXXXXX")); tmpattrsfile = mktemp (stringappend (tmp_base, tmp_len, "atXXXXXX")); tmptabfile = mktemp (stringappend (tmp_base, tmp_len, "taXXXXXX")); tmpdefsfile = mktemp (stringappend (tmp_base, tmp_len, "deXXXXXX")); #else - if (!noparserflag) + if (!no_parser_flag) actfile = mktemp (stringappend (tmp_base, tmp_len, "act.XXXXXX")); tmpattrsfile = mktemp (stringappend (tmp_base, tmp_len, "attrs.XXXXXX")); tmptabfile = mktemp (stringappend (tmp_base, tmp_len, "tab.XXXXXX")); tmpdefsfile = mktemp (stringappend (tmp_base, tmp_len, "defs.XXXXXX")); #endif /* not MSDOS */ - if (!noparserflag) + if (!no_parser_flag) faction = xfopen (actfile, "w+"); fattrs = xfopen (tmpattrsfile, "w+"); ftable = xfopen (tmptabfile, "w+"); - if (definesflag) + if (defines_flag) { defsfile = stringappend (name_base, base_length, ".h"); fdefines = xfopen (tmpdefsfile, "w+"); } #if !(defined (MSDOS) || (defined(_WIN32) && !defined(__CYGWIN32__))) - if (!noparserflag) + if (!no_parser_flag) unlink (actfile); unlink (tmpattrsfile); unlink (tmptabfile); @@ -364,7 +364,7 @@ open_extra_files (void) xfclose (fparser); - if (!noparserflag) + if (!no_parser_flag) { filename = (char *) getenv ("BISON_HAIRY"); #ifdef MSDOS @@ -417,7 +417,7 @@ done (void) xfclose (ftmp); xfclose (ftable); - if (definesflag) + if (defines_flag) { ftmp = xfopen (defsfile, "w"); fflush (fdefines); @@ -430,7 +430,7 @@ done (void) } #if defined (VMS) & !defined (__VMS_POSIX) - if (faction && !noparserflag) + if (faction && !no_parser_flag) delete (actfile); if (fattrs) delete (tmpattrsfile); @@ -442,7 +442,7 @@ done (void) sys$exit(SS$_ABORT); */ #else #if (defined (MSDOS) || (defined(_WIN32) && !defined(__CYGWIN32__))) - if (actfile && !noparserflag) + if (actfile && !no_parser_flag) unlink (actfile); if (tmpattrsfile) unlink (tmpattrsfile); diff --git a/src/getargs.c b/src/getargs.c index ad0f13f6..af8e68ae 100644 --- a/src/getargs.c +++ b/src/getargs.c @@ -27,36 +27,38 @@ char *spec_file_prefix; /* for -b. */ char *spec_name_prefix; /* for -p. */ -int debugflag = 0; -int definesflag = 0; -int nolinesflag = 0; -int noparserflag = 0; -int rawtoknumflag = 0; -int toknumflag = 0; -int verboseflag = 0; -int statisticsflag = 0; -int yaccflag = 0; /* for -y */ +int debug_flag = 0; +int defines_flag = 0; +int locations_flag = 0; +int no_lines_flag = 0; +int no_parser_flag = 0; +int raw_flag = 0; +int token_table_flag = 0; +int verbose_flag = 0; +int statistics_flag = 0; +int yacc_flag = 0; /* for -y */ extern char *program_name; static struct option longopts[] = { - {"debug", 0, &debugflag, 1}, - {"defines", 0, &definesflag, 1}, + {"debug", 0, &debug_flag, 1}, + {"defines", 0, &defines_flag, 1}, {"file-prefix", 1, 0, 'b'}, - {"fixed-output-files", 0, &yaccflag, 1}, + {"fixed-output-files", 0, &yacc_flag, 1}, {"help", 0, 0, 'h'}, {"name-prefix", 1, 0, 'p'}, /* was 'a'; apparently unused -wjh */ - {"no-lines", 0, &nolinesflag, 1}, - {"no-parser", 0, &noparserflag, 1}, + {"locations", 0, &locations_flag, 1}, + {"no-lines", 0, &no_lines_flag, 1}, + {"no-parser", 0, &no_parser_flag, 1}, {"output", 1, 0, 'o'}, {"output-file", 1, 0, 'o'}, - {"raw", 0, &rawtoknumflag, 1}, - {"token-table", 0, &toknumflag, 1}, - {"verbose", 0, &verboseflag, 1}, + {"raw", 0, &raw_flag, 1}, + {"statistics", 0, &statistics_flag, 1}, + {"token-table", 0, &token_table_flag, 1}, + {"verbose", 0, &verbose_flag, 1}, {"version", 0, 0, 'V'}, - {"yacc", 0, &yaccflag, 1}, - {"statistics", 0, &statisticsflag, 1}, + {"yacc", 0, &yacc_flag, 1}, {0, 0, 0, 0} }; @@ -93,6 +95,7 @@ Operation modes:\n\ fputs (_("\ Parser:\n\ -t, --debug instrument the parser for debugging\n\ + --locations enable locations computation\n\ -p, --name-prefix=PREFIX prepend PREFIX to the external symbols\n\ -l, --no-lines don't generate `#line' directives\n\ -n, --no-parser generate the tables only\n\ @@ -159,7 +162,7 @@ getargs (int argc, char *argv[]) break; case 'y': - yaccflag = 1; + yacc_flag = 1; break; case 'h': @@ -171,31 +174,31 @@ getargs (int argc, char *argv[]) exit (0); case 'v': - verboseflag = 1; + verbose_flag = 1; break; case 'd': - definesflag = 1; + defines_flag = 1; break; case 'l': - nolinesflag = 1; + no_lines_flag = 1; break; case 'k': - toknumflag = 1; + token_table_flag = 1; break; case 'r': - rawtoknumflag = 1; + raw_flag = 1; break; case 'n': - noparserflag = 1; + no_parser_flag = 1; break; case 't': - debugflag = 1; + debug_flag = 1; break; case 'o': diff --git a/src/getargs.h b/src/getargs.h index 7a7bdef2..00e84d07 100644 --- a/src/getargs.h +++ b/src/getargs.h @@ -25,15 +25,16 @@ extern char *spec_file_prefix; /* for -b */ extern char *spec_name_prefix; /* for -p */ -extern int debugflag; /* for -t */ -extern int definesflag; /* for -d */ -extern int nolinesflag; /* for -l */ -extern int noparserflag; /* for -n */ -extern int rawtoknumflag; /* for -r */ -extern int statisticsflag; -extern int toknumflag; /* for -k */ -extern int verboseflag; /* for -v */ -extern int yaccflag; /* for -y */ +extern int debug_flag; /* for -t */ +extern int defines_flag; /* for -d */ +extern int locations_flag; +extern int no_lines_flag; /* for -l */ +extern int no_parser_flag; /* for -n */ +extern int raw_flag; /* for -r */ +extern int statistics_flag; +extern int token_table_flag; /* for -k */ +extern int verbose_flag; /* for -v */ +extern int yacc_flag; /* for -y */ void getargs PARAMS ((int argc, char *argv[])); diff --git a/src/lex.c b/src/lex.c index 4ed8d457..bcf8cd66 100644 --- a/src/lex.c +++ b/src/lex.c @@ -513,12 +513,12 @@ lex (void) } /* the following table dictates the action taken for the various % - directives. A setflag value causes the named flag to be set. A + directives. A set_flag value causes the named flag to be set. A retval action returns the code. */ struct percent_table_struct { const char *name; - void *setflag; + void *set_flag; int retval; } percent_table[] = @@ -539,25 +539,26 @@ percent_table[] = { "semantic_parser", NULL, SEMANTIC_PARSER }, { "pure_parser", NULL, PURE_PARSER }, { "prec", NULL, PREC }, - { "no_lines", &nolinesflag, NOOP}, /* -l */ - { "raw", &rawtoknumflag, NOOP }, /* -r */ - { "token_table", &toknumflag, NOOP}, /* -k */ + { "locations", &locations_flag, NOOP}, /* -l */ + { "no_lines", &no_lines_flag, NOOP}, /* -l */ + { "raw", &raw_flag, NOOP }, /* -r */ + { "token_table", &token_table_flag, NOOP}, /* -k */ #if 0 /* These can be utilized after main is reoganized so open_files() is deferred 'til after read_declarations(). But %{ and %union both put information into files that have to be opened before read_declarations(). */ - { "yacc", &yaccflag, NOOP}, /* -y */ - { "fixed_output_files", &yaccflag, NOOP}, /* -y */ - { "defines", &definesflag, NOOP}, /* -d */ - { "no_parser", &noparserflag, NOOP}, /* -n */ + { "yacc", &yacc_flag, NOOP}, /* -y */ + { "fixed_output_files", &yacc_flag, NOOP}, /* -y */ + { "defines", &defines_flag, NOOP}, /* -d */ + { "no_parser", &no_parser_flag, NOOP}, /* -n */ { "output_file", &spec_outfile, SETOPT}, /* -o */ { "file_prefix", &spec_file_prefix, SETOPT}, /* -b */ { "name_prefix", &spec_name_prefix, SETOPT}, /* -p */ /* These would be acceptable, but they do not affect processing */ - { "verbose", &verboseflag, NOOP}, /* -v */ - { "debug", &debugflag, NOOP}, /* -t */ + { "verbose", &verbose_flag, NOOP}, /* -v */ + { "debug", &debug_flag, NOOP}, /* -t */ /* {"help", , NOOP}, *//* -h */ /* {"version", , NOOP}, *//* -V */ #endif @@ -629,12 +630,12 @@ parse_percent_token (void) break; if (tx->retval == SETOPT) { - *((char **) (tx->setflag)) = optarg; + *((char **) (tx->set_flag)) = optarg; return NOOP; } - if (tx->setflag) + if (tx->set_flag) { - *((int *) (tx->setflag)) = 1; + *((int *) (tx->set_flag)) = 1; return NOOP; } return tx->retval; diff --git a/src/output.c b/src/output.c index 62cc0e12..a426ad8f 100644 --- a/src/output.c +++ b/src/output.c @@ -203,7 +203,7 @@ output_headers (void) if (semantic_parser) fprintf (fguard, GUARDSTR, attrsfile); - if (noparserflag) + if (no_parser_flag) return; fprintf (faction, (semantic_parser ? ACTSTR : ACTSTR_SIMPLE), attrsfile); @@ -238,7 +238,7 @@ output_trailers (void) fprintf (faction, "\n"); - if (noparserflag) + if (no_parser_flag) return; if (semantic_parser) @@ -300,8 +300,8 @@ output_gram (void) /* With the ordinary parser, yyprhs and yyrhs are needed only for yydebug. */ - /* With the noparser option, all tables are generated */ - if (!semantic_parser && !noparserflag) + /* With the no_parser option, all tables are generated */ + if (!semantic_parser && !no_parser_flag) fprintf (ftable, "\n#if YYDEBUG != 0\n"); output_short_table (ftable, "yyprhs", rrhs, @@ -332,7 +332,7 @@ output_gram (void) fprintf (ftable, "\n};\n"); - if (!semantic_parser && !noparserflag) + if (!semantic_parser && !no_parser_flag) fprintf (ftable, "\n#endif\n"); } @@ -361,7 +361,7 @@ output_rule_data (void) fputs ("#endif\n\n", ftable); - if (toknumflag || noparserflag) + if (token_table_flag || no_parser_flag) { fprintf (ftable, "#define YYNTOKENS %d\n", ntokens); fprintf (ftable, "#define YYNNTS %d\n", nvars); @@ -370,7 +370,7 @@ output_rule_data (void) fprintf (ftable, "#define YYMAXUTOK %d\n\n", max_user_token_number); } - if (!toknumflag && !noparserflag) + if (!token_table_flag && !no_parser_flag) fprintf (ftable, "\n#if YYDEBUG != 0 || defined (YYERROR_VERBOSE)\n\n"); /* Output the table of symbol names. */ @@ -436,11 +436,11 @@ output_rule_data (void) /* add a NULL entry to list of tokens */ fprintf (ftable, ", NULL\n};\n"); - if (!toknumflag && !noparserflag) + if (!token_table_flag && !no_parser_flag) fprintf (ftable, "#endif\n\n"); /* Output YYTOKNUM. */ - if (toknumflag) + if (token_table_flag) { output_short_table (ftable, "yytoknum", user_toknums, 0, 1, ntokens + 1); @@ -1195,7 +1195,7 @@ output_parser (void) /* See if the line starts with `#line. If so, set write_line to 0. */ - if (nolinesflag) + if (no_lines_flag) if (c == '#') { c = getc (fpars); @@ -1254,7 +1254,7 @@ output_program (void) { int c; - if (!nolinesflag) + if (!no_lines_flag) fprintf (ftable, "#line %d \"%s\"\n", lineno, infile); c = getc (finput); @@ -1298,7 +1298,7 @@ output (void) putc (c, ftable); } reader_output_yylsp (ftable); - if (debugflag) + if (debug_flag) fputs ("\ #ifndef YYDEBUG\n\ # define YYDEBUG 1\n\ @@ -1309,7 +1309,7 @@ output (void) if (semantic_parser) fprintf (ftable, "#include \"%s\"\n", attrsfile); - if (!noparserflag) + if (!no_parser_flag) fprintf (ftable, "#include \n\n"); /* Make "const" do nothing if not in ANSI C. */ @@ -1333,7 +1333,7 @@ output (void) output_stos (); output_rule_data (); output_actions (); - if (!noparserflag) + if (!no_parser_flag) output_parser (); output_program (); } diff --git a/src/print.c b/src/print.c index f578ee09..f187b05b 100644 --- a/src/print.c +++ b/src/print.c @@ -351,10 +351,10 @@ print_results (void) if (any_conflicts) print_conflicts (); - if (verboseflag) + if (verbose_flag) print_grammar (); - if (verboseflag) + if (verbose_flag) for (i = 0; i < nstates; i++) print_state (i); } diff --git a/src/reader.c b/src/reader.c index b22f42d1..498dd6d6 100644 --- a/src/reader.c +++ b/src/reader.c @@ -67,9 +67,6 @@ static int gensym_count; static bucket *errtoken; static bucket *undeftoken; - -/* Nonzero if any action or guard uses the @n construct. */ -static int yylsp_needed; /*===================\ @@ -256,7 +253,7 @@ copy_at (FILE *fin, FILE *fout, int stack_offset) if (c == '$') { fprintf (fout, "yyloc"); - yylsp_needed = 1; + locations_flag = 1; } else if (isdigit (c) || c == '-') { @@ -266,7 +263,7 @@ copy_at (FILE *fin, FILE *fout, int stack_offset) n = read_signed_integer (fin); fprintf (fout, "yylsp[%d]", n - stack_offset); - yylsp_needed = 1; + locations_flag = 1; } else complain (_("@%s is invalid"), printable_version (c)); @@ -284,7 +281,7 @@ copy_definition (void) /* -1 while reading a character if prev char was %. */ int after_percent; - if (!nolinesflag) + if (!no_lines_flag) fprintf (fattrs, "#line %d \"%s\"\n", lineno, infile); after_percent = 0; @@ -631,7 +628,7 @@ parse_union_decl (void) typed = 1; - if (!nolinesflag) + if (!no_lines_flag) fprintf (fattrs, "\n#line %d \"%s\"\n", lineno, infile); else fprintf (fattrs, "\n"); @@ -919,7 +916,7 @@ copy_action (symbol_list * rule, int stack_offset) stack_offset = 0; fprintf (faction, "\ncase %d:\n", nrules); - if (!nolinesflag) + if (!no_lines_flag) fprintf (faction, "#line %d \"%s\"\n", lineno, infile); putc ('{', faction); @@ -1058,7 +1055,7 @@ copy_guard (symbol_list * rule, int stack_offset) stack_offset = 0; fprintf (fguard, "\ncase %d:\n", nrules); - if (!nolinesflag) + if (!no_lines_flag) fprintf (fguard, "#line %d \"%s\"\n", lineno, infile); putc ('{', fguard); @@ -1303,7 +1300,7 @@ readgram (void) { if (t == IDENTIFIER || t == BAR) { - int actionflag = 0; + int action_flag = 0; int rulelength = 0; /* number of symbols in rhs of this rule so far */ int xactions = 0; /* JF for error checking */ bucket *first_rhs = 0; @@ -1399,7 +1396,7 @@ readgram (void) /* If we just passed an action, that action was in the middle of a rule, so make a dummy rule to reduce it to a non-terminal. */ - if (actionflag) + if (action_flag) { bucket *sdummy; @@ -1433,7 +1430,7 @@ readgram (void) p1->next = p; p1 = p; - actionflag = 0; + action_flag = 0; } if (t == IDENTIFIER) @@ -1447,7 +1444,7 @@ readgram (void) else /* handle an action. */ { copy_action (crule, rulelength); - actionflag = 1; + action_flag = 1; xactions++; /* JF */ } rulelength++; @@ -1478,10 +1475,10 @@ readgram (void) else if (t == LEFT_CURLY) { /* This case never occurs -wjh */ - if (actionflag) + if (action_flag) complain (_("two actions at end of one rule")); copy_action (crule, rulelength); - actionflag = 1; + action_flag = 1; xactions++; /* -wjh */ t = lex (); } @@ -1622,7 +1619,7 @@ output_token_defines (FILE *file) continue; fprintf (file, "#define\t%s\t%d\n", symbol, - ((translations && !rawtoknumflag) + ((translations && !raw_flag) ? bp->user_token_number : bp->value)); if (semantic_parser) fprintf (file, "#define\tT%s\t%d\n", symbol, bp->value); @@ -1750,7 +1747,7 @@ packsymbols (void) error_token_number = errtoken->value; - if (!noparserflag) + if (!no_parser_flag) output_token_defines (ftable); if (startval->class == unknown_sym) @@ -1760,7 +1757,7 @@ packsymbols (void) start_symbol = startval->value; - if (definesflag) + if (defines_flag) { output_token_defines (fdefines); @@ -1892,7 +1889,6 @@ reader (void) semantic_parser = 0; pure_parser = 0; - yylsp_needed = 0; grammar = NULL; @@ -1918,7 +1914,7 @@ reader (void) fprintf (ftable, "\ /* %s, made from %s\n\ by GNU bison %s. */\n\ -\n", noparserflag ? "Bison-generated parse tables" : "A Bison parser", infile, VERSION); +\n", no_parser_flag ? "Bison-generated parse tables" : "A Bison parser", infile, VERSION); fputs ("#define YYBISON 1 /* Identify Bison output. */\n\n", ftable); read_declarations (); @@ -1933,7 +1929,7 @@ reader (void) reader_output_yylsp (fdefines); /* Write closing delimiters for actions and guards. */ output_trailers (); - if (yylsp_needed) + if (locations_flag) fputs ("#define YYLSP_NEEDED\n\n", ftable); /* Assign the symbols their symbol numbers. Write #defines for the token symbols into FDEFINES if requested. */ @@ -1949,26 +1945,22 @@ reader (void) void reader_output_yylsp (FILE *f) { - if (yylsp_needed) + if (locations_flag) fputs ("\ \n\ #ifndef YYLTYPE\n\ -typedef\n\ - struct yyltype\n\ -\ - {\n\ - int timestamp;\n\ - int first_line;\n\ - int first_column;\ +typedef struct yyltype\n\ +{\n\ + int timestamp;\n\ + int first_line;\n\ + int first_column;\ \n\ - int last_line;\n\ - int last_column;\n\ - char *text;\n\ - }\n\ -\ - yyltype;\n\ + int last_line;\n\ + int last_column;\n\ + char *text;\n\ +} yyltype;\n\ \n\ -#define YYLTYPE yyltype\n\ +# define YYLTYPE yyltype\n\ #endif\n\ \n", f); diff --git a/src/reduce.c b/src/reduce.c index b326d3a8..60c7c2e8 100644 --- a/src/reduce.c +++ b/src/reduce.c @@ -483,7 +483,7 @@ dump_grammar (void) static void print_notices (void) { - if (yaccflag && nuseless_productions) + if (yacc_flag && nuseless_productions) fprintf (stderr, _("%d rules never reduced\n"), nuseless_productions); fprintf (stderr, _("%s contains "), infile); @@ -523,7 +523,7 @@ reduce_grammar (void) reduced = (bool) (nuseless_nonterminals + nuseless_productions > 0); - if (verboseflag) + if (verbose_flag) print_results (); if (reduced == FALSE) @@ -537,14 +537,14 @@ reduce_grammar (void) reduce_grammar_tables (); #if 0 - if (verboseflag) + if (verbose_flag) { fprintf (foutput, "REDUCED GRAMMAR\n\n"); dump_grammar (); } #endif - if (statisticsflag) + if (statistics_flag) fprintf (stderr, _("reduced %s defines %d terminal%s, %d nonterminal%s\ , and %d production%s.\n"), infile, diff --git a/src/vmsgetargs.c b/src/vmsgetargs.c index cc4670e3..cea0522f 100644 --- a/src/vmsgetargs.c +++ b/src/vmsgetargs.c @@ -27,14 +27,14 @@ Boston, MA 02111-1307, USA. */ * VMS version of getargs: Uses DCL command parsing * (argc and argv are ignored) */ -int verboseflag; -int definesflag; -int debugflag; -int nolinesflag; -extern int noparserflag; -extern int toknumflag; -extern int rawtoknumflag; -extern int yaccflag; +int verbose_flag; +int defines_flag; +int debug_flag; +int no_lines_flag; +extern int no_parser_flag; +extern int token_table_flag; +extern int raw_flag; +extern int yacc_flag; extern char * version_string; /* Allocate storgate and initialize, since bison uses them elsewhere. */ @@ -50,27 +50,27 @@ getargs(argc,argv) static char output_spec[256], name_prefix_spec[256], file_prefix_spec[256]; extern char *infile; - verboseflag = 0; - definesflag = 0; - debugflag = 0; - yaccflag = 0; - nolinesflag = 0; - noparserflag = 0; - toknumflag = 0; - rawtoknumflag = 0; + verbose_flag = 0; + defines_flag = 0; + debug_flag = 0; + yacc_flag = 0; + no_lines_flag = 0; + no_parser_flag = 0; + token_table_flag = 0; + raw_flag = 0; /* * Check for /VERBOSE qualifier */ - if (cli_present("BISON$VERBOSE")) verboseflag = 1; + if (cli_present("BISON$VERBOSE")) verbose_flag = 1; /* * Check for /DEFINES qualifier */ - if (cli_present("BISON$DEFINES")) definesflag = 1; + if (cli_present("BISON$DEFINES")) defines_flag = 1; /* * Check for /FIXED_OUTFILES qualifier */ - if (cli_present("BISON$FIXED_OUTFILES")) yaccflag = 1; - if (cli_present("BISON$YACC")) yaccflag = 1; + if (cli_present("BISON$FIXED_OUTFILES")) yacc_flag = 1; + if (cli_present("BISON$YACC")) yacc_flag = 1; /* * Check for /VERSION qualifier */ @@ -78,23 +78,23 @@ getargs(argc,argv) /* * Check for /NOLINES qualifier */ - if (cli_present("BISON$NOLINES")) nolinesflag = 1; + if (cli_present("BISON$NOLINES")) no_lines_flag = 1; /* * Check for /NOPARSER qualifier */ - if (cli_present("BISON$NOPARSER")) noparserflag = 1; + if (cli_present("BISON$NOPARSER")) no_parser_flag = 1; /* * Check for /RAW qualifier */ - if (cli_present("BISON$RAW")) rawtoknumflag = 1; + if (cli_present("BISON$RAW")) raw_flag = 1; /* * Check for /TOKEN_TABLE qualifier */ - if (cli_present("BISON$TOKEN_TABLE")) toknumflag = 1; + if (cli_present("BISON$TOKEN_TABLE")) token_table_flag = 1; /* * Check for /DEBUG qualifier */ - if (cli_present("BISON$DEBUG")) debugflag = 1; + if (cli_present("BISON$DEBUG")) debug_flag = 1; /* * Get the filename */ diff --git a/tests/calc.m4 b/tests/calc.m4 index af1b298d..a8756206 100644 --- a/tests/calc.m4 +++ b/tests/calc.m4 @@ -38,7 +38,6 @@ AT_DATA([calc.y], ]$4[ static int power (int base, int exponent); -static int read_signed_integer (FILE *stream); static void yyerror (const char *s); static int yylex (void); extern void perror (const char *s); @@ -88,33 +87,67 @@ FILE *yyin; static void yyerror (const char *s) { +#ifdef YYLSP_NEEDED + fprintf (stderr, "%d.%d:%d.%d: ", + yylloc.first_line, yylloc.first_column, + yylloc.last_line, yylloc.last_column); +#endif fprintf (stderr, "%s\n", s); } static int -read_signed_integer (FILE *stream) +yygetc () +{ + int res = getc (yyin); +#ifdef YYLSP_NEEDED + if (res == '\n') + { + yylloc.last_line++; + yylloc.last_column = 0; + } + else + yylloc.last_column++; +#endif + return res; +} + + +static void +yyungetc (int c) +{ +#ifdef YYLSP_NEEDED + /* Wrong when C == `\n'. */ + yylloc.last_column--; +#endif + ungetc (c, yyin); +} + +static int +read_signed_integer (void) { - int c = getc (stream); + int c = yygetc (); int sign = 1; int n = 0; if (c == '-') { - c = getc (stream); + c = yygetc (); sign = -1; } while (isdigit (c)) { n = 10 * n + (c - '0'); - c = getc (stream); + c = yygetc (); } - ungetc (c, stream); + yyungetc (c); return sign * n; } + + /*---------------------------------------------------------------. | Lexical analyzer returns an integer on the stack and the token | | NUM, or the ASCII character read if not a number. Skips all | @@ -126,19 +159,32 @@ yylex (void) { int c; +#ifdef YYLSP_NEEDED + yylloc.first_column = yylloc.last_column; + yylloc.first_line = yylloc.last_line; +#endif + /* Skip white space. */ - while ((c = getc (yyin)) == ' ' || c == '\t') - ; + while ((c = yygetc ()) == ' ' || c == '\t') + { +#ifdef YYLSP_NEEDED + yylloc.first_column = yylloc.last_column; + yylloc.first_line = yylloc.last_line; +#endif + } + /* process numbers */ if (c == '.' || isdigit (c)) { - ungetc (c, yyin); - yylval = read_signed_integer (yyin); + yyungetc (c); + yylval = read_signed_integer (); return NUM; } + /* Return end-of-file. */ if (c == EOF) return 0; + /* Return single chars. */ return c; } @@ -160,7 +206,7 @@ main (int argn, const char **argv) if (argn == 2) yyin = fopen (argv[1], "r"); else - yyin = stdin; + yyin = stdin; if (!stdin) { @@ -170,6 +216,10 @@ main (int argn, const char **argv) #if YYDEBUG yydebug = 1; +#endif +#ifdef YYLSP_NEEDED + yylloc.last_column = 0; + yylloc.last_line = 1; #endif yyparse (); return 0; @@ -204,14 +254,19 @@ AT_DEFINE([_AT_CHECK_CALC], [1], [], [])])]) -# _AT_CHECK_CALC_ERROR(BISON-OPTIONS, INPUT, [IF-YYERROR-VERBOSE]) -# ---------------------------------------------------------------- +# _AT_CHECK_CALC_ERROR(BISON-OPTIONS, INPUT, +# [ERROR-LOCATION], [IF-YYERROR-VERBOSE]) +# ------------------------------------------------------------ # Run `calc' on INPUT, and expect STDERR. AT_DEFINE([_AT_CHECK_CALC_ERROR], [AT_CHECK([echo "$2" | calc 2>&1 >/dev/null | grep 'parse error' >&2], 0, [], - [parse error[]ifelse(regexp([$1], [--yyerror-verbose]), - [-1], [], [$3]) +[ifelse(regexp([$1], [--location]), + [-1], [], [$3: ])[]dnl +parse error[]dnl +ifelse(regexp([$1], [--yyerror-verbose]), + [-1], [], [$4])[]dnl + ])]) @@ -248,13 +303,22 @@ _AT_CHECK_CALC([$1], # Some parse errors. _AT_CHECK_CALC_ERROR([$1], [+1], + [1.0:1.1], [, unexpected `'+'']) _AT_CHECK_CALC_ERROR([$1], [1//2], + [1.2:1.3], [, unexpected `'/'', expecting `NUM' or `'-'' or `'('']) _AT_CHECK_CALC_ERROR([$1], [error], + [1.0:1.1], [, unexpected `$undefined.']) _AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3], + [1.6:1.7], [, unexpected `'='']) +_AT_CHECK_CALC_ERROR([$1], + [ ++1], + [2.0:2.1], + [, unexpected `'+'']) AT_CLEANUP(calc calc.c calc.h calc.output) ])# AT_CHECK_CALC @@ -271,12 +335,15 @@ AT_CHECK_CALC() AT_CHECK_CALC([--raw]) AT_CHECK_CALC([--defines]) +AT_CHECK_CALC([--locations]) AT_CHECK_CALC([--name-prefix=calc]) AT_CHECK_CALC([--verbose]) AT_CHECK_CALC([--yacc]) AT_CHECK_CALC([--yyerror-verbose]) -AT_CHECK_CALC([--defines --name-prefix=calc --verbose --yacc --yyerror-verbose]) -# When --debug, a lot of data is sent to STDERR, we can't test it. +AT_CHECK_CALC([--locations --yyerror-verbose]) + +AT_CHECK_CALC([--defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose]) + AT_CHECK_CALC([--debug]) -AT_CHECK_CALC([--debug --defines --name-prefix=calc --verbose --yacc --yyerror-verbose]) +AT_CHECK_CALC([--debug --defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose])