In sensible gramma rs, these strings share some common c - - PDF document

in sensible gramma rs these strings share some common c
SMART_READER_LITE
LIVE PREVIEW

In sensible gramma rs, these strings share some common c - - PDF document

Con text-F ree Grammars Notation for recursiv e description of languages. Example: Rol l ! < RO LL > C l ass S tuds < =RO LL > C l ass ! < C LAS S > T ext < =C LAS S > T ext ! C


slide-1
SLIDE 1 Con text-F ree Grammars Notation for recursiv e description
  • f
languages. Example: Rol l !< RO LL > C l ass S tuds < =RO LL > C l ass ! < C LAS S > T ext < =C LAS S > T ext ! C har T ext T ext ! C har C har ! a
  • (other
c hars) S tuds ! S tud S tuds S tuds !
  • S
tud !< S T U D > T ext < =S T U D >
  • Generates
\do cumen ts" suc h as: <ROLL><CLASS>cs 154</ CLASS > <STUD>Sally</ST UD> <STUD>Fred</STU D>
  • </ROLL>
  • V
ariables (e.g., S tuds) represen t sets
  • f
strings (i.e., languages).

In sensible gramma rs, these strings share some common c haracteristic
  • r
roll.
  • T
erminals (e.g., a
  • r
< RO LL >) = sym b
  • ls
  • f
whic h strings are comp
  • sed.

\T ags" lik e < RO LL > could b e considered either a single terminal
  • r
the concatenation
  • f
6 terminals.
  • Pr
  • ductions
= rules
  • f
the form H ead ! B
  • dy
.

H ead is a v ariable.

B
  • dy
is a string
  • f
zero
  • r
more v ariables and/or terminals.
  • Start
Symb
  • l
= v ariable that represen ts \the language."
  • Notation:
G = (V ; ; P ; S ) = (v ariables, terminals, pro ductions, start sym b
  • l).
Example A simpler example generates strings
  • f
0's and 1's suc h that eac h blo c k
  • f
0's is follo w ed b y at least as man y 1's. S ! AS j
  • A
! 0A1 j A1 j 01
  • Note
v ertical bar separates dieren t b
  • dies
for the same head. 1
slide-2
SLIDE 2 Deriv ations
  • A
)
  • whenev
er there is a pro duction A !
  • .

Subscript with name
  • f
grammar, e.g., ) G , if necessary .

Example: 011AS ) 0110A1S .
  • )
*
  • means
string
  • can
b ecome
  • in
zero
  • r
more deriv ation steps.

Examples: 011AS ) * 011AS (zero steps); 011AS ) * 0110A1S (one step); 011AS ) * 0110011 (three steps). Language
  • f
a CF G L(G) = set
  • f
terminal strings w suc h that S ) * G w , where S is the start sym b
  • l.
Aside: Notation
  • a;
b; : : : are terminals; : : : ; y ; z are strings
  • f
terminals.
  • Greek
letters are strings
  • f
v ariables and/or terminals,
  • ften
called sentential forms.
  • A;
B ; : : : are v ariables.
  • :
: : ; Y ; Z are v ariables
  • r
terminals.
  • S
is t ypically the start sym b
  • l.
Leftmost/Righ t most Deriv ations
  • W
e ha v e a c hoice
  • f
v ariable to replace at eac h step.

Deriv ations ma y app ear dieren t
  • nly
b ecause w e mak e the same replacemen ts in a dieren t
  • rder.

T
  • a
v
  • id
suc h dierences, w e ma y restrict the c hoice.
  • A
leftmost deriv ation alw a ys replaces the leftmost v ariable in a sen ten tial form.

Yields left-sentential forms.
  • R
ightmost dened analogously .
  • )
lm , ) rm , etc., used to indicate deriv ations are leftmost
  • r
righ tmost. 2
slide-3
SLIDE 3 Example
  • S
) lm AS ) lm A1S ) lm 011S ) lm 011AS ) lm 0110A1S ) lm 0110011S ) lm 0110011
  • S
) rm AS ) rm AAS ) rm AA ) rm A0A1 ) rm A0011 ) rm A10011 ) rm 0110011 Deriv ation T rees
  • No
des = v ariables, terminals,
  • r
.

V ariables at in terior no des; terminals and
  • at
lea v es.

A leaf can b e
  • nly
if it is the
  • nly
c hild
  • f
its paren t.
  • A
no de and its c hildren from the left m ust form the head and b
  • dy
  • f
a pro duction. Example S A S A S
  • A
1 1 A 1 1 Equiv alence
  • f
P arse T rees, Leftmost, and Righ tmost Deriv ations The follo wing ab
  • ut
a grammar G = (V ; ; P ; S ) and a terminal string w are all equiv alen t: 1. S ) * w (i.e., w is in L(G)). 2. S ) * lm w 3. S ) * rm w 4. There is a parse tree for G with ro
  • t
S and yield (lab els
  • f
lea v es, from the left) w .
  • Ob
viously (2) and (3) eac h imply (1). 3
slide-4
SLIDE 4 P arse T ree Implies LM/RM Deriv ations
  • Generalize
all statemen ts to talk ab
  • ut
an arbitrary v ariable A in place
  • f
S .

Except no w (1) no longer means w is in L(G).
  • Induction
  • n
the heigh t
  • f
the parse tree. Basis : Heigh t 1: T ree is ro
  • t
A and lea v es w = a 1 ; a 2 ; : : : ; a k .
  • A
! w m ust b e a pro duction, so A ) lm w and A ) rm w . Induction : Heigh t > 1. T ree is ro
  • t
A with c hildren X 1 ; X 2 ; : : : ; X k .
  • Those
X i 's that are v ariables are ro
  • ts
  • f
shorter trees.

Th us, the IH sa ys that they ha v e LM deriv ations
  • f
their yields.
  • Construct
a LM deriv ation
  • f
w from A b y starting with A ) lm X 1 X 2
  • X
k , then using LM deriv ations from eac h X i that is a v ariable, in
  • rder
from the left.
  • RM
deriv ation analogous. Deriv ations to P arse T rees Induction
  • n
length
  • f
the deriv ation. Basis : One step. There is an
  • b
vious parse tree. Induction : More than
  • ne
step.
  • Let
the rst step b e A ) X 1 X 2
  • X
k .
  • Subsequen
t c hanges can b e reordered so that all c hanges to X 1 and the sen ten tial forms that replace it are done rst, then those for X 2 , and so
  • n
(i.e., w e can rewrite the deriviation as a LM deriv ation).
  • The
deriv ations from those X i 's that are v ariables are all shorter than the giv en deriviation, so the IH applies.
  • By
the IH, there are parse trees for eac h
  • f
these deriv ations.
  • Mak
e the ro
  • ts
  • f
these trees b e c hildren
  • f
a new ro
  • t
lab eled A. Example Consider deriv ation S ) AS ) AAS ) AA ) A1A ) A10A1 ) 0110A1 ) 0110011 4
slide-5
SLIDE 5
  • Sub
deriv ation from A is: A ) A1 ) 011
  • Sub
deriv ation from S is: S ) AS ) A ) 0A1 ) 0011
  • Eac
h has a parse tree; put them together with new ro
  • t
S . 5