




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Part of the study of programming languages involves getting the computer to read the programs we type.
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))
A state machine is a set of states along with transitions between them. Example state machine:
start q 0 b q 1 q 2
a
b
a
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!
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
start q 0 a q 1
start q 0 8 q 1
start q 0 h^ q 1 i q 2
start^ q 0
q 1
q 2
a
b
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
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)
Write a regular expression for the following kinds of words
Describe in English the following regular expressions