[antlr-interest] Wrong rule in parser gets matched

Dan Spaven danspaven at hotmail.com
Sun Feb 26 13:18:32 PST 2006


Thanks for the reply Martin but it doesn't seem to be working. My look ahead 
was 2 anyway and when i put the predicate in i got a nondetermnism error. I 
put the predicate into my sensorCont rule and not the expression rule as it 
is sensorCont that is being wrongly matched for an expression.

I've just been having a look at the code that ANTLR generates for my 
expression rules and i think here lies the problem. Here's the code ANTLR 
generates when the look ahead is 2 and there is no predicate:

public final void statement() throws RecognitionException, 
TokenStreamException {

     try {      // for error handling
	switch ( LA(1)) {
                      .
                      .   other alternatives
                      .
                      case IDENT:
	      case LITERAL_on:
                      case LITERAL_output:
	      case LITERAL_global:
                      case LITERAL_off:
	      case LITERAL_float:
	      case LITERAL_forward:
	      case LITERAL_fd:
	      case LITERAL_backward:
	      case LITERAL_bk:
	      case LITERAL_direction:
	      case LITERAL_dir:
	      case LITERAL_reverse:
	      case LITERAL_power:
	      case LITERAL_sensor:
	     {
		actionStatement();
		astFactory.addASTChild(currentAST, returnAST);
		statement_AST = (antlr.CommonAST)currentAST.root;
		break;
	     }
                      case MINUS:
	      case PLUS:
	     {
		expression();
		astFactory.addASTChild(currentAST, returnAST);
		System.out.println("called expression STATEMENT");
		statement_AST = (antlr.CommonAST)currentAST.root;
		break;
	     }
          }

As you can see it only ever tests a lookahead of 1. Also and expression is 
matched when the lookahead is a MINUS or a PLUS no mention of ASSIGN at all. 
Can anyone enlighten me on why this code is generated and is it my 
expression code that needs to be changed?

Thanks,

Dan


>From: Martin Probst <mail at martin-probst.com>
>To: antlr-interest at antlr.org
>Subject: Re: [antlr-interest] Wrong rule in parser gets matched
>Date: Sun, 26 Feb 2006 20:25:59 +0100
>
> > I understand
> > that this is because both statements can start with an IDENT but i can't 
>for
> > the life of me solve the problem. Could this be solved by a semantic
> > predicate that somehow recognises the variable type? Or does my code 
>need
> > restructuring?
>
>You can try to increase lookahead to 2. Alternatively, you can use a
>predicate, e.g.
>
>expression
>| ( IDENT ("is" | "as") ) => sensorCont
>
>Martin
>




More information about the antlr-interest mailing list