[antlr-interest] C++ beginner questions

Bryan Ewbank ewbank at gmail.com
Thu Sep 29 16:39:45 PDT 2005


Hi David,

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

On 9/29/05, David Maxwell <david at crlf.net> wrote:
> 1) Is there a list of Java -> C++ equivalent methods?

There was none I could find, but in general if you replace "." with
"->" for function calls you are dead on.  Likewise, replace "." with
"::" for data members is a pretty good guess.  For your example, I
seem to remember that $setType(Token::SKIP) works okay -- but I use a
lex-based scanner, as in one of the C++ examples (seach for
example/c++/*.l for guidance :-)

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

This has been an issue for me to; I'm sure there are some workarounds
that I've done in C++ that could be done more elegantly, but it
works...

> 2) Are there any examples targetting C++ that do NOT use the AST
> funcionality?

Not as far as I know.  You might reconsider using a tree to split
"parse" from "operate"; I've found it a very powerful model even when
replacing a yacc product.

> For example, I can't find even a single instance of using a token's
> text, in a C++ example. Reading the generated C++ doesn't make it any
> clearer either.
>
> I've labelled tokens in the _Paser_ and used ->getText() there, but in
> the _Lexer_ it doesn't work for me. The label ends up becoming a goto
> target label, and not an object with a getText() method. Even with gdb
> on the generated program, I can't find any variable that holds the
> token's text.

I think you can use "#tokenname" in the lexer, but I have to again
shrug here, as I use lex.

> 4) Is there any equivalent to the Lex/Yacc documentation 'How to resolve
> shift/reduce conflicts' - for how to address lexical nondeterminisms in
> antlr?
>
> For example, I've noticed that rule order in the Lexer DOES matter, but
> I can't find any documentation about how to order rules to get the
> desired results. I've just been following 'most specific first', but
> I'd appreciate a more precise answer.

Focusing on the "yacc" side of this, I have been very generous with
"greedy=true" in my grammar; it's not always needed - in fact, I seem
to remember it's the default behavior(?) - but it always clears up
what's going on.

The key thing to resolve conflicts is to eliminate duplicate left
prefixes from your rules.  You can't simple take a yacc grammar and
throw it at ANTLR (LALR -> LL; ugh).  Common left-prefixes will cause
you grief, especially coming from yacc.  I seem to recall being unable
to see that type of problem for several days/weeks.  I've been a happy
antlr user now for about a year, and I would prefer not to go back
(but my boss has other ideas :-).

> I'll probably have more questions as I do more, but that's enough for
> now ;-)

This is the place; welcome to our little corner of the web!


More information about the antlr-interest mailing list