[antlr-interest] Function Call,

Craig Main craig at palantir.co.za
Wed Jun 29 08:26:39 PDT 2005


Bryan,

Thanks very much for your assistance.
I have printed out the tree, and I obtain the following output.

( RULESET ( = FRED 100 ) ( = TEST ( + 10 10 ) ) ( = ITEM ( + TEST 10 ) ) ( =
TE
MP ( + 4 ( func ( ELIST ( + 10 1 ) 2 ) ) ) ) )

When the tree is parsed, with your corrected rule, I get

<AST>: unexpected AST node: func

I suspect that there is another problem. Surely METHOD_CALL should appear in
the tree output? ELIST and RULESET appear in the tree.
I suspect that the grammar rule:

function : id:ID^ {#id.setType(METHOD_CALL);} LPAREN! arguments RPAREN!

Is not actually setting the type properly or something?

I have included the rules in the event that anyone else is following the
thread.

rules
                                 : #(RULESET (statement)*)
                                 ;

statement                          {object result = null;}
                                 : #(ASSIGN id:ID result=expression {
log.Info(string.Format("{0} = {1}", id.getText(), result)); } ) 
                                 ;

expression
returns [object result]            {result = null;object l,r;}
                                 : #(PLUS l=term r=term)
{result=ExpressionOperation.Add(l,r);}
                                 | #(MINUS l=term r=term)
{result=ExpressionOperation.Subtract(l,r);}
                                 | #(MULTIPLY l=term r=term)
{result=ExpressionOperation.Multiply(l,r);}
                                 | #(DIVIDE l=term r=term)
{result=ExpressionOperation.Divide(l,r);}                                 
                                 | #(METHOD_CALL #(ELIST (expression)* )
){log.Info("test");}
                                 | result=term {log.Info("term");}
                                 ;

term
returns [object result]            {result = null;}
                                 : result=literal
                                 ;

//value                            : function | literal
//                                 ;

literal
returns [object result]            {result = null;}
                                 : id:ID { result = id.getText(); }
                                 | fl:FLOAT { result =
double.Parse(fl.getText()); }
                                 ;


rules                   : (statement)* 
                          {#rules = #([RULESET, "RULESET"], #rules);}
                          EOF!
                        ;

statement               : assignment_statement SEMI!
                        ;

assignment_statement    : id:ID ASSIGN^ expression
                        ;

expression              : term ((PLUS^|MINUS^) term)*
                        ;

term                    : factor ((MULTIPLY^|DIVIDE^) factor)*
                        ;

factor                  : value
                        ;

value                   : (ID LPAREN) => function
                        | literal
                        ;

function                : id:ID^ {#id.setType(METHOD_CALL);} LPAREN!
arguments RPAREN!
                        ;

arguments               : (expression (COMMA! expression)*)?
                          {#arguments = #(#[ELIST,"ELIST"], arguments);}
                        ;

literal                 : id:ID^
                        | fl:FLOAT^
                        ;







-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Bryan Ewbank
Sent: 29 June 2005 05:03 PM
To: ANTLR Interest
Subject: Re: [antlr-interest] Function Call,

Remove the extra parenthesis around the #(ELIST ... ) subrule.  Again,
I think the trace output for tree parsers is most helpful...

WRONG:
    #(METHOD_CALL ( #(ELIST (expression)*) ) )

RIGHT:
    #(METHOD_CALL #(ELIST (expression)* ) )

Hope this helps,
- Bryan




More information about the antlr-interest mailing list