tags:

views:

94

answers:

2

so, I am parsing Hayes modem AT commands. Not read from a file, but passed as char * (I am using C).

1) what happens if I get something that I totally don't recognize? How do I handle that? 2) what if I have something like

my_token: "cmd param=" ("value_1" | "value_2");

and receive an invalid value for "param"?

I see some advice to let the back-end program (in C) handle it, but that goes against the grain for me. Catch teh problem as early as you can, is my motto.

Is there any way to catch "else" conditions in lexer/parser rules?

Thanks in advance ...

+1  A: 

That's the thing: the whole point of your parser and lexer is to blow up if you get bad input, then you catch the blow up and present a pretty error message to the user.

RCIX
oops, it's not as easy as that. I forgot to mention what I said in another thread. The input is not in a file which is parsed all at once, as a compiler might.It's for an embedded system. Whenever the user sends an AT command then that command is parsed and executed. Some time later comes another command ...Anyway, I could validate the input in the generated C code, but I prefer to detect it as early as possible. I want to catch it in the lexer or parser and then use a quick C code to reply "ERROR".
Mawg
A: 

I think you're looking for Custom Syntax Error Recovery to embed in your grammar.

EDIT

I've no experience with ANTLR and C (or C alone for that matter), so follow this advice with caution! :)

Looking at the page: http://www.antlr.org/api/C/using.html, perhaps the part at the bottom, Implementing Customized Methods is what you're after.

HTH

Bart Kiers
Mawg
The example says<pre>myRule: TOK TOK1 TOK2 subRule?;catch [RecognitionException re] { // First, let's report the error as the user needs to know about it // reportError(re); myRecoverMethodForMyRule();}</pre>But how can I implement it? have one rule for valid input, followed by one where I wildcard the tokens?Thanks for your helkp so far, you are getting me close to it...
Mawg
Or should I be overriding recoverFromMismatchedToken(() ?
Mawg
mawg, see my edit.
Bart Kiers
Thanks, that look like it will do it (it will have to ;-)Let's hope that it is not in the generated code, in case I regenerate ;-)
Mawg
It looks to me that it's pretty much the same as how I embed code in ANTLR and Java: in the grammar itself. And when generating the lexer/parser, the code from the grammar gets injected in the lexer/parser source files.
Bart Kiers
Mawg
And I guess I can't sayFood : 'curry' | 'icecream';eat: 'swallow' Food;and catch'swallow beer' as an error?
Mawg