0

I have a complete parser grammer than generates an AST which i could say is correct using the rewrite rules and tree operators. At the moment i am stuck at the phase of creating a tree grammar.I have this error:

The following sets of rules are mutually left-recursive [direct_declarator, declarator] and [abstract_declarator, direct_abstract_declarator]

rewrite syntax or operator with no output option; setting output=AST

Here is my Tree Grammar.

tree grammar walker;

options {
  language = Java;
  tokenVocab = c2p;
  ASTLabelType = CommonTree;
  backtrack = true;
}

@header  
{
package com.frankdaniel.compiler;
}

translation_unit 
    :  ^(PROGRAM (^(FUNCTION external_declaration))+)
    ; 

external_declaration
options {k=1;}
    : (declaration_specifiers? declarator declaration*)=> function_definition 
    | declaration
    ; 


function_definition
    :   declaration_specifiers? declarator (declaration+ compound_statement|compound_statement) 
    ;

declaration

    : 'typedef' declaration_specifiers? init_declarator_list    
    | declaration_specifiers init_declarator_list? 
    ;

declaration_specifiers
    :   ( type_specifier|type_qualifier)+
    ;

init_declarator_list
    : ^(INIT_DECLARATOR_LIST init_declarator+)
    ;

init_declarator
    : declarator (ASSIGN^ initializer)?  
    ;


type_specifier : (CONST)? (VOID | CHAR | INT | FLOAT );


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

type_qualifier
    : CONST 
    ;

declarator
    : pointer? direct_declarator
    | pointer
    ;

direct_declarator
    :   (IDENTIFIER|declarator) declarator_suffix*
    ;   

declarator_suffix
    :   constant_expression
    |   parameter_type_list
    |   identifier_list

    ;

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
    :   (abstract_declarator | abstract_declarator_suffix ) abstract_declarator_suffix*
    ;

abstract_declarator_suffix
    :   constant_expression
    |   parameter_type_list
    ;

initializer
    : assignment_expression
    | initializer_list?
    ;

initializer_list
    : ^(INITIALIZER_LIST initializer+)
    ;


// EXPRESSIONS
argument_expression_list
    :   ^(EXPRESSION_LIST assignment_expression+)
    ;

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


additive_expression
    : ^((PLUS|MINUS) multiplicative_expression  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
        (   expression
        |   argument_expression_list
        |   IDENTIFIER
        |   IDENTIFIER
        |   PPLUS
        |   MMINUS
        )*  
    ;

unary_operator
    : 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 : ^(OR logical_and_expression logical_and_expression);
logical_and_expression : ^(AND equality_expression equality_expression);

//equality_expression : (a=relational_expression) ((e=EQUAL|e=NONEQUAL)^ b=relational_expression)?;
equality_expression : ^((EQUAL|NONEQUAL) relational_expression relational_expression);

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



// STATEMENTS
statement
    : compound_statement
    | expression_statement
    | selection_statement
    | iteration_statement 
    | jump_statement
    ;

compound_statement
    : ^(STATEMENT declaration* statement_list? )
    ;

statement_list
    : statement+
    ;

expression_statement
    :expression
    ;

selection_statement
    :^(IF expression statement (^(ELSE statement))? )
    |^(SWITCH expression statement)
    ;

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

jump_statement
    : CONTINUE
    | BREAK
    | RETURN
    | ^(RETURN expression)
    ;
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
Undisputed007
  • 639
  • 1
  • 10
  • 31

1 Answers1

2

It seems obvious that the following two rules are left recursive:

{code} declarator : pointer? direct_declarator | pointer ;

direct_declarator : (IDENTIFIER|declarator) declarator_suffix* ;
{code}

Rule "declarator" has reference to "direct_declarator", and "direct_declarator" has reference to "declarator", and there's no other predicates to pilot the rule evaluation.

Benjamin
  • 68
  • 4
  • How may i fix resolve this please,been trying different approaches to no avail. – Undisputed007 May 03 '13 at 12:25
  • Left recursion error happens when i remove the LPAREN! and RPAREN! which is what i think i am suppose to do when writing a tree grammar. Can Tree operator rules be used in Tree Grammar ? if so then i could leave it. – Undisputed007 May 03 '13 at 16:08