284 |
Frequently Asked Questions |
Frequently Asked Questions |
285 |
|
|
286 |
* Parser Stack Overflow:: Breaking the Stack Limits |
* Parser Stack Overflow:: Breaking the Stack Limits |
287 |
|
* How Can I Reset @code{yyparse}:: @code{yyparse} Keeps some State |
288 |
* Strings are Destroyed:: @code{yylval} Loses Track of Strings |
* Strings are Destroyed:: @code{yylval} Loses Track of Strings |
289 |
|
|
290 |
Copying This Manual |
Copying This Manual |
6354 |
|
|
6355 |
@menu |
@menu |
6356 |
* Parser Stack Overflow:: Breaking the Stack Limits |
* Parser Stack Overflow:: Breaking the Stack Limits |
6357 |
|
* How Can I Reset @code{yyparse}:: @code{yyparse} Keeps some State |
6358 |
* Strings are Destroyed:: @code{yylval} Loses Track of Strings |
* Strings are Destroyed:: @code{yylval} Loses Track of Strings |
6359 |
@end menu |
@end menu |
6360 |
|
|
6369 |
This question is already addressed elsewhere, @xref{Recursion, |
This question is already addressed elsewhere, @xref{Recursion, |
6370 |
,Recursive Rules}. |
,Recursive Rules}. |
6371 |
|
|
6372 |
|
@node How Can I Reset @code{yyparse} |
6373 |
|
@section How Can I Reset @code{yyparse} |
6374 |
|
|
6375 |
|
The following phenomenon gives raise to several incarnations, |
6376 |
|
resulting in the following typical questions: |
6377 |
|
|
6378 |
|
@display |
6379 |
|
I invoke @code{yyparse} several times, and on correct input it works |
6380 |
|
properly; but when a parse error is found, all the other calls fail |
6381 |
|
too. How can I reset @code{yyparse}'s error flag? |
6382 |
|
@end display |
6383 |
|
|
6384 |
|
@noindent |
6385 |
|
or |
6386 |
|
|
6387 |
|
@display |
6388 |
|
My parser includes support for a @samp{#include} like feature, in |
6389 |
|
which case I run @code{yyparse} from @code{yyparse}. This fails |
6390 |
|
although I did specify I needed a @code{%pure-parser}. |
6391 |
|
@end display |
6392 |
|
|
6393 |
|
These problems are not related to Bison itself, but with the Lex |
6394 |
|
generated scanners. Because these scanners use large buffers for |
6395 |
|
speed, they might not notice a change of input file. As a |
6396 |
|
demonstration, consider the following source file, |
6397 |
|
@file{first-line.l}: |
6398 |
|
|
6399 |
|
@verbatim |
6400 |
|
%{ |
6401 |
|
#include <stdio.h> |
6402 |
|
#include <stdlib.h> |
6403 |
|
%} |
6404 |
|
%% |
6405 |
|
.*\n ECHO; return 1; |
6406 |
|
%% |
6407 |
|
int |
6408 |
|
yyparse (const char *file) |
6409 |
|
{ |
6410 |
|
yyin = fopen (file, "r"); |
6411 |
|
if (!yyin) |
6412 |
|
exit (2); |
6413 |
|
/* One token only. */ |
6414 |
|
yylex (); |
6415 |
|
if (!fclose (yyin)) |
6416 |
|
exit (3); |
6417 |
|
return 0; |
6418 |
|
} |
6419 |
|
|
6420 |
|
int |
6421 |
|
main () |
6422 |
|
{ |
6423 |
|
yyparse ("input"); |
6424 |
|
yyparse ("input"); |
6425 |
|
return 0; |
6426 |
|
} |
6427 |
|
@end verbatim |
6428 |
|
|
6429 |
|
@noindent |
6430 |
|
If the file @file{input} contains |
6431 |
|
|
6432 |
|
@verbatim |
6433 |
|
input:1: Hello, |
6434 |
|
input:2: World! |
6435 |
|
@end verbatim |
6436 |
|
|
6437 |
|
@noindent |
6438 |
|
then instead of getting twice the first line, you get: |
6439 |
|
|
6440 |
|
@example |
6441 |
|
$ @kbd{flex -ofirst-line.c first-line.l} |
6442 |
|
$ @kbd{gcc -ofirst-line first-line.c -ll} |
6443 |
|
$ @kbd{./first-line} |
6444 |
|
input:1: Hello, |
6445 |
|
input:2: World! |
6446 |
|
@end example |
6447 |
|
|
6448 |
|
Therefore, whenever you change @code{yyin}, you must tell the Lex |
6449 |
|
generated scanner to discard its current buffer, and to switch to the |
6450 |
|
new one. This depends upon your implementation of Lex, see its |
6451 |
|
documentation for more. For instance, in the case of Flex, a simple |
6452 |
|
call @samp{yyrestart (yyin)} suffices after each change to |
6453 |
|
@code{yyin}. |
6454 |
|
|
6455 |
@node Strings are Destroyed |
@node Strings are Destroyed |
6456 |
@section Strings are Destroyed |
@section Strings are Destroyed |
6457 |
|
|