LR(1) derivation example

Grammar

E -> T
E -> E + T
T -> i
T -> ( E )
T -> i [ E ]

Input

i [ i ]

STEP 1

Stack  :

Input  : i [ i ] <eof>

Action : shift

STEP 2

Stack  :
i

Input  : [ i ] <eof>

Action : goto

STEP 3

Stack  :
i

Input  : [ i ] <eof>

Action : shift

STEP 4

Stack  :
i
[

Input  : i ] <eof>

Action : goto

STEP 5

Stack  :
i
[

Input  : i ] <eof>

Action : shift

STEP 6

Stack  :
i
[
i

Input  : ] <eof>

Action : goto

STEP 7

Stack  :
i
[
i

Input  : ] <eof>

Action : reduce T -> i . {+, ]}

STEP 8

Stack  :
i
[
T
└── i

Input  : ] <eof>

Action : goto

STEP 9

Stack  :
i
[
T
└── i

Input  : ] <eof>

Action : reduce E -> T . {+, ]}

STEP 10

Stack  :
i
[
E
└── T
    └── i

Input  : ] <eof>

Action : goto

STEP 11

Stack  :
i
[
E
└── T
    └── i

Input  : ] <eof>

Action : shift

STEP 12

Stack  :
i
[
E
└── T
    └── i
]

Input  : <eof>

Action : goto

STEP 13

Stack  :
i
[
E
└── T
    └── i
]

Input  : <eof>

Action : reduce T -> i [ E ] . {<eof>, +}

STEP 14

Stack  :
T
├── i
├── [
├── E
│   └── T
│       └── i
└── ]

Input  : <eof>

Action : goto

STEP 15

Stack  :
T
├── i
├── [
├── E
│   └── T
│       └── i
└── ]

Input  : <eof>

Action : reduce E -> T . {<eof>, +}

STEP 16

Stack  :
E
└── T
    ├── i
    ├── [
    ├── E
    │   └── T
    │       └── i
    └── ]

Input  : <eof>

Action : accept

E
└── T
    ├── i
    ├── [
    ├── E
    │   └── T
    │       └── i
    └── ]

First, Follow, Nullable sets

first    (E) = {(, i}
follow   (E) = {), +, ]}
nullable (E) = false

first    (T) = {(, i}
follow   (T) = {), +, ]}
nullable (T) = false

first    (() = {(}
follow   (() = {(, i}
nullable (() = false

first    ()) = {)}
follow   ()) = {), +, ]}
nullable ()) = false

first    (+) = {+}
follow   (+) = {(, i}
nullable (+) = false

first    ([) = {[}
follow   ([) = {(, i}
nullable ([) = false

first    (]) = {]}
follow   (]) = {), +, ]}
nullable (]) = false

first    (i) = {i}
follow   (i) = {), +, [, ]}
nullable (i) = false

Automaton


The example on this page is auto-generated with dOvs teaching LR parser generator tool. The tool and the webpage is maintained by Aslan Askarov.