[antlr-interest] Re: parser tree structure

kozchris csnyder at alumni.ncsu.edu
Tue Oct 12 07:49:01 PDT 2004



The tree you get is because you wind up doing the following:
1)add child to current expr
2) add expr root
3) repeat

therefore 1;2;3;
1=> (expr 1) => (expr (expr 1 2)) => (expr (expr (expr 1 2) 3))

I think this code may be what you are trying to do.
It adds a STMTLIST root where each exprssion is a child.
therefore 1;2;3;4+5;
 ( STMTLIST ( EXPR 1 ) ( EXPR 2 ) ( EXPR 3 ) ( EXPR ( + 4 5 ) ) )

Chris

class CalcLexer extends Lexer;

protected DIGIT : '0'..'9' ;
INT : (DIGIT)+ ;
PLUS : "+";
MULTI: "*";
SEMI: ";";
NEWLINE : ('\r''\n')=> '\r''\n' //DOS
	| '\r' //MAC
	| '\n' //UNIX
	{ newline(); };

class CalcParser extends Parser;
   options { buildAST = true; }

   tokens { EXPR; STMTLIST; }
   stmts:
	(stmt)+ {##=#(#[STMTLIST, "STMTLIST"],##);} NEWLINE!
    ;

   stmt
     :  expr SEMI! {##=#(#[EXPR, "EXPR"],##);}
     ;

   expr
     :	INT ((PLUS^|MULT^) INT)*
     ;


--- In antlr-interest at yahoogroups.com, "whaefelinger"
<ora.et.labora at w...> wrote:
> 
> 
> Hello,
> I'm wondering about the structure of my generated parse tree. Here is
> what I have (quite similar to my recent posting - but much improved 
> now thanks to "kozchris"):
> 
> ================== parser ======================================
> /** stmts shall be a "list" of expressions terminated by ";" */
> stmts
>     :  ( expr {
>                 ## = #(#[STMT,"stmt"],##) ;
>          } 
>          ";"!
>         )+ 
>     ;
> 
> /** an exression shall be just a number (for simplicity) */
> expr:	INT ;
> ================================================================
> 
> Here's the tree generated on input of "1;"
> 
>  stmt 
>    |
>    +--1
> 
> Here's the tree generated on input of "1; 2;"
> 
>  stmt
>    |
>    +--stmt
>         |
>         +---1
>         |
>         +---2
> 
> I wonder here about the top most 'stmt'?
> 
> 
> Here's the tree generated on input "1; 2 ; 3;"
> 
>   stmt
>    |   
>    +--stmt
>        |
>        +--stmt
>        |    |
>        +--3 +--1
>             |
>             +--2
> 
> Again there's this top node having just one child.
> 
> I dot not fully understand how this tree layout is
> reflected by the node construction rule 
> 
>      #(#[STMT,"stmt"],##) 
> 
> Instead I expected to end up in something like:
> 
>   stmt               stmt            stmt
>    |                   |               |
>    +--stmt             +--stmt         +--1
>    |    |              |    |
>    +--3 +--stmt        +--2 +--1
>         |    |
>         +--2 +--1
>             
>    "1;2;3;"           "1;2;"          "1;"
> 
> 
> which appears bit more regular in my eyes. Is there
> a way to archieve this?





 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 





More information about the antlr-interest mailing list