Computation, Computers, and Programs CFGPDA - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

Computation, Computers, and Programs CFGPDA

Description:

Computation, Computers, and Programs CFG/PDA. http://www.cs.caltech.edu/courses/cs20/a ... Computers, and Programs CFG/PDA. http://www.cs.caltech.edu ... – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 42
Provided by: csCal
Category:

less

Transcript and Presenter's Notes

Title: Computation, Computers, and Programs CFGPDA


1
CS20a summary (Oct 24, 2002)
  • Context-free languages
  • Grammars G (V, T, P, S)
  • Pushdown automata
  • N-PDA CFG
  • D-PDA lt CFG
  • Today
  • What languages are context-free?
  • Pumping lemma (similar to pumping lemma for
    regular languages)
  • Ogdens lemma

2
Regular languages
  • Intuition if a FA accepts a string that is long
    enough, it must repeat a state
  • But it cant remember that the state was repeated
  • So it can be forced to repeat the state over and
    over

3
Pumping lemma for regular languages
4
Pumping lemma for CFL
5
Pumping lemma
  • Suppose L is in Chomsky Normal Form
  • A ? a
  • A ? BC

6
Derivation trees
7
Size of derivation trees
8
Pumping
9
Pumping example 1
10
Pumping lemma is too weak
11
Ogdens lemma
12
Building a path
13
Building the path
14
Branch points
15
An example (part 1)
16
An example (part 2)
17
Closure properties
18
Intersection
19
Complement
20
Intersection with regular languages
21
Intersection with regular languages
22
Determining if a string is in a language
23
CYK algorithm
24
LR(k) Parsing
  • Left-to-right parsing, rightmost derivation, k
    tokens lookahead
  • Stack machine
  • Example
  • S -gt E
  • E -gt number
  • E -gt E E
  • E -gt E E

25
Parsing 1 2 3
26
LR(0)
  • How do we build the parser table?
  • New grammar
  • S -gt E E -gt numberE -gt ( L )L -gt EL -gt L E

27
LR(0) parsing
  • Build a DFA
  • States are sets of productions position info
  • state 1S -gt . S E -gt . numberE -gt . ( L )
  • Actions
  • shift t add token t to the stack
  • reduce i apply the ith production to the stack
  • goto i goto state i

28
LR(0) DFA
29
Operations
  • closure state -gt state
  • goto state -gt symbol -gt state

30
Goto
  • goto I X moves the . past the X symbol

31
Building the table
32
Building the table
  • If E contains I -gtX J
  • if X is a terminal, add shift X to state I
  • if X is a non-terminal, add goto J to state I
  • If I contains A -gt alpha . add reduce A -gt
    alpha to state I\

33
LR(1) parsing
  • An LR(1) item contains
  • a production
  • the position (represented by a dot)
  • a lookahead token

34
FIRST function
  • Returns the set of terminals that may begin a
    production

35
Closure function
  • Modified to get the next lookahead symbol

36
Goto function
  • Modified for LR(1) items

37
An example
  • E -gt number
  • E -gt E E
  • E -gt E E

38
LR(1) state diagram
39
Disambiguating expressions
  • E -gt number E E E E ( E )
  • Inherently ambiguous
  • Rewrite into a two-level grammar
  • E general expression
  • E -gt T E T
  • T pure multiplication
  • T -gt S T S
  • S simple expression
  • S -gt number ( E )

40
How can we disambiguate ifthenelse?
  • E -gt number
  • E -gt E E E E ( E )
  • E -gt if E then E
  • E -gt if E then E else E

41
Matched expression
  • E -gt M U
  • M -gt number M M M M ( E )
  • M -gt if E then M else M
  • U -gt if E then U
  • U -gt if E then M else U
Write a Comment
User Comments (0)
About PowerShow.com