[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 11:40:23 PDT 2006


Thank you very much, Bryan!
Rewriting like you suggest don't work (I guess that even you expect it, as we don't known how to treat the SEMI token)
But your answer shed some light in the problem...
I don't know the consequences, but here it goues what I did:
I moved the SEMI from "( s2:compoundStatement | SEMI )" and place it in the CompoundStatement rule:

compoundStatement
    :    lc:LCURLY^ {#lc.setType(SLIST);}
            // include the (possibly-empty) list of statements
            (statement)*
        RCURLY!|
        SEMI
    ;

It just work well!
Again, thank you very much, Bryan!!!

Bryan Ewbank <ewbank at gmail.com> escreveu: The first thing that jumps out - perhaps not the root of the problem,
but it will cause you errors if it is not already - is that "s2" is
not always defined when it is used:

On 9/14/06, luciano mantuaneli  wrote:
> 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:

The value of "s2" will be undefined when "( s2:compoundStatement | SEMI )"
matches a semi.  A rewrite might be:

>      ( s2:compoundStatement
>           { #field = #(#[METHOD_DEF,"METHOD_DEF"],
>                 mods, #(#[TYPE,"TYPE"],rt), IDENT, param, tc, s2);
>           }
>      | SEMI
>           // ?? what should the value of #field be for this side?
>      )

This emphasizes another issue - what is #field supposed to be when a
SEMI is seen?

    

---------------------------------
  Você quer respostas para suas perguntas? Ou você sabe muito e quer compartilhar seu conhecimento? Experimente o Yahoo! Respostas!

 		
---------------------------------
 Novidade no Yahoo! Mail: receba alertas de novas mensagens no seu celular. Registre seu aparelho agora!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20060914/eef480ce/attachment.html 


More information about the antlr-interest mailing list