[antlr-interest] TreeParser grammar development: How to match an or-based parser grammar rule in the AST?

luciano mantuaneli mantu_lists at yahoo.com.br
Thu Sep 14 05:10:18 PDT 2006


Greetings.
I'm developing a TreeParser to run through the tree generated by this Java 1.4 grammar . I'm having some dificulty in match interface methods, and I guess that the problem lives in my incapacity on understand how to match, in my TreeParser,  this rule:

field!
    : ...

        |    // A generic method has the typeParameters before the return type.
            // This is not allowed for variable definitions, but this production
            // allows it, a semantic check could be used if you wanted.
            (typeParameters)? t:typeSpec[false]  // method or variable declaration(s)
            (    IDENT  // the name of the method

                // parse the formal parameter declarations.
                LPAREN! param:parameterDeclarationList RPAREN!

                rt:declaratorBrackets[#t]

                // get the list of exceptions that this method is
                // declared to throw
                (tc:throwsClause)?

                ( s2:compoundStatement | SEMI )
                {#field = #(#[METHOD_DEF,"METHOD_DEF"],
                             mods,
                             #(#[TYPE,"TYPE"],rt),
                             IDENT,
                             param,
                             tc,
                             s2);}
            |    v:variableDefinitions[#mods,#t] SEMI
//                {#field = #(#[VARIABLE_DEF,"VARIABLE_DEF"], v);}
                {#field = #v;}
            )
        )
        |...
        ;

I guess my problem is how to match the "( s2:compoundStatement | SEMI )" in my TreeParser.
Here is the rule of my TreeParser which (try to) matches an method declaration:

...
    methodDef{
        String type, name;
        ArrayList types = new ArrayList();
    }:
        #(METHOD_DEF dontCare type=typeDef name=anything (parameterList)? (throwsDecl)? methodBlock/*block*/)
    ;
...
    methodBlock{
    }:
        stmList|
        SEMI
    ;
...
    anything returns [String result]{
        result = "";
    }:
        literal:.{
            result = literal.getText();
        }
    ;
    dontCare:
        .
    ;

    


If I run my TreeParser, I got  the message "<AST>:0:0: unexpected end of subtree" every time when a interface method (ended with ";") is found, and then I "lose" those methods.
My question: What am I doing wrong? Why my code can't match  the  text???
Please, help me people!...
If you wish, you can get the full code which I'm developing in this site:
http://www.comp.ufscar.br/~lumantu/Projetos/J2CSharp/Out/

Thanks in advance
My best regards
Mantu
 		
---------------------------------
 Yahoo! Acesso Grátis - Internet rápida e grátis. Instale o discador agora! 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20060914/b5aa22ec/attachment.html 


More information about the antlr-interest mailing list