28/10/2013
1
REGULAR
LANGUAGES/GRAMMARS, AND
FINITE AUTOMATA
Grammar
Productions of the form:
String of variables
and terminals
Variable Terminal
symbols
Start
variable
Variable
Definition: Regular grammars
xY(grammatiche lineari destre)
Yx (grammatiche lineari sinistre)
where x ( ) e Y ( V )
A
{V, , S, P}
28/10/2013
2
G = {S,V, , P}
= {a,b,c}
V = {S, A }
S a A
A b
A c b
L(g) = {ab,acb}
G = {S,V, , P}
= {a,b,c}
V = {S, A, B }
S a A
A b
A c B
B b
L(g) = {ab,acb}
EXAMPLE
V = {S,X}
= {a, b}
P = {S (empty string}
S aX
X bS
S = {S}
S
S aX abS ab
S aX abS abaX ababS abab
L = {, ab, abab, ababab, …….}
28/10/2013
3
WHAT IS A REGULAR LANGUAGE?
A language which sentences can be built by a
Regular Grammar
A language that can be build by composition
using concatanation operator, union operator
and Kleene star of empty language and unitary
language
All finite language are regular language
REGULAR EXPRESSION
A regular expression is: 1. (empty string) denoting the language L = { }
2. a , a is a regular expression denoting the language L = {a}
3. If R and S are regular expression, R S (or R|S) is a regular expression denoting the language L = L{R} L{S}
4. If R and S are regular expression, R . S (or R) is a regular expression denoting the language L = L{R} . L{S}
5. If R is a regular expression, R* is a regular expression denoting the language L = L{R}* (R* is the kleene star of R). Example : (ab)* = {, ab, abab, etc.}
6. If R is a regular expression, R+ is a regular expression denoting the language L = L{R}+ (R+ is the positive clousure of R). Example : (ab)+ = {ab, abab, etc.}
28/10/2013
4
REGULAR EXPRESSION
Regular expression on alphaber Regular expr. Language {} a {a} (r1) | (r2) L(r1) L(r2) (r1) (r2) L(r1) L(r2) (r)* (L(r))*
(r) L(r) (r)+
= (r)(r)*
(r)? = (r) |
EXAMPLES
(c|d|e)(a|b)
denoting the language L = {ca, cb, da, db, ea, eb}
(a|b)*c(a|b)*
denoting the language L containing all string with
zero or more “a” followed by “c” and the string
terminate with zero or more “b”
28/10/2013
5
REGULAR EXPRESSION
The parentheses can be omitted in regular expressions using the rules of precedence
* highest
concatenation next
| lowest
ab*|c means (a(b)*)|(c)
Examples = {0,1}
0|1 => {0,1}
(0|1)(0|1) => {00,01,10,11}
0* => { ,0,00,000,0000,....}
(0|1)* => all strings with 0 and 1, included the
empty string
DEFINIZIONI REGOLARI
LA scrittura di espressioni regolari per alcuni linguaggi può essere complessa perche le espressioni regolari che lo descrivono possono essere complesse, In questo caso si usano le definizioni regolari”.
Noi possiamo attribuire dei nomi come simboli per la definizione di altre espressioni regolari.
Una definizione regolare è una sequenza di definizioni nella forma:
d1 r1 dove di è un nome
e
d2 r2 ri è una espressione regolare sui simboli
. {d1,d2,...,di-1}
dn rn
28/10/2013
6
DEFINIZIONI REGOLARI
Esempio: Identificatori in C lettera A | B | ... | Z | a | b | ... | z
cifra 0 | 1 | ... | 9
identificatore lettera (lettera | cifra ) *
SE noi scriviamo direttamente l’espressione regolare avremo
(A|...|Z|a|...|z) ( (A|...|Z|a|...|z) | (0|...|9) ) *
NoZero = 1 | ... | 9 Segno = +|-|
Interi = Segno 0 | Segno NoZero (NoZero | 0)* =
Segno (0 | NoZero (NoZero | 0)*)
PROPRIETA’ ALGEBRICHE
Assioma Descrizione
r |s = s|r | e’ commutativo
r |(s|t) = r |(s|t) | è associativo
(rs)t = r (st) La concatenazione è associativa
r (s|t) = rs|rt (s|t)r = sr |tr
La concatenazione gode della prorprietà distributiva su |
r = r
r = r
è l’elemento neutro per la concatenazione
r* = ( r |)*
r** = r* * è idenpotente
28/10/2013
7
FINITE AUTOMATON (FA)
Informally, a state diagram that comprehensively captures all possible states and transitions that a machine can take while responding to a stream or sequence of input symbols
Recognizer for “Regular Languages”
Deterministic Finite Automata (DFA) The machine can exist in only one state at any given
time
Non-deterministic Finite Automata (NFA) The machine can exist in multiple states at the same
time
13
DETERMINISTIC FINITE AUTOMATA -
DEFINITION
A Deterministic Finite Automaton (DFA) consists of: Q ==> a finite set of states
∑ ==> a finite set of input symbols (alphabet)
q0 ==> a start state
F ==> set of final states
δ ==> a transition function, which is a mapping between Q x ∑ ==> Q
A DFA is defined by the 5-tuple: {Q, ∑ , q0,F, δ }
14
28/10/2013
8
WHAT DOES A DFA DO ON READING AN
INPUT STRING?
Input: a word w in ∑*
Question: Is w acceptable by the DFA?
Steps: Start at the “start state” q0
For every input symbol in the sequence w do Compute the next state from the current state, given
the current input symbol in w and the transition function
If after all symbols in w are consumed, the current state is one of the final states (F) then accept w;
Otherwise, reject w.
15
REGULAR LANGUAGES
Let L(A) be a language recognized by a DFA A.
Then L(A) is called a “Regular Language”.
16
28/10/2013
9
EXAMPLE #1
Build a DFA for the following language: L = {w | w is a binary string that contains 01 as a
substring}
Steps for building a DFA to recognize L: ∑ = {0,1}
Decide on the states: Q
Designate start state and final state(s)
δ: Decide on the transitions:
Final states == same as “accepting states”
Other states == same as “non-accepting states”
17
DFA FOR STRINGS CONTAINING 01
18
q0
start q1
0
Regular expression: (0|1)*01(0|1)*
1 0,1 0
1
q2
Final
state
• What if the language allows
empty strings?
• What makes this DFA deterministic? • Q = {q0,q1,q2}
• ∑ = {0,1}
• start state = q0
• F = {q2}
• Transition table
q2 q2 *q2
q2 q1 q1
q0 q1 q0
1 0
state
s
symbols
28/10/2013
10
EXAMPLE #2
Clamping Logic: A clamping circuit waits for a ”1” input, and turns on
forever. However, to avoid clamping on spurious noise, we’ll design a DFA that waits for two consecutive 1s in a row before clamping on.
Build a DFA for the following language: L = { w | w is a bit string which contains the substring 11}
State Design: q0 : start state (initially off), also means the most recent
input was not a 1
q1: has never seen 11 but the most recent input was a 1
q2: has seen 11 at least once 19
EXAMPLE #3
Build a DFA for the following language:
L = { w | w is a binary string that has
even number of 1s}
?
20
28/10/2013
11
LANGUAGE OF A DFA
A DFA A accepts string w if there is a path from q0 to an accepting (or final) state that is labeled by w
i.e., L(A) = { w | δ(q0,w) F }
I.e., L(A) = all strings that lead to a final state from q0
21
NON-DETERMINISTIC FINITE AUTOMATA
(NFA)
A Non-deterministic Finite Automaton (NFA)
is of course “non-deterministic”
Implying that the machine can exist in more than one state
at the same time
Transitions could be non-deterministic
22
qi
1
1
qj
qk
… • Each transition function therefore
maps to a set of states
28/10/2013
12
NON-DETERMINISTIC FINITE AUTOMATA
(NFA)
A Non-deterministic Finite Automaton (NFA) consists of: Q ==> a finite set of states
∑ ==> a finite set of input symbols (alphabet)
q0 ==> a start state
F ==> set of final states
δ ==> a transition function, which is a mapping between Q x ∑ ==> subset of Q
An NFA is also defined by the 5-tuple: {Q, ∑ , q0,F, δ }
23
FA WITH -TRANSITIONS
We can allow explicit -transitions in finite automata i.e., a transition from one state to another
state without consuming any additional input symbol
Makes it easier sometimes to construct NFAs
Definition: -NFAs are those NFAs with at least one explicit -transition defined.
-NFAs have one more column in their transition table
24
28/10/2013
13
EXAMPLE OF AN -NFA
25
L = {w | w is empty, or if non-empty will end in 01}
δE 0 1
*q’0 Ø Ø {q’0,q0}
q0 {q0,q1} {q0} {q0}
q1 Ø {q2} {q1}
*q2 Ø Ø {q2}
ECLOSE(q’0)
ECLOSE(q0)
start
q0 q1
0
0,1
1 q2
q’0
ECLOSE(q1)
ECLOSE(q2)
HOW TO USE AN NFA?
Input: a word w in ∑*
Question: Is w acceptable by the NFA?
Steps: Start at the “start state” q0
For every input symbol in the sequence w do
Determine all possible next states from all current states, given the current input symbol in w and the transition function
If after all symbols in w are consumed and if at least one of the current states is a final state then accept w;
Otherwise, reject w.
26
28/10/2013
14
ADVANTAGES & CAVEATS FOR NFA
Great for modeling regular expressions
String processing - e.g., grep, lexical analyzer
Could a non-deterministic state machine
be implemented in practice?
A parallel computer could exist in multiple
“states” at the same time
Probabilistic models could be viewed as
extensions of non-deterministic state machines
(e.g., toss of a coin, a roll of dice) 27
DIFFERENCES: DFA VS. NFA
28
DFA
1. All transitions are deterministic Each transition leads to
exactly one state
2. For each state, transition on all possible symbols (alphabet) should be defined
3. Accepts input if the last state is in F
4. Sometimes harder to construct because of the number of states
5. Practical implementation is feasible
NFA
1. Some transitions could be non-deterministic A transition could lead to a
subset of states
2. Not all symbol transitions need to be defined explicitly (if undefined will go to a dead state – this is just a design convenience, not to be confused with “non-determinism”)
3. Accepts input if one of the last states is in F
4. Generally easier than a DFA to construct
5. Practical implementation has to be deterministic (convert to DFA) or in the form of parallelism
But, DFAs and NFAs are equivalent in their power to capture langauges !!
28/10/2013
15
EXAMPLE OF AN -NFA
Simulate for w=101:
29
L = {w | w is empty, or if non-empty will end in 01}
δE 0 1
*q’0 Ø Ø {q’0,q0}
q0 {q0,q1} {q0} {q0}
q1 Ø {q2} {q1}
*q2 Ø Ø {q2}
ECLOSE(q’0)
ECLOSE(q0)
start
q0 q1
0
0,1
1 q2
q’0
q0’
q0 q0’
q1
0
q2
1
q0
1
Ø
1
x
To simulate any transition:
Step 1) Go to all immediate destination states.
Step 2) From there go to all their -closure states as well.
EXAMPLE OF ANOTHER -NFA
Simulate for w=101:
?
30
δE 0 1
*q’0 Ø Ø {q’0,q0,q3}
q0 {q0,q1} {q0} {q0,q3}
q1 Ø {q2} {q1}
*q2 Ø Ø {q2}
q3 Ø {q2} {q3}
start
q0 q1
0
0,1
1 q2
q’0
q3
1
To simulate any transition:
Step 1) Go to all immediate destination states.
Step 2) From there go to all their -closure states as well.
28/10/2013
16
EQUIVALENCE
Definition
Two FSA are equivalente if recognize the same
languda
Minimum-FSA
The FSA (in the set of equivalent FSA) with
minimum number of states
MINIMIZZARE IL NUMERO DI STATI PER
UN DFA
Dividere gli stati in due insiemi:
› G1 : insiemi di stati finali
› G2 : insieme di stati non finali states Per ogni nuovo gruppo G
› Partizionare G in sottogruppo take che gli stati s1 e s2 sono nello stesso gruppo iff
per tutti I simboli di input a, gli stati s1 e s2 hanno transizioni a stati nello stesso gruppo.
Lo stato iniziale del DFA minimizzato è il gruppo contenente lo stato inziale del DFA originale.
GLi stati finiale del DFA minimizzato sono I gruppi contenente gli stati finiali del DFA originale.
28/10/2013
17
ESEMPIO SI MINIMIZZAZIONE
b a
a
a
b
b
3
2
1
G1 = {2}
G2 = {1,3}
G2 cannot be partitioned because
move(1,a)=2 move(1,b)=3
move(3,a)=2 move(2,b)=3
So, the minimized DFA (with minimum states)
{1,3}
a
a
b
b
{2}
MINIMIZING DFA – ESEMPIO
b
b
b
a
a
a
a
b 4
3
2
1 Groups: {1,2,3} {4}
a b 1->2 1->3
2->2 2->3
3->4 3->3
{1,2} {3} no more partitioning
So, the minimized DFA
{1,2}
{4}
{3}
b
a
a
a
b
b
28/10/2013
18
EQUIVALENCY OF DFA, NFA, -
NFA
Theorem: A language L is accepted by some -
NFA if and only if L is accepted by some
DFA
Implication:
DFA ≡ NFA ≡ -NFA
(all accept Regular Languages)
35
ELIMINATING -TRANSITIONS
Let E = {QE,∑,δE,q0,FE} be an -NFA
Goal: To build DFA D={QD,∑,δD,{qD},FD} s.t. L(D)=L(E)
Construction: 1. QD= all reachable subsets of QE factoring in -closures
2. qD = ECLOSE(q0)
3. FD=subsets S in QD s.t. S∩FE≠Φ
4. δD: for each subset S of QE and for each input symbol a∑:
Let R= U δE(p,a) // go to destination states
δD(S,a) = U ECLOSE(r) // from there, take a union of all their -closures
36
p in s
r in R
28/10/2013
19
CLOSURE
-closure of a state q, ECLOSE(q), is the set of all states (including itself) that can be reached from q by repeatedly making an arbitrary number of -transitions.
void close(NFASet S) {
while (x in S and x →y and y notin S) {
S = S U {y}
}
} Per calcolare la -chiusura di un insieme s:
› Aggiungere s alla -chiusura
› Se t appartiene alla -chiusura(s) e esiste una mossa da t a u, aggiungere u alla chiusura e continuare fino a quando possibile
EQUIVALENCE OF DFA & NFA
Theorem: A language L is accepted by a DFA if and
only if it is accepted by an NFA.
Proof: 1. If part:
Prove by showing every NFA can be converted to an equivalent DFA (in the next few slides…)
2. Only-if part is trivial: Every DFA is a special case of an NFA where each
state has exactly one transition for every input symbol. Therefore, if L is accepted by a DFA, it is accepted by a corresponding NFA. 38
28/10/2013
20
BUILT DSA EQUIVALENT TO A NFA
Two step
Elimination of mosse
Elimination of non determinism
One step
COSTRUZIONE DI UN DSA DA UN NFA
Eliminazione delle epsilon mosse
N (Q, T, , q0, F) automa con epsilon mosse
N’ (Q, T, ’, q0, F’) automa senza epsilon mosse
con F’ = F q0 se -chiusura(q0) contiene uno stato di F
F’ = F altrimenti
per ogni a in T esiste ’(q,a) se esiste (q,a) oppure esiste uno
stato p tale che (p,a), p= (q, )
28/10/2013
21
COSTRUZIONE DI UN DSA DA UN NFA
Eliminazione delle transizioni non
deterministiche
N (Q, T, , q0, F) automa non deterministico
senza epsilon mosse
M (Q’, T, ’, q’0, F’) automa deterministico
dove
Q’ = 2Q
F’ = {p Q’ | p contiene uno stato appartenente a F}
q’0 = [q0 ]
p Q’ e a T
’(p,a) = {s | s (q,a) F }
ESEMPIO
a a
a
a
b
b
2
3 4
5
1
28/10/2013
22
ESEMPIO
a a
a
a
b
b
2
3 4
5
a
1
a
ESEMPIO
a a
a
a
b
b
2
3 4
5
a
1
a
28/10/2013
23
ESEMPIO
a b
a
a
a
b
[2]
[4,5] [2,4]
[5]
a
[1]
[2,3,4]
b
CONVERSIONE DI UN NFA IN UN DFA
Inserire -closure({s0}) come stato non marcato in DS
while (c’è uno stato non marcato S1 in DS) do {
marcare S1
per ogni simbolo di ingresso a do {
S2 -closure(move(S1,a))
if (S2 DS)
aggiungere S2 in DS come stato non marcato
transfunc[S1,a] S2
}
}
Uno stato S è finale (o accettante) nel DS se contiene uno stato
finale in NFA
Lo stato iniziale e -closure({s0}
28/10/2013
24
DFA MakeDeterministic(NFA N) { DFA D; NFASet T D.StartState = { N.StartState } close(D.StartState) D.States = { D.StartState } while (è possibile aggiungere
stati/transizioni){ S in D.States and c in T = {y in N.States : x→c y per qualche
x in S} close(T); if (T notin D.States) { D.States = D.States U {T} D.Trans = D.Trans U {S →c T} } } D.AcceptingStates = { S in D.States : uno
stato di accettazione di N in S} }
ESEMPIO DI CONVERSIONE DI UN NFA IN
UN DFA
b
a
a 0 1
3
4 5
2
7 8 6
28/10/2013
25
ESEMPIO DI CONVERSIONE DI UN NFA IN
UN DFA
S0 = -closure({0}) = {0,1,2,4,7} S0 in DS come stato non marcato
marcare S0
-closure(move(S0,a)) = -closure({3,8}) = {1,2,3,4,6,7,8} = S1 S1 in DS
-closure(move(S0,b)) = -closure({5}) = {1,2,4,5,6,7} = S2 S2 in DS
transfunc[S0,a] S1 transfunc[S0,b] S2
marcare S1
-closure(move(S1,a)) = -closure({3,8}) = {1,2,3,4,6,7,8} = S1
-closure(move(S1,b)) = -closure({5}) = {1,2,4,5,6,7} = S2
transfunc[S1,a] S1 transfunc[S1,b] S2
marcare S2
-closure(move(S2,a)) = -closure({3,8}) = {1,2,3,4,6,7,8} = S1
-closure(move(S2,b)) = -closure({5}) = {1,2,4,5,6,7} = S2
transfunc[S2,a] S1 transfunc[S2,b] S2
ESEMPIO DI CONVERSIONE DI UN NFA IN
UN DFA
S0 è lo stato iniziale dato che 0 membro di S0={0,1,2,4,7}
S1 è lo stato di accettazione (stato finale) del DFA dato che 8 appartiene a S1 = {1,2,3,4,6,7,8}
b
a
a
b
b
a
S1
S2
S0
28/10/2013
26
ESEMPIO
NFA TO DFA BY SUBSET
CONSTRUCTION
Let N = {QN,∑,δN,q0,FN}
Goal: Build D={QD,∑,δD,{q0},FD} s.t.
L(D)=L(N)
Construction:
1. QD= all subsets of QN (i.e., power set)
2. FD=set of subsets S of QN s.t. S∩FN≠Φ
3. δD: for each subset S of QN and for each input
symbol a in ∑:
δD(S,a) = U δN(p,a)
52 p in s
28/10/2013
27
NFA TO DFA CONSTRUCTION:
EXAMPLE
L = {w | w ends in 01}
53
q0 q1 0
0,1
q2 1
NFA:
δN 0 1
q0 {q0,q1} {q0}
q1 Ø {q2}
*q2 Ø Ø
DFA:
δD 0 1
Ø Ø Ø
{q0} {q0,q1} {q0}
{q1} Ø {q2}
*{q2} Ø Ø
{q0,q1} {q0,q1} {q0,q2}
*{q0,q2} {q0,q1} {q0}
*{q1,q2} Ø {q2}
*{q0,q1,q2} {q0,q1} {q0,q2}
1. Determine transitions
δD 0 1
{q0} {q0,q1} {q0}
{q0,q1} {q0,q1} {q0,q2}
*{q0,q2} {q0,q1} {q0}
{q0}
1
0 {q0,q1}
1 {q0,q2}
0
0
1
Idea: To avoid enumerating all of
power set, do
“lazy creation of states”
2. Retain only those states
reachable from {q0}
0. Enumerate all possible subsets
NFA TO DFA: REPEATING THE
EXAMPLE USING LAZY CREATION
L = {w | w ends in 01}
54
q0 q1 0
0,1
q2 1
NFA:
δN 0 1
q0 {q0,q1} {q0}
q1 Ø {q2}
*q2 Ø Ø
DFA:
δD 0 1
{q0} {q0,q1} {q0}
{q0,q1} {q0,q1} {q0,q2}
*{q0,q2} {q0,q1} {q0}
{q0}
1
0 {q0,q1}
1 {q0,q2}
0
0
1
Main Idea:
Introduce states as you go
(on a need basis)
28/10/2013
28
A BAD CASE WHERE
#STATES(DFA)>>#STATES(NFA)
L = {w | w is a binary string s.t., the kth symbol from its end is a 1}
NFA has k+1 states
But an equivalent DFA needs to have at least 2k states
(Pigeon hole principle) m holes and >m pigeons
=> at least one hole has to contain two or more pigeons
55
A FEW SUBTLE PROPERTIES OF DFAS AND
NFAS
The machine never really terminates.
It is always waiting for the next input symbol or making
transitions.
The machine decides when to consume the next symbol
from the input and when to ignore it.
(but the machine can never skip a symbol)
=> A transition can happen even without really consuming
an input symbol (think of consuming as a free token)
A single transition cannot consume more than one symbol. 56
28/10/2013
29
EXAMPLE: -NFA DFA
57
L = {w | w is empty, or if non-empty will end in 01}
start
q0 q1
0
0,1
1 q2
q’0
δE 0 1
*q’0 Ø Ø {q’0,q0}
q0 {q0,q1} {q0} {q0}
q1 Ø {q2} {q1}
*q2 Ø Ø {q2}
δD 0 1
*{q’0,q0}
…
EXAMPLE: -NFA DFA
58
L = {w | w is empty, or if non-empty will end in 01}
start
q0 q1
0
0,1
1 q2
q’0
δE 0 1
*q’0 Ø Ø {q’0,q0}
q0 {q0,q1} {q0} {q0}
q1 Ø {q2} {q1}
*q2 Ø Ø {q2}
δD 0 1
*{q’0,q0} {q0,q1} {q0}
{q0,q1} {q0,q1} {q0,q2}
{q0} {q0,q1} {q0}
*{q0,q2} {q0,q1} {q0}
{q’0, q0}
0
start
{q0,q1} {q0,q2} 1
0
q0
1
1
0
0
1
union unio
n
28/10/2013
30
Regular Grammar FSA
FSA Regular Grammar
Regular Expression Regular Grammar
Regular Grammar Regular Expression
Regular Expression FSA
FSA Regular Expression
28/10/2013
31
GRAMMATICHE REGOLARI - AUTOMI A
STATI FINITI (FSA)
FSA(Q, T, , q0, F)
G(V, T, P, S)
Algoritmo di costruzione
› V = Q
› S = q0
› q Q, a T se una transizione etichettata
con a da q a r Q
› allora esiste in P la produzione q a r
› q F, una produzione q
Dove Q = insieme degli stati
T insieme di simboli terminali
insieme delle transizioni,
q0 = stato iniziali,
F = insieme stati finali
GRAMMATICHE REGOLARI - AUTOMI A
STATI FINITI (FSA)
Esempio
Espressione regolare : a*·b·c*
Grammatica regolare:
V = {S, A, C}
T = {a, b, c}
P = { S A
A aA
A bC
C cC
C }
S = {S}
a
P0 b
P1
c
28/10/2013
32
ESEMPIO
V = {S}
T = {a, b}
P = {S
S abS
S = {S}
P1
a
b
P0
ESEMPIO Grammatica per il linguaggio dei numeri interi V = {S,D} T = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} P = {S D S D S D 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 } S = {S} Grammatica per il linguaggio dei numeri interi (non sono accettati
numeri come 04) V = {S,D} T = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} P = {S D S 0 S D S D 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 } S = {S}
0,1,2,3,…9
0,1,2,3…9 P1 P0
1,2,3,…9
0,1,2,3…9 P1 P0
P2
0
28/10/2013
33
ESEMPIO
Esempio
V = {S}
T = {a, b}
P = {S
S abS
S = {S}
Il seguente FSA riconosce a*b*
non anbn
La ricorsione è necessaria per modellare anbn
(Cioè non è un FSA
b a
P0 b
P1
COSTRUZIONE DEL RICONOSCITORE DI
UN FSA
lettera = a .. z, A..Z
cifra = 0..9
Del è un qualsiasi carattere diverso da a..z e 0..9
0
2
1 lettera | _
lettera | cifra
Del
28/10/2013
34
2
1
0
Error
1
1
Error
1
Error
a..z 0..9
Error
Error
1
-
Error
2
Error
del
SI
NO
NO
FINALI
NFA (ESEMPIO)
1 0 2 a b
start
a
b
0 rappresenta lo stato iniziale s0
{2} è l’insieme degli stati finali F
= {a,b}
S = {0,1,2}
La funzione di transizione e descritta
dalla tabella
a b
0 {0,1} {0}
1 _ {2}
2 _ _
Grafo di transizione di un NFA
Il linguaggio riconosciuto da questo NFA è
(a | b) * a b
28/10/2013
35
ESEMPIO: COMMENTI C++
ESPRESSIONI REGOLARI - GRAMMATICA
REGOLARE
Esiste una corrispondenza biunivoca tra linguaggio regolare ed espressione regolare
Esempio
Espressione regolare : a*·b·c*
Regular Grammar:
V = {S, A, C}
T = {a, b, c}
P = { S A
A aA
A bC
C cC
C }
S = {S}
28/10/2013
36
ESEMPIO
a
A b
B C
c
b
d
Q = {A, B, C}
T = {a, b, c, d}
F = {B, C}
q0 = A
A = (A,a), B = (A,b), C = (B,c),
C = (C,d), A = (C,b)
V = {A, B, C}
T = {a, b, c, d}
B , C
A a A, A a B, B c C, C d C, C b A
S = A
ESEMPIO
Linea di commento che inizia con “//” a termina con “eol”
V = {S, A, C}
T = {/, c, e}
P = { S // A
A c A
A e}
e = eol
c = qualsiasi carattere
E = // c* e
P0 P1
P1
P2
-
-
e
c
28/10/2013
37
CONVERSIONE DI UNA
ESPRESSIONE REGOLARE IN UN
AUTOMA A STATI FINITI
La conversione di una espressione regolare in
un NFA produce un NFA tale che ogni stato ho
solo uno o due successori ed ha un unico stato
finale
Un espressione regolare può essere convertito in
un NFA automaticamente utilizzando le regole
di Thomson’s.
› Queste regole sono semplici e sistematiche
› Non è detto che sia uil metodo più efficente per
effettuare la conversione
› Garantisceh che il risultante NFA abbia uno stato
iniziale e uno stato finale.
FORME BASE
Per , si costruisce il seguente NFA
Per a in T, si costruisce il seguente NFA i f
start
i f a start
28/10/2013
38
RECURSIVE RULES
Detti N(s) e N(t) gli NFA for s and t.
NFA N(s|t):
i f start
N(s)
N(t)
RECURSIVE RULES
NFA N(st):
i f start
N(s) N(t)
28/10/2013
39
RECURSIVE RULES
NFA N(s*)
NFA N((s)) = N(s)
i f start
N(s)
THOMSON’S CONSTRUCTION (EXAMPLE -
(A|B) * A )
a: a
b
b:
(a | b)
a
b
b
a
(a|b) *
b
a
a (a|b) * a
28/10/2013
40
CONVERSIONE DI UNA ESPRESSIONE
REGOLARE DIRETTAMENTE IN UN DFAS
E’ possibile convertire direttamente una espressione
regolare in un DFA (senza creare prima un NFA).
1. Concatenare l’espressione con un simbolo speciale #.
r (r)# “espressione regolare aumentata”
2. Creare un albero sintattico della “espressione
regolare aumentata”
3. In tale albero sintattico tutti gli elementi del
vocabolario (più # e ) sono delle foglie dell’albero
4. Numerare con “position number” tutti gli elementi del
vocabolario (più # e )
REGULAR EXPRESSION DFA (CONT.)
(a|b) * a (a|b) * a # “espressione regolare aumentata”
*
|
b
a
#
a
1
4
3
2
Albero sintattico di (a|b) * a #
• Ogni simbolo e numerato (positions)
• Ogni simbolo è una foglia
• inner nodes sono operatori
28/10/2013
41
FOLLOWPOS
Definiamo la funzione followpos
followpos(i) -- è l’insime di posizioni che seguono una
posizione I nella stringa generata dall’espressione regolare
aumentata
Esempio, ( a | b) * a #
1 2 3 4
followpos(1) = {1,2,3}
followpos(2) = {1,2,3}
followpos(3) = {4}
followpos(4) = {}
FIRSTPOS, LASTPOS, NULLABLE
Per valutare followpos, noi abbiamo bisogno di
tre funzioni che sono definite per I nodi
dell’albero
firstpos(n) -- l’insieme di posizioni del first
simbolo della stringa generata dalla sub-
expression di radice n.
lastpos(n) -- l’insieme di posizioni del last
simbolo della stringa generata dalla sub-
expression di radice n.
nullable(n) -- true se la stringa vuota
appartine alla sub-expression di radice n, false
altrimenti
28/10/2013
42
COME VALUTARE FIRSTPOS, LASTPOS,
NULLABLE
n nullable(n) firstpos(n) lastpos(n)
foglia true
Foglia i false {i} {i}
|
c1 c2
nullable(c1) or
nullable(c2)
firstpos(c1)
firstpos(c2)
lastpos(c1)
lastpos(c2)
c1 c2
nullable(c1) and
nullable(c2)
if (nullable(c1))
firstpos(c1)firstpos(c2)
else firstpos(c1)
if (nullable(c2))
lastpos(c1)
lastpos(c2)
else lastpos(c2)
*
c1
true firstpos(c1) lastpos(c1)
COME VALUTARE FOLLOWPOS
Due regole per calcolare la funzione followpos:
1. Se n un concatenation-node ()con figlio sinistro
c1 e figlio destro c2, e i lastpos(c1), allora
firstpos(c2) followpos(i).
2. Se n è un star-node (*), e i lastpos(n), allora
firstpos(n) followpos(i).
Se firstpos e lastpos sono stati calcolati per ogni
nodo, followpos può essere calcolato con una
visita depth-first dell’albero sintattico.
28/10/2013
43
EXAMPLE -- ( A | B) * A #
*
| b
a
#
a
1
4
3
2 {1} {1}
{1,2,3}
{3}
{1,2,3}
{1,2}
{1,2}
{2}
{4}
{4}
{4} {3}
{3} {1,2}
{1,2}
{2}
green – firstpos
blue – lastpos
followpos(1) = {1,2,3}
followpos(2) = {1,2,3}
followpos(3) = {4}
followpos(4) = {}
• Dopo aver calcolato followpos possiamo creare il DFA
per l’espressione regolare.
ALGORITMO (RE DFA)
Creare l’albero sintattico di (r) # Calcolare le funzioni followpos, firstpos, lastpos, nullable Porre firstpos(root) in S (S è l’insieme degli stati del DFA) come stato non
marcato. while ( s S non marcato) {
s
for each simbolo a { s’
followpos(s1) ... followpos(sn)
let s1,...,sn posizioni in s e simboli per quelle posizioni sono a
move(s,a) s’
if (s’ non e vuoto e non in S put s’ into S come stato non marcato.
the start state of DFA is firstpos(root) the accepting states of DFA are all states containing the position of #
28/10/2013
44
ESEMPIO -- ( A | B) * A #
followpos(1)={1,2,3} followpos(2)={1,2,3} followpos(3)={4} followpos(4)={}
S1=firstpos(root)={1,2,3} marcare S1 a: followpos(1) followpos(3)={1,2,3,4}=S2 move(S1,a)=S2 b: followpos(2)={1,2,3}=S1 move(S1,b)=S1
marcare S2
a: followpos(1) followpos(3)={1,2,3,4}=S2 move(S2,a)=S2 b: followpos(2)={1,2,3}=S1 move(S2,b)=S1
start state: S1 accepting states: {S2}
S1 S2 a
b
b
a
EXAMPLE -- ( A | ) B C* #
followpos(1)={2} followpos(2)={3,4} followpos(3)={3,4} followpos(4)={}
S1=firstpos(root)={1,2}
marcare S1
a: followpos(1)={2}=S2 move(S1,a)=S2
b: followpos(2)={3,4}=S3 move(S1,b)=S3
marcare S2
b: followpos(2)={3,4}=S3 move(S2,b)=S3
marcare S3
c: followpos(3)={3,4}=S3 move(S3,c)=S3
start state: S1
accepting states: {S3}
S3
S2
S1
c
a b
b
28/10/2013
45
ESEMPI DI ESPRESSIONI REGOLARI
1. letter(letter|digit)*
2. digits = digit digit*
opt_frac = . digits |
opt_exp = (E(+|-| ) digits |
num = digits opt_frac opt_exp
ESERCIZI 1
Data la seguente espressione regolare
R = (a*|)*d*(a| | c*)*
1. Costruire l’automa minimo deterministico
2. Definire una grammatica regolare per lo stesso
linguaggio
28/10/2013
46
ESERCIZIO 2
Date le espressioni regolari r,s, u determinare,
giustificandolo, se valgono le seguenti proprietà
(u*(r*(s*)*)*)* = (r | (s|u)*)*
(u*|r*s*)* = (r |u |s)*
(u(ru | su | )*)* = (ur | us)* u*
ESERCIZIO 3
Dato il linguaggio dei numeri interi secondo lo
standard del linguaggio c costruire l’automa
minimo che riconosce tale linguaggio
28/10/2013
47
ESERCIZIO 4
Dato l’alfabeto = {a, b, c, d} sia L il linguaggio
tale che l’occorrenza del primo simbolo a implica
che la stringa nel seguito conterrà solo a e d.
Determinare una espressione regolare che generi
L ed un automa a stati finiti deterministico che
riconosca L.
Top Related