Reputation: 2916
I've setup a flex/bison system that can run and parse through yyparse and is able to be used inside a repl-type system. When a user enters a certain state (say: defining a method), and an systax error has been made, i'm looking for a way to deal with this in such a way, that bison can act like the user has never entered the line at all.
For instance:
1> class foo { <enter>
2> a = 1; <enter>
3> asfasdfa <enter>
In this case, the parser should return back to the state it was prior to reading the last line. Right now, it will trigger a syntax error and rewinds the complete stack.
I know that bisons yyparse() function has got some local stack state values (yyvsp, yyssp and yylsp) that is used for state tracking, but there seems no way to modify these outside the standard functionality like YYERROR etc.
My question is: is it even remotely possible to do these save/restore states in bison, and if not, what is the best way to deal with such errors.
Upvotes: 0
Views: 375
Reputation: 8769
You should have a look at push parsers in Bison, they might provide you with the control you are looking for.
http://www.gnu.org/software/bison/manual/html_node/Push-Decl.html
Upvotes: 2
Reputation: 241901
bison doesn't have a facility to save and restore parser states. You could probably create one by reverse-engineering the bison skeleton, but that would be a lot of work, and it would also be quite fragile since bison's runtime might change without notice. (To be fair, you're not forced to update bison, but you probably want the flexibility to be able to do so.)
However, parsing is pretty fast. In a REPL environment, you don't notice the parsing time. So there's nothing stopping you from simply reparsing the input from the beginning, up to the line before the error.
However, there's a catch: bison parsers don't necessarily detect a syntax error until the next token is read (although sometimes they do, depending on the precise nature of the error). So you cannot be sure that the last line read is the line actually containing the syntax error.
Upvotes: 2