0

我正在尝试制作一个关于类似 java 的语言的解析器,但是使用 else 语句时,会出现移位/减少冲突。我尝试了野牛bison_file.y --report=state,关于冲突的结果是:

状态 62 31 语句: if_statement 。65 if_else_statement: if_statement 。ELSE statement ELSE shift, and go to state 84 ELSE [reduce using rule 31 (statement)] $default reduce using rule 31 (statement)

我想不出避免冲突的方法。有什么好主意吗?在这里,我提交完整的代码:

%{
#include <stdio.h>
#include <math.h>
void yyerror(char *);
extern int yylval;
extern FILE *yyin;
extern FILE *yyout;
extern yylineno;
extern int yyparse(void);
extern int yylex(void);
extern int yywrap() { return 1; }
extern char* yytext;
int errors;
%}

%debug
%start m_class


%token IF ELSE INT CHAR CLASS NEW GURISE VOID WHILE
%token PUBLIC PROTECTED PRIVATE STATIC FINAL ABSTRACT
%token PLUS MINUS MUL DIV MODULO
%token EQ NEQ GRT LT GREQ LEQ
%token OR AND NOT
%token AR_PAR DEK_PAR AR_AGK DEK_AGK AR_STRO DEK_STRO
%token SEMICOLON ANATHESI COMA
%token MY_INT SINT MY_CHAR ID

%right          ANATHESI
%left           OR AND
%nonassoc       EQ NEQ GRT LT GREQ LEQ
%left           PLUS MINUS MUL DIV MODULO
%right          NOT

%%

m_class: m_class class_declaration
        | class_declaration
        ;

class_declaration: CLASS ID class_body
        ;

class_body: AR_STRO variable_declaration constructor method_declaration DEK_STRO
        ;

variable_declaration:variable variable_declaration
    |variable
    |array_declaration
    |array_declaration variable_declaration
        ;

variable:  var_type ID SEMICOLON
    ;

var_type: INT
        |CHAR
        ;

array_declaration: ID ANATHESI NEW var_type AR_AGK MY_INT DEK_AGK SEMICOLON
    ;


constructor: modifier ID AR_STRO variable_declaration DEK_STRO
        ;

modifier: PUBLIC
        | PROTECTED
        | PRIVATE
        | STATIC
        | FINAL
        | ABSTRACT
        ;
method_declaration: modifier meth_type ID parameters meth_body
        ;

meth_type: VOID
        | var_type
        ;

parameters: AR_PAR par_body DEK_PAR
        ;

par_body: var_type ID
        | par_body COMA var_type ID
        ;

meth_body: AR_STRO bodybuilder DEK_STRO
        ;

bodybuilder: statement GURISE expression SEMICOLON
        |statement bodybuilder
    |statement
        ;

statement: anathesh
        | if_statement
    | if_else_statement
        | while_statement
        ;

statementsss: statement
    |
    ;

anathesh:atath SEMICOLON
        | atath numeric_expression SEMICOLON
        ;

atath: ID ANATHESI orisma
    |ID AR_AGK MY_INT DEK_AGK ANATHESI orisma
    ;

orisma: ID
    |MY_INT
    |SINT
    |MY_CHAR
    ;

expression: testing_expression
        | numeric_expression
        | logical_expression
        | ID
        | MY_INT
        | SINT
        | MY_CHAR
        ;

numeric_expression:  expression PLUS  expression
        | expression MINUS expression
        | expression MUL expression
        | expression DIV expression
        | expression MODULO expression
        ;

testing_expression: expression EQ expression
        | expression NEQ expression
        | expression GRT expression
        | expression LT expression
        | expression GREQ expression
        | expression LEQ expression
        ;
logical_expression: expression OR expression
        | expression AND expression
        | expression NOT expression
        ;

if_statement: IF abc
    |
        ;

if_else_statement: if_statement ELSE statement
        ;

abc: sin8iki statement
    ;

sin8iki: AR_PAR testing_expression DEK_PAR
        | AR_PAR logical_expression DEK_PAR
        ;

while_statement: WHILE sin8iki statement
        ;


%%



void yyerror(char *s) {
        errors++;
printf("\n------- ERROR AT LINE #%d.\n\n", yylineno);
fprintf(stderr, "%d: error: '%s' at '%s', yylval=%u\n", yylineno, s, yytext, yylval);

}
int main (int argc, char **argv) {
        ++argv;
        --argc;
    errors=0;
        if (argc > 0)
                yyin = fopen (argv[0], "r");
        else
                yyin = stdin;
        yyout = fopen ("output","w");
        yyparse ();
    if(errors==0)
        printf("komple");
        return 0;
}
4

1 回答 1

1

这里解析器将令牌压入堆栈,当IF abc被压入时和下一个令牌ELSE存在冲突,解析器应IF abc根据 if_statement规则减少或将下一个令牌ELSE移入堆栈。您必须确定规则的优先级,在这种情况下,您必须为ELSE令牌赋予比if_statement使用 %nonassoc 和 %prec 更高的优先级。尝试这个:

if_statement: IF abc %prec else_priority

在优先领域:

%nonassoc else_priority
%nonassoc ELSE

您必须按此顺序编写优先级(优先级越低,优先级越低)。希望这能解决你的问题。

于 2013-08-30T09:14:04.777 回答