I am a total ANTLR newbie, and have read quite a bit, but am a bit  
flumoxed by the results I am getting with my antlr grammar

My grammar is heavily based on the expression parser example.

I am confused by some console output I am seeing of the form:

line 1:5 no viable alternative at input '&&'
line 0:-1 no viable alternative at input '<EOF>'
line 1:5 no viable alternative at input '&&'
line 0:-1 no viable alternative at input '<EOF>'

Despite seeing this console output, my boolean expression parser seems  
to work & return valid values.

All the "operators" in my base parser get called with the values I  
would expect

so when I give it the input

true && true

despite the fact that I get the following consle messages

line 1:5 no viable alternative at input '&&'
line 0:-1 no viable alternative at input '<EOF>'

My method band() gets called with 2 Boolean, both set to true

If I pass input

false || true

my method bor() gets called with the Boolean values false & true

I also intended it to support more complex expressions like

(1.1 < 2.2) && (((1/1.0) * (4.0-8)) > -5.0)

Once again we get console output about no viable alternative

but the final result of the expression is as expected.

I have attached the grammar below

I am using antlr 3.0.1 with Java 5 on a Mac 10.5

I would really appreciate any hints that you might be able to provide

Regards,
Matthew


=== GRAMMAR ====
grammar Expr;

options {
        superClass=ExprParserBase;
}

@header {
package au.com.observant.viz.expression;
import au.com.observant.viz.expression.ExprParserBase;
}
@lexer::header {
package au.com.observant.viz.expression;
}

@members {
        Object result;
        public Object getResult() {
                return result;
        }
}

prog returns [Object value]
        :       b=boolExpr {$value = $b.value;}
        ;

boolExpr returns [Object value]
        :       b=relExpr {$value = $b.value;}
                (       BAND b=relExpr {$value = band($value,$b.value);}
                |       BOR b=relExpr {$value = bor($value,$b.value);}
                )*
        ;

relExpr returns [Object value]
     :   e=expr {$value = $e.value;}
         (   '>' e=expr {$value = gt($value,$e.value);}
         |   '<' e=expr {$value = lt($value,$e.value);}
         |   '>=' e=expr {$value = gte($value,$e.value);}
         |   '<=' e=expr {$value = lte($value,$e.value);}
         |   '==' e=expr {$value = eq($value,$e.value);}
         |   '!=' e=expr {$value = neq($value,$e.value);}
         )
     ;

expr returns [Object value]
     :   e=multExpr {$value = $e.value;}
         (   '+' e=multExpr {$value = add($value,$e.value);}
         |   '-' e=multExpr {$value = subtract($value,$e.value);}
         )*
     ;

multExpr returns [Object value]
     :   e=power {$value = $e.value;}
        (   '*' e=power {$value = multiply($value, $e.value);}
        |   '/' e=power {$value = divide($value, $e.value);}
        )*
     ;

power returns [Object value]
        :       f=unary
                        {$value = $f.value;}
                (POW p=unary
                        {$value=powerOf($value,$p.value);}
                )?
        ;

unary returns [Object value]
        :       f=atom
                        {$value = $f.value;}
        |       '!' f=atom
                        {$value = negate($f.value);}
        |       '-' f=atom
                        {$value = negate($f.value);}
        ;

atom returns [Object value]
     :   INTEGER {$value = Double.parseDouble($INTEGER.text);}
     |   FLOAT {$value = Double.parseDouble($FLOAT.text);}
     |   FIELD { $value = findField($FIELD.text); }
     |   ID { $value = findField($ID.text); }
     |   '(' boolExpr ')' {$value = $boolExpr.value;}
     |   EXP '(' expr ')' {$value = exp($expr.value);}
     |   TRUE {$value = Boolean.TRUE;}
     |   FALSE {$value = Boolean.FALSE;}
     ;

EXP   : 'exp';
TRUE   : 'true';
FALSE   : 'false';
ID : LETTER ( LETTER | '0'..'9' | '_' )*;
fragment LETTER: ('a'..'z' | 'A'..'Z');
POW   : '^';
BAND   : '&&';
BOR   : '||';
INTEGER 
        :       ('0'..'9')+
        ;

FLOAT
        :       ('0'..'9')* '.' ('0'..'9')+
        ;
FIELD     : ID '.' ID ;
WS  :   (' '|'\t')+ {skip();} ;


List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.

Reply via email to