Theoretical Aspects of Lexical Analysis/Exercise 4: Difference between revisions

From Wiki**3

Root (talk | contribs)
New page: Use Thompson's algorithm to build the NFA for the following regular expression. Build the corresponding DFA and minimize it. * <nowiki>(a|b)*abb(a|b)*</nowiki> == Solution == [[category...
 
Root (talk | contribs)
No edit summary
 
(26 intermediate revisions by the same user not shown)
Line 1: Line 1:
Use Thompson's algorithm to build the NFA for the following regular expression. Build the corresponding DFA and minimize it.  
{{TOCright}}
* <nowiki>(a|b)*abb(a|b)*</nowiki>
 
==Problem ==
Use Thompson's algorithm to build the NFA for the following regular expression. Build the corresponding DFA and minimize it.
 
* '''<nowiki>(a|b)*abb(a|b)*</nowiki>'''


== Solution ==
== Solution ==


[[category:Teaching]]
The non-deterministic finite automaton (NFA), built by applying Thompson's algorithm to the regular expression '''<nowiki>(a|b)*abb(a|b)*</nowiki>''' is the following:
[[category:Compilers]]
{{CollapsedCode|NFA|
<kroki lang="graphviz">
digraph nfa {
    { node [shape=circle style=invis] s }
  rankdir=LR; ratio=0.5
  node [shape=doublecircle,fixedsize=true,width=0.2,fontsize=10]; 17
  node [shape=circle,fixedsize=true,width=0.2,fontsize=10];
 
  s -> 0
  0 -> 1
  1 -> 2
  1 -> 4
  2 -> 3 [label="a",fontsize=10]
  4 -> 5 [label="b",fontsize=10]
  3 -> 6
  5 -> 6
  6 -> 1
  6 -> 7
  0 -> 7
 
  7 -> 8 [label="a",fontsize=10]
  8 -> 9 [label="b",fontsize=10]
  9 -> 10 [label="b",fontsize=10]
 
  10 -> 11
  11 -> 12
  11 -> 14
  12 -> 13 [label="a",fontsize=10]
  14 -> 15 [label="b",fontsize=10]
  13 -> 16
  15 -> 16
  16 -> 11
  16 -> 17
  10 -> 17
 
  fontsize=10
}
</kroki>
}}
 
Applying the determination algorithm to the above NFA, the following determination table is obtained:
 
{| class="mw-collapsible mw-collapsed" border="1" cellspacing="0" style="font-family: Arial; text-align: center; border-collapse: collapse;"
|+ '''Determination table'''
|-
! style="background-color:#FFCC99; height:44px; width:84px;" | '''In'''
! style="background-color:#FFCC99; width:84px;" | '''&alpha;&isin;&Sigma;'''
! style="background-color:#FFCC99; width:84px;" | '''move(In, &alpha;)'''
! style="background-color:#FFCC99; width:237px;" | '''&epsilon;-closure(move(In, &alpha;))'''
! style="background-color:#FFCC99; width:84px;" | '''In+1 = &epsilon;-closure(move(In, &alpha;))'''
|- style="background-color:#FFFFCC; height:17px;"
| '''-''' || - || 0 || 0, 1, 2, 4, 7 || 0
|- style="background-color:#F5F5F5; height:17px;"
| 0 || a || 3, 8 || 1, 2, 3, 4, 6, 7, 8 || 1
|- style="background-color:#F5F5F5; height:17px;"
| 0 || b || 5 || 1, 2, 4, 5, 6, 7 || 2
|- style="background-color:#FFFFCC; height:17px;"
| 1 || a || 3, 8 || 1, 2, 3, 4, 6, 7, 8 || 1
|- style="background-color:#FFFFCC; height:17px;"
| 1 || b || 5, 9 || 1, 2, 4, 5, 6, 7, 9 || 3
|- style="background-color:#F5F5F5; height:17px;"
| 2 || a || 3, 8 || 1, 2, 3, 4, 6, 7, 8 || 1
|- style="background-color:#F5F5F5; height:17px;"
| 2 || b || 5 || 1, 2, 4, 5, 6, 7 || 2
|- style="background-color:#FFFFCC; height:17px;"
| 3 || a || 3, 8 || 1, 2, 3, 4, 6, 7, 8 || 1
|- style="background-color:#FFFFCC; height:17px;"
| 3 || b || 5, 10 || 1, 2, 4, 5, 6, 7, 10, 11, 12, 14,&nbsp;17 || '''4'''
|- style="background-color:#F5F5F5; height:17px;"
| '''4''' || a || 3, 8, 13 || 1, 2, 3, 4, 6, 7, 8, 11, 12, 13, 14, 16,&nbsp;17 || '''5'''
|- style="background-color:#F5F5F5; height:17px;"
| '''4''' || b || 5, 15 || 1, 2, 4, 5, 6, 7, 11, 12, 14, 15, 16,&nbsp;17 || '''6'''
|- style="background-color:#FFFFCC; height:17px;"
| '''5''' || a || 3, 8, 13 || 1, 2, 3, 4, 6, 7, 8, 11, 12, 13, 14, 16,&nbsp;17 || '''5'''
|- style="background-color:#FFFFCC; height:17px;"
| '''5''' || b || 5, 9, 15 || 1, 2, 4, 5, 6, 7, 9, 11, 12, 14, 15, 16,&nbsp;17 || '''7'''
|- style="background-color:#F5F5F5; height:17px;"
| '''6''' || a || 3, 8, 13 || 1, 2, 3, 4, 6, 7, 8, 11, 12, 13, 14, 16,&nbsp;17 || '''5'''
|- style="background-color:#F5F5F5; height:17px;"
| '''6''' || b || 5, 15 || 1, 2, 4, 5, 6, 7, 11, 12, 14, 15, 16,&nbsp;17 || '''6'''
|- style="background-color:#FFFFCC; height:17px;"
| '''7''' || a || 3, 8, 13 || 1, 2, 3, 4, 6, 7, 8, 11, 12, 13, 14, 16,&nbsp;17 || '''5'''
|- style="background-color:#FFFFCC; height:17px;"
| '''7''' || b || 5, 10, 15 || 1, 2, 4, 5, 6, 7, 10, 11, 12, 14, 15, 16,&nbsp;17 || '''8'''
|- style="background-color:#F5F5F5; height:17px;"
| '''8''' || a || 3, 8, 13 || 1, 2, 3, 4, 6, 7, 8, 11, 12, 13, 14, 16,&nbsp;17 || '''5'''
|- style="background-color:#F5F5F5; height:17px;"
| '''8''' || b || 5, 15 || 1, 2, 4, 5, 6, 7, 11, 12, 14, 15, 16,&nbsp;17 || '''6'''
|}
 
{{CollapsedCode|Determination table|{| border="1" cellspacing="0" style="font-family: Arial; text-align: center; border-collapse: collapse;"
|-
! style="background-color:#FFCC99; height:44px; width:84px;" | '''In'''
! style="background-color:#FFCC99; width:84px;" | '''&alpha;&isin;&Sigma;'''
! style="background-color:#FFCC99; width:84px;" | '''move(In, &alpha;)'''
! style="background-color:#FFCC99; width:237px;" | '''&epsilon;-closure(move(In, &alpha;))'''
! style="background-color:#FFCC99; width:84px;" | '''In+1 = &epsilon;-closure(move(In, &alpha;))'''
|- style="background-color:#FFFFCC; height:17px;"
| '''-''' || - || 0 || 0, 1, 2, 4, 7 || 0
|- style="background-color:#F5F5F5; height:17px;"
| 0 || a || 3, 8 || 1, 2, 3, 4, 6, 7, 8 || 1
|- style="background-color:#F5F5F5; height:17px;"
| 0 || b || 5 || 1, 2, 4, 5, 6, 7 || 2
|- style="background-color:#FFFFCC; height:17px;"
| 1 || a || 3, 8 || 1, 2, 3, 4, 6, 7, 8 || 1
|- style="background-color:#FFFFCC; height:17px;"
| 1 || b || 5, 9 || 1, 2, 4, 5, 6, 7, 9 || 3
|- style="background-color:#F5F5F5; height:17px;"
| 2 || a || 3, 8 || 1, 2, 3, 4, 6, 7, 8 || 1
|- style="background-color:#F5F5F5; height:17px;"
| 2 || b || 5 || 1, 2, 4, 5, 6, 7 || 2
|- style="background-color:#FFFFCC; height:17px;"
| 3 || a || 3, 8 || 1, 2, 3, 4, 6, 7, 8 || 1
|- style="background-color:#FFFFCC; height:17px;"
| 3 || b || 5, 10 || 1, 2, 4, 5, 6, 7, 10, 11, 12, 14,&nbsp;17 || '''4'''
|- style="background-color:#F5F5F5; height:17px;"
| '''4''' || a || 3, 8, 13 || 1, 2, 3, 4, 6, 7, 8, 11, 12, 13, 14, 16,&nbsp;17 || '''5'''
|- style="background-color:#F5F5F5; height:17px;"
| '''4''' || b || 5, 15 || 1, 2, 4, 5, 6, 7, 11, 12, 14, 15, 16,&nbsp;17 || '''6'''
|- style="background-color:#FFFFCC; height:17px;"
| '''5''' || a || 3, 8, 13 || 1, 2, 3, 4, 6, 7, 8, 11, 12, 13, 14, 16,&nbsp;17 || '''5'''
|- style="background-color:#FFFFCC; height:17px;"
| '''5''' || b || 5, 9, 15 || 1, 2, 4, 5, 6, 7, 9, 11, 12, 14, 15, 16,&nbsp;17 || '''7'''
|- style="background-color:#F5F5F5; height:17px;"
| '''6''' || a || 3, 8, 13 || 1, 2, 3, 4, 6, 7, 8, 11, 12, 13, 14, 16,&nbsp;17 || '''5'''
|- style="background-color:#F5F5F5; height:17px;"
| '''6''' || b || 5, 15 || 1, 2, 4, 5, 6, 7, 11, 12, 14, 15, 16,&nbsp;17 || '''6'''
|- style="background-color:#FFFFCC; height:17px;"
| '''7''' || a || 3, 8, 13 || 1, 2, 3, 4, 6, 7, 8, 11, 12, 13, 14, 16,&nbsp;17 || '''5'''
|- style="background-color:#FFFFCC; height:17px;"
| '''7''' || b || 5, 10, 15 || 1, 2, 4, 5, 6, 7, 10, 11, 12, 14, 15, 16,&nbsp;17 || '''8'''
|- style="background-color:#F5F5F5; height:17px;"
| '''8''' || a || 3, 8, 13 || 1, 2, 3, 4, 6, 7, 8, 11, 12, 13, 14, 16,&nbsp;17 || '''5'''
|- style="background-color:#F5F5F5; height:17px;"
| '''8''' || b || 5, 15 || 1, 2, 4, 5, 6, 7, 11, 12, 14, 15, 16,&nbsp;17 || '''6'''
|}
}}
 
Graphically, the DFA is represented as follows:
 
{{CollapsedCode|DFA|
<kroki lang="graphviz">
digraph dfa {
    { node [shape=circle style=invis] s }
  rankdir=LR; ratio=0.5
  node [shape=doublecircle,fixedsize=true,width=0.2,fontsize=10]; 4 5 6 7 8
  node [shape=circle,fixedsize=true,width=0.2,fontsize=10];
  s -> 0
  0 -> 1 [label="a",fontsize=10]
  0 -> 2 [label="b",fontsize=10]
  1 -> 1  [label="a",fontsize=10]
  1 -> 3  [label="b",fontsize=10]
  2 -> 1 [label="a",fontsize=10]
  2 -> 2 [label="b",fontsize=10]
  3 -> 1 [label="a",fontsize=10]
  3 -> 4 [label="b",fontsize=10]
  4 -> 5 [label="a",fontsize=10]
  4 -> 6 [label="b",fontsize=10]
  5 -> 5 [label="a",fontsize=10]
  5 -> 7 [label="b",fontsize=10]
  6 -> 5 [label="a",fontsize=10]
  6 -> 6 [label="b",fontsize=10]
  7 -> 5 [label="a",fontsize=10]
  7 -> 8 [label="b",fontsize=10]
  8 -> 5 [label="a",fontsize=10]
  8 -> 6 [label="b",fontsize=10]
  fontsize=10
}
</kroki>
}}
The minimization tree is as follows:
 
{{CollapsedCode|Minimization tree|
[[image:aula3p4mintree.jpg|350px]]
}}
 
<!--The tree expansion for non-splitting sets has been omitted for simplicity ("a" transition for non-final states and the {0, 2} "a" and "b" transitions. The final states are all indistinguishable, regarding both "a" or "b" transitions (remember that, at this stage, we assume that individual states -- i.e., the final states -- are all indistinguishable).
-->
Given the minimization tree above, the final minimal DFA is as follows:
 
{{CollapsedCode|Minimal DFA|
<kroki lang="graphviz">
digraph dfamin {
    { node [shape=circle style=invis] s }
  rankdir=LR; ratio=0.5
  node [shape=doublecircle,fixedsize=true,width=0.4,fontsize=10]; 45678
  node [shape=circle,fixedsize=true,width=0.3,fontsize=10];
  s -> 02
  02 -> 1 [label="a",fontsize=10]
  02 -> 02 [label="b",fontsize=10]
  1 -> 1  [label="a",fontsize=10]
  1 -> 3  [label="b",fontsize=10]
  3 -> 1 [label="a",fontsize=10]
  3 -> 45678 [label="b",fontsize=10]
  45678 -> 45678 [label="a",fontsize=10]
  45678 -> 45678 [label="b",fontsize=10]
  fontsize=10
}
</kroki>
}}
 
[[category:Compiladores]]
[[category:Ensino]]
 
[[en:Theoretical Aspects of Lexical Analysis]]
[[en:Theoretical Aspects of Lexical Analysis]]

Latest revision as of 18:36, 26 April 2026

Problem

Use Thompson's algorithm to build the NFA for the following regular expression. Build the corresponding DFA and minimize it.

  • (a|b)*abb(a|b)*

Solution

The non-deterministic finite automaton (NFA), built by applying Thompson's algorithm to the regular expression (a|b)*abb(a|b)* is the following:

NFA

Applying the determination algorithm to the above NFA, the following determination table is obtained:

Determination table
In α∈Σ move(In, α) ε-closure(move(In, α)) In+1 = ε-closure(move(In, α))
- - 0 0, 1, 2, 4, 7 0
0 a 3, 8 1, 2, 3, 4, 6, 7, 8 1
0 b 5 1, 2, 4, 5, 6, 7 2
1 a 3, 8 1, 2, 3, 4, 6, 7, 8 1
1 b 5, 9 1, 2, 4, 5, 6, 7, 9 3
2 a 3, 8 1, 2, 3, 4, 6, 7, 8 1
2 b 5 1, 2, 4, 5, 6, 7 2
3 a 3, 8 1, 2, 3, 4, 6, 7, 8 1
3 b 5, 10 1, 2, 4, 5, 6, 7, 10, 11, 12, 14, 17 4
4 a 3, 8, 13 1, 2, 3, 4, 6, 7, 8, 11, 12, 13, 14, 16, 17 5
4 b 5, 15 1, 2, 4, 5, 6, 7, 11, 12, 14, 15, 16, 17 6
5 a 3, 8, 13 1, 2, 3, 4, 6, 7, 8, 11, 12, 13, 14, 16, 17 5
5 b 5, 9, 15 1, 2, 4, 5, 6, 7, 9, 11, 12, 14, 15, 16, 17 7
6 a 3, 8, 13 1, 2, 3, 4, 6, 7, 8, 11, 12, 13, 14, 16, 17 5
6 b 5, 15 1, 2, 4, 5, 6, 7, 11, 12, 14, 15, 16, 17 6
7 a 3, 8, 13 1, 2, 3, 4, 6, 7, 8, 11, 12, 13, 14, 16, 17 5
7 b 5, 10, 15 1, 2, 4, 5, 6, 7, 10, 11, 12, 14, 15, 16, 17 8
8 a 3, 8, 13 1, 2, 3, 4, 6, 7, 8, 11, 12, 13, 14, 16, 17 5
8 b 5, 15 1, 2, 4, 5, 6, 7, 11, 12, 14, 15, 16, 17 6
Determination table
{

Graphically, the DFA is represented as follows:

DFA

The minimization tree is as follows:

Minimization tree

Given the minimization tree above, the final minimal DFA is as follows:

Minimal DFA