0% found this document useful (0 votes)
117 views63 pages

Phases of Compiler PDF

This document discusses the phases of a compiler including language definition, regular expressions, finite automata, and conversion between different representations. It begins by defining languages and operations on languages like concatenation. It then covers regular expressions, deterministic finite automata (DFAs), nondeterministic finite automata (NFAs), and how to convert between regular expressions, NFAs and DFAs. It provides examples of constructing automata to recognize various languages over different alphabets.

Uploaded by

avantika gaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views63 pages

Phases of Compiler PDF

This document discusses the phases of a compiler including language definition, regular expressions, finite automata, and conversion between different representations. It begins by defining languages and operations on languages like concatenation. It then covers regular expressions, deterministic finite automata (DFAs), nondeterministic finite automata (NFAs), and how to convert between regular expressions, NFAs and DFAs. It provides examples of constructing automata to recognize various languages over different alphabets.

Uploaded by

avantika gaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

Phases of Compiler

Phases of Compiler
Language definition.
• Ʃ= alphabet= set of symbols
• String= sequence of symbols
• Empty string=epsilon= e, ε, λ or even Λ
• Ʃ*= all possible string over alphabet Ʃ
• L= language over Ʃ={strings over Ʃ* satisfying certain property }
• ={x Ɛ Ʃ* : x satisfies certain properties}
• For example if Ʃ={0,1}
• L1={0,1}
• L2={00,01,10,11} etc
• Since Language is a set we may apply set operations on languages
• Concatenation of Languages
• L1oL2=L1L2={xy such that x is in L1 and y is in L2}
• L1={0,01,110}
• L2={10,110}
• L1L2={010,0110,0110,01110, 11010, 110110}
• ={010,0110,01110, 11010, 110110}
Kleen star and Kleen Plus

• 𝐿0 = {ε}
• 𝐿1 =L
• 𝐿2 = LL
• 𝐿3 =LLL

• 𝐿∗ = concatenation of L with itself any no. of times


• =𝐿0 U 𝐿1 U 𝐿2 U 𝐿3 U ………………….. U 𝐿∞
• = closure/any no. of of L
• = kleen star operation on L
• = zero or more instances
• 𝐿+ = concatenation of L with itself one or more no. of times
• =𝐿1 U 𝐿2 U 𝐿3 U ………………….. U 𝐿∞
• = positive closure/one or more no. of of L
• = kleen Plus operation on L= one or more instances
• If L={a}
• 𝐿∗ ={ε, a, aa, aaa, aaaa,…….}
• 𝐿+ ={a,aa,aaa,aaaa,…….}
• Similarly other operations like Union, Intersection, Difference and
Complement may be performed.
• L1
Concepts/techniques for Lexical Analysis
Deterministic Finite Automaton(DFA)
• DFA=<Q, Ʃ , δ , s, F>
• Where
• Q= set of states
• Ʃ=alphabet
• A transition function δ: Q × Σ → Q mapping the current state q ∈ Q and
input symbol a ∈ Σ to a new state δ(q, a) ∈ Q
• s ∈ Q is initial state
• F= set of final state
• DFA may be represented by
• (i)A five tupple system as above
Representation of DFA
DFA may be represented by
(i)A five tuple system as above =<Q, Ʃ , δ , s, F>
(ii) state transition diagram
(iii)state transition diagram
DFA represented as five tuple system
DFA M=<Q, Ʃ , δ , s, f> Where
Q={q0,q1,q2}
Ʃ={0,1}
s=starting state=q0
f = {q1}
δ:
{
δ (q0,0)= q2,
δ (q0,1)= q0,
δ (q1,0)= q1,
δ (q1,1)= q1
δ (q2,0)= q2,
δ (q2,1)= q1
}
DFA represented as State transition diagram
DFA to recognize/accept all string over {0,1}
containing 01 as substring
DFA represented as State transition Table

Q\Ʃ 0 1
q0 q2 q0
*q1 q1 q1
q2 q2 q1

Q\Ʃ 0 1
q0 q2 q0
q1 q1
q2 q2 q1
Acceptance of a string by DFA
• A string is said to be accepted If after reading the string DFA reaches
final state starting from initial state using its transition function for a
given state and read input.
• Language of DFA= a set of all strings acceptable by it
• DFA can be constructed for a given language
• Language of DFA can be found if DFA is given.
• DFA is said to accept a language L if all its strings are accepted by DFA
δ ( q0, 100101001)
=δ ( q2, 00101001)
=δ ( q2, 0101001)
=δ ( q2, 101001)
=δ ( q1, 01001)
=δ ( q1, 1001)
=δ ( q1, 001)
=δ ( q1, 01)
=δ ( q1, 1)
q1which belongs to final state
Hence string get accepted
Construct a DFA that accept all string over {0,
1} containing even no. of 0 and even n0. of 1
State transition diagram of DFA
• Construct a DFA that accept all string over {a, b} that contain even no. of a and
odd n0. of b
• Construct a DFA that accept all string over {a, b} that does not contain even no.
of a and odd n0. of b
Design a DFA that accept
all strings over {0,1,2,3} whose
Sum of digits
is divisible by 4
Design a DFA that accept all strings over {a,b}that contain exactly 2 a’s
and odd no. of b’s Design
q0: No a , even no of b
q1: No a , odd no of b
q2: one a , even no of b
q3: One a , odd no of b
q4: two a , even no of b
q5: two a , odd no of b
q6: More than two a’s
DFA
• Design a DFA that accepts all string over {0,1} that is divisible by 3
Design a DFA that accepts strings over {a,b} that contain substring aaa
q0: No progress towards aaa
q1: last processed symbol was a
q2: last two processed symbol were aa
q3: aaa has been found in the string
DFA
• Construct a DFA that accept all strings over {a, b} that contain substring abba
• Design a DFA that accepts all strings over {a,b} ending with abb
• Design a DFA that accepts the language L= {𝑎𝑖 𝑏𝑖 : i ≤ 𝑛}, for a fixed integer n
Simulating DFA
Non deterministic Finite Automaton (NFA)
definition
• NFA=<Q, Ʃ , δ , s, F> Where
Q= set of states
Ʃ=alphabet
A transition function δ: Q × ΣU{ } →  2Q mapping the current
state q ∈ Q and input symbol a ∈ Σ to a new state δ(q, a) ∈ 2Q
s ∈ Q is initial state
F= set of final state
• A deterministic finite automaton (DFA) is a special case of an NFA where:
There are no moves on input , and For each state s and input symbol a, there
is exactly one edge out of s labelled a
Non deterministic Finite Automaton (NFA)
Example
• NFA=<Q, Ʃ , δ , s, F> Where
Q= set of states={q0,q1,q2}
Ʃ=alphabet={0,1}
A transition function δ: 
δ(q0,0)= {q0,q1}, δ(q0,1)= {q0}, δ(q1,0)=Ø
δ(q1,1)={q2}, δ(q2,0)= Ø, δ(q2,1)= Ø
s = initial state=q0
F= set of final state={ q2}
• A deterministic finite automaton (DFA) is a special case of an NFA where: There are no moves on input e, and
• For each state s and input symbol a, there is exactly one edge out of s labelled a
State transition table & State transition
diagram
Q\Ʃ 0 1

→q0 {q0,q1} {q0}

q1 Ø {q2}

*q2 Ø Ø
Acceptance by NFA
• An NFA accepts input string x if and only if there is some path in the
transition graph of NFA from the start state to one of the accepting
states, such that the symbols along the path spell out x
• A string x is said to be accepted if NFA can find a path to reach any
one of the final state reading the input x from initial state.
• The language defined (or accepted) by an NFA is the set of strings
labeling some path from the start to an accepting state.
• What does this NFA does ?

• NFA accepting all strings over {0,1} ending with 01


• Design a NFA that accepts all strings over {a,b} ending with abb
Which is more powerful ? DFA or NFA
• NFA and DFA are equivalent
• For each nondeterministic finite automaton, there is an equivalent
deterministic finite automaton.
• Languages accepted by DFA are called regular languages
• DFA can be constructed for a regular languages
• Regular languages may also be represented by a notation called regular
expression
• DFA can be constructed for a given regular expression
• Convert regular expression into NFA
• Convert NFA into DFA
• Minimized DFA
Regular expression : a notation to represent
regular languages
Algebraic laws for regular expressions
Regular Definitions
NFA from regular expression
NFA for Union of two regular expression s and
t
NFA for Concatenation of two regular
expressions
NFA for kleen star S*
• Find NFA for (aIb)*abb or (a+b)*abb
NFA for a+b
NFA for (a+b}*
NFA for (a+b}*a
NFA for (a+b}*abb

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy