[antlr-interest] Problem with Separators
    Craig Main 
    craig at palantir.co.za
       
    Sun Dec 25 21:50:27 PST 2005
    
    
  
Hi,
 
I am using a grammar and a tree to recognise a simple expression grammar.
At the moment, the rules for using semi colons ';' are very strict, and I
need to relax them somewhat.
 
The grammar parses the following spectacularly well.
if (10 < 4 and 4 < 1) {
     c = 10
} else
   m = 20;
a = 10;
b = 40
 
I would like to be able to (optionally) terminate the last statement (b =
40) with a semi-colon, and also optionally terminate the c = 10 with a semi
colon so that the following is also valid.
 
if (10 < 4 and 4 < 1) {
     c = 10;
} else
   m = 20;
a = 10;
b = 40;
 
Could someone help with this please? I am really struggling with k=1, I am
happy to increase k, but my attempts have failed so far. I am sure I am
missing something simple.
I have placed the grammar below. I am aware that I need to somehow use the
semi as a statement terminator rather than a statement separator. I have
attempted to do this but I am really struggling to keep it all working with
that approach. If you have the time please give me some specifics, I would
appreciate it.
 
// THE GRAMMAR.G FILE, THE TREE FOLLOWS
 
rules                     {log.Debug(null);}
                        : (statement (SEMI! statement)*)?
                          {#rules = #([RULESET, "RULESET"], #rules);}
                          EOF!
                        ;
 
statementSequence       : statementBlock
                        | statement
                        | SEMI!
                        ;
 
statementBlock            {log.Debug(null);}
                        : LCURL! (statement (SEMI! statement)*)? RCURL!
                        ;
 
statement                 {log.Debug(null);}
                        : assignmentStatement
                        | constantStatement
                        | ifStatement
                        ;
 
ifStatement               {log.Debug(null);}
                        : TOK_IF^ LPAREN! conditional RPAREN!
statementSequence
                          (
                             options {
                                 warnWhenFollowAmbig = false;
                             }
                          : TOK_ELSE statementSequence
                          )?
                        ;
 
conditional               {log.Debug(null);}
                        : logicalOr
                        ;
 
logicalOr               : logicalAnd (TOK_OR^ logicalAnd)*
                        ;
 
logicalAnd              : equalityExpression (TOK_AND^ equalityExpression)*
                        ;
 
equalityExpression      : relational ((NOT_EQUAL^ | EQUAL^) relational)*
                        ;
 
relational                {log.Debug(null);}
                        : expression
                          ((LTHAN^
                          | GTHAN^
                          | LTE^
                          | GTE^)
                          expression)*
                        ;
 
constantStatement : TOK_CONST^ id:ID ASSIGN factor
                        ;
 
assignmentStatement : id:ID ASSIGN^ expression
                        ;
 
expression : term ((PLUS^|MINUS^) term)*
                        ;
 
term : factor ((MULTIPLY^|DIVIDE^) factor)*
                        ;
 
factor : (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^
                        | LPAREN^ expression RPAREN
                        ;
// THE TREE GRAMMAR FILE
rules                   : #(RULESET (statement)*)
                        ;
 
statement                        : #(ASSIGN id:ID result=expression {
log.Info(string.Format("{0} = {1}", id.getText(), result)); } ) 
                                 | #(TOK_CONST cid:ID ASSIGN result=term)
{log.Info("constant decl");}
                                 | {log.Debug("inside");}
                                   #(TOK_IF condition (statement)*
({log.Debug("in-else");} TOK_ELSE (statement)* {log.Debug("out-else");})?)
                                   {log.Debug("outside");}
                                 ;
condition                        
returns [object result]
                                 : #(TOK_OR l=condition r=condition)
                                    | #(TOK_AND l=condition r=condition)
                                    | #(NOT_EQUAL l=condition r=condition)
                                    | #(EQUAL l=condition r=condition)
                                    | #(LTHAN l=condition r=condition)
                                    | #(GTHAN l=condition r=condition)
                                    | #(LTE l=condition r=condition)
                                    | #(GTE l=condition r=condition)
                                 | result=expression
{log.Debug("condition->expression");}
                                 ;
expression
returns [object result]         : #(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 {log.Debug("term");}
                                 ;
term
returns [object result]         : result=literal
                                 | #(m:METHOD_CALL #(ELIST (expr=expression
{log.Info(string.Format("e{0}",expr));})*)) {log.Info(string.Format("Method
Call {0}", m.getText()));}
                                 ;
 
literal
returns [object result]          : id:ID { result = id.getText(); }
                                 | fl:FLOAT { result =
double.Parse(fl.getText()); }
                                 | #(LPAREN expression)
                                 ;
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20051226/4269fb3f/attachment-0001.html
    
    
More information about the antlr-interest
mailing list