0

当我生成我的树解析器时,我收到错误,指出该方法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;
}

错误


compiler\Walker.g:第 3:4 行之后的节点不匹配的树节点:PARAMETER_LIST 需要 SEMICOLON 编译器 \Walker.g:第 3:9 行之后的节点不匹配的树节点:UP 需要 SEMICOLON compiler\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:来自第 11:13 行之后的节点不匹配的树节点:EXPRESSION 期待 SEMICOLON 编译器\Walker.g:来自第 11:18 行的节点不匹配的树节点:2期望 SEMICOLON 编译器\Walker.g:第 11:25 行之后的节点不匹配的树节点:UP 期望 SEMICOLON 编译器\Walker.g:第 13:13 行之后的节点不匹配的树节点:UP 期望 SEMICOLON 编译器\Walker.g:来自的节点第 14:13 行不匹配的树节点:0 期待分号编译器\Walker.g:来自第 16:9 行的节点不匹配的树节点:+ 期待分号编译器\Walker.g:来自第 16:10 行的节点不匹配的树节点:1 期待分号编译器\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

4

1 回答 1

0

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

options {
    ASTLabelType=CommonTree;
}
于 2013-04-18T18:14:54.433 回答