[antlr-interest] How to resolve ambiguous grammar of property file: example A = B C K = L

pganelin ganelin at mail.com
Thu Apr 14 08:36:30 PDT 2005


I originally posted it to jguru forum but got no response. May be here I 
would have more luck :-)


I have ambiguous grammar (properties for some system). I attached 
grammar file below. Examples of the statements are

A = B ;
A = B C ;
A = B K = L ;
A = B C K = L ;
A -X = B C K  = L ;
A -X = B C K - M  = L ;


These statements should be grouped like this.

(A = B) ;
(A = B C) ;
(A = B ) (K = L) ;
(A = B C) (K = L) ;
(A - X = B C) (K - M  = L) ;


The human rule is simple. If equal sign follows identifier or identifier 
dash identifier than it is the name of the property. Otherwise it is 
part of the lsit
The ambiguous grammar would is attached.
I tried different lookaheads but so far was not able to solve the 
problem. I hope somebody else could propose ideas to try.

/Note 1./ If property name is ident only then lookahead = 2 would solve 
the problem.

/Note 2./ Something like this would solve the problem but unfortunately 
lookahead here is not supported by ANTLR.

statement:
      (( variable EQ  ) => variable EQ  value)* SEMI
;


/Note 3./ One possible solution would be create a different grammar and 
then rewire the tree. For such approach the interpretation would be like 
this (i.e create extra property names without value)

(A = B) ;
(A = B ) ( C) ;
(A = B ) (K = L) ;
(A = B ) (C) (K = L) ;
(A -X = B ) (C) (K - M  = L) ;

This would be my last choice if nothing else left, but may be there are 
better ways to do this.
=============================equ.g grammar file ===================

header{
package com.equ;
import java.util.*;
}

class EquParser extends Parser;
options {
    k = 1;
	buildAST = true;
}

program:
    (statement) *
;

statement:
      (variable EQ  value)* SEMI
;

variable:
   IDENT
   | IDENT MINUS IDENT
;

value:
   (IDENT)+
;



//----------------------------------------------------------------------------
class EquLexer extends Lexer;
//----------------------------------------------------------------------------
options {
	importVocab=EquParser;       // call the vocabulary "Equ"
	testLiterals=true;          // automatically test for literals
	caseSensitive=false;
	caseSensitiveLiterals = false;
}


IDENT		    :	('a' .. 'z' )+		;
EQ		        :	'=' ;
SEMI			:	';'		;
MINUS			:	'-'		;


// Whitespace -- ignored
WS	:	SPACE
		{ _ttype = Token.SKIP; }
	;

// Whitespace -- ignored
protected
SPACE	:	(	' '
		|	'\t'
		|	'\f'
			// handle newlines
		|	(	options {generateAmbigWarnings=false;}
			:	"\r\n"  // Evil DOS
			|	'\r'    // Macintosh
			|	'\n'    // Unix (the right way)
			)
			{ newline(); }
		)+
	;


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20050414/d55ac033/attachment-0001.html


More information about the antlr-interest mailing list