[antlr-interest] ambigous language...

Karl Meissner meissnersd at yahoo.com
Mon Jun 30 08:46:52 PDT 2003


I have a design problem that I was hoping you antlr gurus had suggestions about. 
I need to deal with an ambiguous grammar. 

I am trying to write a grammar to accept a language.  The language has been accepted by an
international body and the spec is set in stone...
So I am trying really hard to follow the spec

The problem is there an ambiguity the initialization of arrays and structures.

An array can be init with a common separated list
To init a three element array of type myArrayType named ar

VAR  // declare ar and init it
  ar : myArrayType := 1, 2, 3 ;
END_VAR

So far so good. 

Variable initialization for structs are also a comma separated list.   To init a struct that has
two floats called  real_val and imaginary_val

TYPE
   myStruct : STRUCT
	FLOAT real_val;
	FLOAT  imaginary_val; 
	END_STRUCT;
END_TYPE

VAR
  cplx  : myStruct (  
         real_val := 2.0 ,
         imaginary_val := 0.0 ) ;
END_VAR

will init the variable cplx of type myStruct

The design problem is that structs can contain simple primitive types (int, float, string) arrays,
user defined enumerated types,  other structs, etc.   
In this context  "," is ambiguous.  They really should have use “;” to seperate the struct
initializations...


Suppose the user declares a struct with the following fields
TYPE 
  Weekday :  // enum type definition
ENUM  
Monday, Tuesday, Wednesday, Thursday, Friday 
END_ENUM; 

  problemType :  
STRUCT 
    x : Weekday;
    y : Weekday[3] ;  //an array
    z : Weekday;
 	 END_STRUCT

END_TYPE

Then an acceptable initialization can be

VAR
  d: problemType ( 
      x := Monday  ,   
      y := Tuesday, Wednesday, Thursday, , // <--comma
 z :=  Friday 					 
   ); 
END_VAR


I am trying to write a parse rule that will accept the struct initalization and make a nice AST
that I can use to 
emit code. 

The best I can come up with a something like this, which is unfortunately ambigious....

struct_declaration : 
    stuct_name : struct_type_name “(“
	s_list
    “)” “;”
;

// the comma make this nondeterministic
s_list :  s_element ( "," s_element )* ;

s_element : sname ":="  (  init_lookahead ) ;


init_lookahead : 
	( ar_list ) => ar_list | 
	( primitive_literal ) primitive_literal  
;


sname : SYMBOL ; 

ar_list : primitive_literal ( "," primitive_literal )* ; 


Any ideas how to express this cleanly with antlr?



__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list