2013-08-29 66 views
0

我試圖做一個Java語言的解析器,但與其他語句,移位/減少衝突出現。 我試圖野牛bison_file.y --report=state和有關衝突的結果是:轉換/減少衝突與if ... else語句

國家62 31聲明:if_statement。 65 if_else_statement:if_statement。 ELSE語句 ELSE轉變,並進入狀態84 ELSE [減少使用第31條(聲明) $默認降低使用第31條(聲明)

我想不出辦法來避免衝突。任何好主意? 在這裏,我提交的完整代碼:

%{ 
#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; 
} 
+1

可能的重複http://stackoverflow.com/questions/12731922/reforming-the-grammar-to-remove-shift-reduce-conflict-in-if-then-else – FrankPl

回答

1

這裏解析器推動令牌入堆棧,當IF abc推和下一個標記是ELSE會有衝突,解析器應根據IF abc減少if_statement規則或它應該將下一個標記ELSE轉移到堆棧中。 您必須確定規則的優先級,在這種情況下,您必須通過使用%nonassoc和%prec來爲ELSE令牌賦予比if_statement更高的優先級。試試這個:

if_statement: IF abc %prec else_priority 

,並在優先領域:

%nonassoc else_priority 
%nonassoc ELSE 

你必須寫的優先級順序(以下少一個更優先)。希望這能解決你的問題。