简体   繁体   English

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

[英]ANTLR Tree Grammar -> Generated java class has errors (getText)

When i generate my Tree Parser, i get errors which says the method getText() is undefined for the type object. 当我生成树解析器时,出现错误,该错误表明类型对象的getText()方法未定义。 Can't enter the whole class here since its about 500000 Characters. 大约50万个字符,因此无法在此处输入整个课程。

But these are the similar error lines I get is 但这是我得到的类似错误行

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

My Tree grammar 我的树语法


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?)
    ;

THE TEST FILE 测试文件


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

THE ERROR 错误


compiler\\Walker.g: node from after line 3:4 mismatched tree node: PARAMETER_LIST expecting SEMICOLON compiler\\Walker.g: node from after line 3:9 mismatched tree node: UP expecting SEMICOLON compiler\\Walker.g: node from line 5:2 mismatched tree node: int expecting SEMICOLON compiler\\Walker.g: node from line 6:2 mismatched tree node: int expecting SEMICOLON compiler\\Walker.g: node from after line 6:6 mismatched tree node: UP expecting SEMICOLON compiler\\Walker.g: node from after line 8:2 mismatched tree node: EXPRESSION_LIST expecting SEMICOLON compiler\\Walker.g: node from after line 9:2 mismatched tree node: EXPRESSION_LIST expecting SEMICOLON compiler\\Walker.g: node from after line 9:15 mismatched tree node: UP expecting SEMICOLON compiler\\Walker.g: node from line 10:9 mismatched tree node: 1 expecting SEMICOLON compiler\\Walker.g: node from line 11:9 mismatched tree node: 2 expecting SEMICOLON compiler\\Walker.g: node from after line 11:13 mismatched tree node: EXPRESSION expecting SEMI 编译器\\ 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:第11:13行之后的节点与树节点不匹配:EXPRESSION期望SEMI COLON compiler\\Walker.g: node from line 11:18 mismatched tree node: 2 expecting SEMICOLON compiler\\Walker.g: node from after line 11:25 mismatched tree node: UP expecting SEMICOLON compiler\\Walker.g: node from after line 13:13 mismatched tree node: UP expecting SEMICOLON compiler\\Walker.g: node from line 14:13 mismatched tree node: 0 expecting SEMICOLON compiler\\Walker.g: node from line 16:9 mismatched tree node: + expecting SEMICOLON compiler\\Walker.g: node from line 16:10 mismatched tree node: 1 expecting SEMICOLON compiler\\Walker.g: node from after line 19:6 mismatched tree node: UP expecting SEMICOLON compiler\\Walker.g: node from after line 20:4 mismatched tree node: EXPRESSION_LIST expecting SEMICOLON compiler\\Walker.g: node from after line 20:28 mismatched tree node: UP expecting SEMICOLON compiler\\Walker.g: node from after line 22:4 mismatched tree node: EXPRESSION_LIST expecting SEMICOLON compiler\\Walker.g: node from after line 22:41 mismatched tree node: UP expecting SEMICOLON COLON编译器\\ Walker.g:第11:18行的节点与树节点不匹配:2个期望SEMICOLON编译器\\ Walker.g:第11:25行之后的节点不匹配树节点:UP期望SEMICOLON编译器\\ Walker.g:第1行之后的节点13:13不匹配的树节点:UP期待SEMICOLON编译器\\ Walker.g:第14行的不匹配树节点: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行之后的节点。 .g:第22:41行之后的节点与树节点不匹配:UP期望SEMICOLON

You need to specify the AST node type. 您需要指定AST节点类型。 If unspecified, the default used in the generated code is just Object . 如果未指定,则在生成的代码中使用的默认值只是Object

options {
    ASTLabelType=CommonTree;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM