Finite State Automata and Linear Grammars


Language Theory and Compilers

3rd lecture, May 10, 2019

Martin J. Dürst


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

Today's Schedule


Schedule for Next Week

May 15th (Wednesday, 1st period / 補講、、水曜日1限、E-202)
Implementation of lexical analysis, use of tools for lexical analysis
May 17
Applications of lexical analysis, exercises using tools for lexical analysis

About makeup classes: The material in the makeup class is part of the final exam. If you have another makeup class at the same time, please inform the teacher today.

補講について: 補講の内容は期末試験の対象。補講が別の授業とかぶる場合には今日申し出ること。


Types of Grammars

Grammar types are distinguished by restrictions on rewriting rules:

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

1. αAβαγβ, where α and β are sequences of 0 or more (non)terminals, and γ is a sequence of 1 or more (non)terminals:
Context-sensitive grammar, (Chomsky) type 1 grammar

2. Aγ, where γ is a sequence of 1 or more (non)terminals:
Context-free grammar, (Chomsky) type 2 grammar

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


Cygwin Download and Installation

(no need to submit, but bring your note PC with you if you have problems)
On your notebook PC, install cygwin (detailled instructions with screenshots).
Make sure you select/install all of gcc, flex, bison, diff, make and m4.


Checking flex, bison, gcc,... Installation

To check your installation of the various programs, start up a Cygwin Terminal session, and use the following commands to check the version of each software:


Summary of Last Lecture

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.


Plan for this Lecture

These all are equivalent, and define/accept regular languages


Finite State Automaton Example

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

Finite state automata are often represented with a state transition diagram


Arrow from outside: initial state
Circles: states
Double circles: accepting state(s)
Arrows with labels: transitions


Workings of a Finite State Automaton


Examples of Finite State Automata


State Transition Tables

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

The state transition table for our example automaton is:

a b
→A B 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 defined as a quintuple (Q, Σ, δ, q0, F)


Nondeterministic Finite Automata


ε Transition

(epsilon transition)


Example of NFA


Comparing DFAs and NFAs

Deterministic (DFA) Nondeterministic (NFA)
concurrently occupied states one single state multiple states (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 ε transition)


Equivalence of DFA and NFA


Example of Conversion from NFA to DFA

State transition table for the example NFA on an earlier slide:

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


Conversion from an NFA to an Equivalent DFA


Linear Grammar

Simple Rewriting Rules
Rule Shape Name
AcB right linear rule (nonterminal on the right)
ABc left linear rule (nonterminal on the left)
Ac constant rule

A left linear grammar is a grammar only using left linear rules and constant rules

A right linear grammar is a grammar only using right linear rules and constant rules

(in both cases, a special rule Sε is allowed)

Left linear grammars and right linear grammars are together called linear grammars (or regular grammars)

(a grammar that contains both left linear rules and right linear rules is not a linear grammar, but a kind of context-free grammar)


(Right) Linear Grammars and FSAs

Right linear grammars and NFAs correspond as follows (not considering ε transitions):

There is a similar correspondence for left linear grammars (imagine reading the input backwards)


Example of Linear Grammar and NFA


A → aB | bA

B → bA | aC | a

C → bA | aC | a


Conversion between Right Linear Grammar and NFA

From automaton to grammar:

From grammar to automaton:


Today's Summary

Callenge: Regular languages can be represented by state transition diagrams/tables of NFAs/DFAs, or with regular grammars, but a more compact representation is desirable



Deadline: May 14, 2019 (Tuesday!), 19:00

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

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

  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

    ε 0 1
    →S {B} {C} {A}
    A {} {B} {B, D}
    B {} {D} {}
    *C {B} {A} {S}
    D {} {A, B} {C}
  3. Create the state transition table of the DFA that is equivalent to the NFA in 2. (do not rename states)
  4. Check the versions of flex, bison, gcc, make, and m4 that you installed (no need to submit, but bring your computer to the next lecture if you have a problem)



Finite state automaton (FSA)
deterministic finite automaton (DFA)
Non-deterministic finite automaton (NFA)
(left/right) linear grammar
(左・右) 線形文法
regular grammar
state transition diagram
initial/start state
accepting/final state
state transition table
state transition function
同時 (な・に)
ε transition
ε 遷移
ε closure
ε 閉包
(left/right) linear rule
(左・右) 線形規則
constant rule
renaming (of states)