The issue we’re having with ANTLR is that we have a grammar that’s parsing something like this:
Hello, my name is bob.
bob offset: 5
Keep in mind that the “bob.” in the first line is dynamic, and could be anything. One of those things is “bob”. The “bob offset” line is not dynamic, and is in every file of the type that we are parsing.
So, to parse this, we have a couple of rules:
greeting: 'Hello, my name is' id1=IDENT '.' NEWLINE
{ System.out.println("Name: " + $id1.text"); }
;
bob_offset: 'bob offset:' id1=5 NEWLINE
{ System.out.println("bob offset: " + $id1.text); }
;
So, the issue is that ‘bob offset:’ is a token that the lexer reads. Now, when the greeting rule goes, an error is thrown because it’s trying to match ‘bob’ to ‘bob offset:’, but it can’t.
The solution that would be ideal is if ANTLR had some way to specify context- or parser rule-specific lexer rules. This way, the ‘bob offset:’ token wouldn’t be mistaken anywhere else in the grammar.
Any thoughts on this issue would be appreciated.
We ended up having to work around this with more parser rules to flesh it out more specifically for ANTLR.