[antlr-interest] CORBA IDL grammar union switch problem

Lukáš Zapletal lukas at zapletalovi.com
Mon Apr 21 07:26:32 PDT 2008


Hello,

I am new to Antlr and I am having problem with the grammar. Lets
suppose the input:

union testUnion switch(char) {
  case 'a' :
  case 'b' :
  case 'c' : long x;
  default : long y;
};

When I parse this with Antlr the empty cases 'a' and 'b' are skipped.
There is no way to read constants 'a' and 'b' because when I call
getNextSibling() on the first "case" the constant 'c' is returned. I
need to read all 'a', 'b' and 'c' constants and do some actions.

I do not know how to traverse the AST tree to get these values. Is
something wrong in the grammar?  The most important grammar definition
follows (taken from corba.g). Thanks for your help.

union_type
	:   "union"^
	    identifier
	    "switch"! LPAREN! switch_type_spec RPAREN!
	    LCURLY! switch_body RCURLY!
	;

switch_type_spec
	:   integer_type
	|   char_type
	|   boolean_type
	|   enum_type
	|   scoped_name
	;

switch_body
	:   case_stmt_list
	;

case_stmt_list
	:  (case_stmt)+
	;

case_stmt
	:   // case_label_list
	    ( "case"^ const_exp COLON!
	    | "default"^ COLON!
	    )+
	    element_spec SEMI!
	;

// case_label_list
// 	:   (case_label)+
// 	;


// case_label
// 	:   "case"^ const_exp COLON!
// 	|   "default"^ COLON!
// 	;

element_spec
	:   type_spec declarator
	;


-- 
Lukas Zapletal
http://lukas.zapletalovi.com


More information about the antlr-interest mailing list