[antlr-interest] Parser - > Tree walker
    CARLOS MELGAREJO 
    carlos.melgarejo at rogers.com
       
    Tue May 22 11:15:17 PDT 2007
    
    
  
Thanks Jim and Lucio,
 I really appreciate it guys.
 After a lot of trial-error, finally it works following your suggestion. I got a lot of error messages when I used antlrworks-1.0.jar the new
version that I just donwloaded today, then I tried using the plane command line $java org.antlr.Tool and again have some
error messages, finally I used the all version of antlrworks-1.0b11.jar and this one works just fine. 
  I believe was the some problem that I was facing yesterday, I tried first using token substitution, and then after a few changes stop working. 
  
 
----- Original Message ----
From: Jim Idle <jimi at temporal-wave.com>
To: CARLOS MELGAREJO <carlos.melgarejo at rogers.com>; antlr-interest at antlr.org
Sent: Tuesday, May 22, 2007 1:17:50 PM
Subject: RE: [antlr-interest] Parser - > Tree walker
 
 
<!--
 _filtered {font-family:"Cambria Math";panose-1:2 4 5 3 5 4 6 3 2 4;}
 _filtered {font-family:Calibri;panose-1:2 15 5 2 2 2 4 3 2 4;}
 _filtered {font-family:Tahoma;panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0in;margin-bottom:.0001pt;font-size:12.0pt;font-family:"Times New Roman", "serif";}
a:link, span.MsoHyperlink
	{color:blue;text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{color:purple;text-decoration:underline;}
span.EmailStyle17
	{font-family:"Calibri", "sans-serif";color:#1F497D;}
.MsoChpDefault
	{font-size:10.0pt;}
 _filtered {margin:1.0in 1.0in 1.0in 1.0in;}
div.Section1
	{}
-->
Probably the easiest thing to do is change what you are
producing for the unary operators. You probably don’t even need unary +:
 
  
 
Add
 
  
 
tokens
 
{
 
     UNARYNEG;
 
}
 
  
 
After the options,
 
  
 
  
 
Then:
 
  
 
unaryExpression 
:  '+' ue=unaryExpression      -> $ue
                
|  '-' ue=unaryExpression      -> ^(UNARYNEG $ue)
 
     
           |  unaryExpressionNotPlusMinus
                 ;
 
…
 
  
 
From:
antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On
Behalf Of CARLOS MELGAREJO
Sent: Tuesday, May 22, 2007 10:10 AM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Parser - > Tree walker
 
  
 
  Hi all,
  I'm trying to create a grammar that recognize simple expressions and
other goodies and generate an evaluation at the end.
  First I read "The definitive ANTLR Reference" book(for the
version 3) and I got all exited, and then after a lot of testing examples I was
encourage to start working in my project.
  So, making the long story short this is my Lexer-Parser grammar look
like:
grammar Expert;
options {
    output=AST;
    ASTLabelType=CommonTree;
}
// Starting point
statement : expression  ; 
expression  : conditionalAndExpression ( 'or'^ conditionalAndExpression
)*  ;
conditionalAndExpression : exclusiveOrExpression ( 'and'^ exclusiveOrExpression
)* ;
exclusiveOrExpression : equalityExpression ( 'xor'^ equalityExpression )* ;
equalityExpression : relationalExpression (('='^ | '!='^)
relationalExpression)*  ;
relationalExpression : additiveExpression ( ('<='^|'>='^|'<'^|'>'^)
additiveExpression )* ;       
additiveExpression : multiplicativeExpression ( ('+'^ | '-'^)
multiplicativeExpression )*  ;
multiplicativeExpression : unaryExpression ( ( '*'^ | '/'^ ) unaryExpression
)*  ;
unaryExpression  :  '+'^ unaryExpression 
                         
|  '-'^ unaryExpression
                   
      |  unaryExpressionNotPlusMinus
                   
      ;
  
unaryExpressionNotPlusMinus
    : 'not'^ unaryExpression
    | atom
    ;     
    
atom
    : literal  
    | log 
    | Date 
    | '('! expression  ')'!
    ;
literal    
    :   FloatingPointLiteral  
    |  
StringLiteral         
    ; 
......................
  So far so good, I'm able to create a pretty good AST, the only problem
that I'm facing with this grammar is that I build the following tree node:
 ^('-' expr expr)     // Arithmetic expression 
 ^('-'
expr)            //
Unary expression.
   My problem is that I don't know how to walk this tree, because of
the ambiguity in the rule abode created with '-'. 
   How I can solve the problem with unary expressions?. 
   Thanks in advance.
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070522/0509d214/attachment-0001.html 
    
    
More information about the antlr-interest
mailing list