Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Lab Assignment-I

Assignment 1

Uploaded by

aaggarwal4be21
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab Assignment-I

Assignment 1

Uploaded by

aaggarwal4be21
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab Assignment

Compiler Construction (UCS802)


Regular Expression: Regular expressions are widely used to specify patterns. We use regular
expressions to describe tokens of a programming language. A regular expression is built up of
simpler regular expressions (using defining rules). A regular expression can be created with a set
of Alphabets defined for a language and set of operations for defining the strings in the language.
(𝑎|𝑏)∗ 𝑎𝑏𝑏 is a regular expression representing a language of all the strings ending with string
𝑎𝑏𝑏 with alphabet set {𝑎, 𝑏}, and set of operations defined as {∗, |, 𝑐𝑜𝑛𝑐𝑎𝑡𝑒𝑛𝑎𝑡𝑖𝑜𝑛}, with
precedence set in the same order as defined above. The parenthesis can be used to simplify the
regular expressions.
A recognizer for a language is a program that takes a string x, and answers “yes” if x is a sentence
of that language, and “no” otherwise. We call the recognizer of the tokens as a finite automaton.
We may use a deterministic or non-deterministic automaton as a lexical analyzer.
Both deterministic and non-deterministic finite automaton recognize regular sets. Deterministic
automatons are widely used lexical analyzers.
First, we define regular expressions for tokens; then we convert them into a DFA to get a lexical
analyzer for our tokens.
Programming Assignment 1:
Regular Expression è NFA è DFA, two steps: first to NFA(5 marks), then to DFA(5 marks)
Non-Deterministic Finite Automata (NFA)
A non-deterministic finite automaton (NFA) is a mathematical model that consists of:
• S - a set of states
• S - a set of input symbols (alphabet)
• move – a transition function move to map state-symbol pairs to sets of states.
• s0 - a start (initial) state
• F – a set of accepting states (final states)
Implementation of NFA
Define a Node structure for defining one state of NFA, we should be able to move from this Node
to another Node for any input symbol defined in S, and ε.
Implementation of Regular Expression
A Simple regular expression can be created using two Nodes. For example,
A regular expression for ε and a
a ε
Operations on Regular Expressions
Each regular expression has one start node and one end node. Let the two regular Expressions
be represented by 𝑟2 and 𝑟3 .
𝑟2 𝑟3
a. Concatenation of two regular Expressions: (𝑟2 𝑟3): The new regular expression will be
represented by:

𝑟2 𝑟3

b. 𝑟2 | 𝑟3
𝑟2
ε
ε
ε
ε 𝑟3

c. Kleene Clousure 𝑟2∗


ε
ε ε
𝑟2

ε
Programming Assignment –II
Given a NFA for a regular expression created using Thomson’s construction rules defined in
programming assignment-I create a Deterministic automata DFA. The DFA will match all the
words of grammar generated by the regular expression.

Deterministic Finite Automata (DFA)


A Deterministic Finite Automaton (DFA) is a special form of a NFA. No state in DFA has e-
transition. Ffor each symbol 𝑎 and state 𝑠, there is at most one labeled edge 𝑎 leaving 𝑠 i.e. the
transition function is from pair of state-symbol to state (not set of states).

DFA for regular Expression (𝒂|𝒃)∗ 𝒂𝒃

a
b a
a b
0 1 2

Implementation of DFA
𝑠 ç 𝑠7 \\ { 𝑠𝑡𝑎𝑟𝑡 𝑓𝑟𝑜𝑚 𝑡ℎ𝑒 𝑖𝑛𝑖𝑡𝑖𝑎𝑙 𝑠𝑡𝑎𝑡𝑒 }
𝑐 ç 𝑛𝑒𝑥𝑡𝑐ℎ𝑎𝑟 \\{ 𝑔𝑒𝑡 𝑡ℎ𝑒 𝑛𝑒𝑥𝑡 𝑐ℎ𝑎𝑟𝑎𝑐𝑡𝑒𝑟 𝑓𝑟𝑜𝑚 𝑡ℎ𝑒 𝑖𝑛𝑝𝑢𝑡 𝑠𝑡𝑟𝑖𝑛𝑔 }
𝑤ℎ𝑖𝑙𝑒 (𝑐 ! = 𝑒𝑜𝑠)𝑑𝑜 \\{ 𝑑𝑜 𝑢𝑛𝑡𝑖𝑙 𝑡ℎ𝑒 𝑒𝑛𝑑 𝑜𝑓 𝑡ℎ𝑒 𝑠𝑡𝑟𝑖𝑛𝑔 }
𝑏𝑒𝑔𝑖𝑛
𝑠 ç 𝑚𝑜𝑣𝑒(𝑠, 𝑐) \\{ 𝑡𝑟𝑎𝑛𝑠𝑖𝑡𝑖𝑜𝑛 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 }
𝑐 ç 𝑛𝑒𝑥𝑡𝑐ℎ𝑎𝑟
𝑒𝑛𝑑
𝑖𝑓 (𝑠 𝑖𝑛 𝐹)𝑡ℎ𝑒𝑛 \\{ 𝑖𝑓 𝑠 𝑖𝑠 𝑎𝑛 𝑎𝑐𝑐𝑒𝑝𝑡𝑖𝑛𝑔 𝑠𝑡𝑎𝑡𝑒 }
𝑟𝑒𝑡𝑢𝑟𝑛 “𝑦𝑒𝑠”
𝑒𝑙𝑠𝑒
𝑟𝑒𝑡𝑢𝑟𝑛 “𝑛𝑜”
Conversion of NFA to DFA
To convert and Non-deterministic Finite automata (NFA) into a Deterministic Finite Automata
(DFA)
The ∈-closure of a state is the set of all states, including S itself, that you can get to via ∈-
transitions. The ∈-closure of state 𝑆 is denoted: L𝑆
𝑝𝑢𝑠ℎ 𝑎𝑙𝑙 𝑠𝑡𝑎𝑡𝑒𝑠 𝑜𝑓 𝑇 𝑜𝑛𝑡𝑜 𝑠𝑡𝑎𝑐𝑘
𝑖𝑛𝑖𝑡𝑖𝑎𝑙𝑖𝑧𝑒 𝜖 − 𝑐𝑙𝑜𝑠𝑢𝑟𝑒(𝑇) 𝑡𝑜 𝑇
𝒘𝒉𝒊𝒍𝒆 (𝑠𝑡𝑎𝑐𝑘 𝑖𝑠 𝑛𝑜𝑡 𝑒𝑚𝑝𝑡𝑦) 𝑑𝑜
𝑏𝑒𝑔𝑖𝑛
𝑝𝑜𝑝 𝑡, 𝑡ℎ𝑒 𝑡𝑜𝑝 𝑒𝑙𝑒𝑚𝑒𝑛𝑡, 𝑜𝑓𝑓 𝑠𝑡𝑎𝑐𝑘;
𝒇𝒐𝒓 (𝑒𝑎𝑐ℎ 𝑠𝑡𝑎𝑡𝑒 𝑢 𝑤𝑖𝑡ℎ 𝑎𝑛 𝑒𝑑𝑔𝑒 𝑓𝑟𝑜𝑚 𝑡 𝑡𝑜 𝑢 𝑙𝑎𝑏𝑒𝑙𝑙𝑒𝑑 𝜖 𝑑𝑜
𝑏𝑒𝑔𝑖𝑛
𝒊𝒇 (𝑢 𝑖𝑠 𝑛𝑜𝑡 𝑖𝑛 𝜖 − 𝑐𝑙𝑜𝑠𝑢𝑟𝑒(𝑇)) 𝑑𝑜
𝑏𝑒𝑔𝑖𝑛
𝑎𝑑𝑑 𝑢 𝑡𝑜 𝜖 − 𝑐𝑙𝑜𝑠𝑢𝑟𝑒(𝑇)
𝑝𝑢𝑠ℎ 𝑢 𝑜𝑛𝑡𝑜 𝑠𝑡𝑎𝑐𝑘
𝑒𝑛𝑑
𝑒𝑛𝑑
𝑒𝑛𝑑
Example:

The ∈ −closure of state 1: 1\ = { 1, 2, 4 }


The ∈ −closure of state 3: 3L = { 3, 2, 4 }

The ∈ −closure of a set of states 𝑆2 , 𝑆3 , … , 𝑆_ is 𝑆L2 ⋃𝑆L3 … ⋃ \\\


𝑆_

Example: The e-closure for above states 1 and 3 is


{ 1, 2, 4 } ∪ { 3, 2, 4 } = { 1, 2, 3, 4 }

To construct a DFA from NFA the following procedure is followed:

𝑝𝑢𝑡 e − 𝑐𝑙𝑜𝑠𝑢𝑟𝑒({𝑠0}) 𝑎𝑠 𝑎𝑛 𝑢𝑛𝑚𝑎𝑟𝑘𝑒𝑑 𝑠𝑡𝑎𝑡𝑒 𝑖𝑛𝑡𝑜 𝑡ℎ𝑒 𝑠𝑒𝑡 𝑜𝑓 𝐷𝐹𝐴 (𝐷𝑆)


𝑤ℎ𝑖𝑙𝑒 (𝑡ℎ𝑒𝑟𝑒 𝑖𝑠 𝑜𝑛𝑒 𝑢𝑛𝑚𝑎𝑟𝑘𝑒𝑑 𝑆1 𝑖𝑛 𝐷𝑆) 𝑑𝑜
𝑏𝑒𝑔𝑖𝑛
𝑚𝑎𝑟𝑘 𝑆1
𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑖𝑛𝑝𝑢𝑡 𝑠𝑦𝑚𝑏𝑜𝑙 𝑎 𝑑𝑜
𝑏𝑒𝑔𝑖𝑛
𝑆2 ç e − 𝑐𝑙𝑜𝑠𝑢𝑟𝑒(𝑚𝑜𝑣𝑒(𝑆1, 𝑎))
𝑖𝑓 (𝑆2 𝑖𝑠 𝑛𝑜𝑡 𝑖𝑛 𝐷𝑆) 𝑡ℎ𝑒𝑛
𝑎𝑑𝑑 𝑆2 𝑖𝑛𝑡𝑜 𝐷𝑆 𝑎𝑠 𝑎𝑛 𝑢𝑛𝑚𝑎𝑟𝑘𝑒𝑑 𝑠𝑡𝑎𝑡𝑒
𝑡𝑟𝑎𝑛𝑠𝑓𝑢𝑛𝑐[𝑆1, 𝑎] ç 𝑆2
𝑒𝑛𝑑
𝑒𝑛𝑑
Example 1: To convert the following nfa:
we get:

This constructs a dfa that has no epsilon-transitions and a single accepting state.

Example 2: To convert the nfa for an identifier to a dfa


we get:

You might also like