Docsity

Lexical Analyzer Exercises and Solutions, Exams of Operating Systems

Toggle sidebar
bg1
Home Work -Lexical Analyzer
Name: Parvez Mohammad Shariar 朴维
ID: 1911562106
3.6.1: Figure 3.19 in the exercises of Section 3.4 computes the failure function for the KMP
algorithm. Show how, given that failure function, we can construct, from a keyword b1b2...bn an
n + 1-state DFA that recognizes .*b1b2...bn, where the dot stands for "any character."
Moreover, this DFA can be constructed in O(n) time.
Answer:
Take the string "abbaabb" in exercise 3.4.3-3 as example, the failure function is:
• n : 1, 2, 3, 4, 5, 6, 7
• f(n): 0, 0, 0, 1, 1, 2, 3
The DFA is:
Pseudocode of building the DFA:
for (i = 0; i< n; i ++) {
move[s[i], c] = {
if ( c == b1b2…bn[i] ) {
goto s[i+1]
} else {
goto s[f(i)]
}
}
}
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Lexical Analyzer Exercises and Solutions and more Exams Operating Systems in PDF only on Docsity!

Home Work - Lexical Analyzer

Name: Parvez Mohammad Shariar 朴维

ID: 1911562106

3.6.1: Figure 3.19 in the exercises of Section 3.4 computes the failure function for the KMP algorithm. Show how, given that failure function, we can construct, from a keyword b1b2...bn an n + 1-state DFA that recognizes .*b1b2...bn, where the dot stands for "any character." Moreover, this DFA can be constructed in O(n) time. Answer: Take the string "abbaabb" in exercise 3.4.3-3 as example, the failure function is:

  • n : 1, 2, 3, 4, 5, 6, 7
  • f(n): 0, 0, 0, 1, 1, 2, 3 The DFA is: Pseudocode of building the DFA: for (i = 0; i< n; i ++) { move[s[i], c] = { if ( c == b1b2…bn[i] ) { goto s[i+1] } else { goto s[f(i)] } } }

It is obviously that with the known f(n), this DFA can be constructed in O(n) time. 3.6.2: Design finite automata (deterministic or nondeterministic) for each of the languages of Exercise 3.3.5. 3.6.3: For the NFA of Fig. 3.29, indicate all the paths labeled aabb. Does the NFA accept aabb? Answer:

  • (0) - a-> (1) - a-> (2) - b-> (2) - b-> ((3))
  • (0) - a-> (0) - a-> (0) - b-> (0) - b-> (0)
  • (0) - a-> (0) - a-> (1) - b-> (1) - b-> (1)
  • (0) - a-> (1) - a-> (1) - b-> (1) - b-> (1)
  • (0) - a-> (1) - a-> (2) - b-> (2) - b-> (2)
  • (0) - a-> (1) - a-> (2) - b-> (2) - ε-> (0) - b-> (0)
  • (0) - a-> (1) - a-> (2) - ε-> (0) - b-> (0) - b-> (0) This NFA accepts "aabb" Give the transition tables for the NFA of:
  1. Exercise 3.6.3.
  2. Exercise 3.6.4.
  3. Figure 3.26. Answer: Table 1 state a b ε 0 {0,1} {0} ∅ 1 {1,2} {1} ∅ 2 {2} {2,3} {0} 3 ∅ ∅ ∅ Table 2

NFA State DFA State a b {2} B B ∅ {4} C ∅ C **DFA

Transition table NFA State DFA State a b** {0} A B A {0,1} B C B {0,1,2} C C D {0,2,3} D C D DFA

3. Transition table NFA State DFA State a b {0,1,2,3} A A A DFA 3.7.2: use Algorithm 3.22 to simulate the NFA's:

  1. Fig. 3.29.
  2. Fig. 3.30. on input aabb. Answer:
    • start->{0}-a->{0,1}-a->{0,1,2}-b->{0,2,3}-b->{0,2,3}
    • start->{0,1,2,3}-a->{0,1,2,3}-a->{0,1,2,3}-b->{0,1,2,3}-b->{0,1,2,3} 3.7.2: Convert the following regular expressions to deterministic finite automata, using algorithms 3.23 and 3.20:
  3. (a|b)*
  4. (a|b)*
  5. ((ε|a)|b) Answer:

2. NFA Transition table NFA State DFA State a b {0,1,2,3,4,5,8,9,10,11} A B C {1,2,3,4,5,6,8,9,10,11} B B C {1,2,3,4,5,7,8,9,10,11} C B C DFA

NFA

Transition table NFA State DFA State a b {0,1,2,3,4,6,7,9,10} A B C {1,2,3,4,5,6,7,9,10} B B C {1,2,3,4,6,7,8,9,10} C B C DFA