ANTLR4.2 Odd Bug.

36 views
Skip to first unread message

Cody Christopher

unread,
Dec 4, 2014, 2:24:03 AM12/4/14
to
Hi guys,

Still fairly new to ANTLR, but have done a couple of combined grammars successfully with associated Visitors in java, however I have not been able to fix this bug...

See attached for the .g4

With this input:

S @ 0 { B = 24, A = 24.4 };

I get this error trace from java:

line 1:2 token recognition error at: '@'
line 1:0 mismatched input 'S' expecting {<EOF>, 'S', 'C'}
line 1:6 token recognition error at: '{'
line 1:14 token recognition error at: ','
line 1:25 token recognition error at: '}'

But it's specifically the first two that I can't resolve.

Changing
AT_S : '@'
to
AT_S : 'at'
(and altering the input respectively) fixes the '@' error.
 
I have no idea why this is, I'd much prefer to use '@' 

The second one I can't seem to resolve at all. Even removing the other options I still get
line 1:0 mismatched input 'S' expecting 'S'

The same grammar and input run fine in Testrig from within Antlrworks2

Help much appreciated!

Os.g4

Jim Idle

unread,
Dec 4, 2014, 2:47:04 AM12/4/14
to antlr-di...@googlegroups.com
You have made the classic beginners mistake of defining literals in the parser rules and then redefining the same sequences in the lexer. :)

Replace all your literals (',' '=' etc) with references to the their lexical tokens (COMMA EQ). In general, I always advise people starting out to use lexer tokens only and avoid 'literals'. They are not a bad thing, just a bit confusing at first until you grok the whole lexer/parser thing. They are useful for instance when they are syntactic sugar that you don't need to see or use in the visitor/listener.

Good luck,

Jim

On Thu, Dec 4, 2014 at 3:24 PM, Cody Christopher <cjchri...@gmail.com> wrote:
Hi guys,

Still fairly new to ANTLR, but have done a couple of combined grammars successfully with associated Visitors in java, however I have not been able to fix this bug...

See attached for the .g4

With this input:

S @ 0 { B = 24, A = 24.4 };

I get this error trace from java:

line 1:2 token recognition error at: '@'
line 1:0 mismatched input 'S' expecting {<EOF>, 'S', 'C'}
line 1:6 token recognition error at: '{'
line 1:14 token recognition error at: ','
line 1:25 token recognition error at: '}'

But it's specifically the first two that I can't resolve.

Changing
AT_S : '@'
to
AT_S : 'at'
removes the first. 
I have no idea why this is, I'd much prefer to use '@' 

The second one I can't seem to resolve at all. Even removing the other options I still get
line 1:0 mismatched input 'S' expecting 'S'

The same grammar and input run fine in Testrig from within Antlrworks2

Help much appreciated!

--
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.

Message has been deleted

Cody Christopher

unread,
Dec 4, 2014, 7:58:20 PM12/4/14
to
Replace all your literals (',' '=' etc) with references to the their lexical tokens (COMMA EQ). In general, I always advise people starting out to use lexer tokens only and avoid 'literals'. 

Thanks, but that isn't the problem in this case (I've done it anyway for consistency reasons though).

I'm still getting the above errors, which were always to do with defined lexer tokens anyway!

------------------
Edit: Woops, turns out I was calling the wrong Lexer in the code. I think the error messages could be improved!
Reply all
Reply to author
Forward
0 new messages