14
Optimization of DFA By: Kiran Acharya

Optimization of dfa

Embed Size (px)

DESCRIPTION

optimization of DFA is explained

Citation preview

Page 1: Optimization of dfa

Optimization of DFA

By: Kiran Acharya

Page 2: Optimization of dfa

Three Methods to Optimize

• Directly build DFA from regular expression.

• Minimize states.

• Compact transition tables

Page 3: Optimization of dfa

Constructing Direct DFA

• First we augment the given regular expression by concatenating it with a special symbol #.

• r will be (r)# augmented regular expression.• Secondly we create a syntax tree for this

augmented regular expression. • In this syntax tree, all alphabet symbols (plus # and

the empty string) in the augmented regular expression will be on the leaves,

• all inner nodes will be the operators in that augmented regular expression

Page 4: Optimization of dfa

Example till now…

Page 5: Optimization of dfa

Followpos, Firstpos,Lastpos

• followpos(i) is the set of positions which can follow the position i in the strings generated by the augmented regular expression.

Page 6: Optimization of dfa

• firstpos(n) the set of the positions of the first symbols of strings generated by the sub-expression rooted by n.

• lastpos(n) the set of the positions of the last symbols of strings generated by the sub-expression rooted by n

• nullable(n)true if the empty string is a member of strings generated by the sub-expression rooted by n false otherwise.

Page 7: Optimization of dfa

Calculating Firstpos and Lastpos & nullable

Page 8: Optimization of dfa

Evaluating Followpos

• If n is concatenation-node with left child c1 and right child c2,and i is a position in lastpos(c1), then all positions in firstpos(c2) are in followpos(i).

• If n is a star-node, and I is a position in lastpos(n), then all positions in firstpos(n) are in followpos(i).

Page 9: Optimization of dfa

example

Page 10: Optimization of dfa

(a|b)*a#

Page 11: Optimization of dfa

Minimizing number of states of DFA

partition the set of states into two groups: • G 1 : set of accepting states• G2 : set of non-accepting states

For each new group G partition G into subgroups such that states s1 and s 2 are in the same group iff for

all input symbols a, states s1and s2 have transitions to states in the same group.

• Start state of the minimized DFA is the group containing the start state of the original DFA.

• Accepting states of the minimized DFA are the groups containing the accepting states of the original DFA.

Page 12: Optimization of dfa

example

Page 13: Optimization of dfa

Compacting Transition Tables

• transition function of a DFA two dimensional table indexed by states and characters

• typical lexical analyzer has hundreds of states ASCII alphabet of 128 input characters < 1 MB

• compilers “live” in small devices too 1 MB could be too much

Page 14: Optimization of dfa

Alternate Representations

• list of character-state pairs

• ending by a default state

1. chosen for any input character not on the list2. the most frequently occurring next state

• thus, the table is reduced by a large factor