[antlr-interest] ANTLR3 C Target

Jim Idle jimi at temporal-wave.com
Wed Dec 26 13:11:12 PST 2007


Consult the source code documentation (see ANTLR API documentation off 
the home page) and the examples in the examples download on the download 
page, under the C examples subdirectory. If you just want to use the 
runtime then you dont really need the source, just the examples, but 
if you want to start overriding things, then you need to read the source 
code or the doxygen docs.

1) Just override the nextToken 'method' call in the token source. Copy 
the code from antlr3Lexer.c, then add your own code. You will slow thing 
down dramatically by doing things like that, but I assume that this is 
debugging purposes etc.
2) See: http://www.antlr.org/api/C/index.html and look at 
antlr3StringStream
3) You need to override things to do what you want. In this case the 
displayRecognitionError() function, which as the documentation says, is 
basically a place holder for your own function. 

I have posted this before, but perhaps the search of the posting 
archives (home page of the web site) isnt bringing it up. To override 
api functions (though there are other ways because it is C and there are 
always other ways), do:

@parser::apifuncs
{
    // Install custom error message display
    //
    RECOGNIZER->displayRecognitionError = produceError;
}

In your parser or tree parser grammar. Then provide:


void produceError
(pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_UINT8 *tokenNames)
{
...
}

Which of course does not actually need to print anything out, it could 
just accumulate a collection of the errors for alter processing by 
something else. 

I usually (like in the default version) increment recognizer->errorCount 
and check if this is 0 or not after parsing, but you can do anything you 
like of course.

Jim

> -----Original Message-----
> From: Maurizio de Pascale [mailto:mdepascale at dii.unisi.it]
> Sent: Wednesday, December 26, 2007 9:39 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] ANTLR3 C Target
> 
> Hi,
> I'm moving my small antlr 2.7 project to antlr 3.
> since it looks like C++ support is (currently?) gone I'm using the C
> runtime.
> Unfortunately, finding info about it (on both the wiki and the web)
> looks quite hard, so I appeal to your good (xmas) will...
> 
> Mostly, I need to reproduce with the C API features of the (old?) 
object
> oriented counterpart.
> 1) I used to have a stream filter between the parser and the lexer for
> logging purpose. Can this be implemented and how?
> 2) How to provide input from a memory buffer instead of a file?
> 3) without exceptions, how to know if errors occured? the starting
> parser rule returns void :( and it looks like the parser simply 
outputs
> error info to the standard output.
> How to get programmatically these info/errors and how to disable the
> error output?
> 
> Thank you in advance and Merry Christmas,
> Maurizio de Pascale
> mdepascale at dii.unisi.it




More information about the antlr-interest mailing list