I have a combined grammar where I need to provide for two identifier lexer rules. Both identifiers can be used at the same time. Identifier1 comes before Identifer2 in grammar.
First identifier is static, whereas second identifier rule changes on the basis of some flag.(Using predicate).
I want the second identifier to match in parser rules. But as both identifiers may match some common inputs, It does not fall on identifer2.
I have created small grammar to make it understandable. Grammar is as:
@lexer::members
{
private boolean flag;
public void setFlag(boolean flag)
{
this.flag = flag;
}
}
identifier1 :
ID1
;
identifier2 :
ID2
;
ID1 : (CHARS) *;
ID2 : (CHARS | ({flag}? '_'))* ;
fragment CHARS
:
('a' .. 'z')
;
If I try to match identifer2 rule as :
ANTLRStringStream in = new ANTLRStringStream("abcabde");
IdTestLexer lexer = new IdTestLexer(in);
lexer.setFlag(true);
CommonTokenStream tokens = new CommonTokenStream(lexer);
IdTestParser parser = new IdTestParser(tokens);
parser.identifier2();
It shows error: line 1:0 missing ID2 at 'abcabde'