[antlr-interest] [v2 to v3][C++/C] throw C++ exception from parser/tree parser.

Jim Idle jimi at temporal-wave.com
Wed Sep 7 09:36:45 PDT 2011


You remember that this is all open source, freely given right? Generally,
don't try and mix C++ things in with the C. Exceptions are almost
certainly not want you want for error reporting while parsing anyway.

As I said earlier, copy the existing routine and adapt it. It does as many
things as it can to show you how to access the information. I can't
provide a universal error message handler as there is no way to know what
information your particular parser will have available or how you want the
messages to look and so on. All your customer error handler need do is
call a C++ object that you provide and that object can collect the errors
so that you can print them out at the end etc. The source code is right
there and well commented :)

Jim





> -----Original Message-----
> From: Ruslan Zasukhin [mailto:ruslan_zasukhin at valentina-db.com]
> Sent: Wednesday, September 07, 2011 6:35 AM
> To: antlr-interest at antlr.org; Jim Idle
> Subject: [v2 to v3][C++/C] throw C++ exception from parser/tree parser.
>
> Hi Terrence,
> Hi Jim,
>
> First of all again: thank you for great job and product(s), Please do
> not take below my text as complains, but mainly as explanation where
> C++ developer get problems with ANTLR3 and why ...
> And some suggestions how this can be may be improved.
> Long letter but should be easy to read :-)
>
>
> ===================================
> So ... in ANTLR2 it was very simple to handle errors from box:
>     it throws exception, few lines of code with catch() -> DONE.
>
> All our db engine expects exceptions from ANTLR v2/v3 and its wrapper
> code.
> Two days I am reading reading reading ...
>
> I have found 2-3 such questions from C++ developers:
>     if we can throw from my displayError()?
>
> Answer from Jim was like this:
>
> > On 1/15/09 8:23 PM, "Jim Idle" <jimi at temporal-wave.com> wrote:
> >
> >> You can probably use them carefully, but as you point out, you have
> >> to be careful with memory. The runtime tracks all its normal memory
> >> allocations so  as long as you close the 'classes' correctly you
> >> should generally be OK. However, you should make sure that throwing
> >> exceptions does not bypass the normal rule clean up, such as
> >> resetting error and backtracking flags and so on,
>
> Okay, but  I'd expect to see more details (x5 - x10 times more text and
> code
> example) at this section
>
>     http://www.antlr.org/api/C/index.html
>
> * what is known TODAY?  at 2011  ?
>         Can we throw here C++ exc?
>         don't this break logic of C code of parsers?
>         True working example
>
> * EXAMPLE?
>
>     I have open folder Examples/C  and made search on "exception".
>     Found only in the JAVA files, used for tests of parser.
>     There is no example for C++ exceptions.
>     There is no example with override displayRecognitionError()
>
> * Or look on this code-example.
>          http://www.antlr.org/api/C/index.html
>
> The only here ERROR-related line is
>     if (psr->pParser->rec->errorCount > 0)
>
> Then silent ...  And questions come to mind
>
> ** So, if not throw() exceptions, then after tree-parser, I check if
> there was any errors and IF they was ... What next ???
>
> I have read that C target builds LIST of exception objects, But where
> is TEXT and example how navigate that list?
>
>
> ** and if I will throw error from displayRecognitionError() then
>     such check of counter is useless ...
>
>
>
> ==========================================
> Okay, next ...
> There is good helpful pages ANTLR2 to ANTLR3 ...   Great!
>
> But this page mainly about grammar and Java. And zero info here to help
> existed C++ developers port their ANTLR2 products.
> Hmm.
>
>
> ================
> Also when I watch default displayRecognitionError() from .c with many
> points, which do print to stderr as:
>
> -----------------------
> void displayRecognitionError(
>     pANTLR3_BASE_RECOGNIZER recognizer,
>     pANTLR3_UINT8*            tokenNames )
> {
> ...
>              ANTLR3_FPRINTF(stderr, "-end of input-(");
> ------------------------
>
>
> I wonder, why not provide here same function, which do sprintf() into
> string buffer,  and TWO very small wrapper-functions, which get this
> string and
>
> 1) print it to stderr as now;
> 2) throw it as c++ exception;
>
> //------------------------------------------------------
> pANTLR3_STRING buildRecognitionError(
>     pANTLR3_BASE_RECOGNIZER recognizer,
>     pANTLR3_UINT8*            tokenNames )
> {
>     ............
>     return resStr;
> }
>
>
> //------------------------------------------------------
> void displayRecognitionError_stderr(
>     pANTLR3_BASE_RECOGNIZER recognizer,
>     pANTLR3_UINT8*            tokenNames )
> {
>       pANTLR3_STRING res = buildRecognitionError( recognizer,
> tokenNames  );
>       ANTLR3_FPRINTF( stderr, res );
> }
>
>
> //------------------------------------------------------
> void displayRecognitionError_throw(
>     pANTLR3_BASE_RECOGNIZER recognizer,
>     pANTLR3_UINT8*            tokenNames )
> {
>       pANTLR3_STRING res = buildRecognitionError( recognizer,
> tokenNames  );
>       throw SomeException( res );
> }
>
>
> Yes, not big deal may be, but will simplify usage of ANTLR3 from box.
> I could read in ANTLR 3.5:
>     just install displayRecognitionError_throw, and your parser
>     will start throw C++ exceptions.
>     30 seconds deal, instead of two days and still in doubts ...
>
>
> --
> Best regards,
>
> Ruslan Zasukhin
> VP Engineering and New Technology
> Paradigma Software, Inc
>
> Valentina - Joining Worlds of Information
> http://www.paradigmasoft.com
>
> [I feel the need: the need for speed]
>


More information about the antlr-interest mailing list