4. Parsing - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

4. Parsing

Description:

The language of G : b* ab*. The leftmost derivations. S bS S Sb. bSb bSb. bab bab. ambiguity ... Exercise : ... S aS |AB| B. A abA | ab. B BB | ba ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 12
Provided by: A2A
Category:
Tags: parsing

less

Transcript and Presenter's Notes

Title: 4. Parsing


1
4. Parsing
2
  • An Introduction
  • 4.1 Leftmost Derivations and Ambiguity
  • L (G) is the set of terminal strings that can be
    derived in any manner, from the start symbol.
  • A terminal string may be generated by a number
    of different derivations.
  • e.g. String abcbaa is derived from 4 distinct
    derivation.
  • - derivation of Pascal expression were given in
    a left most form.
  • Theorem 4.1.1
  • Let G (V,?,P,S) be a context-free grammar.
  • A string w is in L (G) if, and only if, then is
    a leftmost derivation of
  • from s.

3
  • Definition 4.1.2
  • A context-free grammar G is ambiguous if there
    is a string w ? L (G) that can be derived by two
    distinct leftmost derivations.
  • Example 4.1.1
  • Let G be the grammar
  • S ? aS Sa a
  • G is ambiguous since the string as has two
    distinct leftmost derivations.
  • S ? aS S ? Sa
  • ? aa ? aa
  • the language of a G is a . This language is
    generated by the unambiguous grammar
  • S ? aS a

4
  • Example 4.1.2
  • L (G) S? bS Sb a
  • The language of G b ab.
  • The leftmost derivations
  • S ? bS S ? Sb
  • ? bSb ? bSb
  • ? bab ? bab
  • ? ambiguity
  • Quiz Show that grammar
  • G1 S? bS aA
  • A? bA ?
  • where its language bnabm is an ambiguity

5
  • 4.2 The Graph of a grammar
  • The leftmost derivation of a context-free
    grammar G can be represented by a labeled
    directed graph g(G).
  • A string w is adjacent to v in g(G) if v ?Lw ,
  • ( i.e w can be obtained from v by one leftmost
    rule application)
  • Definition 4.2.1
  • Let G (V,?,P,S) be a context-free grammar.
    The left most graph of the grammar G, g(G), is
    the labeled directed graph (N,P,A), where the
    node and arcs are defined by
  • i) N w ? (V??) S ?Lw
  • ii) A v, w, r ? N x N x P v ?Lw by
    application of rule r
  • A path from S to w in g(G) represents a
    derivation of w from S in the grammar G. The
    label on the arc from v to w specifics the rule
    application to v to obtain w.

6
S
  • S?aS

S?aS
S?Sb
aS
Sb
ab
S?aS
S?aS
S?Sb
S?Sb
S?ab
S?ab
aaS
aSb
aab
Sbb
abb
S?Sb
S?aS
S?Sb
S?aS
S?aS
S?Sb
S?ab
S?ab
aSbb
aaaS
aaSb
aabb
Sbbb
abbb
aaab
S?Sb
7
  • Ambiguous grammar
  • S? aS Sb ab

  • ? rules generate cycles

S
S? a
S? ss
S??
ss
a
S? a
S? ss
S??
sss
aS
Ambiguous
S? ss
S? ss a ?
ssss
ass
8
  • 4.3 Top-Down Parser ( Breadth-First )
  • Objective To determine whether an input
    string is derivable from the rules of a grammar.
  • The application rule A ? w in the left most
    derivation.
  • has the form uAv ?uwv
  • string u, consisting solely of terminals, ?
    terminal prefix.
  • Algorithm 4.3.1
  • Breadth-First Top Down Parsing Algorithm.
  • Input context-free grammar G ( V,S,P,S)
    queue Q
  • Initialize T with root S, INSERT (S,Q)
  • Repeat
  • 2.1 q REMOVE (Q)
  • 2.2 Q 0
  • 2.3 done false

9
  • Let q uAv where A is the leftmost variable in
    q.
  • 2.4 repeat
  • 2.4.1 if there is no A rule number greater
    than I then done false
  • 2.4.2 if not done then
  • Let A w be the first A rule with
    number greater than I and let j be the
    number of this rule.
  • 2.4.2.1 if uwv ? S and the terminal prefix of
    uwv matches a prefix of p then
  • 2.4.2.1.1 INSERT (uwv, Q)
  • 2.4.2.1.2 Add node uwv to T. Set a pointer
    from uwv to q.
  • end if
  • 2.4.3 i j
  • until done or p uwv
  • until EMPTY (Q) or p uwv

10
  • If p uwv then accept else reject
  • ? search tree T containing nodes from g(G)
  • 4.4 A Depth-First Top-Down Parser

  • S
  • A
  • T
  • b (A)
  • (T)
    ( AT) ( TT) ( bT)
    (bb)
  • (b) ((A))

11
  • Exercise
  • Build the sub graph of the grammar of G
    consisting of the left sentential forms that are
    generated by derivations of length 3 or less
  • S ? aS AB B
  • A ? abA ab
  • B ? BB ba
Write a Comment
User Comments (0)
About PowerShow.com