Skip to main content
added 7 characters in body
Source Link
[1] E =-> E + E
[2] E |-> E * E
[3] |E number-> num
    numbernum    + +       *       $       E
10   Shift/2S2      ---     ---     ---     1
1   ---     S3      S4      Accept  ---
2   ---     R3  Shift/3  Shift/4  R3      R3      ---
3   Shift/5S2      ---     ---     ---     5
4   Shift/6S2      ---     ---     ---     6
5   ---     R1/S3  S R1/RS4 Err  S/RR1 Err     ---
6   ---     R2/S3  S R2/RS4 Err  S/RR2 Err     ---

(Well, not exactly. It is missing End-Of-String, and goto states. But you get the point.)

With precedence, you are saying which of "shift" and "reduce" you want in the case of conflicts.

    numbernum    '+' +     '*'  *       $       E
10   Shift/2S2      ---     ---     ---     1
1   ---     S3      S4      Accept  ---
2   ---     R3   Shift/3  Shift/4 R3      R3      ---
3   Shift/5S2      ---     ---     ---     5
4   Shift/6S2      ---     ---     ---     6
5   ---     R1   Reduce/2 Shift/4  S4      R1      ---
6   ---     R2  Reduce/2 Reduce/2   R2      R2      ---
E = E + E | E * E | number
    number    +        *
1   Shift/2   ---      ---
2   ---       Shift/3  Shift/4
3   Shift/5   ---      ---
4   Shift/6   ---      ---
5   ---       S/R Err  S/R Err
6   ---       S/R Err  S/R Err

(Well, not exactly. It is missing End-Of-String, and goto states. But you get the point.)

With precedence, you are saying which of "shift" and "reduce" you want in the case of conflicts.

    number    '+'      '*'
1   Shift/2   ---      ---       
2   ---       Shift/3  Shift/4
3   Shift/5   ---      ---
4   Shift/6   ---      ---
5   ---       Reduce/2 Shift/4
6   ---       Reduce/2 Reduce/2
[1] E -> E + E
[2] E -> E * E
[3] E -> num
    num     +       *       $       E
0   S2      ---     ---     ---     1
1   ---     S3      S4      Accept  ---
2   ---     R3      R3      R3      ---
3   S2      ---     ---     ---     5
4   S2      ---     ---     ---     6
5   ---     R1/S3   R1/S4   R1      ---
6   ---     R2/S3   R2/S4   R2      ---

With precedence, you are saying which of "shift" and "reduce" you want in the case of conflicts.

    num     +       *       $       E
0   S2      ---     ---     ---     1
1   ---     S3      S4      Accept  ---
2   ---     R3      R3      R3      ---
3   S2      ---     ---     ---     5
4   S2      ---     ---     ---     6
5   ---     R1      S4      R1      ---
6   ---     R2      R2      R2      ---
edited body
Source Link

Consider the following rules:

E = E + E | E * E | number

Without precedence, it would produce a shift/reduce error when encountering the second operator. The table looks something like this:

    number    +        *
1   Shift/2   ---      ---
2   ---       Shift/3  Shift/4
3   Shift/5   ---      ---
4   Shift/6   ---      ---
5   ---       S/R Err  S/R Err
6   ---       S/R Err  S/R Err

(Well, not exactly. It is missing End-Of-String, and goto states. But you get the point.)

With precedence, you are saying which of "shift" and "reduce" you want in the case of conflicts.

  1. If the first operator has a lower precedence, you shift.
  2. If the first operator has a higher precedence, you reduce.
  3. If the operators have equal precedences, and both are
    • left associative, you reduce.
    • right associative, you shift.
    • otherwise, you fail.

Cleaning up the errors in the table, you get something like this:

    number    '+'      '*'
1   Shift/2   ---      ---       
2   ---       Shift/3  Shift/4
3   Shift/5   ---      ---
4   Shift/6   ---      ---
5   ---       Reduce/12 Shift/4
6   ---       Reduce/12 Reduce/12

Consider the following rules:

E = E + E | E * E | number

Without precedence, it would produce a shift/reduce error when encountering the second operator. The table looks something like this:

    number    +        *
1   Shift/2   ---      ---
2   ---       Shift/3  Shift/4
3   Shift/5   ---      ---
4   Shift/6   ---      ---
5   ---       S/R Err  S/R Err
6   ---       S/R Err  S/R Err

(Well, not exactly. It is missing End-Of-String, and goto states. But you get the point.)

With precedence, you are saying which of "shift" and "reduce" you want in the case of conflicts.

  1. If the first operator has a lower precedence, you shift.
  2. If the first operator has a higher precedence, you reduce.
  3. If the operators have equal precedences, and both are
    • left associative, you reduce.
    • right associative, you shift.
    • otherwise, you fail.

Cleaning up the errors in the table, you get something like this:

    number    '+'      '*'
1   Shift/2   ---      ---       
2   ---       Shift/3  Shift/4
3   Shift/5   ---      ---
4   Shift/6   ---      ---
5   ---       Reduce/1 Shift/4
6   ---       Reduce/1 Reduce/1

Consider the following rules:

E = E + E | E * E | number

Without precedence, it would produce a shift/reduce error when encountering the second operator. The table looks something like this:

    number    +        *
1   Shift/2   ---      ---
2   ---       Shift/3  Shift/4
3   Shift/5   ---      ---
4   Shift/6   ---      ---
5   ---       S/R Err  S/R Err
6   ---       S/R Err  S/R Err

(Well, not exactly. It is missing End-Of-String, and goto states. But you get the point.)

With precedence, you are saying which of "shift" and "reduce" you want in the case of conflicts.

  1. If the first operator has a lower precedence, you shift.
  2. If the first operator has a higher precedence, you reduce.
  3. If the operators have equal precedences, and both are
    • left associative, you reduce.
    • right associative, you shift.
    • otherwise, you fail.

Cleaning up the errors in the table, you get something like this:

    number    '+'      '*'
1   Shift/2   ---      ---       
2   ---       Shift/3  Shift/4
3   Shift/5   ---      ---
4   Shift/6   ---      ---
5   ---       Reduce/2 Shift/4
6   ---       Reduce/2 Reduce/2
Source Link

Consider the following rules:

E = E + E | E * E | number

Without precedence, it would produce a shift/reduce error when encountering the second operator. The table looks something like this:

    number    +        *
1   Shift/2   ---      ---
2   ---       Shift/3  Shift/4
3   Shift/5   ---      ---
4   Shift/6   ---      ---
5   ---       S/R Err  S/R Err
6   ---       S/R Err  S/R Err

(Well, not exactly. It is missing End-Of-String, and goto states. But you get the point.)

With precedence, you are saying which of "shift" and "reduce" you want in the case of conflicts.

  1. If the first operator has a lower precedence, you shift.
  2. If the first operator has a higher precedence, you reduce.
  3. If the operators have equal precedences, and both are
    • left associative, you reduce.
    • right associative, you shift.
    • otherwise, you fail.

Cleaning up the errors in the table, you get something like this:

    number    '+'      '*'
1   Shift/2   ---      ---       
2   ---       Shift/3  Shift/4
3   Shift/5   ---      ---
4   Shift/6   ---      ---
5   ---       Reduce/1 Shift/4
6   ---       Reduce/1 Reduce/1