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

Ruslan Zasukhin ruslan_zasukhin at valentina-db.com
Wed Sep 7 06:35:11 PDT 2011


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