Problems with synching after a syntactic error
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Lance_Walton
Posted On:   Thursday, July 17, 2003 10:08 AM

Hi. I have the following grammar: class MyParser extends Parser; options { k = 1; exportVocab=MyVocab; defaultErrorHandler = true; buildAST = false; } tokens { Shutdown = "shutdown"; Quit = "quit"; Help = "help"; } commandSequence : (command)* (shutdown | Quit) ; command : help ; help : Help { // some action }; shutdown: Shutdown { // another action }; // Lexer omitted I have Java that starts it up like this: MyParser parser = new MyParser(new My   More>>

Hi.

I have the following grammar:

			
class MyParser extends Parser;

options {
k = 1;
exportVocab=MyVocab;
defaultErrorHandler = true;
buildAST = false;
}

tokens {
Shutdown = "shutdown";
Quit = "quit";
Help = "help";
}

commandSequence :
(command)*
(shutdown | Quit)
;

command :
help
;

help : Help { // some action };

shutdown: Shutdown { // another action };

// Lexer omitted


I have Java that starts it up like this:

			
MyParser parser = new MyParser(new MyLexer(System.in));
parser.commandSequence();


This works fine as long as I type in 'help', 'quit' or 'shutdown', which is great.

If I type in anything else, (i.e. 'bob') I get a message like 'line 1:6: unexpected token: bob'. Typing in 'help', 'quit' or 'shutdown' after this doesn't seem to have any effect (i.e. 'quit' and 'shutdown' don't terminate the parsing and 'help' doesn't cause the action that it previously did). What I want is to receive the error message but then carry on parsing as before.

I've read the bit in the ANTLR manual entitled 'defaultErrorHandler: Controlling default exception-handling', which indicates that ANTLR will 'synchronize to the follow set of the rule' after an exception occurs. In my case though, this doesn't seem to be working as I had hoped.

I guess that there's something wrong with my set up. Can anyone help?

Regards,

Lance

   <<Less

Re: Problems with synching after a syntactic error

Posted By:   Monty_Zukowski  
Posted On:   Friday, July 18, 2003 08:28 AM

You probably need to catch the exception yourself. First, look at the generated code to see what it is doing. Then read the docs on exception handling and put an exception handler in your (command)* closure.
About | Sitemap | Contact