Lõplikud automaadid · 2019. 2. 25. · 1.2 Nondeterministic Finite Automata 7...

Post on 22-Jan-2021

6 views 0 download

Transcript of Lõplikud automaadid · 2019. 2. 25. · 1.2 Nondeterministic Finite Automata 7...

Leksiline analüüsLõplikud automaadid

1.2 Nondeterministic Finite Automata 7

A finite automaton is, in the abstract sense, a machine that has a finite numberof states and a finite number of transitions between these. A transition betweenstates is usually labelled by a character from the input alphabet, but we will also usetransitions marked with ε, the so-called epsilon transitions.

A finite automaton can be used to decide if an input string is a member in someparticular set of strings. To do this, we select one of the states of the automatonas the starting state. We start in this state and in each step, we can do one of thefollowing:

• Follow an epsilon transition to another state, or• Read a character from the input and follow a transition labelled by that character.

When all characters from the input are read, we see if the current state is markedas being accepting. If so, the string we have read from the input is in the languagedefined by the automaton.

We may have a choice of several actions at each step: We can choose betweeneither an epsilon transition or a transition on an alphabet character, and if there areseveral transitions with the same symbol, we can choose between these. This makesthe automaton nondeterministic, as the choice of action is not determined solelyby looking at the current state and input. It may be that some choices lead to anaccepting state while others do not. This does, however, not mean that the stringis sometimes in the language and sometimes not: We will include a string in thelanguage if it is possible to make a sequence of choices that makes the string leadto an accepting state.

You can think of it as solving a maze with symbols written in the corridors. Ifyou can find the exit while walking over the letters of the string in the correct order,the string is recognized by the maze.

We can formally define a nondeterministic finite automaton by:

Definition 1.1 A nondeterministic finite automaton consists of a set S of states.One of these states, s0 ∈ S, is called the starting state of the automaton and a subsetF ⊆ S of the states are accepting states. Additionally, we have a set T of transitions.Each transition t connects a pair of states s1 and s2 and is labelled with a symbol,which is either a character c from the alphabet ", or the symbol ε, which indicatesan epsilon-transition. A transition from state s to state t on the symbol c is writtenas sct .

Starting states are sometimes called initial states and accepting states can also becalled final states (which is why we use the letter F to denote the set of acceptingstates). We use the abbreviations FA for finite automaton, NFA for nondeterministicfinite automaton and (later in this chapter) DFA for deterministic finite automaton.

We will mostly use a graphical notation to describe finite automata. States aredenoted by circles, possibly containing a number or name that identifies the state.This name or number has, however, no operational significance, it is solely usedfor identification purposes. Accepting states are denoted by using a double circleinstead of a single circle. The initial state is marked by an arrow pointing to it fromoutside the automaton.

LugemiskontrollMillest koosneb mittedeterministlik lõplik automaat?

Lõpliku automaadi definitsioonDFA: Üleminekurelatsioon on (osaline) funktsioon S ⨉ Σ → S.

NFA: Üleminekurelatsioonis on mitu üleminekut sama olek-sisend paari jaoks.

a a

b

Automaadi keel!

a a

b

ab*a automaatKas tunneb ära sõne "abba"?

ab*a automaatJärgijäänud sõne: “abba”

Olek: q0 (algolek)

ab*a automaatJärgijäänud sõne: “bba”

Olek: q1

ab*a automaatJärgijäänud sõne: “ba”

Olek: q1

ab*a automaatJärgijäänud sõne: “a”

Olek: q1

ab*a automaatJärgijäänud sõne: “” Olek: q2 (lõppolek!)

NFA näideKus on mitte-determinism?

NFA epsilon-ülemineku näideKus on mitte-determinism?

DFA SimuleerimineKäsitis kirjutatud DFA…

def s0(inpStr):if inpStr == "": error()elif: inpStr == "a": return s1(inpStr[1:])else: error()

def s1(inpStr):if inpStr == "": error()elif: inpStr == "a": return s2(inpStr[1:])elif: inpStr == "b": return s1(inpStr[1:])else: error()

def s2(inpStr):if inpStr == "": return Trueelse: error()

a a

b

Tabeljuhitav DFA simulaatorPalju vabadust, kuidas tabelit implementeerida!

while do

endifthenelse

a a

b

a b

Kodutöö II osa kohta…• Hea uudis: olete ise proovinud asju kuidagi lahendada!

• Mure: kui kindlad olete (eurodes), et lahendus on korrektne?

• Mida selgemini ja ilusamini üritate programmeerida, seda rohkem hakkate programmeerimist nautima.

• See on siiski ülioluline, et progremmeerimine valmistaks rõõmu, kui teil on plaan sellel erialal tööle minna!

Mealy Masinhttp://www.jflap.org/tutorial/mealy/mealyMachines.html

http://www.agilemodeling.com/artifacts/stateMachineDiagram.htm

x ; yVäljundSisend

Tokeniseerimise näideSisend: + + k a + l - a EOF Väljund: [echo, echo, acc, acc, push, echo, acc, push, echo, acc, push]

Kõikide teiste tähtede puhul

¬TOK TOK OK

¬{+,�}/acc

{+,�}/push; echo

{+,�}/echo ¬{+,�}/acc

{EOF}/push

Väljundsõnade tõlgendamine• + + k a + l - a EOF [echo, echo, acc, acc, push, echo, acc, push, echo, acc, push]

• echo(c) lihtsalt tagastab uue tokeni c.

• acc(c) kogub antud tähte (accumulator += c).

• push() lisab akkumuleeritud sõne eraldi tokenina (+ tühjenda akku).

• Tulemus: [+, +, ka, +, l, -, a]

Automaadid elus• Paljude süsteemide all peitub

olekumasin.

• Eraldame süsteemist andmed välja (näiteks kasutajanimi).

• Juhtimisvoo on tihtipeale lõplik (kasutaja autentimise workflow).

• Google: UML state machine ja model-based testing.