















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!
















Scanner
source code scanner^ parser^ il
errors
tokens
A scanner must recognize various parts of the language’s syntax.
Input is separated into tokens based on lexical analysis.
x = x + y;
becomes
<id, x> = <id, x> + <id, y> ;
Specifying patterns
A scanner must recognize various parts of the language’s syntax.
Some parts are easy:
white space some combination of < 6 b > and tab keywords and operators specified as literal patterns — do, end comments opening and closing delimiters — /* · · · */
Regular expressions
Regular expressions represent languages.
Languages are sets of strings.
Operations include Kleene closure, concatenation, and union.
Regular expression Language (a) { ”a” } (a) | (b) { ”a”, ”b” } (a)(b) { ”ab” } (a)∗^ { ””, ”a”, ”aa”, ... } (a)+^ { ”a”, ”aa”, ... }
We assume closure, concatenation, union as the order of precedence.
ab | cd∗^ = (ab) | (c(d∗))
= { ”ab”, ”c”, ”cd”, ”cdd”, ... }
a(bc)∗^ = { ”a”, ”abc”, ”abcbc”, ... }
Recognizers
From a regular expression, we can construct a deterministic finite automaton (dfa).
Recognizer for identifier:
letter
letter digit
other
digit other
error
accept
identifier letter → (a | b | c | ... | z | A | B | C | ... | Z) digit → (0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9) id → letter (letter | digit)∗
Tables for the recognizer
Two tables control the recognizer.
char class:
a − z A − Z 0 − 9 other value letter letter digit other
next state:
class S0 S1 S2 S letter S1 S1 — — digit S3 S1 — — other S3 S2 — —
To change languages, we can just change tables.
Improved efficiency
Table driven implementation is slow relative to direct code. Each state
transition involves:
We can do better by “encoding” the state table in the scanner code.
This takes many fewer instructions per cycle.
Nondeterministic finite automata
What about the regular expression (a | b)∗abb?
Start S1 S2 S3 S
a|b
a b b
State Start has ≤ transition to S1.
State S1 has multiple transitions on a!
⇒ nondeterministic finite automaton (nfa)
Different definition for accept
A nfa accepts x if and only if there is some path through the transition graph
from the start state to an accepting state such that the labels along the edges
spell x.
nfas versus dfas
What is the relationship between a nfa and a dfa?
dfa is special case of nfa
dfa can be simulated on a nfa
nfa can be simulated on a dfa
Converting regular expressions to nfas
Build two-state automaton for atomic regular expression a, with a as the edge.
Compose automata as follows:
Subset construction algorithm
Input: nfa N Output: dfa D with Dstates and Dtran that accepts same language Method: let s be a state in nfa and T a set of states, using the following definitions
Operation Description ≤-closure(s) Set of nfa states reachable from nfa state s on ≤-transitions alone.
≤-closure(T) Set of nfa states reachable from some nfa state s in T on ≤-transitions alone.
move(T,a) Set of nfa states to which there is a transition on input symbol a from some nfa state s in T.
Example subset construction
nfa for a*b
≤-closure( ) = { } = MOVE( , ) = { } ≤-closure( ) = { } = MOVE( , ) = { } ≤-closure( ) = { } = MOVE( , ) = { } ≤-closure( ) = { } = MOVE( , ) = { } ≤-closure( ) = { } =
Building minimum-state dfas
Important theoretical result
Every regular language is recognized by a minimum-state dfa that is unique up to state names.
Look for states that can be distinguished from each other (i.e., end up in
accepting/nonaccepting state for identical input).
dfa state minimization algorithm
Theorem 3.10, pages 67–71 in Hopcroft and Ullman’s book Introduction to Automata Theory, Languages, and Computation
Issues
Complexity Tradeoffs
For regular expression r and input x
Space Time nfa O(|r|) O(|r| ∗ |x|) dfa O(2|r|) O(|x|)
Other approaches
So what is hard?
Language features that can cause problems:
reserved words PL/I had no reserved words if then then then = else; else else = then;
significant blanks FORTRAN and Algol68 ignore blanks do 10 i = 1, do 10 i = 1.
string constants special characters in strings newline, tab, quote, comment delimiter
These problems can be swept under the rug (avoided) by intelligent language design.