0% found this document useful (0 votes)
18 views

CC Assignment2 PDF

This document contains student M. Bilal Khalid's answers to questions on an assignment for their Compiler Construction course. The questions cover topics like converting an epsilon NFA to a DFA, regular definitions for numeric strings, regular expressions for whitespace patterns and ascending number sequences, and input buffering. Examples are provided for converting epsilon NFAs to DFAs for various regular expressions.

Uploaded by

rimsha4ch
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)
18 views

CC Assignment2 PDF

This document contains student M. Bilal Khalid's answers to questions on an assignment for their Compiler Construction course. The questions cover topics like converting an epsilon NFA to a DFA, regular definitions for numeric strings, regular expressions for whitespace patterns and ascending number sequences, and input buffering. Examples are provided for converting epsilon NFAs to DFAs for various regular expressions.

Uploaded by

rimsha4ch
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/ 6

Name: M.

Bilal Khalid Roll Number: 160640


Semester: VII-A Subject: Compiler Construction
Assignment #: 2 (Late Submission due to Sickness.)

Q1: Learn how to convert ɛ-NFA to DFA. Submit process of conversion


with 3 examples in written form.
Solution:
The ɛ-NFA can be converted into DFA : -

 Search for the ɛ-Closures in all the states of NFA.


 Draw Transition table which will include ɛ-Closures of the corresponding states.
ɛ-Closures: In ɛ-closure we use epsilon that is used to go to the next state without any input. ɛ-closure
is use to find all the states that can be reached from the current state.
Example 1:
We have the following ɛ-NFA for RE= (a*b*a*).

0 1 2
ɛ ɛ

a b a

Transition Table:

a
States (ɛ-Closures) State (ɛ-Closures) after State (ɛ-Closures) after
reading 0 reading 1
0,1,2= A (initial state+ Final 0,1,2 = A 1,2 = B
State)
1,2 = B (final State) 2=C 1,2 = B
2 = C (final state) 2=C Ø=D
Ø=D Ø=D Ø=D

This study source was downloaded by 100000874451805 from CourseHero.com on 03-02-2024 08:00:39 GMT -06:00

https://www.coursehero.com/file/53277439/CC-Assignment2pdf/
A b B
b

D C
b
a, b

Example 2: a
We have the following ɛ-NFA for RE= (ab)*.
ɛ

1 ɛ 2 a ɛ b ɛ 6
3 4 5

ɛ
Transition Table:
States (ɛ-Closures) State (ɛ-Closures) after State (ɛ-Closures) after
reading 0 reading 1
1,2,6 = A (initial state) Ø=B 3,4 = C
Ø=B Ø=B Ø=B
3,4 = C 2,5,6 = D Ø=B
2,5,6 = D (final state) Ø=B 3,4 = C

We keep on building the Transition table until we get no new states. As in the table above, we have
considered all states (A, C, D) for input on the left side. Now we can make DFA using this Transition
table.

This study source was downloaded by 100000874451805 from CourseHero.com on 03-02-2024 08:00:39 GMT -06:00

https://www.coursehero.com/file/53277439/CC-Assignment2pdf/
A 0 B
0,1

1 1 0

C D
0

Example 3: 1

We have the following ɛ-NFA for RE= (a|b)*abb.

2 a 3

ɛ ɛ
0 ɛ ɛ a b
1 6 7 8 9
ɛ ɛ b

4 b 5
10

Transition Table:

1
States (ɛ-Closures) State (ɛ-Closures) after State (ɛ-Closures) after
reading 0 reading 1
0,1,2,4,7 = A (initial state) 3,6,7,1,2,4,8 = B 5,6,7,1,2,4 = C
3,6,7,1,2,4,8 = B 3,6,7,1,2,4,8 = B 5,6,7,1,2,4,9 = D
5,6,7,1,2,4 = C 3,6,7,1,2,4,8 = B 5,6,7,1,2,4 = C
5,6,7,1,2,4,9 = D 3,6,7,1,2,4,8 = B 5,6,7,1,2,4,10 = E
5,6,7,1,2,4,10 = E (final state) 3,6,7,1,2,4,8 = B 5,6,7,1,2,4 = C

This study source was downloaded by 100000874451805 from CourseHero.com on 03-02-2024 08:00:39 GMT -06:00

https://www.coursehero.com/file/53277439/CC-Assignment2pdf/
a

A a B

b a b a

a
C D
b

b b
E

Q2: Write down the specification of Regular Definition for: “Unsigned


numbers in C are strings such as 6250, 36.25, 6.235E4 or 1.25E-3. Use the
shorthand notation.
Answer:
([\d+] (([.][\d+])? ([E] [+|-]? [\d+])?)?)

Q3: Specify a pattern for white spaces using regular expressions and make
transition diagram to recognize it.
Solution:
RE= \s +\ t + \n + \r

0 \ 1 s 2 Single space

t 3 tab space

n 4 New line

r 5 Carriage Return

other 6 Escape Character

Q4: Specify a pattern for all Strings in which {1,2,3} exist in ascending
order and make transition diagram to recognize it.

This study source was downloaded by 100000874451805 from CourseHero.com on 03-02-2024 08:00:39 GMT -06:00

https://www.coursehero.com/file/53277439/CC-Assignment2pdf/
Solution:
RE= 1+ 2+ 3+

1 2 3

1 2 3

Q5: Write a 4-liner summery of Input Buffering.


Answer:
If we talk about computer memory, the input buffer is a location that contain and holds all incoming
information before it further continues to the CPU for processing.
The lexical analyzer scans the input from left to right one character at a time. It uses two pointers
begin ptr(bp) and forward ptr(fp) to keep track of the pointer of the input scanned.
Initially both the pointers are at same point on the first character of the input string. The forward ptr
moves ahead to search for end of lexeme. When the blank space is encountered, it refers to the end of
lexeme. The fp will be moved ahead at white space, when fp encounters white space, it ignores and
moves ahead. then both the begin ptr(bp) and forward ptr(fp) are set at next token.
The input character is thus read from secondary storage but reading in this way from secondary
storage is costly. hence buffering technique is used. A block of data is first read into a buffer, and then
second by lexical analyzer.

Q6: Construct a nondeterministic finite automaton (NFA) for the regular


expression ((a+b)*c)|d using Thompson's Construction Algorithm.
Solution:
ɛ

a
ɛ ɛ
c
ɛ
ɛ ɛ
ɛ b

d
ɛ

Q7: Convert the following NFA with -transitions into a DFA using the
subset construction.

This study source was downloaded by 100000874451805 from CourseHero.com on 03-02-2024 08:00:39 GMT -06:00

https://www.coursehero.com/file/53277439/CC-Assignment2pdf/
Solution:
Transition table for NFA:
Current State State after reading State after reading
0 1
q0 q0 q0,q1
q1 - q2
q3 q1 q1
q4 q3 q0,q3

But for DFA, for 1 input from current state transition happens for one state only. Here, in above NFA
for input 1 q0 is going to q0 and q1. So, we need to solve this issue.
Transition table for DFA:
Current State State after reading State after reading
0 1
q0 = S1 q0 = S1 {q0,q1} = S2 (one combined
state)
{q0,q1} = S2 q0 = S1 {q0,q1,q2} = S3
{q0,q1,q2} = S3 {q0,q1} = S2 {q0,q1,q2} = S3
Corresponding DFA:
0
S1 1 S2

0
0 1

S3

This study source was downloaded by 100000874451805 from CourseHero.com on 03-02-2024 08:00:39 GMT -06:00

https://www.coursehero.com/file/53277439/CC-Assignment2pdf/
Powered by TCPDF (www.tcpdf.org)

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