Download - filelanguage and automata 6
-
8/14/2019 filelanguage and automata 6
1/5
C o n t e x t - F r e e G r a m m a r s
N o t a t i o n f o r r e c u r s i v e d e s c r i p t i o n o f l a n g u a g e s .
E x a m p l e :
R o l l ! R O L L C l a s s S t u d s = R O L L
C l a s s ! C L A S S T e x t = C L A S S
T e x t ! C h a r T e x t
T e x t ! C h a r
C h a r ! a o t h e r c h a r s
S t u d s ! S t u d S t u d s
S t u d s !
S t u d ! S T U D T e x t = S T U D
G e n e r a t e s d o c u m e n t s " s u c h a s :
R O L L C L A S S c s 1 5 4 C L A S S
S T U D S a l l y S T U D
S T U D F r e d S T U D
R O L L
V a r i a b l e s e . g . , S t u d s r e p r e s e n t s e t s o f s t r i n g s
i . e . , l a n g u a g e s .
3I n s e n s i b l e g r a m m a r s , t h e s e s t r i n g s s h a r e
s o m e c o m m o n c h a r a c t e r i s t i c o r r o l l .
T e r m i n a l s e . g . , a o r R O L L = s y m b o l s
o f w h i c h s t r i n g s a r e c o m p o s e d .
3 T a g s " l i k e R O L L c o u l d b e
c o n s i d e r e d e i t h e r a s i n g l e t e r m i n a l o r t h e
c o n c a t e n a t i o n o f 6 t e r m i n a l s .
P r o d u c t i o n s = r u l e s o f t h e f o r m H e a d !
B o d y
3 H e a d i s a v a r i a b l e .
3B o d y i s a s t r i n g o f z e r o o r m o r e v a r i a b l e s
a n d o r t e r m i n a l s .
S t a r t S y m b o l = v a r i a b l e t h a t r e p r e s e n t s t h e
l a n g u a g e . "
N o t a t i o n : G = V ; P ; S = v a r i a b l e s ,
t e r m i n a l s , p r o d u c t i o n s , s t a r t s y m b o l .
E x a m p l e
A s i m p l e r e x a m p l e g e n e r a t e s s t r i n g s o f 0 ' s a n d 1 ' s
s u c h t h a t e a c h b l o c k o f 0 ' s i s f o l l o w e d b y a t l e a s t
a s m a n y 1 ' s .
S ! A S
A ! 0 A 1 A 1 0 1
N o t e v e r t i c a l b a r s e p a r a t e s d i e r e n t b o d i e s f o r
t h e s a m e h e a d .
1
-
8/14/2019 filelanguage and automata 6
2/5
D e r i v a t i o n s
A w h e n e v e r t h e r e i s a p r o d u c t i o n
A !
3 S u b s c r i p t w i t h n a m e o f g r a m m a r , e . g . ,
G
, i f n e c e s s a r y .
3E x a m p l e : 0 1 1 A S 0 1 1 0 A 1 S
*
m e a n s s t r i n g c a n b e c o m e i n z e r o
o r m o r e d e r i v a t i o n s t e p s .
3 E x a m p l e s : 0 1 1 A S *
0 1 1 A S z e r o s t e p s ;
0 1 1 A S
*
0 1 1 0 A 1 S o n e s t e p ; 0 1 1 A S
*
0 1 1 0 0 1 1 t h r e e s t e p s .
L a n g u a g e o f a C F G
L G = s e t o f t e r m i n a l s t r i n g s w s u c h t h a t
S
*
G
w , w h e r e S i s t h e s t a r t s y m b o l .
A s i d e : N o t a t i o n
a ; b ; : : : a r e t e r m i n a l s ; : : : ; y ; z a r e s t r i n g s o f
t e r m i n a l s .
G r e e k l e t t e r s a r e s t r i n g s o f v a r i a b l e s a n d o r
t e r m i n a l s , o f t e n c a l l e d s e n t e n t i a l f o r m s
A ; B ; : : : a r e v a r i a b l e s .
: : : ; Y ; Z a r e v a r i a b l e s o r t e r m i n a l s .
S i s t y p i c a l l y t h e s t a r t s y m b o l .
L e f t m o s t R i g h t m o s t D e r i v a t i o n s
W e h a v e a c h o i c e o f v a r i a b l e t o r e p l a c e a t e a c h
s t e p .
3 D e r i v a t i o n s m a y a p p e a r d i e r e n t o n l y
b e c a u s e w e m a k e t h e s a m e r e p l a c e m e n t s
i n a d i e r e n t o r d e r .
3T o a v o i d s u c h d i e r e n c e s , w e m a y r e s t r i c t
t h e c h o i c e .
A l e f t m o s t d e r i v a t i o n a l w a y s r e p l a c e s t h e
l e f t m o s t v a r i a b l e i n a s e n t e n t i a l f o r m .
3 Y i e l d s l e f t - s e n t e n t i a l f o r m s
R i g h t m o s t d e n e d a n a l o g o u s l y .
l m
r m
, e t c . , u s e d t o i n d i c a t e d e r i v a t i o n s a r e
l e f t m o s t o r r i g h t m o s t .
2
-
8/14/2019 filelanguage and automata 6
3/5
E x a m p l e
S
l m
A S
l m
A 1 S
l m
0 1 1 S
l m
0 1 1 A S
l m
0 1 1 0 A 1 S
l m
0 1 1 0 0 1 1 S
l m
0 1 1 0 0 1 1
S
r m
A S
r m
A A S
r m
A A
r m
A 0 A 1
r m
A 0 0 1 1
r m
A 1 0 0 1 1
r m
0 1 1 0 0 1 1
D e r i v a t i o n T r e e s
N o d e s = v a r i a b l e s , t e r m i n a l s , o r
3 V a r i a b l e s a t i n t e r i o r n o d e s ; t e r m i n a l s a n d
a t l e a v e s .
3A l e a f c a n b e o n l y i f i t i s t h e o n l y c h i l d
o f i t s p a r e n t .
A n o d e a n d i t s c h i l d r e n f r o m t h e l e f t m u s t
f o r m t h e h e a d a n d b o d y o f a p r o d u c t i o n .
E x a m p l e
S
A S
A S
A 1
0 1 0 A 1
0 1
E q u i v a l e n c e o f P a r s e T r e e s , L e f t m o s t , a n d
R i g h t m o s t D e r i v a t i o n s
T h e f o l l o w i n g a b o u t a g r a m m a r G = V ; P ; S
a n d a t e r m i n a l s t r i n g w a r e a l l e q u i v a l e n t :
1 S
*
w i . e . , w i s i n L G
2 S
*
l m
w
3 S
*
r m
w
4 . T h e r e i s a p a r s e t r e e f o r G w i t h r o o t S a n d
y i e l d l a b e l s o f l e a v e s , f r o m t h e l e f t w
O b v i o u s l y 2 a n d 3 e a c h i m p l y 1 .
3
-
8/14/2019 filelanguage and automata 6
4/5
P a r s e T r e e I m p l i e s L M R M D e r i v a t i o n s
G e n e r a l i z e a l l s t a t e m e n t s t o t a l k a b o u t a n
a r b i t r a r y v a r i a b l e A i n p l a c e o f S
3 E x c e p t n o w 1 n o l o n g e r m e a n s w i s i n
L G
I n d u c t i o n o n t h e h e i g h t o f t h e p a r s e t r e e .
B a s i s : H e i g h t 1 : T r e e i s r o o t A a n d l e a v e s w =
a
1
a
2
; : : : ; a
k
A ! w m u s t b e a p r o d u c t i o n , s o A
l m
w a n d
A
r m
w
I n d u c t i o n : H e i g h t 1 . T r e e i s r o o t A w i t h
c h i l d r e n X
1
X
2
; : : : ; X
k
T h o s e X
i
' s t h a t a r e v a r i a b l e s a r e r o o t s o f
s h o r t e r t r e e s .
3T h u s , t h e I H s a y s t h a t t h e y h a v e L M
d e r i v a t i o n s o f t h e i r y i e l d s .
C o n s t r u c t a L M d e r i v a t i o n o f w f r o m A b y
s t a r t i n g w i t h A
l m
X
1
X
2
X
k
, t h e n
u s i n g L M d e r i v a t i o n s f r o m e a c h X
i
t h a t i s a
v a r i a b l e , i n o r d e r f r o m t h e l e f t .
R M d e r i v a t i o n a n a l o g o u s .
D e r i v a t i o n s t o P a r s e T r e e s
I n d u c t i o n o n l e n g t h o f t h e d e r i v a t i o n .
B a s i s : O n e s t e p . T h e r e i s a n o b v i o u s p a r s e t r e e .
I n d u c t i o n : M o r e t h a n o n e s t e p .
L e t t h e r s t s t e p b e A X
1
X
2
X
k
S u b s e q u e n t c h a n g e s c a n b e r e o r d e r e d s o t h a t
a l l c h a n g e s t o X
1
a n d t h e s e n t e n t i a l f o r m s
t h a t r e p l a c e i t a r e d o n e r s t , t h e n t h o s e
f o r X
2
, a n d s o o n i . e . , w e c a n r e w r i t e t h e
d e r i v i a t i o n a s a L M d e r i v a t i o n .
T h e d e r i v a t i o n s f r o m t h o s e X
i
' s t h a t a r e
v a r i a b l e s a r e a l l s h o r t e r t h a n t h e g i v e n
d e r i v i a t i o n , s o t h e I H a p p l i e s .
B y t h e I H , t h e r e a r e p a r s e t r e e s f o r e a c h o f
t h e s e d e r i v a t i o n s .
M a k e t h e r o o t s o f t h e s e t r e e s b e c h i l d r e n o f a
n e w r o o t l a b e l e d A
E x a m p l e
C o n s i d e r d e r i v a t i o n S A S A A S A A
A 1 A A 1 0 A 1 0 1 1 0 A 1 0 1 1 0 0 1 1
4
-
8/14/2019 filelanguage and automata 6
5/5
S u b d e r i v a t i o n f r o m A i s A A 1 0 1 1
S u b d e r i v a t i o n f r o m S i s S A S A
0 A 1 0 0 1 1
E a c h h a s a p a r s e t r e e ; p u t t h e m t o g e t h e r w i t h
n e w r o o t S
5