I am attempting to mimic the grammar G shown below using ANTLR v4.9.3 …
My attempt to do so is shown below …
grammar G ;
s : t s | ;
t : 'aaa' t 'bbb' | ;
I invoke the ANTLR Tool as follows …
org.antlr.v4.Tool G.g4
The tool's response is …
The following sets of rules are mutually left-recursive [s]
My question is …
How does one create an ANTLR grammar for grammar G ?
In order to eliminate the errors, grammar G has been updated to …
grammar G ;
s : t* EOF ;
t : 'aaa' 'bbb' | 'aaa' t 'bbb' ;
I am also trying to programatically distinguish between strings that are in L(G), and strings that are not in L(G) (where L(G) is the language generated by grammar G). In the following code, the first string is in L(G), but the second string is not in L(G).
String [] stringArray = { "aaaaaabbbbbbaaabbbaaaaaabbbbbb",
"aaabbbaaaaaabbbbbbaaabbbb" } ;
for ( int i = 0 ; i < stringArray.length ; i ++ )
{
CharStream charStream = CharStreams.fromString ( stringArray[i] ) ;
GLexer lexer = new GLexer( charStream ) ;
CommonTokenStream tokens = new CommonTokenStream( lexer ) ;
GParser parser = new GParser( tokens ) ;
ParseTree tree = parser.s() ;
} // end for i loop
I'd like the code to print messages such as …
The string "aaaaaabbbbbbaaabbbaaaaaabbbbbb" is in L(G).
… and …
The string "aaabbbaaaaaabbbbbbaaabbbb" is not in L(G).
How does one programmatically distinguish between strings that parse successfully (and are in L(G)), and strings that fail to parse successfully (and are not in L(G)) ?