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 directory %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.
Sometimes I deal with these cases by flattening my grammars.
In your case, it might make sense to add tokens to your lexer for newline and command separators (;) so you can explicitly put them in your Bison grammar, so the parser will expect a full line of input for a command before accepting as a commmand.
Or, for an arbitrary list of arguments like a real shell: