views:

332

answers:

2

I am using a C lexer that is Flex-generated, and a C++ parser that is Bison-generated. I have modified the parser to acccept only string input.

I am calling the parser function yyparse() in a loop, and reading line by line of user input. I stop the loop if the input is "quit".

The problem I am facing is that when input doesn't match any rule, then the parser stops abruptly, and at next iteration starts off at same state, expecting the rule which was stopped (due to syntax error) to complete.

It works fine if the input is valid and matches a parser rule.

On syntax error I have redefined the yyerror() function, that displays a simple error message.

How do I clear the state of the parser when the input doesn't match any parser rule, so that at next iteration the parser starts afresh?

+2  A: 

According to my Lex & Yacc book there is a function yyrestart(file) .

Else (and I quote a paragraph of the book:

This means that you cannot restart a lexer just by calling yylex(). You have to reset it into the default state using BEGIN INITIAL, discard any input text buffered up by unput(), and otherwise arrange so that the next call to input() will start reading the new input.

Gamecat
A: 

Interesting question - I have a parser that can be compiled with Bison, Byacc, MKS Yacc or Unix Yacc, and I don't do anything special to deal with resetting the grammar whether it fails or succeeds. I don't use a Flex or Lex tokenizer; that is hand-coded, but it works strictly off strings. So, I have to agree with Gamecat; the most likely cause of the trouble is the lexical analyzer, rather than the parser proper.

(If you want to obtain my code, you can download SQLCMD from the IIUG (International Informix User Group) web site. Although the full product requires Informix ESQL/C, the grammar can, in principle, be converted into a standalone test program. Sadly, however, it appears I've not run that test for a while - there are some issues with the test compilation. Some structure element names changed in April 2006, plus there are linkage issues. I will need to re-reorganize the code so that the grammar can be tested standalone again.)

Jonathan Leffler
Always nice if someone agrees with me ;-).
Gamecat