我有一个完整的解析器语法而不是生成一个AST,我可以说使用重写规则和树操作符是正确的。目前我陷入了创建树语法的阶段。我有这个错误:
以下几组规则是相互左递归的[direct_declarator,declarator]和[abstract_declarator,direct_abstract_declarator]
重写语法或运算符,没有输出选项;设置输出= AST
这是我的树语法。
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)
;
答案 0 :(得分:2)
很明显,以下两条规则是递归的:
{代码} 声明符 :指针? direct_declarator |指针 ;
direct_declarator
:(IDENTIFIER |声明符)declarator_suffix *
;
{代码}
规则“声明者”引用“direct_declarator”,“direct_declarator”引用“声明者”,并且没有其他谓词可以引导规则评估。