How do I get Bison/YACC to not recognize a command until it parses the whole string?
Posted
by chucknelson
on Stack Overflow
See other posts from Stack Overflow
or by chucknelson
Published on 2010-04-08T15:53:42Z
Indexed on
2010/04/08
16:13 UTC
Read the original article
Hit count: 313
I have some bison grammar:
input: /* empty */
| input command
;
command:
builtin
| external
;
builtin:
CD { printf("Changing to home directory...\n"); }
| CD WORD printf("Changing to directroy %s\n", $2); }
;
I'm wondering how I get Bison to not accept (YYACCEPT?) something as a command
until it reads ALL of the input. So I can have all these rules below that use recursion or whatever to build things up, which either results in a valid command or something that's not going to work.
One simple test I'm doing with the code above is just entering "cd mydir mydir". Bison parses CD
and WORD
and goes "hey! this is a command, put it to the top!". Then the next token it finds is just WORD
, which has no rule, and then it reports an error.
I want it to read the whole line and realize CD WORD WORD
is not a rule, and then report an error. I think I'm missing something obvious and would greatly appreciate any help - thanks!
Also - I've tried using input command NEWLINE
or something similar, but it still pushes CD WORD
to the top as a command and then parses the extra WORD
separately.
© Stack Overflow or respective owner