As noted in your links, flex
has syntax for recognizing the end of an input file or stream (e.g., an input from a string).
In fact, flex
effectively has such a rule operating at all times. By default, the rule calls yywrap
. You turned this off (with %noyywrap
). That's fine, except...
The default action on encountering an "EOF token" is to return 0.
The parsers generated by bison (and byacc) need to see this zero token. See this answer to END OF FILE token with flex and bison (only works without it).
Your lexer returns a 0
token on encountering a newline. That will cause all kinds of trouble. and is no doubt leading to what you observe when reading from a file.
Edit: OK, with that out of the way and the update applied, let's consider your grammar.
Remember that bison adds a special production that looks for the zero-token. Let's represent that with $
(as people generally do, or sometimes it's $end
). So your entire grammar (with no actions and with "error" removed since it's also special) is:
$all : input $;
input: word | eof | /* empty */;
word: TWORD;
eof: TEOF;
which means the only sentences your grammar accepts are:
TWORD $
or:
TEOF $
or:
$
So when you call yyparse()
, the loop inside yyparse()
will read-ahead one token from the lexer and accept (and return) the result if the token is the zero-valued end-of-file $
. If not, the token needs to be one of TWORD
or TEOF
(anything else results in a call to yyerror()
and an attempt to resync). If the token is one of the two valid tokens, yyparse()
will call the lexer once more to verify that the next token is the zero-valued end-of-file $
token.
If all of that succeeds, yyparse()
will return success.
Adding the actions back in, you should see printf
output, and get a value stored in parseValue
, based on whichever reduction rule is used to recognize the (at most one) token.