[antlr-interest] Statement Parsing,
Craig Main
craig at palantir.co.za
Wed Jun 29 00:13:17 PDT 2005
Hi,
Could someone let me know how to parse the statement below?
I am struggling to obtain the function name and parameters.
I am trying to parse ID ( param , param , param ) in a tree.
I have included the tree rules, and the grammar rules. I cannot seem to get
the tree to match the function at all.
I don't think the tree node below matches anything.
| #(ELIST ID (expression)* )
The tree is as follows.
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);}
| result=term
| #(ELIST ID (expression)* )
;
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 grammar is as follows.
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/0b07c8db/attachment-0001.html
More information about the antlr-interest
mailing list