Antlr superfluous Predicate required? -


I have a file where I want to ignore some parts of it. In lexer I use gate semantic predictions to avoid the construction of tokens of any part without the file. My rules are similar to the following.

  A: {! ignore}? = & Gt; 'a' ; START_IGNORE: 'foo' {ignore = true; Skip ();}; END_IGNORE: 'off' {ignore = false; Skip ();}; Ignore: {ignore}? = & Gt; . {Skip ();};   

However, unless I change the START and END to use the semantic predictions (in the form of the below), this does not work.

 < Code> A: {! Ignore}? = & Gt; 'a' ; START_IGNORE: {true}? = & Gt; 'Foo' {ignore = true; Skip ();}; END_IGNORE: {true}? = & Gt; 'Off' {ignore = false; Skip ();}; Ignore: {ignore}? = & Gt; . {Skip ();};   

Why do I have to add predictions?

Edit: I am using the ANTLR 3.4

Why do I have to add predictions?

You do not do this. At least, not using ATLR v3.3 I do not know how to how are actually testing you, but do not use ANLLWorks interpreter or Eclipse ANTLR IDE plugin. Always do a little test from the command line.

  grammar t; @ Parser :: Members {Public Static Zero Main (String [] Args) Exception {TLX Lexor = New Telx (throws new ANTLR string stream ("AFU ABC OF A")); TParser parser = new TParser (new CommonTokenStream (lexer)); Parser.parse (); }} @ Letter :: members {private boolean unknown = false; } Pars: (T =. {System.out.printf ("[\% 02d] type = \% s text = '\% s' \ n", $ t.getCharPositionInLine (), tokenname [$ t.type] , $ T.text);}) * EOF; a: {! ignore}? = & Gt; 'a' ; START_IGNORE: 'foo' {ignore = true; Skip ();}; END_IGNORE: 'off' {ignore = false; Skip ();}; Ignore: {ignore}? = & Gt; . {Skip ();}; Location: '' {skip ();};   

Run it like this:

 java-cp antlR -3.3. Jar org.antlr.Tool tg javac -cp -nl-3.3.jpg *. Java Java - CP: NLL -3.3. Jar tippers  

will print the following:

 [00] type = one text = 'A' [16] type = a text = 'A'   

IE: From input "A Foo ABC of A" Low: "Foo ABC Off" is left Pad.

Comments