[antlr-interest] Walking AST, and rule dilemma

Craig Main craig at palantir.co.za
Sun Jun 26 03:13:44 PDT 2005


Hi Subhobroto,

 

I have been trying to achieve the same thing for some time now.

I was advised a week or two ago to drop the TreeParser and just parse the
expressions using a standard grammar parser without the tree.

 

I think using trees is a better approach, because you are able to walk the
parsed tree using several different tree parsers that process the grammar in
different ways. This is often quite useful.

 

I have been struggling for a while to get the tree correct.

I will send you my whole grammar, although it does not use a tree.

 

Regards

Craig.

 

  _____  

From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Subhobroto Sinha
Sent: 25 June 2005 08:25 PM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Walking AST, and rule dilemma

 

  
I want to walk exprs. like "a = 5 + b" [AST : ( = a ( + 5 b ) )], using
something like :

          #(PLUS in2:INT statement)
          {
               show(";add to it '" + in2->getText() + "'");
          }

but I have a rule #(PLUS id2:ID statement) is already defined [ID :
('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*; INT :
('0'..'9')+]

The later rule matches exprs. like "a = b + 5" - but it's obvious that this
rule will not match the first expression. If I would have tried to
incorporate both rules, it would be a non-deterministic rule

What do I do ?

Also, how do I handle exprs. like "a = b" ?
For example, if such exprs. are encountered, I would like to print "'b' is
assigned to 'a'"

The complete ANTLR grammar containing these rules is available at
http://www.geocities.com/subhobrotosinha/novel.txt
The driver (C++) is available at
http://www.geocities.com/subhobrotosinha/noveldriver.txt

It should be obvious that I have been using ANTLR seriously for the last 6
hours, so PLEASE make comments regarding my approach and code, even if you
cannot help me out re. this topic.

Subhobroto Sinha

http://www.geocities.com/subhobrotosinha 



 <http://clients.rediff.com/signature/track_sig.asp> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20050626/913e4661/attachment.html


More information about the antlr-interest mailing list