5b ContextFree Grammars Parsing - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

5b ContextFree Grammars Parsing

Description:

A aAb | Ab | Exercise 5.1.7(f) S aS | aSb | SS | {aibjcjdie3: i, j 0} ... Leftmost: S AB bAB bbAB bbaB bbaaB... Rightmost: S AB AaB AabA ... But SAME derivation tree ... – PowerPoint PPT presentation

Number of Views:153
Avg rating:3.0/5.0
Slides: 20
Provided by: daveh5
Category:

less

Transcript and Presenter's Notes

Title: 5b ContextFree Grammars Parsing


1
5b Context-Free Grammars Parsing Ambiguity
5.1,5.2,5.3
  • Define Context-Free Grammar (cfg)
  • CFG Simulation
  • Constructing a Grammar for a Language
  • Leftmost and Rightmost Derivations
  • Derivation Trees
  • Top-Down Parsing
  • Membership Testing for Context-Free Languages
  • Simple Grammars
  • Ambiguous Grammars
  • Inherently Ambiguous Languages

2
Context-Free Grammar
  • G (V,T,S,P) ordered quadruple
  • All productions are of the form
  • A ? x,
  • where A ? V and x ? (V ? T)
  • Why Called "Context Free"?
  • vs. Context Sensitive...

3
Determine Language generated by a Given Grammar
  • List all words of length less than or equal to 4
    that are generated by the grammar of Example 5.5
    page 130
  • S ? SS aaSb bSaa ?
  • Does L(G) x x ? a,b and na(x) 2nb(x)?

4
Context-Free Simulator
  • http//cs.union.edu/csc140/simulators
  • Enter and run the following grammar
  • S?SSaSbbSabaab

5
and now for something completely different
  • Mad Libs on the Context-Free Generator

6
Construct Grammar for a Given Language
  • Exercise 5.1.7(a) page 134
  • S ? A aA aaA aaaA
  • A ? aAb Ab ?
  • Exercise 5.1.7(f)
  • S ? aS aSb SS ?
  • aibjcjdie3 i, j ? 0
  • aibjckdn i,j,k,n ? 0 , i lt n and j ? k
  • aibjbiaj i, j ? 0
  • Note bjbi bji bij bibj
  • What if it were aibjcidj ?

7
CF Grammar for set of all Regular Expressions
over a given Alphabet, say a,b
  • S ? ? ? a b
  • S ? SS SS S (S)
  • Note ? and ? are symbols in ? of grammar
  • Does NOT handle precedence of operators see page
    146, i.e. grammar is ambiguous--more on this
    later

8
Leftmost Rightmost Derivations
  • Leftmost Always apply production to leftmost
    non-terminal at any given point
  • Rightmost rightmost non-terminal
  • Note Multiple derivations possible for same
    derivation tree, but only one left-most or
    right-most

9
Derivation Trees
  • No matter which order non-terminals are replaced,
    unless the grammar is ambiguous there is only one
    derivation tree per word
  • Example S ? AB A ? bA a B ? aB bA
  • Derive w bbaaba
  • Leftmost S ? AB ? bAB ? bbAB ? bbaB ? bbaaB
  • Rightmost S ? AB ? AaB ? AabA ?
  • But SAME derivation tree

10
Parsing
  • Finding a sequence of productions by which w in
    L(G) is derived
  • (i.e. construct a derivation tree)
  • Example
  • S ? SS aSa' aa' bSb' bb' cSc' cc'
  • w cc'bcaa'bb'cc'c'b'aa'
  • Note that in this example there is more than one
    derivation tree (ambiguousstay tuned)

11
Membership Test for CFLIs word w in L(G)?
  • Eliminate productions of the form A?? and A?B.
  • Why?
  • (more on how to do this later)
  • All other productions are "productive" in that we
    make progress toward a word in L(G) by applying
    each production (either lengthen intermediate
    word or convert non-terminal to terminal)
  • Therefore, in a "finite length of time" we can
    try ALL possible productions that yield all words
    of w.
  • If w is one of them, it is in L.
  • If not, w is not in L.

12
S-Grammars
  • All productions in a Simple Grammar (S-Grammar)
    are of the form
  • A ? ax,
  • where A ? V, a ? T, x ? V, and any pair (A,a)
    occurs at most once in P.
  • The terminal, a, is sometimes called the "handle"
  • It determines uniquely which production to use at
    any given point in a derivation
  • Good News--Parsing becomes almost trivial with
    S-Grammars
  • Bad News--Not all languages are convertible to
    S-Grammars

13
Ambiguity
  • Two distinct derivation trees for same sentence
    in same grammar
  • "Time flies like an arrow." per CJH
  • Time marches on (no turning back) as in straight
    path of arrow
  • Time is fleeting, as quickly as an arrow
  • Use a stopwatch to time arrows the same way you
    time flies
  • Time magazine, when properly rolled up and
    thrown, flies like an arrow
  • Time flies like to line up and sit on an arrow

14
Ambiguous Grammars
  • Example, give all derivation trees for w
  • S? SS aSa' aa' bSb' bb' cSc' cc'
  • w cc'bcaa'bb'cc'c'b'aa'
  • Exercise 5.2.13 page 145
  • Consider 2 different left-most derivations
  • S ? aSbS ? abSabS ? ababS ? abab
  • S ? aSbS ? abS ? abaSbS ? ababS ? abab

15
Ambiguous Grammar does not imply Ambiguous
Language
  • Exercise 5.2.14
  • S ? aSb SS ?
  • Put two different trees for ababab on board
  • Non-ambiguous Grammar for same Language
  • S ? AS ? A ? aSb ab(alternate solution in
    text)
  • Put (the only) tree for ababab on board

16
Programming Languages pages 142-3
  • Example 5.11All Expressions treated equally
  • Example 5.12Factors vs. Terms vs. Expressions
  • Forces evaluation of Factors first, then Terms,
    then Expressions

17
Inherently Ambiguous CF Languages
  • L1 anbncm, L2 anbmcm
  • Each language has a non-ambiguous grammar
  • Suppose starting symbols were S1 and S2
    respectively
  • and that set of non-terminals was mutually
    disjoint
  • However, L L1 ? L2 is inherently ambiguous
  • L is CF since we could construct it with S ? S1
    S2
  • Consider a3b3c3 ? L, get two distinct derivation
    trees
  • Not just different symbols, but different shapes
  • Proof that some languages are inherently
    ambiguous is "beyond the scope of this course"

18
In-Class ExerciseExercise 5.1.8 (d g) page
134
  • Work in groups of 2 or 3
  • Enter grammars on simulator and generate words
  • "-gt" for arrow, no blank spaces
  • Answers on next slide (no peeking)
  • First group done with both put (d) on board
  • Second group done with both put (g) on board

19
In-Class ExerciseAnswers
  • 5.1.8(d)
  • S ? aSc B
  • B ? bBcc ?
  • 5.1.8(g)
  • S ? aSc aA bB Cc
  • A ? aAc aA B
  • B ? bBc bB ?
  • C ? bCc Cc ?
Write a Comment
User Comments (0)
About PowerShow.com