Docsity

CS 440 - Regular Languages: Understanding NFAs and DFAs - Prof. Alan Mattox Beckman, Papers of Programming Languages

Toggle sidebar
bg1
CS 440 — Regular Languages February 4, 2009
1 Introduction and Objectives
Part of the study of programming languages involves getting the computer to read the programs
we type.
•Be able to explain the problem of parsing.
•Know how to recognize a word using an NFA or a DFA.
•Know the difference between a DFA and an NFA.
•Be able to convert a NFA into a DFA, both by hand and in Haskell.
•Vocabulary to know: deterministic, nondeterministic, lexing, scanning, accept state, transi-
tion.
•Know the syntax of regular expressions.
•Know how to convert between regular expressions and state machines.
•Know the limitations of regular languages.
Computer programs are entered as a stream of ASCII (usually) characters. For example:
4+ifx>4then5else0
We want to convert them into an Abstract Syntax Tree:
PlusExp
IntExp 4 IfExp
GtExp
VarExp xIntExp 4
IntExp 5IntExp 0
We can define this tree in Haskell directly like this:
1data Exp =PlusExp Exp Exp
2|VarExp String
3|IntExp Int
4|IfExp Exp Exp
5|GtExp Exp Exp
6
7t=PlusExp
8(IntExp 4)
9(IfExp (GtExp (VarExp "x") (IntExp 4))
10 (IntExp 5)
11 (IntExp 0))
Mattox Beckman Page 1 Illinois Institute of Technology
pf3
pf4
pf5
pf8

Partial preview of the text

Download CS 440 - Regular Languages: Understanding NFAs and DFAs - Prof. Alan Mattox Beckman and more Papers Programming Languages in PDF only on Docsity!

1 Introduction and Objectives

Part of the study of programming languages involves getting the computer to read the programs we type.

  • Be able to explain the problem of parsing.
  • Know how to recognize a word using an NFA or a DFA.
  • Know the difference between a DFA and an NFA.
  • Be able to convert a NFA into a DFA, both by hand and in Haskell.
  • Vocabulary to know: deterministic, nondeterministic, lexing, scanning, accept state, transi- tion.
  • Know the syntax of regular expressions.
  • Know how to convert between regular expressions and state machines.
  • Know the limitations of regular languages.

Computer programs are entered as a stream of ASCII (usually) characters. For example:

4 + if x > 4 then 5 else 0

We want to convert them into an Abstract Syntax Tree: PlusExp

IntExp 4 IfExp

GtExp

VarExp xIntExp 4

IntExp 5IntExp 0

We can define this tree in Haskell directly like this: 1 data Exp = PlusExp Exp Exp 2 | VarExp String 3 | IntExp Int 4 | IfExp Exp Exp 5 | GtExp Exp Exp 6 7 t = PlusExp 8 (IntExp 4) 9 (IfExp (GtExp (VarExp "x") (IntExp 4)) 10 (IntExp 5) 11 (IntExp 0))

  • Start with characters and convert them into words.
    • (a.k.a. tokens)
    • This is called lexing, scanning, or tokenizing.
  • Convert the tokens into a tree.
    • This is called parsing.

2 State Machines

A state machine is a set of states along with transitions between them. Example state machine:

  • It consists only of the letters a and b.
  • The letter b occurs twice, once at the very end.
  • We can use a state machine...

start q 0 b q 1 q 2

a

b

a

  • q 0 is the start state.
  • The transitions consume a character of input.
  • q 2 is an accepting state.
    • You can have more than one accepting state.
    • You can have transitions out of an accepting state.

Example 1 What kind of strings will this state machine accept?

start q^0 q^1

q 2 q 3

start^ q 0

q 1 q 2

q 3 q 4

q 5

a

a

x

b

d

d

c

Activity!

  1. Convert Example 2 to a DFA
  2. Draw an NFA that accepts even binary numbers.
  3. Draw a DFA that accepts even binary numbers.
  4. Convert this example into a DFA.
    • Regular Languages were developed by Noam Chomsky in his quest to describe human lan- guages.
  • Computer Scientists like them because they are able to describe “words” or “tokens” very easily.

Bad Examples:

Integers a bunch of digits

Reals an integer, a dot, and an integer

Past Tense English Verbs a bunch of letters ending with “ed”

Proper Nouns a bunch of letters, the first of which must be capitalized

  • We need something a bit more formal if we want to communicate properly.
  • We will use a pattern (or a regular expression) to represent the kinds of words we want to describe.
  • As it will turn out, these expressions will correspond to NFAs.
  • Kinds of patterns we will use:
    • Single letters
    • Repetition
    • Grouping
    • Choices

3.1 Single Letters

  • To match a single character, just write the character.
  • To match the letter “a”...
    • Regular Expression: a
    • State machine:

start q 0 a q 1

  • To match the character “8”...
    • Regular Expression: 8
    • State machine:

start q 0 8 q 1

3.4 Grouping

  • To groups things together, use parenthesis.
  • To match one or more copies of the word “hi”...
    • Regular expression: (hi)+
    • State machine:

start q 0 h^ q 1 i q 2



3.5 Choice

  • To make a choice, use the vertical bar (also called “pipe”).
  • To match an a or a b...
    • Regular expression: a|b
    • State machine:

start^ q 0

q 1

q 2

a

b

3.6 Examples

Expression (Some) Matches (Some) Rejects aba aa, aba, abbba ba, aaba, abaa (0|1) any binary number,  (0|1)+ any binary number empty string (0|1)0 even binary numbers (aa)a odd number of as (aa)a(aa) odd number of as (aa|bb)((ab|ba)(aa|bb)(ab|ba)(aa|bb)) even number of as and b

3.7 Some Notational Shortcuts

  • A range of characters: [Xa-z] matches X and between a and z (inclusively).
  • Any character at all:.
  • Escape: \

Expression (Some) Matches [0-9]+ integers X.Y anything at all between an X and a Y [0-9].[0-9]* floating point numbers (positive, without exponents)

3.8 Things to know...

  • They are greedy. X.*Y will match XabaaYaababY entirely, not just XabaaY.
  • They cannot count very well.
    • They can only count as high as you have states in the machine.
    • This regular expression matches some primes: aa|aaa|aaaaa|aaaaaaa
    • You cannot match an infinite number of primes.
    • You cannot match “nested comments”. (*.**)

4 Activity

4.1 Problems I

Write a regular expression for the following kinds of words

  • hexadecimal numbers
  • numbers in scientific notation
  • file names ending in .C
  • numbers between 0 and 255

Describe in English the following regular expressions

  • [a-zA-Z][a-zA-Z0-9]+
  • [a-z]*(es|ed|ing)
  • <[a-z0-9]+@[a-z0-9]+(.[a-z0-9]+)+>