Finite State Automata

(有限オートマトン)

Language Theory and Compilers

3rd lecture, May 2, 2025

https://www.sw.it.aoyama.ac.jp/2025/Compiler/lecture3.html

Martin J. Dürst

AGU

© 2005-25 Martin J. Dürst 青山学院大学

 

Today's Schedule

 

Overall Schedule

May 28 (Wednesday), 09:00~10:30 (makeup lecture)

 

Summary of Last Lecture

 

Previous Homework 1

For the language L = { sg, ns, n },
list the 10 shortest words of L* (in lexicographic order).

Solution: ε, n, nn, nnn, nns, ns, nsg, nsn, sg, sgn

Additional problem (solution voluntary):
List all words of L* of length 4 (in lexicographic order).

Solution: nnnn, nnns, nnsg, nnsn, nsgn,
nsnn, nsns, nssg, sgnn, sgns, sgsg

 

Previous Homework 2

Using the grammar from the slide
"Example of Grammar and Derivation",
find 3 words produced by that grammar
(different from each other and from ccwwcc).

Give the full derivation for each word
(rule numbers and underlines not needed).

Guess and explain
what language this grammar defines.

Hint: If your guess is not simple,
maybe you have made a mistake.

 

Previous Homework 2 Solution

[Grammar: Scwc (1), ScPTc (2), RQPTc (3), TQPc (4), PQQP (5), cQcc (6), Pcwc (7), Pwww (8)]

Solution example (partial):

Scwc

ScPTccPQPTcccPQPQPccccQPPQPccccQPQPPccccQQPPPccccQQPPwccccQQPwwccccQQwwwcccccQwwwccccccwwwccc

S → ... → ccccwwwwcccc

Guess: The grammar defines the language with all the words of the form cwc, ccwwcc, cccwwwccc,...
i.e. cnwncn (n≥1).

 

Previous Homework 2 (Additional Problem)

Additional problem (solution voluntary): Prove or justify your guess

Example solution summary: The first derivation step will eliminate S. T will only appear once in any intermediate sequence, and will later disappear. After using (3) x times (x≥0) and using (4) one time, there will be a single c on the left, cx+2 on the right, and a sequence of x+1 Qs and x+2 Ps in the middle (c(PQ)x+1Pcx+2 ). Using (5), all Qs can be moved to the left, and all Ps to the right, giving cQx+1Px+2cx+2. Then using (7) and (8), the Ps are converted to ws, and using (6), the Qs are converted to cs, resulting cx+2wx+2cx+2. This is cnwncn, with n=x+2≥2. The case of n=1 is covered separately by rewriting rule (1).

 

Today's Schedule

 

Types of Grammars

Grammar types are distinguished
by restrictions on rewriting rules:
(α, β, γ are sequences of (non)terminals)

Type 0. No restrictions: Phrase structure grammar, (Chomsky) type 0 grammar

Type 1. αAβαγβ, where |α|≧0, |β|≧0, |γ|≧1:
Context-sensitive grammar (α and β are the context),
(Chomsky) type 1 grammar

Type 2. Aγ, where |γ|≧1:
Context-free grammar, (Chomsky) type 2 grammar

Type 3. AaB or Aa (alternative: ABa or Aa):
Regular grammar, (Chomsky) type 3 grammar

(for all types, Sε is also allowed)

 

Remarks on Homework 2

 

Language Type Overview

grammar type lanugage type automaton
phrase structure grammar (psg) 0 phrase structure language Turing machine
context-sensitive grammar (csg) 1 context-sensitive language linear-bounded automaton
context-free grammar (cfg) 2 context-free language push-down automaton
regular grammar (rg) 3 regular language finite state automaton

Regular languages are used for lexical analysis.

 

Language Type Overview

grammar type lanugage type automaton
phrase structure grammar (psg) 0 phrase structure language Turing machine
context-sensitive grammar (csg) 1 context-sensitive language linear-bounded automaton
context-free grammar (cfg) 2 context-free language push-down automaton
regular grammar (rg) 3 regular language finite state automaton

Regular languages are used for lexical analysis.

 

Today's Schedule

 

Finite State Automaton Example

(automaton (αὐτόματον) is Greek; plural: automata)

Finite state automata (FSA) are often represented with a state transition diagram

有限オートマトンの状態遷移図

Circles:
states (finite number!)

Arrow from outside:
initial state

Double circles:
accepting state(s)

Arrows with labels:
transitions

 

Workings of a Finite State Automaton

 

Questions

有限オートマトンの状態遷移図

Does the automaton accept ffgfg ? No!

Does the automaton accept fgfff ? Yes!

What language does the automaton accept?
Any sequence of f and g ending with 2 or more fs.

 

Examples of Finite State Automata

 

State Transition Tables

Finite state automata can be represented with a state transition table.

State transition table for our example:


f g
→A B A
B C A
*C C A

Leftmost column: state

Top row: input symbol

→: start state
(first state if not otherwise indicated)

*: accepting state(s)

Table contents: state after transition

 

Formal Definition of FSAs

A finite state automaton is a quintuple (Q, Σ, δ, q0, F):

 

Today's Schedule

 

Nondeterministic Finite Automata

 

Example of NFA

 

How an NFA Works

 

ε Transition

(epsilon transition)

 

Comparing DFAs and NFAs


Deterministic (DFA) Nondeterministic (NFA)
concurrently occupied states one state set of states
acceptance criterion current state is accepting state one of the occupied states is accepting state
ε transition prohibited allowed
type of transition function δ: Q × Σ
Q
δ: Q × (Σ ∪ {ε})
P(Q)

(there are also NFAs without ε transitions)

 

Equivalence of DFA and NFA

 

Today's Schedule

 

Example of Conversion
from NFA to DFA

State Transition Table for an NFA

ε 0 1
→S {B} {C} {A}
A {C} {} {D, B}
B {} {D} {A}
*C {} {D} {A, B}
D {} {A, B} {}

 

Conversion from NFA to DFA:
Algorithm Principle

Summary: Set of States ⇒ State

Conversion from NFA to DFA:
Steps

  1. Create a transition table for the DFA,
    starting with the set of start states
  2. For each set of states and each input, calculate the set of states that can be reached
  3. If there is a new set of states, copy it from the table contents to the leftmost column, and continue at 2.
  4. Label all sets of states that contain an accepting state of the NFA as accepting states of the DNA
  5. (not necessary) Rename sets of states with new labels

Conversion from NFA to DFA:
Conclusions

 

Minimization of DFAs

Task: Create the smallest DFA equivalent to a given DFA.

Overall idea: work backwards

  1. Separate states into two sets, accepting states and non-accepting states
  2. For each state, check which other states are reached for each input symbol
  3. Partition each set of states into sets that can reach the same set with the same input symobls
  4. Repeat 2. and 3. until there is no further change

Purpose of minimization:

 

Example of DFA Minimization

 

Efficient Implementation of a DFA

/* state transition table */
State   next_state[state_count][symbol_count];
Boolean final_state[state_count];         /* final state? */
State   current_state = start_state;
Symbol  next_symbol;

while ((next_symbol=getchar()) != EOF &&  /* end of input */
         current_state != no_state)       /* dead end */
    current_state = next_state[current_state][next_symbol];
if (final_state[current_state])
    printf("Input accepted!");
else
    printf("Input not accepted!");

 

Today's Summary

Challenge (for next lecture): DFAs and NFAs have complex descriptions. Are there simpler descriptions of regular languages?

 

Previous Homework 3:
Cygwin Download and Installation

(no need to submit, but contact me by e-mail
if you have any problems)

Install cygwin on your notebook computer
(detailled instructions with images).

Make sure that you select/install gcc (gcc-core), flex, bison, diff (diffutils), make, and m4 .

If you have an earlier cygwin installation,
make sure to check/update.

 

Checking flex, bison, gcc,... Installation

Start up a Cygwin Terminal session.

Check each software with the following commands:

 

Homework Submission

Deadline: May 7, 2025 (Wednesday), 18:40

Format: A4 single page (using both sides is okay; NO cover page, portrait orientation), easily readable handwriting (NO printouts), name (kanji and kana) and student number at the top right

Where to submit: Box in front of room O-529 (building O, 5th floor)

 

Homework

  1. Draw a state transition diagram for a finite state automaton that recognizes all inputs that (at the same time)
  2. Draw the state transition diagram for the NFA in the state transition table below
      ε O l
    →S {P} {Q} {E}
    E {} {P} {P, T}
    P {} {T} {}
    *Q {P} {} {S}
    T {} {E, P} {Q}
  3. Create the state transition table of the DFA that is equivalent to the NFA in homework 2 (do not rename states).
  4. Check the versions of flex, bison, gcc, diff, make, and m4 that you installed (see slide 4, no need to submit, but contact me by mail if you have a problem)

 

Glossary

Finite state automaton (FSA)
有限オートマトン
deterministic finite automaton (DFA)
決定性有限オートマトン
Non-deterministic finite automaton (NFA)
非決定性有限オートマトン
regular grammar
正規文法
state transition diagram
状態遷移図
transition
遷移
initial/start state
初期状態
accepting/final state
受理状態
accept
受理する
finite
有限
state transition table
状態遷移表
state transition function
動作関数
simultaneous(ly)
同時 (な・に)
ε transition
ε 遷移
ε closure
ε 閉包
equivalence
同等性
renaming (of states)
状態の書換え