ss ui lecture 6

24
System Software (5KS03) Unit 1 : Introduction to Compiling Lecture : 6 Design of lexical analyzer generator, State minimization of a DFA A S Kapse, Assistant Professor, Department Of Computer Sci. & Engineering Anuradha Engineering College, Chikhli

Upload: avinash-kapse

Post on 16-Apr-2017

48 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SS UI Lecture 6

System Software (5KS03)Unit 1 : Introduction to Compiling

Lecture : 6 Design of lexical analyzer generator, State minimization of a DFA

A S Kapse,Assistant Professor,

Department Of Computer Sci. & Engineering Anuradha Engineering College, Chikhli

Page 2: SS UI Lecture 6

Contents… Design of lexical analyzer generator, State minimization of a DFA

Page 3: SS UI Lecture 6

Objectives…

Upon completion of this lecture, you will be able

To understand the Lexical Analysis To understand Role of lexical analyses To understand input buffering & state

minimization of DFA

Page 4: SS UI Lecture 6

Review…./ Concepts What do you mean by State? What do you mean by NFA & DFA? What do you mean by Parse tree?

Page 5: SS UI Lecture 6

5

Deterministic and Nondeterministic Automata Deterministic Finite Automata (DFA)

One transition per input per state No -moves

Nondeterministic Finite Automata (NFA) Can have multiple transitions for one input in a

given state Can have -moves

Finite automata have finite memory Need only to encode the current state

Page 6: SS UI Lecture 6

6

Execution of Finite Automata A DFA can take only one path through the

state graph Completely determined by input

NFAs can choose Whether to make -moves Which of multiple transitions for a single input to

take

Page 7: SS UI Lecture 6

7

Acceptance of NFAs An NFA can get into multiple states

• Input:

0

1

1

0

1 0 1

• Rule: NFA accepts if it can get in a final state

Page 8: SS UI Lecture 6

8

NFA vs. DFA (1) NFAs and DFAs recognize the same set of

languages (regular languages)

DFAs are easier to implement There are no choices to consider

Page 9: SS UI Lecture 6

9

NFA vs. DFA (2) For a given language the NFA can be simpler

than the DFA0

10

0

01

0

1

0

1

NFA

DFA

• DFA can be exponentially larger than NFA

Page 10: SS UI Lecture 6

10

Regular Expressions to Finite Automata

High-level sketch

Regularexpressions

NFA

DFA

LexicalSpecification

Table-driven Implementation of DFA

Page 11: SS UI Lecture 6

11

Regular Expressions to NFA (1) For each kind of rexp, define an NFA

Notation: NFA for rexp A

A

• For

• For input aa

Page 12: SS UI Lecture 6

12

Regular Expressions to NFA (2) For AB

A B

• For A | B

A

B

Page 13: SS UI Lecture 6

13

Regular Expressions to NFA (3) For A*

A

Page 14: SS UI Lecture 6

14

Example of RegExp -> NFA conversion Consider the regular expression

(1 | 0)*1 The NFA is

1C E0D F

B

G

A H 1I J

Page 15: SS UI Lecture 6

15

Next

Regularexpressions

NFA

DFA

LexicalSpecification

Table-driven Implementation of DFA

Page 16: SS UI Lecture 6

16

NFA to DFA. The Trick Simulate the NFA Each state of resulting DFA

= a non-empty subset of states of the NFA Start state

= the set of NFA states reachable through -moves from NFA start state

Add a transition S a S’ to DFA iff S’ is the set of NFA states reachable from the

states in S after seeing the input a considering -moves as well

Page 17: SS UI Lecture 6

17

NFA -> DFA Example

10 1

A BC

D

E

FG H I J

ABCDHI

FGABCDHI

EJGABCDHI

0

1

0

10 1

Page 18: SS UI Lecture 6

18

NFA to DFA. Remark An NFA may be in many states at any time

How many different states ?

If there are N states, the NFA must be in some subset of those N states

How many non-empty subsets are there? 2N - 1 = finitely many, but exponentially many

Page 19: SS UI Lecture 6

19

Implementation A DFA can be implemented by a 2D table T

One dimension is “states” Other dimension is “input symbols” For every transition Si a Sk define T[i,a] = k

DFA “execution” If in state Si and input a, read T[i,a] = k and skip to

state Sk Very efficient

Page 20: SS UI Lecture 6

20

Table Implementation of a DFA

S

T

U

0

1

0

10 1

0 1S T UT T UU T U

Page 21: SS UI Lecture 6

21

Implementation (Cont.) NFA -> DFA conversion is at the heart of tools

such as flex or jflex

But, DFAs can be huge

In practice, flex-like tools trade off speed for space in the choice of NFA and DFA representations

Page 22: SS UI Lecture 6

Video on Compilers

1. Design of lexical analyzer generator,

Page 23: SS UI Lecture 6

Questions..1. What are the steps associated with

transformation from NFA to DFA?2.

Page 24: SS UI Lecture 6

Homework..1. What is parser?2. Define a translator and categorize it.