[antlr-interest] FW: Expression Parser with functions,

Craig Main craig at palantir.co.za
Sun Jun 19 00:43:31 PDT 2005


 

Hi,

 

I have a simple grammar that I am struggling with.

I am trying to figure out how to parse (obtain values for) the expression
arguments in the function call.

 

The rule: | m:FUNC arguments  {r = 0; // get the arguments somehow}

I am trying to work out how I can use the tree to put the list of resulting
arguments into an array of some kind.

 

I am struggling to get to terms with the tree. 

It is easier to do this without making use of the tree at all. Is that a
better approach? 

Maybe I should leave out the tree altogether.

 

 

options {

       language = "CSharp";

}

 

class CalcLexer extends Lexer;

 

 

WS      : ( ' ' | '\t' | '\n' { newline(); }

        | '\r' ) { $setType(Token.SKIP); }

        ;

 

LPAREN  : '(' ;

RPAREN  : ')' ;

STAR    : '*' ;

SLASH   : '/' ;

DASH    : '^' ;

PLUS    : '+' ;

MINUS   : '-' ;

SEMI    : ';' ;

ASSIGN  : '=' ;

COMMA   : ',' ;

 

protected

DIGIT   : '0'..'9'|'.' ;

 

NUM     

options {

   paraphrase = "a numeric value";

}

        : (DIGIT)+ ;

 

ID

options {

  testLiterals = true;

  paraphrase = "an identifer";

}

        :     ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*

;

 

class CalcParser extends Parser;

options { 

   k = 2;

   buildAST = true; 

}

tokens {

   BLOCK;

   FUNC;

   DECL;

}

statement      : (stmt)* 

                 {#statement = #([BLOCK, "BLOCK"], #statement);}

               ;

               

stmt           : assignment SEMI! | const_decl SEMI! ;

assignment     : ID ASSIGN^ expression ;

const_decl     : "const" ID ASSIGN^ NUM;

 

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

mexpr          : dexpr ((STAR^|SLASH^) dexpr)* ;

dexpr          : atom (DASH^ atom)* ;

atom           : NUM

               | LPAREN! expression RPAREN!

               | n:ID^ {#n.setType(DECL);}

               | m:ID^ LPAREN! (expression)? (COMMA expression)* RPAREN!
{#m.setType(FUNC);}

               ;

 

class CalcTreeParser extends TreeParser;

 

statement

        : #(BLOCK (assignment | const_decl)*)

        ;

 

assignment : #(ASSIGN expression) ;

const_decl : #("const" ID ASSIGN NUM) ;

 

expression returns [double r] { double a, b; r = 0; }

        : #(PLUS a=expression b=expression ) {r=a+b;}

        | #(MINUS a=expression b=expression ) {r=a-b;}

        | #(DASH a=expression b=expression ) {r=System.Math.Pow(a,b);}

        | #(STAR a=expression b=expression ) {r=a*b;}

        | #(SLASH a=expression b=expression ) {r=a/b;}

        | n:NUM {r=(double)double.Parse(n.getText());}

        | i:DECL {r = 0;}

        | m:FUNC arguments  {r = 0;}

        ;

 

arguments :

        LPAREN (expression)? (COMMA expression)* RPAREN

         ;

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20050619/2e07e1d6/attachment-0001.html


More information about the antlr-interest mailing list