[antlr-interest] CORBA IDL grammar union switch problem

Andy Tripp antlr at jazillian.com
Tue Apr 22 09:10:49 PDT 2008


I noticed that the java.g that comes with ANTLR2
has some special stuff to handle this "case" case
to avoid the problem you're seeing:

casesGroup
    :   (   // CONFLICT: to which case group do the statements bind?
            // ANTLR generates proper code: it groups the
            // many "case"/"default" labels together then
            // follows them with the statements
            options {
                greedy = true;
            }
            :
            aCase
        )+
        caseSList
        {#casesGroup = #([CASE_GROUP, "CASE_GROUP"], #casesGroup);}
    ;

aCase
    :   ("case"^ expression | "default") COLON!
    ;

caseSList
    :   (statement)*
        {#caseSList = #(#[SLIST,"SLIST"],#caseSList);}
    ;


Maybe someone else can explain exactly what's happening here.

Andy

 
Lukáš Zapletal wrote:
> 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
> 	;
> 
> 



More information about the antlr-interest mailing list