ANTLR3解决语法与非LL(*)决策

时间:2015-05-12 09:30:33

标签: antlr antlr3 antlrworks

作为家庭作业,我应该使用ANTLR为VC language V10.1创建一个解析器。 这是我的工作:

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
    ;

但是当我尝试生成代码时,我在控制台中遇到了这些错误:

[13:48:38] Checking Grammar MyVCgrm2.g...
[13:48:38] warning(200): MyVCgrm2.g:53:27: 
Decision can match input such as "'else'" using multiple alternatives: 1, 2

As a result, alternative(s) 2 were disabled for that input
[13:48:38] error(211): MyVCgrm2.g:78:21: [fatal] rule assignmentExpr has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by 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 to recursive rule invocations reachable from alts 1,3.  Resolve by 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 as "'+' ID" using multiple alternatives: 1, 3

As a result, alternative(s) 3 were disabled for that input
[13:48:38] error(211): MyVCgrm2.g:142:5: [fatal] rule primaryExpr2 has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using backtrack=true option.

警告和错误与这些产品有关(按顺序):ifStmt,assignmentExpr,additiveExpr2,primaryExpr2。
我已经完成了所有左边的因子并且留下了递归抵消。我不明白ANTLR的意思是&#34;通过左因素分解&#34;。 为什么我会收到这些错误,如何解决? 谢谢

0 个答案:

没有答案