Seeking very simple ANTLR error handling example when generating C code
Asked Answered
C

2

5

I want to generate C code. I will not be reading from an input file, one line at a time (as, for instance, a compiler might). Rather, I will be parsing user input as it arrives, one line at a time.

I would prefer to detect and handle bad input in the lexer/parser, e.g

/* lexer tokens */
foo : "FOO";
bar : "BAR";
baz : "BAZ";
/* grammar*/
grammar : foo "=" BAZ 
        | foo "=" BAR 
        | <some non-existent Antrl-else> :  {printf(stderr, "bad input\n");}
        ;

OK, if I can't catch it in the lexer/parser, it seems like I need to use displayRecognitionError() but how??

Can anyone point me at a very simple example which generates C code and shows some error handling of invalid input?

Thanks!


Ok, bounty, yippee!

But only for a real, working answer, with real, working code. No "use method X()" without an wxample.

Calc answered 5/1, 2010 at 1:59 Comment(4)
FYI: that bounty will go to the answer with the most votes after the days expire, regardless if you accept an answer or not.Hysterotomy
Perhaps this thread is of help: markmail.org/message/… (note the various answers posted to that question!)Hysterotomy
I see you are using printf in your example. Is the language you're using C or C++?Dogear
C, I'm afraid, so no C++ exception handling available.Calc
H
5

Handling a recognition exception in Java would go like this:

grammar X;

// ...

@rulecatch{
  catch(RecognitionException rex) {
    // do something
  }
}

// parser rules

// lexer rules 

In other words, simply add some custom C code inside the @rulecatch{ ... } block.

Hysterotomy answered 5/1, 2010 at 10:49 Comment(5)
Bart, I suspect this is the best answer that I will get. I will leave it open for a few days, juts in case someone gives some brilliant C demo code, then award you the answer if not. thanks for your helpCalc
No problem mawg. If I find some time, I might even whip up a little C demo, but my C is rusty to say the least!Hysterotomy
I've quickly looked at the C runtime: configure-ing and make-ing the runtime for C and generating a lexer & parser went okay, but writing a little application with a main method that uses these lexer & parser files did not, and I didn't feel like spending more time on it, sorry. I hope someone else might be able to give you a hand with it. But did you try what I recommended? If you tried but failed, it might help to mention what exactly went wrong. Good luck.Hysterotomy
Thanks for trying Bart. I am drowning in other work, so waiting to see if someone drops the code into my lap for the bounty :-) If not, I will try to follow what you said & figure it out (and you get the points)Calc
Thanks, Bart. When I get a few spare hours (sigh), I will try to follow what you said & figure it out. If I can't, there will no doubt be another question :-)Calc
H
7

What you are most likely looking for is the displayRecognitionError() function. This function is called in the cases that you are interested in, and is part of the C runtime.

If you want to see an example of how to use this function, look at this mailing list post. Although this code mixes C and C++, you should be able to work out what you need from it.

Heterochromosome answered 13/1, 2010 at 15:15 Comment(2)
Thanks very much, I'll check it out. Sorry that I already accepted an answer; I didn't expect another.Calc
No worries - hope that it helps youHeterochromosome
H
5

Handling a recognition exception in Java would go like this:

grammar X;

// ...

@rulecatch{
  catch(RecognitionException rex) {
    // do something
  }
}

// parser rules

// lexer rules 

In other words, simply add some custom C code inside the @rulecatch{ ... } block.

Hysterotomy answered 5/1, 2010 at 10:49 Comment(5)
Bart, I suspect this is the best answer that I will get. I will leave it open for a few days, juts in case someone gives some brilliant C demo code, then award you the answer if not. thanks for your helpCalc
No problem mawg. If I find some time, I might even whip up a little C demo, but my C is rusty to say the least!Hysterotomy
I've quickly looked at the C runtime: configure-ing and make-ing the runtime for C and generating a lexer & parser went okay, but writing a little application with a main method that uses these lexer & parser files did not, and I didn't feel like spending more time on it, sorry. I hope someone else might be able to give you a hand with it. But did you try what I recommended? If you tried but failed, it might help to mention what exactly went wrong. Good luck.Hysterotomy
Thanks for trying Bart. I am drowning in other work, so waiting to see if someone drops the code into my lap for the bounty :-) If not, I will try to follow what you said & figure it out (and you get the points)Calc
Thanks, Bart. When I get a few spare hours (sigh), I will try to follow what you said & figure it out. If I can't, there will no doubt be another question :-)Calc

© 2022 - 2024 — McMap. All rights reserved.