[antlr-interest] v3 generats code without complaint, but there is an error with the generated code

孙纪刚 Jigang (Robert) Sun sunjigang1965 at yahoo.com.cn
Fri Sep 29 03:06:09 PDT 2006


I have garammar

grammar CL;
options {
    language  = CSharp;
     output = AST;
     k=3;
}
tokens
{
REAL;}

program:Comment ;
UpperCaseId: UpperCaseChar (IdChar)*;
LowerCaseId: LowerCaseChar (IdChar)*;
FunnyId: (SpecialChar)+;
fragment UpperCaseChar: 'A'..'Z';
fragment LowerCaseChar: 'a'..'z';
fragment SpecialChar: '~'|'@' | '#' | '$' | '%' | '^' | '?'|'!'|'+'|'-'|'*' | '<' | '>' | '\\' |
'|' 
				| '&'|'='| ':' | '/';'
fragment IdChar: UpperCaseChar|LowerCaseChar|Digit|'_'|'`';
INT: Sign? (
Digit+ ('.' Digit+ ('E' Sign? Digit+ )?{type=REAL;})? 
| '0x' HexDigit+
)
;

fragment HexDigit: '0'..'9'|'A'..'F'|'a'..'f';
fragment Sign: '+'|'-'|'~';
fragment Digit:'0'..'9';
ReservedChar: '(' | ')' | '{' | '}'| '[' | ']' | ';' | ',' | '.' ;

Chars: CharDel (~CharDel)+ CharDel ;
StringDenot: StringDel (~StringDel)+ StringDel;

fragment AnyChar: IdChar|ReservedChar;
fragment StringDel: '"';	
fragment CharDel: '\'';

WS      : (' '|'\t'|'\n'|'\r')+ {channel=99;};
Comment: '/' ('/' (AnyChar|' '|'\t')+ '\r\n' | '*'  (AnyChar|' '|'\t')+ ( '*/') ) ;

The garammar is passed without error message, but when I run the generated code, compiling error
took place:"Control cannot fall through from one case label ('default:') to another".

the following is the piece of code where the problemtic line is in:

       case '/':
            switch ( input.LA(2) ) 
            {
            case '/':
                int LA21_11 = input.LA(3);
                if ( (LA21_11 == '\t' || LA21_11 == ' ' || (LA21_11 >= '(' && LA21_11 <= ')') ||
LA21_11 == ',' || LA21_11 == '.' || (LA21_11 >= '0' && LA21_11 <= '9') || LA21_11 == ';' ||
(LA21_11 >= 'A' && LA21_11 <= '[') || LA21_11 == ']' || (LA21_11 >= '_' && LA21_11 <= '{') ||
LA21_11 == '}') )
                {
                    alt21 = 9;
                }
                else 
                {
                    alt21 = 3;}
                break;
            case '*':
                int LA21_12 = input.LA(3);
                if ( (LA21_12 == '\t' || LA21_12 == ' ' || (LA21_12 >= '(' && LA21_12 <= ')') ||
LA21_12 == ',' || LA21_12 == '.' || (LA21_12 >= '0' && LA21_12 <= '9') || LA21_12 == ';' ||
(LA21_12 >= 'A' && LA21_12 <= '[') || LA21_12 == ']' || (LA21_12 >= '_' && LA21_12 <= '{') ||
LA21_12 == '}') )
                {
                    alt21 = 9;
                }
                else 
                {
                    alt21 = 3;}
                break;
            	default: // problemtic line
                	alt21 = 3;}
        
            break;

If I disable rule "FunnyId: (SpecialChar)+;" everythis is fine.

You can see I have set k=3,so 

Comment: '/' ('/' (AnyChar|' '|'\t')+ '\r\n' | '*'  (AnyChar|' '|'\t')+ ( '*/') ) ;
FunnyId: (SpecialChar)+; 

could recognise a string like "/* remark */" as Comment other than FunnyId.

I ever report to list the silimar problem that successfully generated code code could not run, but
nobody responded me.

Jigang



		
___________________________________________________________ 
抢注雅虎免费邮箱-3.5G容量,20M附件! 
http://cn.mail.yahoo.com


More information about the antlr-interest mailing list