antlr - ANTLR3 Resolving Grammar With non-LL(*) decisions -
as homework should create parser vc language v10.1 using antlr. here work:
grammar myvcgrm2; program : ( funcdecl | vardecl )* ; funcdecl : type identifier paralist compoundstmt ; vardecl : type initdeclaratorlist ';' ; initdeclaratorlist : initdeclarator ( ',' initdeclarator )* ; initdeclarator : declarator ( '=' initialiser )? ; declarator : identifier identifier2 ; identifier2 : |'[' intliteral? ']' ; initialiser : expr | '{' expr ( ',' expr ) * '}' ; // primitive types type : 'void' | 'boolean' | 'int' | 'float' ; // identifiers identifier : id ; // statements compoundstmt : '{' vardecl* stmt* '}' ; stmt : compoundstmt | ifstmt | forstmt | whilestmt | breakstmt | continuestmt | returnstmt | exprstmt ; ifstmt : 'if' '(' expr ')' stmt ( 'else' stmt )? ; forstmt : 'for' '(' expr? ';' expr? ';' expr? ')' stmt ; whilestmt : 'while' '(' expr ')' stmt ; breakstmt : 'break' ';' ; continuestmt : 'continue' ';' ; returnstmt : 'return' expr? ';' ; exprstmt : expr? ';' ; // expressions expr : assignmentexpr ; assignmentexpr : ( condorexpr '=' )* condorexpr ; condorexpr : condandexpr condorexpr2 ; condorexpr2 : '||' condandexpr condorexpr | ; condandexpr : equalityexpr condandexpr2 ; condandexpr2 : '&&' equalityexpr condandexpr2| ; equalityexpr : relexpr equalityexpr2 ; equalityexpr2 : '==' relexpr equalityexpr2 | '!=' relexpr equalityexpr2| ; relexpr : additiveexpr relexpr2 ; relexpr2 : '<' additiveexpr relexpr | '<=' additiveexpr relexpr | '>' additiveexpr relexpr| '>=' additiveexpr relexpr| ; additiveexpr : multiplicativeexpr additiveexpr2 ; additiveexpr2 : '+' multiplicativeexpr additiveexpr2 | '-' multiplicativeexpr additiveexpr2 | ; multiplicativeexpr : unaryexpr multiplicativeexpr2 ; multiplicativeexpr2 : '*' unaryexpr multiplicativeexpr2 | '/' unaryexpr multiplicativeexpr2 | ; unaryexpr : '+' unaryexpr | '-' unaryexpr | '!' unaryexpr | primaryexpr ; primaryexpr : identifier primaryexpr2 | '(' expr ')' | intliteral | floatliteral | boolliteral | stringliteral ; primaryexpr2 : arglist? | '[' expr ']' ; // parameters paralist : '(' properparalist? ')' ; properparalist : paradecl ( ',' paradecl ) * ; paradecl : type declarator ; arglist :'(' properarglist? ')' ; properarglist : arg ( ',' arg ) * ; arg : expr ; boolliteral : 'true'|'false' ; id : ('a'..'z'|'a'..'z'|'_') ('a'..'z'|'a'..'z'|'0'..'9'|'_'|'?')* ; intliteral : '0'..'9'+ ; floatliteral : ('0'..'9')+ '.' ('0'..'9')* exponent? | '.' ('0'..'9')+ exponent? | ('0'..'9')+ exponent ; comment : '//' ~('\n'|'\r')* '\r'? '\n' {$channel=hidden;} | '/*' ( options {greedy=false;} : . )* '*/' {$channel=hidden;} ; ws : ( ' ' | '\t' | '\r' | '\n' ) {$channel=hidden;} ; stringliteral : '"' ( esc_seq | ~('\\'|'"') )* '"' ; char: '\'' ( esc_seq | ~('\''|'\\') ) '\'' ; fragment exponent : ('e'|'e') ('+'|'-')? ('0'..'9')+ ; fragment hex_digit : ('0'..'9'|'a'..'f'|'a'..'f') ; fragment esc_seq : '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\') | unicode_esc | octal_esc ; fragment octal_esc : '\\' ('0'..'3') ('0'..'7') ('0'..'7') | '\\' ('0'..'7') ('0'..'7') | '\\' ('0'..'7') ; fragment unicode_esc : '\\' 'u' hex_digit hex_digit hex_digit hex_digit ;
but when trying generate code got these errors in console:
[13:48:38] checking grammar myvcgrm2.g... [13:48:38] warning(200): myvcgrm2.g:53:27: decision can match input such "'else'" using multiple alternatives: 1, 2 result, alternative(s) 2 disabled input [13:48:38] error(211): myvcgrm2.g:78:21: [fatal] rule assignmentexpr has non-ll(*) decision due recursive rule invocations reachable alts 1,2. resolve left-factoring or using syntactic predicates or using backtrack=true option. [13:48:38] error(211): myvcgrm2.g:111:2: [fatal] rule additiveexpr2 has non-ll(*) decision due recursive rule invocations reachable alts 1,3. resolve left-factoring or using syntactic predicates or using backtrack=true option. [13:48:38] warning(200): myvcgrm2.g:111:2: decision can match input such "'+' id" using multiple alternatives: 1, 3 result, alternative(s) 3 disabled input [13:48:38] error(211): myvcgrm2.g:142:5: [fatal] rule primaryexpr2 has non-ll(*) decision due recursive rule invocations reachable alts 1,2. resolve left-factoring or using syntactic predicates or using backtrack=true option.
warnings , errors these productions (in order): ifstmt, assignmentexpr, additiveexpr2, primaryexpr2.
i've done left factorings , left recursion eliminations. don't understand antlr meant "resolve left-factoring". why these errors , how resolve them? thank you
Comments
Post a Comment