In the extremely simple example below, I want to read in the language of a single a
and assure that no remaining characters come after.
File: example.y
%{
#include <stdio.h>
#include <ctype.h>
int yylex(void);
int yyerror(char *s);
%}
%token A
%token END
%token JUNK
%% /* Grammar Rules */
accept: A END { printf("language accepted!\n"); }
;
%%
File: example.in
%{
#include "ex.tab.h"
#define YY_NO_INPUT
%}
%option nounput
%%
a printf("A found\n"); return A;
<<EOF>> { printf("EOF found\n"); return END; }
. { printf("JUNK found\n"); return JUNK; }
%%
The results of compiling and running this program with the following test input file:
a
produces the following output:
A found
EOF found
language accepted!
EOF found
Error: syntax error
Because EOF is read twice, I think that's why the program is not accepting my input language. My question is, why is EOF being read twice and how do I stop it?
Also, doing the above without the EOF rule causes inputs such as
abbbb
to print the "accept" message but then immediately fail because of the excess input. All I want is either a pass or a fail which is why I'm trying to use EOF to verify I will have one result.