[antlr-interest] semantic predicates use LT() on IntStream - bad

Alexandre Porcelli porcelli at uol.com.br
Thu Nov 1 11:33:30 PDT 2007


Hi Mats,

 I had the same problem, so to solve it, first I created the folowing
methods on @parser::members area:

	private boolean validateLT(int LTNumber, String text){
		if (null == input) return false;
		if (null == input.LT(LTNumber)) return false;
		if (null == input.LT(LTNumber).getText()) return false;
		
		String text2Validate = input.LT(LTNumber).getText();
		if (text2Validate.startsWith("[") && text2Validate.endsWith("]")){
			text2Validate = text2Validate.substring(1, text2Validate.length() - 1);
		}

		return text2Validate.equalsIgnoreCase(text);
	}

	private boolean validateIdentifierKey(String text){
		return validateLT(1, text);
	}

then I could use that:

keyIF
	:	 {validateIdentifierKey("if")}?  ID
	;


Regards,
Alexandre Porcelli


On 11/1/07, Mats Behre <mb.fgel at ownit.nu> wrote:
> Hello
>
> I have solved my earlier problem (http://www.antlr.org/pipermail/antlr-interest/2007-October/024485.html) at least temporarily by post-processing the generated Java file, but now I have stumbled upon a new one.
> The language I'm converting the grammar for, in addition to having loads of keywords, also allows the keywords to be used as identifiers such as variable names, so I have added rules such as this one from the 'The Definitive ANTLR Reference' book:
>
> keyIF : {input.LT(1).getText().equals("if")}? ID ;
>
> Now I get Java compiler errors, because in DFA classes there are methods named 'specialStateTransition', where input is an IntStream, which obviously use these semantic predicates, and IntStream does not have an LT(int) method.
> This time I haven't slightest idea of how to work around the problem, but I'll try to wade through the LL(*) section of the book, hoping to find a solution.
>
> Rgds,
> Mats
>


More information about the antlr-interest mailing list