I am an absolute beginner in yacc/lex and I stumble upon something that seems simple to me, but I am unable to understand. I have the two following rules : S : E;
and E : STR;
(and in the lexer, [a-z]+
is mapped to STR
). My guess is that when I give the input "hithere" for example, the input is consumed and the parser should exit, no ?
The thing is, the parser is still waiting for input, so somehow S : E
is not consumed (or so I guess). If I continue giving input, a syntax error is raised (which is expected).
My question is, in which case does the parser stop asking for input ? Maybe more precisely, why is the rule S : E;
not satisfied for my specific example ?
I attach here my .l and my .y files :
test1.l
:
%{
#include <stdio.h>
#include <stdlib.h>
#include "y.tab.h"
%}
%option noyywrap
%%
[a-z]+ {yylval.str = yytext; return (STR);}
. { ; }
%%
test1.y
:
%{
#include <stdio.h>
#include <stdlib.h>
extern int yylex();
%}
%union {
char *str;
}
%token <str> STR
%type <str> E
%%
S : E {printf("%s\n", $1);}
;
E : STR {$$ = $1;}
;
%%
int yyerror(char *msg) {
printf("%s\n", msg);
return (0);
}
int main() {
yyparse();
return (0);
}
The thing that seems really weird to me is that if I give the input "hithere", "hithere" is printed back on my terminal, so that is a strong indicator to me that S : E;
actually has been recognized and printf()
executed.