Finding a very simple example of ANTLR error handling when generating C code

I want to create C code. I will not read from the input file, one line at a time (like, for example, the compiler). Rather, I will parse user input as it arrives one line at a time.

I would prefer to detect and handle bad input in 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 cannot catch it in lexer / parser, it seems to me that I need to use it displayRecognitionError(), but like <

Can someone point me to a very simple example that generates C code and shows some error handling for invalid input?

Thanks!


Alright, generosity, jupipe!

But only for a real, working answer, with real working code. There is no "use the X () method" without wxample.

+5
2

Java :

grammar X;

// ...

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

// parser rules

// lexer rules 

, C @rulecatch{ ... }.

+5

, , , displayRecognitionError(). , , C.

, . C ++, , .

+7

All Articles