Month: July 2018

Miscellaneous Problems in Compiler Design

Three Address Code | DAGs | Basic Blocks & Flow Graphs-

 

Before you go through this article, make sure that you have gone through the previous articles on-

 

In this article, we will solve Miscellaneous Problems based on these Concepts.

 

PRACTICE PROBLEMS BASED ON ABOVE CONCEPTS-

 

Problem-01:

 

Generate three address code for the following code-

 

c = 0

do

{

if (a < b) then

x++;

else

x–;

c++;

} while (c < 5)

 

Solution-

 

Three address code for the given code is-

  1. c = 0
  2. if (a < b) goto (4)
  3. goto (7)
  4. T1 = x + 1
  5. x = T1
  6. goto (9)
  7. T2 = x – 1
  8. x = T2
  9. T3 = c + 1
  10. c = T3
  11. if (c < 5) goto (2)

 

Problem-02:

 

Generate three address code for the following code-

 

while (A < C and B > D) do

if A = 1 then C = C + 1

else

while A <= D

do A = A + B

 

Solution-

 

Three address code for the given code is-

  1. if (A < C) goto (3)
  2. goto (15)
  3. if (B > D) goto (5)
  4. goto (15)
  5. if (A = 1) goto (7)
  6. goto (10)
  7. T1 = c + 1
  8. c = T1
  9. goto (1)
  10. if (A <= D) goto (12)
  11. goto (1)
  12. T2 = A + B
  13. A = T2
  14. goto (10)

 

Problem-03:

 

Generate three address code for the following code-

 

switch (ch)

{

case 1 : c = a + b;

break;

case 2 : c = a – b;

break;

}

 

Solution-

 

Three address code for the given code is-

 

if ch = 1 goto L1

if ch = 2 goto L2

L1:

T1 = a + b

c = T1

goto Last

L2:

T1 = a – b

c = T2

goto Last

Last:

 

Problem-04:

 

Construct a DAG for the following three address code-

  1. a = b + c
  2. t1 = a x a
  3. b = t1 + a
  4. c = t1 x b
  5. t2 = c + b
  6. a = t2 + t2

 

Solution-

 

Directed acyclic graph for the given three address code is-

 

 

Problem-05:

 

Consider the following code-

 

prod = 0 ;

i = 1 ;

do

{

prod = prod + a[ i ] x b[ i ] ;

i = i + 1 ;

} while (i <= 10) ;

 

  1. Compute the three address code.
  2. Compute the basic blocks and draw the flow graph.

 

Solution-

 

Part-01:

 

Three address code for the given code is-

 

prod = 0

i = 1

T1 = 4 x i

T2 = a[T1]

T3 = 4 x i

T4 = b[T3]

T5 = T2 x T4

T6 = T5 + prod

prod = T6

T7 = i + 1

i = T7

if (i <= 10) goto (3)

 

Part-02:

 

Step-01:

 

We identify the leader statements as-

  • prod = 0 is a leader because first statement is a leader.
  • T1 = 4 x i is a leader because target of conditional or unconditional goto is a leader.

 

Step-02:

 

The above generated three address code can be partitioned into 2 basic blocks as-

 

 

Step-03:

 

The flow graph is-

 

 

To gain better understanding about these Miscellaneous Problems,

Watch this Video Lecture

 

Download Handwritten Notes Here-

 

Next Article- Code Optimization

 

Get more notes and other study material of Compiler Design.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Relation between Left Recursion & Left Factoring

Relationship Between Left Recursion, Left Factoring & Ambiguity-

 

There is no relationship between Left Recursion, Left Factoring and Ambiguity of Grammar.

 

  • All the three concepts are independent and has nothing to do with each other.
  • The presence or absence of left recursion does not impact left factoring and ambiguity anyhow.
  • The presence or absence of left factoring does not impact left recursion and ambiguity anyhow.
  • The presence or absence of ambiguity does not impact left recursion and left factoring anyhow.

 

The following examples support this fact-

 

Example-01: Ambiguous Grammar With Left Factoring-

 

Consider the following grammar-

S → aS / a / ∈

 

Clearly, this grammar has left factoring.

Now, let us draw parse trees for the string w = a-

 

 

Clearly,

  • Two different parse trees exist for the string w = a.
  • Therefore, the grammar is ambiguous.

 

Example-02: Unambiguous Grammar With Left Factoring-

 

Consider the following grammar-

S → aA / aB

A → a

B → b

 

Clearly, this grammar has left factoring.

The language generated by this grammar consists of only two strings L(G) = { aa , ab}.

Now, let us draw parse trees for these strings-

 

 

Clearly,

  • A unique parse tree exists for both the strings.
  • Therefore, the grammar is unambiguous.

 

Example-03: Ambiguous Grammar With Left Recursion-

 

Consider the following grammar-

S → SS / ∈

 

Clearly, this grammar has left recursion.

Now, let us draw parse trees for the string w = ∈-

 

 

Clearly,

  • Infinite parse trees exist for the string w = ∈.
  • Therefore, the grammar is ambiguous.

 

Example-04: Unambiguous Grammar With Left Recursion-

 

Consider the following grammar-

S → Sa / ∈

 

Clearly, this grammar has left recursion.

A unique parse tree exists for all the strings that can be generated from the grammar.

Therefore, the grammar is unambiguous.

 

Example-05: Ambiguous Grammar Without Left Recursion & Without Left Factoring-

 

Consider the following grammar-

S → aA / Ba

A → a

B → a

 

Clearly, this grammar has neither left recursion nor left factoring.

Now, let us draw parse trees for the string w = aa-

 

 

Clearly,

  • Two different parse trees exist for the string w = aa.
  • Therefore, the grammar is ambiguous.

 

Example-06: Unambiguous Grammar With Both Left Recursion & Left Factoring-

 

Consider the following grammar-

S → Sa / ɛ / bB / bD

B → b

D → d

 

Clearly, this grammar has both left recursion and left factoring.

A unique parse tree exists for all the strings that can be generated from the grammar.

Therefore, the grammar is unambiguous.

 

To gain better understanding about relationship between left recursion, left factoring and ambiguity-

Watch this Video Lecture

 

Download Handwritten Notes Here-

 

 

Next Article- Calculating First and Follow

 

Get more notes and other study material of Compiler Design.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Convert to Base 10 | Number System Conversions

Number System Conversions-

 

Before you go through this article, make sure that you have gone through the previous article on Basics of Number System.

 

In number system,

  • It is very important to have a good knowledge of how to convert numbers from one base to another base.
  • Here, we will learn how to convert any given number from any base to base 10.

 

 

Converting to Base 10-

 

A given number can be converted from any base to base 10 using Expansion Method.

According to expansion method, if abc.de is any given number in base x, then its value in base 10 is given as-

 

(abc.de)x = (ax2 + bx + c + dx-1 + ex-2)10

 

Explanation-

 

To use expansion method for conversion,

  • Assign position number to each digit of the given number.
  • Digits to the left of decimal are numbered starting from 0.
  • Digits to the right of decimal are numbered starting from -1.
  • Write a term for each digit as digit x (base of given number)position number of digit
  • Perform the addition of all terms to obtain the number in base 10.
  • This formula can be expanded for any number of digits.

 

 

PRACTICE PROBLEMS BASED ON CONVERSION TO BASE 10-

 

Convert the following numbers to base 10-

  1. (10010)2
  2. (254)8
  3. (AC)16
  4. (10010.101)2
  5. (254.7014)8
  6. (AC.FBA5)16
  7. (0.1402)8
  8. (0.ABDF)16

 

Solutions-

 

1. (10010)2

 

(10010)2 → ( ? )10

 

Using expansion method, we have-

(10010)2

= ( 1 x 24 + 0 x 2+ 0 x 22 + 1 x 21 + 0 x 20 )10

= ( 16 + 0 + 0 + 2 + 0 )10

= ( 18 )10

 

2. (254)8

 

(254)8 → ( ? )10

 

Using expansion method, we have-

(254)8

= ( 2 x 82 + 5 x 8+ 4 x 80 )10

= ( 128 + 40 + 4 )10

= ( 172 )10

 

3. (AC)16

 

(AC)16 → ( ? )10

 

Using expansion method, we have-

(AC)16

= ( A x 161 + C x 160 )10

= ( 10 x 16 + 12 x 1 )10

= ( 160 + 12 )10

= ( 172 )10

 

4. (10010.101)2

 

(10010.101)2 → ( ? )10

 

Using expansion method, we have-

(10010.101)2

= ( 1 x 24 + 0 x 2+ 0 x 22 + 1 x 21 + 0 x 20 + 1 x 2-1 + 0 x 2-2 + 1 x 2-3 )10

= ( 16 + 0 + 0 + 2 + 0 + 0.5 + 0.125 )10

= ( 18.625 )10

 

5. (254.7014)8

 

(254.7014)8 → ( ? )10

 

Using expansion method, we have-

(254.7014)8

= ( 2 x 82 + 5 x 8+ 4 x 80 + 7 x 8-1 + 0 x 8-2 + 1 x 8-3 + 4 x 8-4 )10

= ( 128 + 40 + 4 + 0.875 + 0.0019 + 0.0009 )10

= ( 172.8778 )10

 

6. (AC.FBA5)16

 

(AC.FBA5)16 → ( ? )10

 

Using expansion method, we have-

(AC.FBA5)16

= ( A x 161 + C x 160 + F x 16-1 + B x 16-2 + A x 16-3 + 5 x 16-4 )10

= ( 10 x 16 + 12 x 1 + 15 x 16-1 + 11 x 16-2 + 10 x 16-3 + 5 x 16-4 )10

= ( 160 + 12 + 0.9375 + 0.0429 + 0.0024 + 0.0001 )10

= ( 172.9829 )10

 

7. (0.1402)8

 

(0.1402)8 → ( ? )10

 

Using expansion method, we have-

(0.1402)8

= ( 0 x 80 + 1 x 8-1 + 4 x 8-2 + 0 x 8-3 + 2 x 8-4 )10

= ( 0 + 0.125 + 0.0625 + 0 + 0.0005 )10

= ( 0.188 )10

 

8. (0.ABDF)16

 

(0.ABDF)16 → ( ? )10

 

Using expansion method, we have-

(0.ABDF)16

= ( 0 x 160 + A x 16-1 + B x 16-2 + D x 16-3 + F x 16-4 )10

= ( 0 x 1 + 10 x 16-1 + 11 x 16-2 + 13 x 16-3 + 15 x 16-4 )10

= ( 0 + 0.625 + 0.0429 + 0.0032 + 0.0002 )10

= ( 0.6713 )10

 

To gain better understanding about Conversion to Base 10,

Watch this Video Lecture

 

Next Article- Decimal to Binary Conversion

 

Get more notes and other study material of Number System.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Base of Number System | Types of Number System

Base of Number System-

 

  • Base of a number system is the total number of digits used in that number system.
  • Number system with base ‘b’ has its digits in the range [0 , b-1].
  • It is also called as radix of a number system.

 

Examples-

 

Consider the following examples-

 

Base 10 Number System-

 

Consider base 10 number system popularly called as decimal number system-

  • Total number of digits used in this number system are 10 since it has base 10.
  • These digits are 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9.
  • These digits clearly lie in the range [0 , base-1] = [0 , 9].

 

Base 2 Number System-

 

Consider base 2 number system popularly called as binary number system-

  • Total number of digits used in this number system are 2 since it has base 2.
  • These digits are 0 and 1.
  • These digits clearly lie in the range [0 , base-1] = [0 , 1].

 

Important Note-

 

It is important to note that-

  • All the digits of any number system with base ‘b’ are always less than ‘b’.
  • This is clear from the range [0 , base-1] in which the digits of any number system lie.

 

Types of Number System-

 

Four most commonly used number systems are-

 

 

  1. Decimal Number System
  2. Binary Number System
  3. Octal Number System
  4. Hexadecimal Number System

 

The following table shows the base and digits used in these number systems-

 

Number System Base Digits Used
Decimal Number System 10 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

(Total 10 digits)

Binary Number System 2 0, 1

(Total 2 digits)

Octal Number System 8 0, 1, 2, 3, 4, 5, 6, 7

(Total 8 digits)

Hexadecimal Number System 16 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

(Total 16 digits)

 

To gain better understanding about Basics of Number System,

Watch this Video Lecture

 

Next Article- Converting Any Base to Base 10

 

Get more notes and other study material of Number System.

Watch video lectures by visiting our YouTube channel LearnVidFun.

DFA Solved Examples | How to Construct DFA

Construction Of DFA-

 

Before you go through this article, make sure that you have gone through the previous article on Type-01 Problems.

 

Type-02 Problems-

 

In Type-02 problems, we will discuss the construction of DFA for languages consisting of strings starting with a particular substring.

 

Steps To Construct DFA-

 

Following steps are followed to construct a DFA for Type-02 problems-

 

Step-01:

 

  • Determine the minimum number of states required in the DFA.
  • Draw those states.

 

Use the following rule to determine the minimum number of states-

 

RULE

Calculate the length of substring.

All strings starting with ‘n’ length substring will always require minimum (n+2) states in the DFA.

 

Step-02:

 

  • Decide the strings for which DFA will be constructed.
  • The method for deciding the strings has been discussed in this Video.

 

Step-03:

 

  • Construct a DFA for the strings decided in Step-02.

 

Remember the following rule while constructing the DFA-

 

RULE

While constructing a DFA,

  • Always prefer to use the existing path.
  • Create a new path only when there exists no path to go with.

 

Step-04:

 

  • Send all the left possible combinations to the dead state.
  • Do not send the left possible combinations over the starting state.

 

PRACTICE PROBLEMS BASED ON CONSTRUCTION OF DFA-

 

Problem-01:

 

Draw a DFA for the language accepting strings starting with ‘ab’ over input alphabets ∑ = {a, b}

 

Solution-

 

Regular expression for the given language = ab(a + b)*

 

Step-01:

 

  • All strings of the language starts with substring “ab”.
  • So, length of substring = 2.

 

Thus, Minimum number of states required in the DFA = 2 + 2 = 4.

It suggests that minimized DFA will have 4 states.

 

Step-02:

 

We will construct DFA for the following strings-

  • ab
  • aba
  • abab

 

Step-03:

 

The required DFA is-

 

Problem-02:

 

Draw a DFA for the language accepting strings starting with ‘a’ over input alphabets ∑ = {a, b}

 

Solution-

 

Regular expression for the given language = a(a + b)*

 

Step-01:

 

  • All strings of the language starts with substring “a”.
  • So, length of substring = 1.

 

Thus, Minimum number of states required in the DFA = 1 + 2 = 3.

It suggests that minimized DFA will have 3 states.

 

Step-02:

 

We will construct DFA for the following strings-

  • a
  • aa

 

Step-03:

 

The required DFA is-

 

Problem-03:

 

Draw a DFA for the language accepting strings starting with ‘101’ over input alphabets ∑ = {0, 1}

 

Solution-

 

Regular expression for the given language = 101(0 + 1)*

 

Step-01:

 

  • All strings of the language starts with substring “101”.
  • So, length of substring = 3.

 

Thus, Minimum number of states required in the DFA = 3 + 2 = 5.

It suggests that minimized DFA will have 5 states.

 

Step-02:

 

We will construct DFA for the following strings-

  • 101
  • 1011
  • 10110
  • 101101

 

Step-03:

 

The required DFA is-

 

Problem-04:

 

Draw a DFA that accepts a language L over input alphabets ∑ = {0, 1} such that L is the set of all strings starting with ’00’.

 

Solution-

 

Regular expression for the given language = 00(0 + 1)*

 

Step-01:

 

  • All strings of the language starts with substring “00”.
  • So, length of substring = 2.

 

Thus, Minimum number of states required in the DFA = 2 + 2 = 4.

It suggests that minimized DFA will have 4 states.

 

Step-02:

 

We will construct DFA for the following strings-

  • 00
  • 000
  • 00000

 

Step-03:

 

The required DFA is-

 

Problem-05:

 

Construct a DFA that accepts a language L over input alphabets ∑ = {a, b} such that L is the set of all strings starting with ‘aa’ or ‘bb’.

 

Solution-

 

Regular expression for the given language = (aa + bb)(a + b)*

 

Step-01:

 

Minimum number of states required in the DFA = 5.

It suggests that minimized DFA will have 5 states.

 

Step-02:

 

We will construct DFA for the following strings-

  • aa
  • aaa
  • aaaa
  • bb
  • bbb
  • bbbb

 

Step-03:

 

The required DFA is-

 

Problem-06:

 

Construct a DFA that accepts a language L over input alphabets ∑ = {a, b} such that L is the set of all strings starting with ‘aba’.

 

Solution-

 

Regular expression for the given language = aba(a + b)*

 

Step-01:

 

  • All strings of the language starts with substring “aba”.
  • So, length of substring = 3.

 

Thus, Minimum number of states required in the DFA = 3 + 2 = 5.

It suggests that minimized DFA will have 5 states.

 

Step-02:

 

We will construct DFA for the following strings-

  • aba
  • abaa
  • abaab
  • abaaba

 

Step-03:

 

The required DFA is-

 

Also Read- Converting DFA to Regular Expression

 

To gain better understanding about Construction of DFA,

Watch this Video Lecture

 

Download Handwritten Notes Here-

 

 

Next Article- Minimization of DFA

 

Get more notes and other study material of Theory of Automata and Computation.

Watch video lectures by visiting our YouTube channel LearnVidFun.