0

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;
}

エラー


compiler\Walker.g: 行 3:4 の後のノード 一致しないツリー ノード: PARAMETER_LIST セミコロンを期待する compiler\Walker.g: 行 3:9 の後のノード 一致しないツリー ノード: UP セミコロンを予期する 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 を予期しています compiler\Walker.g: 行 9 の後のノード: 15 不一致のツリー ノード: UP が SEMICOLON コンパイラ\Walker.g を期待: 行からのノード 10:9 不一致のツリー ノード: 1 が SEMICOLON を期待する compiler\Walker.g:行 11:9 からのノードが一致しません ツリー ノード: 2 予期される SEMICOLON コンパイラ\Walker.g: 行 11:13 からのノードが一致しません ツリー ノード: EXPRESSION 予期される SEMICOLON コンパイラ\Walker.g: 行 11:18 からのノード 一致しないツリー ノード: 2予期される SEMICOLON compiler\Walker.g: 行 11:25 の後のノードからのノード 不一致のツリー ノード: UP行 14:13 不一致のツリー ノード: 0 期待 SEMICOLON コンパイラ\Walker.g: 行 16:9 からのノード 不一致ツリー ノード: + 期待 SEMICOLON コンパイラ\Walker.g: 行 16:10 からのノード 不一致ツリー ノード: 1 期待 SEMICOLON コンパイラ\Walker.g: 行 19:6 の後のノードの不一致ツリー ノード: セミコロン コンパイラを期待する UP \Walker.g: 行 20:4 の不一致のツリー ノードの後のノード:EXPRESSION_LIST が SEMICOLON を期待する compiler\Walker.g: 行 20:28 以降のノード ツリー ノードの不一致: UP行 22:41 の後からツリー ノードが一致しません: セミコロンが必要な場合は UP

4

1 に答える 1