[antlr-interest] Function Call,

Craig Main craig at palantir.co.za
Wed Jun 29 03:12:48 PDT 2005


Hi,

 

I have managed to get a little further, but I am still struggling with the
function call.

I am trying to match.

 

TEMP = 4 + func(10+1,2);

 

My problem is that #(ELIST ID expression) never gets matched. I am really
battling to understand how the tree is matched.

 

 

 

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=Add(l,r);}

                                 | #(MINUS l=term r=term)
{result=Subtract(l,r);}

                                 | #(MULTIPLY l=term r=term)
{result=Multiply(l,r);}

                                 | #(DIVIDE l=term r=term)
{result=Divide(l,r);}                                 

                                 | #(ELIST ID expression) {log.Info("#(ELIST
(expression)* )");}

                                 | result=term {log.Info("term");}

                                 | #(METHOD_CALL ELIST)

                                 ;

 

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()); }

                                 ;

 

 

The parser.

 

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^

                        ;

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20050629/42887f18/attachment.html


More information about the antlr-interest mailing list