[antlr-interest] Nested Tree Construction vs. Recognition

Randall R Schulz rschulz at sonic.net
Thu Jul 5 07:06:52 PDT 2007


Hi,

I'm curious about whether it's possible to write tree grammar rules
whose right-hand-sides (or alternatives thereof) can have nested tree
node structure.

I can use such nested structures when generating tree content (an AST,
i.e.). E.g.:

module
    :   Open CLModule interpretableName
        ( Open CLExcludes name* Close )?
        phrase*
        Close
    ->  ^( CLModule interpretableName ^( CLExcludes name* )? phrase* )
    ;


But the compiler yells at me when I try to directly recognize such
structures in the receiving tree grammar. To wit:

module
returns [ CLModule cm ]
    :   ^(  CLModule
            interpretableName
            ^(  CLExcludes
                (   name
                    { ... }
                ) *
            ) ?
            (   phrase
                { ... }
            ) *
        )
        {
            ...
        }
    ;

error(100): CLIFBuilder.g:752:6: syntax error: antlr: CLIFBuilder.g:752:6: expecting RPAREN, found '?'
error(100): CLIFBuilder.g:757:3: syntax error: antlr: CLIFBuilder.g:757:3: expecting SEMI, found ')'
error(10):  internal error: CLIFBuilder.g : java.lang.NullPointerException


Line 752 corresponds to the line with the '?' closure and 757
corresponds to the last parenthesis. In both cases, the column number
is at the end of the line in question.


The NPE suggests a bug of some sort, but I don't know if it's just in
the handling of the error or perhaps something to do with a spurious
diagnostic.


Should I be able to have nested tree nodes in rule right-hand-sides of a
tree grammar? This is one of two that have forced me to write separate
rules in the tree grammar that don't directly reflect a rule in the
tree-building grammar.


Randall Schulz


More information about the antlr-interest mailing list