dcsimg
How to resolve ambiguous grammar of property file: example A = B C K = L
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Pavel_Ganelin
Posted On:   Wednesday, April 13, 2005 09:59 AM

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 differe   More>>

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 lookahead but so far was not able to solve the problem.
I hope somebody else could propose ideas to try.


Note 1. If option 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 : ( ' '
| ' '
| 'f'
// handle newlines
| ( options {generateAmbigWarnings=false;}
: "
" // Evil DOS
| '
' // Macintosh
| '
' // Unix (the right way)
)
{ newline(); }
)+
;
   <<Less
About | Sitemap | Contact