Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

syntax error - Why am I getting 40 shift/reduce conflict and 1 reduce/reduce?

I have set the rules for the parser

%token DIGIT
%token ALPHA
%token IDENT
%token INTEGER
%token REAL
%token BOOLEAN
%token CLASS
%token DOUBLE
%token INT
%token IF
%token ELSE
%token WHILE
%token FOR
%token TRU
%token FALS
%token EXTENDS
%token INSTANCEOF
%token NEW
%token THIS
%token NULLVAL
%token PRIVATE
%token STATIC
%token VOID
%token RETURN
%token SHORT
%token LONG
%token SIGNED
%token UNSIGNED
%token OR
%token AND
%token EQUALS
%token UNEQUAL
%token GR
%token GRE
%token LESS
%token LESSE
%token PLUS
%token MINUS
%token MULTIPLY
%token DIV
%token MOD
%token EQUAL
%token INC
%token DEC  
%token NEG 

%nonassoc if_prec
%nonassoc ELSE
%right EQUAL INC DEC NEG
%left OR AND EQUALS UNEQUAL GR GRE LESS LESSE INSTANCEOF PLUS MINUS MULTIPLY DIV MOD
%%

File: Decl_Class 

Decl_Class: CLASS IDENT '(' EXTENDS IDENT ')' '{' Field '}'

Field: Variable ';'
      | Constructor
      | Method

Variable: Modifier Expr_type Decl_variables


Decl_variables: Decl_variable | Decl_variable ',' Decl_variables

Decl_variable: IDENT | IDENT EQUAL Expr

Constructor: Modifier IDENT '(' Params ')' '{' Instruction '}'

Method: Modifier '(' Expr_type | VOID ')' IDENT '(' Params')' '{' Instruction '}'

Params: Expr_type IDENT | Expr_type IDENT ',' Params

Modifier: SHORT | LONG | SIGNED | UNSIGNED

Expr_type: BOOLEAN | INT | DOUBLE | IDENT

Expr:              INTEGER | REAL | TRU | FALS
                   | THIS | NULLVAL
                   | '(' Expr ')'
                   | Access | Access EQUAL Expr | Access '(' L_Expr ')'
                   | NEW IDENT '(' L_Expr ')'
                   | INC Expr | DEC Expr | Expr INC | Expr DEC
                   | NEG Expr | MINUS Expr | PLUS Expr
                   | Expr Operator Expr
                   | '(' Expr_type ')' Expr
                   | Expr INSTANCEOF Expr_type

Operator: OR | AND | EQUALS | UNEQUAL | GR | GRE | LESS | LESSE | PLUS | MINUS | MULTIPLY | DIV | MOD

Access: IDENT | Expr '.' IDENT

L_Expr: Expr | Expr ',' L_Expr

Instruction: ';'
           | Expr';'
           | Expr_type Decl_variables';'
           | IF '(' Expr ')' Instruction %prec if_prec
           | IF '(' Expr ')' Instruction ELSE Instruction
           | WHILE '(' Expr ')' Instruction
           | FOR '(' L_Expr ';' Expr ';'  L_Expr')' Instruction
           | FOR '(' Expr_type ')' Decl_variables ';' Expr ';'  L_Expr ')' Instruction '{' Instruction '}'
           | RETURN Expr ';'

State 66 conflicts: 1 shift/reduce State 67 conflicts: 1 shift/reduce State 68 conflicts: 1 shift/reduce State 69 conflicts: 1 shift/reduce State 70 conflicts: 1 shift/reduce State 71 conflicts: 1 reduce/reduce State 109 conflicts: 17 shift/reduce State 110 conflicts: 1 shift/reduce State 122 conflicts: 17 shift/reduce

Here are the states with conflicts:


state 66

   37 Expr: Expr . INC
   38     | Expr . DEC
   41     | PLUS Expr .
   42     | Expr . Operator Expr
   44     | Expr . INSTANCEOF Expr_type
   59 Access: Expr . '.' IDENT

    '.'  shift, and go to state 90

    '.'       [reduce using rule 41 (Expr)]
    $default  reduce using rule 41 (Expr)

    Operator  go to state 91


state 67

   37 Expr: Expr . INC
   38     | Expr . DEC
   40     | MINUS Expr .
   42     | Expr . Operator Expr
   44     | Expr . INSTANCEOF Expr_type
   59 Access: Expr . '.' IDENT

    '.'  shift, and go to state 90

    '.'       [reduce using rule 40 (Expr)]
    $default  reduce using rule 40 (Expr)

    Operator  go to state 91


state 68

   35 Expr: INC Expr .
   37     | Expr . INC
   38     | Expr . DEC
   42     | Expr . Operator Expr
   44     | Expr . INSTANCEOF Expr_type
   59 Access: Expr . '.' IDENT

    INSTANCEOF  shift, and go to state 74
    OR          shift, and go to state 75
    AND         shift, and go to state 76
    EQUALS      shift, and go to state 77
    UNEQUAL     shift, and go to state 78
    GR          shift, and go to state 79
    GRE         shift, and go to state 80
    LESS        shift, and go to state 81
    LESSE       shift, and go to state 82
    PLUS        shift, and go to state 83
    MINUS       shift, and go to state 84
    MULTIPLY    shift, and go to state 85
    DIV         shift, and go to state 86
    MOD         shift, and go to state 87
    INC         shift, and go to state 88
    DEC         shift, and go to state 89
    '.'         shift, and go to state 90

    '.'       [reduce using rule 35 (Expr)]
    $default  reduce using rule 35 (Expr)

    Operator  go to state 91


state 69

   36 Expr: DEC Expr .
   37     | Expr . INC
   38     | Expr . DEC
   42     | Expr . Operator Expr
   44     | Expr . INSTANCEOF Expr_type
   59 Access: Expr . '.' IDENT

    INSTANCEOF  shift, and go to state 74
    OR          shift, and go to state 75
    AND         shift, and go to state 76
    EQUALS      shift, and go to state 77
    UNEQUAL     shift, and go to state 78
    GR          shift, and go to state 79
    GRE         shift, and go to state 80
    LESS        shift, and go to state 81
    LESSE       shift, and go to state 82
    PLUS        shift, and go to state 83
    MINUS       shift, and go to state 84
    MULTIPLY    shift, and go to state 85
    DIV         shift, and go to state 86
    MOD         shift, and go to state 87
    INC         shift, and go to state 88
    DEC         shift, and go to state 89
    '.'         shift, and go to state 90

    '.'       [reduce using rule 36 (Expr)]
    $default  reduce using rule 36 (Expr)

    Operator  go to state 91

state 109

   37 Expr: Expr . INC
   38     | Expr . DEC
   42     | Expr . Operator Expr
   42     | Expr Operator Expr .
   44     | Expr . INSTANCEOF Expr_type
   59 Access: Expr . '.' IDENT

    INSTANCEOF  shift, and go to state 74
    OR          shift, and go to state 75
    AND         shift, and go to state 76
    EQUALS      shift, and go to state 77
    UNEQUAL     shift, and go to state 78
    GR          shift, and go to state 79
    GRE         shift, and go to state 80
    LESS        shift, and go to state 81
    LESSE       shift, and go to state 82
    PLUS        shift, and go to state 83
    MINUS       shift, and go to state 84
    MULTIPLY    shift, and go to state 85
    DIV         shift, and go to state 86
    MOD         shift, and go to state 87
    INC         shift, and go to state 88
    DEC         shift, and go to state 89
    '.'         shift, and go to state 90

    INSTANCEOF  [reduce using rule 42 (Expr)]
    OR          [reduce using rule 42 (Expr)]
    AND         [reduce using rule 42 (Expr)]
    EQUALS      [reduce using rule 42 (Expr)]
    UNEQUAL     [reduce using rule 42 (Expr)]
    GR          [reduce using rule 42 (Expr)]
    GRE         [reduce using rule 42 (Expr)]
    LESS        [reduce using rule 42 (Expr)]
    LESSE       [reduce using rule 42 (Expr)]
    PLUS        [reduce using rule 42 (Expr)]
    MINUS       [reduce using rule 42 (Expr)]
    MULTIPLY    [reduce using rule 42 (Expr)]
    DIV         [reduce using rule 42 (Expr)]
    MOD         [reduce using rule 42 (Expr)]
    INC         [reduce using rule 42 (Expr)]
    DEC         [reduce using rule 42 (Expr)]
    '.'         [reduce using rule 42 (Expr)]
    $default    reduce using rule 42 (Expr)

    Operator  go to state 91


state 110

   32 Expr: Access EQUAL Expr .
   37     | Expr . INC
   38     | Expr . DEC
   42     | Expr . Operator Expr
   44     | Expr . INSTANCEOF Expr_type
   59 Access: Expr . '.' IDENT

    INSTANCEOF  shift, and go to state 74
    OR          shift, and go to state 75
    AND         shift, and go to state 76
    EQUALS      shift, and go to state 77
    UNEQUAL     shift, and go to state 78
    GR          shift, and go to state 79
    GRE         shift, and go to state 80
    LESS        shift, and go to state 81
    LESSE       shift, and go to state 82
    PLUS        shift, and go to state 83
    MINUS       shift, and go to state 84
    MULTIPLY    shift, and go to state 85
    DIV         shift, and go to state 86
    MOD         shift, and go to state 87
    INC         shift, and go to state 88
    DEC         shift, and go to state 89
    '.'         shift, and go to state 90

    '.'       [reduce using rule 32 (Expr)]
    $default  reduce using rule 32 (Expr)

    Operator  go to state 91
state 122

   37 Expr: Expr . INC
   38     | Expr . DEC
   42     | Expr . Operator Expr
   43     | '(' Expr_type ')' Expr .
   44     | Expr . INSTANCEOF Expr_type
   59 Access: Expr . '.' IDENT

    INSTANCEOF  shift, and go to state 74
    OR          shift, and go to state 75
    AND         shift, and go to state 76
    EQUALS      shift, and go to state 77
    UNEQUAL     shift, and go to state 78
    GR          shift, and go to state 79
    GRE         shift, and go to state 80
    LESS        shift, and go to state 81
    LESSE       shift, and go to state 82
    PLUS        shift, and go to state 83
    MINUS       shift, and go to state 84
    MULTIPLY    shift, and go to state 85
    DIV         shift, and go to state 86
    MOD         shift, and go to state 87
    INC         shift, and go to state 88
    DEC         shift, and go to state 89
    '.'         shift, and go to state 90

    INSTANCEOF  [reduce using rule 43 (Expr)]
    OR          [reduce using rule 43 (Expr)]
    AND         [reduce using rule 43 (Expr)]
    EQUALS      [reduce using rule 43 (Expr)]
    UNEQUAL     [reduce using rule 43 (Expr)]
    GR          [reduce using rule 43 (Expr)]
    GRE         [reduce using rule 43 (Expr)]
    LESS        [reduce using rule 43 (Expr)]
    LESSE       [reduce using rule 43 (Expr)]
    PLUS        [reduce using rule 43 (Expr)]
    MINUS       [reduce using rule 43 (Expr)]
    MULTIPLY    [reduce using rule 43 (Expr)]
    DIV         [reduce using rule 43 (Expr)]
    MOD         [reduce using rule 43 (Expr)]
    INC         [reduce using rule 43 (Expr)]
    DEC         [reduce using rule 43 (Expr)]
    '.'         [reduce using rule 43 (Expr)]
    $default    reduce using rule 43 (Expr)

    Operator  go to state 91

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...