Docsity

Lecture 2 of CSMC 430: Scanner and Regular Expressions - Prof. Chao Wen Tseng, Study notes of Computer Science

Partial preview of the text

Download Lecture 2 of CSMC 430: Scanner and Regular Expressions - Prof. Chao Wen Tseng and more Study notes Computer Science in PDF only on Docsity!

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:

S0 S1 S

S

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:

  1. classifying the input character
  2. finding the next state
  3. an assignment to the state variable
  4. a branch
  5. a trip through the case statement logic

We can do better by “encoding” the state table in the scanner code.

  1. classify the input character
  2. test character class locally
  3. branch directly to next state

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

  1. no ≤ transitions
  2. single-valued transition function

dfa can be simulated on a nfa

  • obviously

nfa can be simulated on a dfa

  • simulate sets of simultaneous states
  • possible exponential blowup

Converting regular expressions to nfas

Build two-state automaton for atomic regular expression a, with a as the edge.

Compose automata as follows:

  • concatenate
  • union
  • Kleene closure

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

  • construct initial partition of states into accepting and non-accepting states
  • successively refine partition by splitting a group G into smaller groups if states in G have transitions to different groups (two states x, y are in same group iff for all input symbols a x and y have transitions to same group)
  • update transition edges, remove dead states

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

  • generate dfa directly from regular expression
  • two stack simulation of nfa
  • “lazy” construction of dfa

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.