[antlr-interest] Help converting a parser grammar to a tree grammar

Diligent Warrior diligent.warrior at gmail.com
Mon Dec 1 16:27:58 PST 2008


Hello everyone,

I'm working on a grammar for a Java-like language and I'm stuck at this one
part. I'm looking to write the tree grammar from this parser grammar:

callExpression
    :    (primary -> primary) (s=selector -> ^(CALL $callExpression $s))*
    ;

The code would look like:

nodeObject.getLeft().getValue();

And the tree would look like:

( CALL ( CALL nodeObject getLeft ) getValue )

...or in other words:

( CALL ( CALL primary selector ) selector )

Theoretically, the tree should be as deep as it needs to be.

My initial two attempts at the tree grammar were these:

callExpression
    :    primary (^(CALL callExpression selector))*
    ;

callExpression
    :    ( ^( CALL ( primary | callExpression ) selector? ) )
    ;

But after giving them some thought, they don't make sense. Am I at least on
the right track?

So my questions are:
A) Is there a way to translate my parser grammar into a tree grammar?
B) If not, Is there a better way to write my parser grammar?

Ugh, now I'm beginning to doubt my parser grammar isn't even correct.

Any thoughts?

Oh, and for the sake of completeness, here is my tree grammars for primary
and selector

primary
    :    THIS
    |    TRUE
    |    FALSE
    |    INTLIT
    |    ID
    |    ^( CREATE OBJECT ID )
    ;

selector
    :    ^( ID ( ( expression )+ )? )
    ;

As you probably can tell, the only types I have are int, boolean and
objects.

//Thanks for you time,
//DW
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20081202/e669cca5/attachment.html 


More information about the antlr-interest mailing list