[antlr-interest] Struggling with tree function call,
Craig R. Main
craig at palantir.co.za
Tue Jun 28 08:17:36 PDT 2005
Hi,
I am struggling with parsing the parameters to the function in my
grammar.
Could someone point me in the right direction?
The parser is parsing correctly, and the tree looks correct, but I
cannot get the tree parser to recognize the function call.
options {
language = "CSharp";
namespace = "Rules";
}
class RuleTreeParser extends TreeParser;
{
/// <summary>
/// Standard logging implementation
/// </summary>
private readonly log4net.ILog log =
log4net.LogManager.GetLogger(typeof(RuleParser));
}
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);}
| 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()); }
;
class RuleParser extends Parser;
options {
k = 1;
defaultErrorHandler = false;
buildAST = true;
}
tokens
{
METHOD_CALL;
}
{
/// <summary>
/// Standard logging implementation
/// </summary>
private readonly log4net.ILog log =
log4net.LogManager.GetLogger(typeof(RuleParser));
}
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^
;
class RuleLexer extends Lexer;
options {
// Required for * and /
k=3;
charVocabulary = '\0'..'\377';
}
tokens {
TOK_OR = "or";
TOK_AND = "and";
TOK_CONST = "const";
TOK_IF = "if";
TOK_ELSE = "else";
}
MULTIPLY : '*' ;
DIVIDE : '/' ;
PLUS : '+' ;
MINUS : '-' ;
LPAREN : '(' ;
RPAREN : ')' ;
SEMI : ';' ;
ASSIGN : '=' ;
DASH : '^' ;
LTHAN : "<" ;
GTHAN : ">" ;
LTE : "<=";
GTE : ">=";
EQUAL : "==";
NOT_EQUAL : "<>";
LCURL : "{" ;
RCURL : "}" ;
COMMA : "," ;
ID
options {
testLiterals = true;
paraphrase = "an identifier";
}
: ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*
;
protected
DIGIT
options {
paraphrase = "a digit";
}
: '0'..'9'
;
FLOAT
options {
paraphrase = "an integer value";
}
: (DIGIT)+ // base-10
( '.' (DIGIT)*)?
;
LITERAL
options {
paraphrase = "a string literal";
}
: '"' (/*ESC|*/~'"')* '"'
;
WS_ : (' '
| '\t'
| '\n'
| '\r')
{ _ttype = Token.SKIP; }
;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20050628/4af8bd95/attachment-0001.html
More information about the antlr-interest
mailing list