lexer rule not executed

17 views
Skip to first unread message

Philipp Kraus

unread,
Mar 26, 2018, 2:36:00 AM3/26/18
to antlr-discussion
Hello,

I have got the following grammar rules (excerpt):

term :
| LOGICALVALUE
| literal
    ;

literal :
ATOM
( LEFTROUNDBRACKET termlist? RIGHTROUNDBRACKET )?
;

termlist :
term ( COMMA term )*
;

ATOM :
LOWERCASELETTER
( LOWERCASELETTER | UPPERCASELETTER | DIGIT )*
;

LOGICALVALUE :
TRUE
    | FALSE
;

fragment TRUE  : 'true' | 'success';
fragment FALSE : 'false' | 'fail';
fragment DIGIT : [0-9];
fragment LOWERCASELETTER : [a-z];
fragment UPPERCASELETTER : [A-Z];

If I try to parse the string "success" in the term rule, it match to the literal rule, because the ATOM rule matches, but
in this case I need to match to the LOGCIALVALUE token, because it is a "fixed keyword". Can you help me to
deal with this fixed word. IMHO the LOGICALVALUE token is be tested first, so the literal rule should not match, but I think
I don't understand it correct.
In my case, I have got 4 fixed keyword "true, false, success, fail" which are written in lower-case, but if it is strict matched
the terminal should be used, but otherwise the literal rule should be used. How can I define this correct?

Thanks

Phil

John B Brodie

unread,
Mar 26, 2018, 8:52:42 AM3/26/18
to antlr-di...@googlegroups.com, Philipp Kraus

Greetings!

In ANTLR, when 2 lexer rules match exactly the same input sequence of characters, the rule mentioned first in the grammar file is used to create the corresponding Token.

Move ATOM rule to be after all other lexer rules. All keyword rules should appear before any other lexer rule.

Hope this helps...

   -jbb

--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Philipp Kraus

unread,
Mar 26, 2018, 12:11:08 PM3/26/18
to antlr-discussion
The problem is, I need the explicit literal rule, so I cannot move the ATOM lexer rule outside of the literal rule. 
So can I force the behaviour by a flag or anything else?

Thanks

Phil

Philipp Kraus

unread,
Mar 26, 2018, 4:49:23 PM3/26/18
to antlr-discussion
Changing the order of my lexer rules (the LOFICVALUE comes first) solves the problem. Thank 
Reply all
Reply to author
Forward
0 new messages