Posted By:
Slawek_Kolasinski
Posted On:
Friday, March 4, 2005 01:02 PM
Hi I'm new to ANTLR. I'm writting a simple parser for algebraic expressions. My grammar was: E -> T | T + E T -> F | F * T F -> id | (E) | num I've translated it to LL(1): E -> TG G -> +E | T -> FH H -> *T F -> id | (E) | num As far as I know the second grammar is LL(1) and shouldn parse without any problems. I coded it in javaCC and it works ok. The same grammar in ANTLR produces te following warning: zad1.g:25: warning:nondeterminism between alts 1 and 3 of block upon zad1.g:25: k==1:EOF,PLUS,RPAREN I can't write anyt
More>>
Hi
I'm new to ANTLR. I'm writting a simple parser for algebraic expressions. My grammar was:
E -> T | T + E
T -> F | F * T
F -> id | (E) | num
I've translated it to LL(1):
E -> TG
G -> +E |
T -> FH
H -> *T
F -> id | (E) | num
As far as I know the second grammar is LL(1) and shouldn parse without any problems. I coded it in javaCC and it works ok. The same grammar in ANTLR produces te following warning:
zad1.g:25: warning:nondeterminism between alts 1 and 3 of block upon
zad1.g:25: k==1:EOF,PLUS,RPAREN
I can't write anything like
prodExpr ((PLUS^|MINUS^) prodExpr)*
because this is a task on my compiler building classes.
Thanks.
S³awek
My code is:
class P extends Parser;
options
{
k=1;
}
start:
expression EOF
;
expression:
part expCont { System.out.println("exp"); }
;
expCont:
PLUS expression { System.out.println("ec1"); }
| { System.out.println("ec2"); }
;
part:
factor partCont { System.out.println("part"); }
;
partCont:
| MULT part { System.out.println("pc1"); }
| { System.out.println("pc2"); }
;
factor:
ID { System.out.println("f1"); }
| LPAREN expression RPAREN { System.out.println("f2"); }
| NUMBER { System.out.println("f3"); }
;
// Lexer
class L extends Lexer;
options
{
k=2; // needed for newline junk
charVocabulary='u0000'..'u007F'; // allow ascii
}
LPAREN: '(' ;
RPAREN: ')' ;
PLUS : '+' ;
MULT : '*' ;
NUMBER: ('0'..'9') ('.' ('0'..'9')*)? ;
ID: ('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'0'..'9')* ;
WS : ( ' '
| '
' '
'
| '
'
| ' '
)
{$setType(Token.SKIP);} ;
<<Less