[antlr-interest] Help with Tree Parser (ANTLR 2.7.5)

Jeff Barnes jbarnesweb at yahoo.com
Tue Jan 31 00:29:31 PST 2006


To the list this time...

Hi,

I'm not real experienced with ANTLR tree parsers, but
I believe what you are trying to accomplish is
something along the lines of this:

singleCommand returns [String s = null]
{
    int val;
    String id;
    boolean expr = false;
    String thenStr = null, elseStr = null;
}
:
   ...
    | #(D_SCIF expr=expression thenStr=singleCommand
elseStr=singleCommand) {s = "if "+expr+"\n"+thenStr
+"\nelse\n"+elseStr
    | #(D_SCWHILE expression singleCommand)
   ...
    ;

expression returns [boolean exprResult = false] :
...
//boolean expressions
...
;

Or you may be trying to do this, not sure which...
singleCommand returns [String s = null]
{
    int val;
    String id;
    boolean expr = false;
    String thenStr = null, elseStr = null;
}
   ...
    | #(D_SCIF expr=expression
            (
                 {expr}? singleCommand 
            |    singleCommand
            )
    | #(D_SCWHILE expression singleCommand)
   ...
    ;




--- Zack Nammari <xx_ayu at hotmail.com> wrote:


---------------------------------
Hello all,
 
I'm trying to use ANTLR 2.7.5 and Java to write a
small interpreter but I'm not sure how to get the tree
parser to branch on certain conditions.
 
A part of my tree parser grammer is below:
 
singleCommand
{
    int val;
    String id;
}
   ...
    | #(D_SCIF expression singleCommand singleCommand)
    | #(D_SCWHILE expression singleCommand)
   ...
    ;

I would like the D_SCIF to operate as follows:
 
if (expression == true)
  singleCommand
else
  singleCommand
 
... and for the D_SCWHILE:
 
while (expression == true)
  LOOP singleCommand
 
Is this possible???
 
I was hoping something like this would work:
 
#(D_SCIF val=expression { if (val) }singleCommand {
else }singleCommand)
 
... but curly brackets {} are needed for the if-else
to operate correctly and I can't find a way to include
them inside an action.
 
Thank you in advance for any help.
 
Kind regards,
 
Zack Nammari



=========
Jeff Barnes
(206)245-6100

There are two rules for being a successful consultant: Rule 1 - Don't tell people everything you know.


More information about the antlr-interest mailing list