Resolving Non-deterministic tokens
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Paul_Cereghini
Posted On:   Thursday, July 28, 2005 02:34 PM

Hi: Sorry, I am new to grammars and ANTLR. The following grammar: cons: CONS NAME RBRAC ; attr: ATTR NAME RBRAC ; lvar: LVAR NAME RBRAC ; prule: PRULE NAME RBRAC ; udf: UDF NAME args RBRAC ; afunc: AFUNC NAME args RBRAC ; tdf: TDF NAME args RBRAC ; args: LPAREN (arg (DELIM arg)*)* RPAREN ; arg: cons | attr | INT | prule ; atom: INT | cons | attr | lvar | prule | udf | tdf | afunc | LPAREN expr   More>>


Hi:


Sorry, I am new to grammars and ANTLR.


The following grammar:


			
cons: CONS NAME RBRAC
;

attr: ATTR NAME RBRAC
;

lvar: LVAR NAME RBRAC
;

prule: PRULE NAME RBRAC
;

udf: UDF NAME args RBRAC
;

afunc: AFUNC NAME args RBRAC
;

tdf: TDF NAME args RBRAC
;

args: LPAREN (arg (DELIM arg)*)* RPAREN
;

arg: cons
| attr
| INT
| prule
;

atom: INT
| cons
| attr
| lvar
| prule
| udf
| tdf
| afunc
| LPAREN expr RPAREN
;


expr: atom ((PLUS|MINUS|STAR|DIVIDE|CARET|MOD) atom)*
;


is supported by the following tokens:


			
NAME: ('a'..'z'|'A'..'Z'|'_')('a'..'z'|'A'..'Z'|' '|'0'..'9'|'_')*;


MOD: ('M'|'m')('O'|'o')('D'|'d');



As I expect I get a non-deterministic warning from the ANTLR generator, however I thought it would be easy to resolve this in the parser by changing the order of the rules...but I can seem to resolve it. When I try to parse a statement that has a token that should resolve to NAME I get a recognition error that says it has found MOD and it is expecting NAME.

For example the statement below:


a[MOD]/42


Which gives:


			
RECOGNITION - PARSE FAILED!!
Localized message: expecting NAME, found 'MOD'
line 1:3: expecting NAME, found 'MOD'
at antlr.Parser.match(Parser.java:211)
at ExprParser.attr(ExprParser.java:55)
at ExprParser.atom(ExprParser.java:181)
at ExprParser.expr(ExprParser.java:226)
at Main.main(Main.java:14)


However, a[fido] MOD 42, parses without error.


How can I change the grammar to parse both cases without error?


Thanks,


Paul

   <<Less
About | Sitemap | Contact