[antlr-interest] Re: C++ beginner questions

Bill Canfield canfield at computer.org
Fri Sep 30 08:53:29 PDT 2005


> There is a small but thriving C++ contingent here :-)
> 

OK, I'll stand up and be counted for that.  I can answer a couple of 
David's questions.

>>Almost all of the tutorials target Java, and it's often not clear to me
>>how to get the same effect in C++.

As Bryan noted, most things work almost the same.  To get the text of a 
token, use $getText within the action.  ANTLR will replace it with the 
appropriate code when it generates C++.

I'm doing a parser that builds its own structures instead of using ANTLR 
ASTs.  Here's a snippet (from the lexer) that shows some of the stuff in 
action:

> IDENTIFIER
>   :
>     (SimpleID
>     | EscapedID)
>     { if ($getText.find("PATHPULSE$") == 0) {
>         $setType(PATHPULSE);
>       }
>       IdToken *id = new IdToken(_ttype, $getText, getInputFileName(),
>         getLine(), getColumn());
>       $setToken(id);
>     }
>   ;

IdToken is declared like "class IdToken : public antlr::CommonToken".

>>4) Is there any equivalent to the Lex/Yacc documentation 'How to resolve
>>shift/reduce conflicts' - for how to address lexical nondeterminisms in
>>antlr?
>>

First try "greedy=true", like Bryan said.  Another possibility is to 
explicitly code the lookahead yourself by using a semantic predicate. 
Here's an example:

>   |
>     { (LA(1) == IDENTIFIER && LA(2) == COLON && LA(3) == LITERAL_begin)
>       || LA(1) == LITERAL_begin }?
>     stmt = generate_module_block

Syntactic predicates can also remove ambiguity, but *avoid* them as much 
as possible.  They're a real performance dog, they rely on throwing 
exceptions.

And of course, you can factor the rules to truly eliminate the 
nondeterminism.  It's more art than science.

Bill


More information about the antlr-interest mailing list