RIPAL: Responsive and Intuitive Parsing for the Analysis of Language

Pages

Deterministic finite automata

Overview

A deterministic finite automaton (DFA for short) is a tool that can be used to recognize strings as part of a regular language.

Example of a DFA

The following is an example of a DFA:

Example DFA. This DFA contains 4 states - s0, s1, s2 and s3. s0 transitions to s1 on input symbol a. s1 transitions to s2 on input symbol b and s3 on input symbol c. s3 transitions to itself on input symbol d.

It contains 4 states (the circles) and 4 state transitions (the arrows between states).

State s0 is the initial state (indicated by the arrow coming from no other state), while s2 and s3 are accepting states (indicated by the double circle).

This DFA accepts the language represented by the following regular expression:

R = ab ∪ acd*

Anatomy

In general, a DFA consists of:

  1. A set of states, S, typically labelled s0 through sn - 1
    1. One state is the initial state, si - typically s0 by convention
    2. Any number of states can belong to the set of final states, Sf
      1. Sf ⊆ S
  2. A set of transitions, T, between states, consisting of a start state ss ∈ S, an end state sf ∈ S and a transition symbol t ∈ Z

In a DFA, we have a few inherent structural restrictions, as follows:

  1. T = T[si, t] = sf, a singular state
  2. t ∈ Z, a single symbol in our alphabet

The DFA parsing algorithm

To determine whether s = {σ1σ2...σn} belongs to the language recognized by DFA with states S, transitions T[si, t], initial state si:

Set scurrent = si For i = 1 to n symbol = σi If T[scurrent, symbol] does not exist Reject scurrent = T[scurrent, symbol] End for If scurrent is accepting Accept Otherwise Reject

Informally, we begin with our initial state, and transition to subsequent states for each symbol in the input.

We reject the language if we end on a non-accepting state at the end of our input or attempt an invalid transition. We accept if an accepting state is reached at end of input.

Parsing examples

Example

For s = acd

State Input symbol Action
s0 a Transition to s1
s1 c Transition to s3
s3 d Transition to s3
s3 $ (end of string) Accept (since s3 is an accepting state)

Example

For s = a

State Input symbol Action
s0 a Transition to s1
s1 $ (end of string) Reject (since s1 is not an accepting state)

Example

For s = b

State Input symbol Action
s0 b Reject (since no transition exists from s0 for symbol b)

GitHub Repository: https://github.com/bprollinson/ripal

Copyright © 2017 Brendan Rollinson-Lorimer