When i generate my Tree Parser, i get errors which says the method getText()
is undefined for the type object. Can't enter the whole class here since its about 500000 Characters.
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 SEMICOLON 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