Atcd Modules 4 and 5
Atcd Modules 4 and 5
Automata Theory
and
Compiler Design
Shridhar Venkatanarasimhan
1. Pushdown Automata . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.5.1 {0n 1n } . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.5.2 Example of W W R . . . . . . . . . . . . . . . . . . . . 9
2. Bottom-Up Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.1 Definition of a TM . . . . . . . . . . . . . . . . . . . . . . . . 24
3.6 Undecidability . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.6.6 Reductions . . . . . . . . . . . . . . . . . . . . . . . . 28
2.2 LR(0) Automaton with Sets of Items (States) and the GOTO
Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.6 LR(1) Automaton with Sets of Items (States) and the GOTO
Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
examples.
form of the parse tree is constructed from the bottom to the top. Shift-
terminal from the remainder of the input onto the stack or reduces a
structed from a grammar, which make generate and make use of pars-
ing tables which help int making the shift or reduce decisions.
on the top of the stack X, the PDA changes to a possibly different state
p ∈ Q, replaces X by a string γ ∈ Γ∗ .
state and on stack. q0 is the initial state and Z0 is the only initial symbol
on the stack.
The ID for a PDA is given by the current state q, the remainder of the
input w and the current string of stack symbols γ. That is, (q, w, γ) is
A move of a PDA can be given by: (q, aw, Xα) ⊢ (p, w, γα) where state
which P empties it stack after consuming the string. L(P ) is the set of
1.5.1 {0n 1n }
We know that the language {0n 1n }|n ≥ 0} is not regular. We have a PDA
Moves:
Basically, we push 0’s on the stack as they occur and pop a 0 for
1.5.2 Example of W W R
Consider the language over {0, 1} which consists of all even lenght
palindromes.
Moves:
ing input. q0 can also pop off the Z0 without consuming input. Here,
Γ = {Z0 , 0, 1}.
2. BOTTOM-UP PARSING
each step, the current input symbol is either shifted on to the stack, or
to A.
An example of shift-reduce parsing is given for the string id ∗ id$ for the
1. E → E + T
2. E → T
3. T → T ∗ F
4. T → F
5. F → (E)
6. F → id
2. Bottom-Up Parsing 11
Note that this parser makes a left-to-right scan of the input and
process of seeing this production, the parser has recognized α thus far.
2. Bottom-Up Parsing 12
Let us begin with a set of items which represent a state of what the
• E′ → E
• E →E+T
• E→T
• T →T ∗F
• T →F
• F → (E)
• F → id
• E ′ → .E
• E → .E + T
• E → .T
• T → .T ∗ F
• T → .F
• F → .(E)
• F → .id
2. Bottom-Up Parsing 13
Refer to figure 2.2 for the LR(0) automaton which has these states and
An LR(0) Parsing Table has a row for every state. It has a column for
Refer to figure 2.3 for the LR(0) parsing table for the expression
grammar.
stand for shift. If GOT O(Ii , A) = Ij dfor a non-terminal A then the entry
A → α., then for all in FOLLOW(A), the entry for that row is rk where k
Refer to figure 2.4 for the parsing algorithm which makes use of the
Refer to figure 2.5 for the example of parsing the input id ∗ id.
2. Bottom-Up Parsing 14
Fig. 2.2: LR(0) Automaton with Sets of Items (States) and the GOTO Function
2. Bottom-Up Parsing 15
reductions possible.
conflict?
LR(1) parser is strictly more powerful than LR(0) parser, that is, if
have grammars with LR(1) parsing tables without conflicts, for which
lookahead symbol.
Add B → .γ, F IRST (βa) to I. Keep doing these, till no more items can
be added to I.
1. S → CC
2. C → cC
3. C → d
• S′ → S
• S → CC
• C → cC
• C→d
• S ′ → .S, $
• S → .CC, $
• C → .cC, c|d
2. Bottom-Up Parsing 19
• C → .d, c|d
Note how the lookaheads are c and d in the last two items.
αX.β, a. Thus from a set of items I we GOTO a set J(J = CLOSU RE(J))
on X.
Refer to figure 2.6 for the LR(1) automaton which has these states and
An LR(1) Parsing Table has a row for every state. It has a column for
Refer to figure 2.7 for the LR(1) parsing table for the sample gram-
mar.
item A → α., a|b|c..., then for the lookaheads a, b, c, ..., the entry for that
The parsing algorithm for LR(1) is just like that of the LR(0), except that
it makes use of the LR(1) parsing table instead. We have covered the
2. Bottom-Up Parsing 20
Fig. 2.6: LR(1) Automaton with Sets of Items (States) and the GOTO Function
2. Bottom-Up Parsing 21
between LR(0) and LR(1). For any grammar, the LR(0)and LALR parser
always have the same number of states which is much less than an
are the same except that the lookaheads are different. In LALR, we
merge these states to form a single state I47 which has the items with a
union of lookaheads {c, d, $}. Similarly, I36 and I89 are formed as given
in figure 2.8.
The parsing algorithm is just like that for LR(0) and LR(1).
3. TURING MACHINE AND UNDECIDABILITY
The Turing Machine (TM) is the most general and powerful automata
3.1 Definition of a TM
of which can hold a symbol from a tape alphabet Γ. The input is a string
sequence of blanks. The blank (B) is a special tape symbol not found
in Σ. The TM has a finite set of states Q. The initial state is q0 and the
tape has a head scanning the first symbol of the input. The TM makes
Figure 3.2 shows a TM which changes 0’s to 1’s and 1’s to 0’s in the
input.
3. Turing Machine and Undecidability 25
2. Multiple Tracks
3. Subroutines
its own head scanning input on that tape and making a next move.
for an input, the time taken for the corresponding single tape TM is
the algorithm.
3. Turing Machine and Undecidability 27
3.6 Undecidability
halts on all its inputs and accepts all strings in the language. A re-
Every TM can be coded in an alphabet such as {0, 1}. Let M stand for
enumerable.
coding of) the ordered pair < M, w > and simulates M on input w. The
3. Turing Machine and Undecidability 28
found in [1].
3.6.4 above pass < M, w > as input to U and it will eventually halt and
enumerable.
3.6.6 Reductions
not have any algorithm. For example, [1] describes the reduction of Lu
Pearson.
[2] Alfred V. Aho, Monica S. Lam, Ravi Sethi, and Jeffrey D. Ullman;
son.