CSCI 3136 Principles of Programming Languages Lexical Analysis and - - PowerPoint PPT Presentation

csci 3136 principles of programming languages
SMART_READER_LITE
LIVE PREVIEW

CSCI 3136 Principles of Programming Languages Lexical Analysis and - - PowerPoint PPT Presentation

CSCI 3136 Principles of Programming Languages Lexical Analysis and Automata Theory - 2 Summer 2013 Faculty of Computer Science Dalhousie University 1 / 12 Class Location Change for two days in July Lecture class on Monday July 15 and Wed.


slide-1
SLIDE 1

CSCI 3136 Principles of Programming Languages

Lexical Analysis and Automata Theory - 2

Summer 2013 Faculty of Computer Science Dalhousie University

1 / 12

slide-2
SLIDE 2

Class Location Change for two days in July

Lecture class on Monday July 15 and Wed. July 17 would be at Room 226, Chemistry building.

2 / 12

slide-3
SLIDE 3

Context-free Grammar Context-free Language

are gen- erated by

Parser (PDA)

recognizes

Regular Expression Regular Language

are gen- erated by

Scanner (DFA)

recognizes

slide-4
SLIDE 4

Scanner Implementation

Also known as RegEx = ⇒ Scanner (DFA)

  • RegEx =

⇒ NFA (nondeterministic finite automaton)

  • NFA =

⇒ DFA (deterministic finite automaton)

  • Minimizing the DFA

4 / 12

slide-5
SLIDE 5

Deterministic Finite Automata (DFA)

A deterministic finite automaton (DFA) is a simple machine that accepts or rejects a given input string. This defines a formal language: the set of strings accepted by the

  • DFA. We say the DFA recognizes this language.

A language is regular if and only if there exists a DFA that recognizes this language.

5 / 12

slide-6
SLIDE 6

Example of a DFA

Informal definition of a DFA

  • Finite set of states
  • Designated start state
  • Designated set of final states
  • Reads input one symbol at a time.

New state is calculated as a function of current state and read symbol.

  • String is accepted if and only if a

final state is reached after reading the entire string. q1 start q2 1 1 Transition Function 1 → q1 q1 q2 q2

  • q1

q2

6 / 12

slide-7
SLIDE 7

Example of a DFA

q1 start q2 1 1 Which language does this DFA recognize?

7 / 12

slide-8
SLIDE 8

Formal Definition of a DFA

A DFA is a 5-tuple (Q, Σ, δ, q0, F)

  • Q is a finite set of states
  • Σ is a finite set of input symbols (i.e.,

an alphabet)

  • δ : Q × Σ → Q is a transition function
  • q0 ∈ Q is the start state
  • F ⊆ Q is the set of final or accepting

states q0 start q1 1 1 1 → q0 q0 q1 q1

  • q0

q1 The DFA accepts a string σ ∈ Σ∗ if and only if it reaches a final state after consuming all letters in σ. The language recognized by a DFA D is the set of strings L(D) := {σ ∈ Σ∗|D accepts σ}.

8 / 12

slide-9
SLIDE 9

Non-Deterministic Finite Automata (NFA)

A DFA is deterministic in the sense that every input traces exactly

  • ne path through the automaton.

A non-deterministic finite automaton (NFA) is identical to a DFA, except that there are possibly many paths traced by an input. Two sources of non-determinism

  • ǫ-transitions

q1 start q2 ǫ

  • Multiple successor states for the same input symbol

q1 start q2 q3 1 1 An NFA accepts a string σ ∈ Σ∗ if one of the paths traced by σ ends in an accepting state.

9 / 12

slide-10
SLIDE 10

Example of an NFA

q1 start q2 q3 1 0/1 0/ǫ 0/1 Transition Function 1 ǫ → q1 {q1} {q1, q2} – q2 {q3} – {q3} q3

  • {q3}

{q3} – Which language does this NFA recognize?

10 / 12

slide-11
SLIDE 11

Formal Definition of an NFA

An NFA is a 5-tuple (Q, Σ, δ, q0, F)

  • Q is a finite set of states
  • Σ is a finite set of input symbols (i.e., an alphabet)
  • δ : Q × (Σ ∪ {ǫ}) → 2Q is a transition function
  • q0 ∈ Q is the start state
  • F ⊆ Q is the set of final or accepting states

Every input σ ∈ Σ∗ defines a set of paths traced by δ while consuming σ. The NFA accepts a string σ if and only if one such path defined by σ ends in a final states. The language recognized by an NFA D is the set of strings L(D) := {σ ∈ Σ∗|D accepts σ}.

11 / 12

slide-12
SLIDE 12

12 / 12