06.01.2015 Views

Manual

Manual

Manual

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

112 Bison 2.3<br />

{<br />

}<br />

yyparse ("input");<br />

yyparse ("input");<br />

return 0;<br />

If the file ‘input’ contains<br />

input:1: Hello,<br />

input:2: World!<br />

then instead of getting the first line twice, you get:<br />

$ flex -ofirst-line.c first-line.l<br />

$ gcc -ofirst-line first-line.c -ll<br />

$ ./first-line<br />

input:1: Hello,<br />

input:2: World!<br />

Therefore, whenever you change yyin, you must tell the Lex-generated scanner to discard<br />

its current buffer and switch to the new one. This depends upon your implementation of<br />

Lex; see its documentation for more. For Flex, it suffices to call ‘YY_FLUSH_BUFFER’ after<br />

each change to yyin. If your Flex-generated scanner needs to read from several input<br />

streams to handle features like include files, you might consider using Flex functions like<br />

‘yy_switch_to_buffer’ that manipulate multiple input buffers.<br />

If your Flex-generated scanner uses start conditions (see section “Start conditions” in<br />

The Flex <strong>Manual</strong>), you might also want to reset the scanner’s state, i.e., go back to the<br />

initial start condition, through a call to ‘BEGIN (0)’.<br />

11.3 Strings are Destroyed<br />

My parser seems to destroy old strings, or maybe it loses track of<br />

them. Instead of reporting ‘"foo", "bar"’, it reports<br />

‘"bar", "bar"’, or even ‘"foo\nbar", "bar"’.<br />

This error is probably the single most frequent “bug report” sent to Bison lists, but is<br />

only concerned with a misunderstanding of the role of the scanner. Consider the following<br />

Lex code:<br />

%{<br />

#include <br />

char *yylval = NULL;<br />

%}<br />

%%<br />

.* yylval = yytext; return 1;<br />

\n /* IGNORE */<br />

%%<br />

int<br />

main ()<br />

{<br />

/* Similar to using $1, $2 in a Bison action. */<br />

char *fst = (yylex (), yylval);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!