ANTLR树语法 - >生成的java类有错误(getText)

时间:2013-04-18 18:12:34

标签: java antlr antlr3 antlrworks

当我生成我的Tree Parser时,我得到的错误表明方法getText()未定义类型对象。因为它的大约500000个字符所以无法进入全班。

但这些是我得到的类似错误行

public int specialStateTransition(int s, IntStream _input) throws NoViableAltException {
        TreeNodeStream input = (TreeNodeStream)_input;
        int _s = s;
        switch ( s ) {
                case 0 : 
                    int LA3_11 = input.LA(1);


                    int index3_11 = input.index();
                    input.rewind();
                    s = -1;
                    if ( ((synpred5_Walker()&&(isTypeName(input.LT(1).getText())))) ) {s = 1;}

                    else if ( (true) ) {s = 8;}


                    input.seek(index3_11);
                    if ( s>=0 ) return s;
                    break;
                case 1 : 
                    int LA3_16 = input.LA(1);


                    int index3_16 = input.index();
                    input.rewind();
                    s = -1;
                    if ( ((synpred5_Walker()&&(isTypeName(input.LT(1).getText())))) ) {s = 1;}

                    else if ( (true) ) {s = 8;}


                    input.seek(index3_16);
                    if ( s>=0 ) return s;
                    break;
                case 2 : 
                    int LA3_20 = input.LA(1);


                    int index3_20 = input.index();
                    input.rewind();
                    s = -1;
                    if ( ((synpred5_Walker()&&(isTypeName(input.LT(1).getText())))) ) {s = 1;}

                    else if ( (true) ) {s = 8;}


                    input.seek(index3_20);
                    if ( s>=0 ) return s;
                    break;
                case 3 : 
                    int LA3_22 = input.LA(1);


                    int index3_22 = input.index();
                    input.rewind();
                    s = -1;
                    if ( ((synpred5_Walker()&&(isTypeName(input.LT(1).getText())))) ) {s = 1;}

                    else if ( (true) ) {s = 8;}


                    input.seek(index3_22);
                    if ( s>=0 ) return s;
                    break;
                case 4 : 
                    int LA3_23 = input.LA(1);


                    int index3_23 = input.index();
                    input.rewind();
                    s = -1;
                    if ( ((synpred5_Walker()&&(isTypeName(input.LT(1).getText())))) ) {s = 1;}

                    else if ( (true) ) {s = 8;}


                    input.seek(index3_23);
                    if ( s>=0 ) return s;
                    break;
                case 5 : 
                    int LA3_24 = input.LA(1);


                    int index3_24 = input.index();
                    input.rewind();
                    s = -1;
                    if ( ((synpred5_Walker()&&(isTypeName(input.LT(1).getText())))) ) {s = 1;}

                    else if ( (true) ) {s = 8;}


                    input.seek(index3_24);
                    if ( s>=0 ) return s;
                    break;
                case 6 : 
                    int LA3_25 = input.LA(1);


                    int index3_25 = input.index();
                    input.rewind();
                    s = -1;
                    if ( ((synpred5_Walker()&&(isTypeName(input.LT(1).getText())))) ) {s = 1;}

                    else if ( (true) ) {s = 8;}


                    input.seek(index3_25);
                    if ( s>=0 ) return s;
                    break;
                case 7 : 
                    int LA3_26 = input.LA(1);


                    int index3_26 = input.index();
                    input.rewind();
                    s = -1;
                    if ( ((synpred5_Walker()&&(isTypeName(input.LT(1).getText())))) ) {s = 1;}

                    else if ( (true) ) {s = 8;}


                    input.seek(index3_26);
                    if ( s>=0 ) return s;
                    break;
                case 8 : 
                    int LA3_27 = input.LA(1);


                    int index3_27 = input.index();
                    input.rewind();
                    s = -1;
                    if ( ((synpred5_Walker()&&(isTypeName(input.LT(1).getText())))) ) {s = 1;}

                    else if ( (true) ) {s = 8;}


                    input.seek(index3_27);
                    if ( s>=0 ) return s;
                    break;
        }
        if (state.backtracking>0) {state.failed=true; return -1;}
        NoViableAltException nvae =
            new NoViableAltException(getDescription(), 3, _s, input);
        error(nvae);
        throw nvae;
    }
}

我的树语法


tree grammar Walker;

options {

  tokenVocab = c2p;
  ASTLabelType = CommonTree;
  backtrack=true;     
  output=AST;
}

@header { 
 package com.frankdaniel.compiler ;

}  

translation_unit 
    :  external_declaration+
    ; 

external_declaration
options {k=1;}
    : (declaration_specifiers? declarator declaration* L_C_BRACKET! )=> function_definition //-> EXTERNAL_DECLARATOR function_definition
    | declaration
    ; 


function_definition
    :   declaration_specifiers? declarator (declaration+ compound_statement|compound_statement) //-> ^(FUNCTIONDEF declaration_specifiers? declarator (declaration+ compound_statement|compound_statement)) 
    ;

declaration

    : 'typedef' declaration_specifiers? init_declarator_list SEMICOLON! // special case, looking for typedef    
    | declaration_specifiers init_declarator_list? SEMICOLON!
    ;

declaration_specifiers
    :   (   storage_class_specifier
        |   type_specifier
        |   type_qualifier
        )+
    ;

init_declarator_list
    : ^(INIT_DECLARATOR_LIST init_declarator+)
    ;

init_declarator
    : declarator (ASSIGN^ initializer)?
    ;

storage_class_specifier
    : EXTERN
    | STATIC
    | AUTO
    | REGISTER
    ;

type_specifier
    : VOID
    | CHAR
    | INT
    | FLOAT
    | type_id
    ;

type_id
    :   IDENTIFIER
//      {System.out.println($IDENTIFIER.text+" is a type");}
    ;


type_qualifier
    : CONST

    ;

declarator
    : pointer? direct_declarator
    | pointer
    ;

direct_declarator
    :   (IDENTIFIER|LPAREN! declarator RPAREN!)
        declarator_suffix*
    ;

declarator_suffix
    :   constant_expression
    |   RBRACKET! LBRACKET!
    |   parameter_type_list
    |   identifier_list
    |   LPAREN! RPAREN!
    ;

pointer
    : TIMES type_qualifier+ pointer?
    | TIMES pointer
    | TIMES
    ;

parameter_type_list
    : parameter_list
    ;

parameter_list
    : ^(PARAMETER_LIST parameter_declaration)
    ;

parameter_declaration
    : declaration_specifiers (declarator|abstract_declarator)*
    ;

identifier_list
    : ^(IDENTIFIER_LIST IDENTIFIER+)
    ;

type_name
    : specifier_qualifier_list abstract_declarator?
    ;
specifier_qualifier_list
    : ( type_qualifier | type_specifier )+
    ;

abstract_declarator
    : pointer direct_abstract_declarator?
    | direct_abstract_declarator
    ;

direct_abstract_declarator
    :   ( LPAREN! abstract_declarator RPAREN | abstract_declarator_suffix ) abstract_declarator_suffix*
    ;

abstract_declarator_suffix
    :   RBRACKET! LBRACKET!
    |   constant_expression 
    |   LPAREN! RPAREN!
    |   parameter_type_list
    ;

initializer
    : assignment_expression
    | initializer_list
    ;

initializer_list
    :  ^(INITIALIZER_LIST initializer+)
    ;

// EXPRESSIONS

argument_expression_list
    :  ^(EXPRESSION_LIST assignment_expression+)
    ;

multiplicative_expression
    : (cast_expression) (TIMES^ cast_expression | DIV^ cast_expression | MOD^ cast_expression)* 
    ;

additive_expression
    : (multiplicative_expression) (PLUS^ multiplicative_expression | MINUS^ multiplicative_expression)* 
    ;


cast_expression
    : ^(CAST_EXPRESSION type_name cast_expression)
    | unary_expression 
    ;

unary_expression
    : postfix_expression
    | PPLUS unary_expression
    | MMINUS unary_expression
    | unary_operator cast_expression
    ;

postfix_expression
    :   primary_expression
        (   RBRACKET! expression LBRACKET!
        |   LPAREN! RPAREN! 
        |   LPAREN! argument_expression_list RPAREN! 
        |   DOT! IDENTIFIER
//        |   PPLUS
//        |   MMINUS
        )*
    ;

unary_operator
    : BITWISEAND
    | TIMES
    | PLUS
    | MINUS
    | NOT
    ;

primary_expression
    : IDENTIFIER
    | constant
    | expression
    ;

constant 
    :   HEX_LITERAL
    |   OCTAL_LITERAL
    |   DECIMAL_LITERAL
    |   CHARACTER_LITERAL
    |   STRING_LITERAL
    |   FLOATING_POINT_LITERAL
    ;
















expression
    : ^(EXPRESSION assignment_expression+)
    ;

constant_expression
    : conditional_expression
    ;

assignment_expression
    :^(assignment_operator lvalue assignment_expression)
    | conditional_expression
    ;

lvalue
    :   unary_expression
    ;

assignment_operator
    : ASSIGN 
    ;

conditional_expression
    : logical_or_expression (QUESTIONMARK! expression COLON! conditional_expression)?
    ;

logical_or_expression
    : logical_and_expression (OR^ logical_and_expression)*
    ;

logical_and_expression
    : inclusive_or_expression (AND^ inclusive_or_expression)*
    ;

inclusive_or_expression
    : exclusive_or_expression ('|'^ exclusive_or_expression)*
    ;

exclusive_or_expression
    : and_expression ('^'^ and_expression)*
    ;

and_expression
    : equality_expression ('&'^ equality_expression)*  
    ; 
equality_expression
    : relational_expression ((EQUAL|NONEQUAL)^ relational_expression)*  ;

relational_expression
    : shift_expression ((ST|GT|STEQ|GTEQ)^ shift_expression)* 
    ;

shift_expression
    : additive_expression ((LSHIFT|RSHIFT)^ additive_expression)*
    ;










// STATEMENTS

statement
    : labeled_statement
    | compound_statement
    | expression_statement
    | selection_statement
    | iteration_statement
    | jump_statement
    ;

labeled_statement
    : IDENTIFIER statement 
    | CASE constant_expression statement
    | DEFAULT statement
    ;

compound_statement
    : ^(STATEMENT declaration* statement_list? )
    ;

statement_list
    : statement+
    ;

expression_statement
    : expression
    ;

selection_statement
    :IF^ LPAREN! expression RPAREN! i=statement (ELSE^ e=statement)? 
    | ^(SWITCH expression statement)
    ;


iteration_statement
    : ^(WHILE expression statement)
    | ^(DO statement ^(WHILE expression))
    | ^(FOR expression_statement expression_statement expression? statement)
    ;

jump_statement
    : ^(GOTO IDENTIFIER)
    | CONTINUE
    | BREAK
    | ^(RETURN expression?)
    ;

测试文件


int main(void) {
  int n;
  int i;
  int flag;

  printf("Enter value of N > ");
  scanf("%d", &n);
  flag = 1;
  for (i=2; (i<(n/2)) && flag; ) { /* May be we do not need to test
            values of i greater than the square root of n? */
    if ((n % i) == 0) /* If true n is divisible by i */
      flag = 0;
    else
      i=i+1;
  }

  if (flag)
    printf("%d is prime\n", n);
  else
    printf("%d has %d as a factor\n", n, i);
  return 0;
}

错误


编译器\ Walker.g:节点从第3行:第4行不匹配的树节点:PARAMETER_LIST期待SEMICOLON 编译器\ Walker.g:节点从第3行:9行不匹配的树节点:UP期望SEMICOLON编译器\ Walker.g:节点来自第5行:2不匹配的树节点:int期望SEMICOLON编译器\ Walker.g:来自第6行的节点: 2不匹配的树节点:int期望SEMICOLON编译器\ Walker.g:节点从第6行:6不匹配的树节点:UP期望SEMICOLON编译器\ Walker.g:节点从第8行:2不匹配的树节点:EXPRESSION_LIST期待SEMICOLON 编译器\ Walker.g:节点从第9行:2不匹配的树节点:EXPRESSION_LIST期待SEMICOLON 编译器\ Walker.g:节点从第9:15行不匹配的树节点:UP期望SEMICOLON编译器\ Walker.g:节点从第10行:9不匹配的树节点:1期待SEMICOLON编译器\ Walker.g:节点从第11行: 9个不匹配的树节点:2期待SEMICOLON编译器\ Walker.g:节点从第1113行后不匹配的树节点:EXPRESSION期待SEMICOLON 编译器\ Walker.g:节点从第11:18行不匹配树节点:2期待SEMICOLON编译器\ Walker.g:节点从第1125行后不匹配的树节点:UP期望SEMICOLON编译器\ Walker.g:节点从第13行开始:13不匹配的树节点:UP期望SEMICOLON编译器\ Walker.g:节点从第14:13行不匹配的树节点:0期待SEMICOLON编译器\ Walker.g:节点从第16:9行不匹配的树节点:+期待SEMICOLON编译器\ Walker .g:来自第16:10行的节点不匹配的树节点:1期望SEMICOLON编译器\ Walker.g:节点从第19:6行不匹配的树节点:UP期望SEMICOLON编译器\ Walker.g:节点从第20行:4行不匹配树节点:EXPRESSION_LIST期待SEMICOLON 编译器\ Walker.g:节点从20:28后不匹配的树节点:UP期望SEMICOLON编译器\ Walker.g:节点从第22行:4不匹配的树节点:EXPRESSION_LIST期待SEMICOLON 编译器\ Walker.g:节点从22:41后不匹配的树节点:UP期待SEMICOLON

1 个答案:

答案 0 :(得分:0)

您需要指定AST节点类型。如果未指定,则生成的代码中使用的默认值仅为Object

options {
    ASTLabelType=CommonTree;
}