LR Parsing - Aho

download LR Parsing - Aho

of 26

Transcript of LR Parsing - Aho

  • 8/9/2019 LR Parsing - Aho

    1/26

    L R P a r s i n gA . V . A H O a nd S . C . J O H N S O NBel l Labora tor ies , M urra y H z l l , New Je rsey 07974

    T h e L R s y n t a x a n a l y s i s m e t h o d i s a u s e f u l a n d v e r s a t i l e t e c h n i q u e f o r p a r s i n gd e t e r m i n i s t i c c o n t e x t - f r e e la n g u a g e s i n c o m p i l i n g a p p l i c a t i o n s . T h i s p a p e rp r o v i d e s a n i n f o r m a l e x p o s i t io n o f L R p a r s i n g t e c h n i q u e s e m p h a s i z i n g t h em e c h a n i c a l g e n e r a t i o n o f e f fi c ie n t L R p a r s e r s f o r c o n t e x t - f r e e g r a m m a r s .P a r t i c u l a r a t t e n t m n is g i v e n to e x t e n d i n g t h e p a r s e r g e n e r a t i o n t e c h n i q u e s t oa p p l y t o a m b i g u o u s g r a m m a r s .Keywords and phrases : g r a m m a l s , p a r s e l s , c o m p i l e r s , a m b i g u o u s g r a m m a r s ,c o n t e x t - f r ee l a n g u a g e s, L R g r a m m a r s .CR calegorzes . 4 12, 5 23

    1 . I N T R O D U C T I O NA c o m p l e t e s p e c i f i c a t i o n o f a p r o g r a m m i n gl a n g u a g e m u s t p e r f o r m a t l e a s t t w o f u n c-t i o n s . F i r s t, i t m u s t s p e c i f y t h e s y n t a x o f t h el a n g u a g e ; t h a t i s, w h i c h s t ri n g s o f s y m b o l sa r e t o b e d e e m e d w e l l - f o r m e d p r o g r a m s .S e c o n d , i t m u s t s p e c i f y t h e s e m a n t i c s o f t h el a n g u a g e ; t h a t is , w h a t m e a n i n g o r i n t e n ts h o u l d b e a t t r i b u t e d t o e a c h s y n t a c t i c a l l yc o r r e c t p r o g r a m .A c o m p i l e r f o r a p r o g r a m m i n g l a n g u a g em u s t v e r i f y t h a t i ts i n p u t o b e y s t h e s y n t a c t i cc o n v e n t i o n s o f t h e l a n g u a g e s p e c i f i c a t i o n . I tm u s t a l s o t r a n s l a t e i t s i n p u t i n t o a n o b j e c tl a n g u a g e p r o g r a m i n a m a n n e r t h a t i s c o n -s i s t e n t w i t h t h e s e m a n t i c s p e c i f i c a t io n o f t h el a n g u a g e . I n a d d i t i o n , i f t h e i n p u t c o n t a i n ss y n t a c t i c e r r o r s , t h e c o m p i l e r s h o u l d a n -n o u n c e t h e i r p r e s e n c e a n d t r y t o p i n p o i n tt h e i r l o c a t i o n . T o h e l p p e r f o r m t h e s e f u n c -t i o n s e v e r y c o m p i l e r h a s a d e v i c e w i t h i n i tc a l l e d a parser .A c o n t e x t - f r e e g r a m m a r c a n b e u s e d toh e l p sp e c if y t h e s y n t a x o f a p r o g r a m m i n gl a n g u a g e . I n a d d i t i o n , i f t h e g r a m m a r is d e -s i g n e d c a r e f u l l y , m u c h o f t h e s e m a n t i c s o ft h e l a n g u a g e c a n b e r e l a t e d t o t h e r u l e s o ft h e g r a m m a r .T h e r e a r e m a n y d i f fe r e n t t y p e s o f p a r se r sf o r c o n t e x t - f r e e g r a m m a r s . I n t h i s p a p e r w e

    s h a l l r e s t r i c t o u r s e l v e s t o a c l a s s o f p a r s e r sk n o w n a s L R p a r s e r s . T h e s e p a r s e r s a r ee f f i c i e n t a n d w e l l s u i t e d f o r u s e i n c o m p i l e r sf o r p r o g r a m m i n g l an g u a g es . P e r h a p s m o r ei m p o r t a n t is t h e f a c t t h a t w e c a n a u t o m a t i -c a l l y g e n e r a t e L R p a r s e r s f o r a l a r g e a n d u s e -f u l cl as s o f c o n t e x t - f r e e g r a m m a r s . T h e p u r -p o s e o f t h i s a r t i c le is t o s h o w h o w L R p a r s e r sc a n b e g e n e r a t e d f r o m c e r t a i n c o n t e x t - f r e eg r a m m a r s , e v e n s o m e a m b i g u o u s o n e s. A ni m p o r t a n t f e a t u r e o f t h e p a r s e r g e n e r a t i o na l g o r i t h m i s t h e a u t o m a t i c d e t e c t i o n o fa m b i g u i t i e s a n d d i f f ic u l t - to - p a r s e c o n s t r u c t si n t h e l a n g u a g e s p e c i f i c a t i o n .W e b e g i n t h i s p a p e r b y s h o w i n g h o w ac o n t e x t - f r e e g r a m m a r d e f i n e s a l a n g u a g e .W e t h e n d i s c u s s L R p a r s i n g a n d o u t l i n e t h ep a r s e r g e n e r a t i o n a l g o r i t h m . W e c o n c l u d eb y s h o w i n g h o w t h e p e r f o r m a n c e o f L Rp a r se r s c a n b e i m p r o v e d b y a f e w s i m p let r a n s f o r m a t i o n s , a n d h o w e r r o r r e c o v e r y a n d" s e m a n t i c a c t i o n s " c a n b e i n c o r p o r a t e d i n t ot h e L R p a r s i n g f r a m e w o r k .F o r t h e p u r p o s e s o f t h i s p a p e r , a sentencei s a s t r i n g o f t e rmina l symbol s . S e n t e n c e s a r ew r i t t e n s u r r o u n d e d b y a p a i r o f s in g l e q u o t e s .F o r e x a m p l e , ' a ' , 'ab', a n d ' , ' a r e s e n t e n c e s .T h e e m p t y s e n t e n c e is w r i t t e n " . T w o s e n -t e n c e s w r i t t e n c o n t i g u o u s l y a r e t o b e c o n -c a t e n a t e d , t h u s ' a ' 'b ' i s s y n o n y m o u s w i t h

    Com puting Surveys, Vol 6, No 2, June 1974

  • 8/9/2019 LR Parsing - Aho

    2/26

    1 0 0 A . V . A h o a n d S . C . J o h n s o nC O N T E N T S 'ab' . I n t h i s p a p e r t h e t e r m language m e r e l ym e a n s a s e t o f s e n t e n c e s .

    1 l n t r o d u c t m n2 G r a m m a r s3 D e r l v a t m n T r e e s4 P a r s e r s5 R e p r e s e n t i n g t h e P a r s in g A c t m n a n d G o t o T a b l e s6 C o n s t r u c t m n o f a P a r s e r f r o m a G r a m m a r

    6 I S e t s o f I t e m s6 2 C o n s t r u c t i n g t h e C o l l e c tm n o f A cc e s m b le S e t s o fI t e m s

    6 3 C o n s t r u c t i n g t h e P a r s i n g A c t m n a n d G o t oT a b l e s f r o m t h e C o l l e c t m n o f S e t s o f I t e m s

    6 4 C o m p u t i n g L o o k a h e a d S e t s7 P a r s in g A m b i g u o u s G r a m m a r s8 0 p t l m l z a t m n o f L R P a rs e rs

    8 1 M e r g i n g I d e n t m a l S t a t e s8 2 S u b s u m i n g S t a te s8 3 E h m m a t m n o f R e d u c t m n s b y S in g le P r o d u c t m n s9 E r r o r R e c o v e r y

    l 0 O u t p u t11 C o n c l u d i n g R e m a r k sR e f e r e n c e s

    Co pyr ig h t (~) 1974 , A ssoc i a t i on fo r Co m pu t in gM a c h i n e r y , I n c G e n e r a l p e r m i s s i o n t o r e p u b h s h ,bu t no t f o r p ro f i t , a l l o r pa r t o f t h in ma te rml i sg r a n t e d , p r o v i d e d t h a t A C M ' s c o p y r i g h t n o t i c e i sg iven and t ha t r e f e r ence i s made t o t h i s pub l i ca -t i on , t o i t s da t e o f i s sue , and t o t he f ac t t ha t r e -p r i n t i n g p r i w l e g e s w e r e g r a n t e d b y p e r m i s s i o n o ft h e A s s o c i a t i o n f o r C o m p u t i n g M a c h i n e r y .

    2 . G R A M M A R SA g r a m m a r is u s e d t o d e f in e a l a n g u a g e a r idt o i m p o s e a s t r u c t u r e o n e a c h s e n t e n c e i nt h e l a n g u a g e . W e s h a l l b e e x c l u s i v e l y c o n -c e r n e d w i t h context - free grammars, s o m e t i m e sc a ll e d B N F ( fo r B a c k u s - N a u r f o r m ) s pe ci fi -c a t i o n s .

    I n a c o n t e x t - f re e g r a m m a r , w e s p ec i fyt w o d i s j o i n t s e t s o f s y m b o l s t o h e l p d e f in e al a n g u a g e . O n e i s a s e t o f n o n t e r m i n a l s y m b o l s .W e sh a ll r e p re s e n t a n o n t e r m i n a l s y m b o l b ya s t r in g o f o n e o r m o r e c a p i t a l r o m a n l e t te r s .F o r e x a m p l e , L I S T r e p r e s e n t s a n o n t e r m i n a la s d o e s t h e l e t t e r A . I n t h e g r a m m a r , o n en o n t e r m i n a l is d i s t i n g u i s h e d a s a s tar t (o rsentence) s y m b o l .T h e s e c o n d s e t o f s y m b o l s u s e d i n a c o n -t e x t - f re e g r a m m a r is t h e s e t o f t e r m z n a lsymbol s . T h e s e n t en c e s o f t h e l a n g u a g e g e n -e r a t e d b y a g r a m m a r w il l c o n t a i n o n l yt e r m i n a l s y m b o l s . W e s h al l re f e r t o a t e r m i -h a l o r n o n t c r m i n a l s y m b o l a s a g r a m m a rsymbol .A c o n t e x t - f r e e g r a m m a r i ts e lf c o n s is t s o f af i n i te s e t o f r u l e s c a l l e d productzons. Ap r o d u c t i o n h a s t h e f o r m

    l e f t - s i d e ~ r i g h t - s i d e ,w h e r e l e f t- s id e i s a s in g l e n o n t e r m i n a l s y m b o l( s o m e t i m e s c a l le d a s y n t a c t i c c a t e g o r y ) a n dr i g h t - si d e i s a s t r i n g o f z e r o o r m o r e g r a m m a rs y m b o l s . T h e a r r o w i s s i m p l y a s p e c i a ls y m b o l t h a t s e p a r a te s t h e le f t a n d r i g h ts id e s . F o r e x a m p l e ,

    L I S T ~ L I S T ' , ' E L E M E N Tis a p r o d u c t i o n i n w h i c h L I S T a n d E L E -M E N T a re n o n t e r m i n a l s ym b o l s , a n d t h eq u o t e d c o m m a r e p r es e n t s a t e r m i n a l s y m -b o l .A g r a m m a r i s a re w r i t i n g s y s t e m . I f a A ' ri s a s t r i n g o f g r a m m a r s y m b o l s a n d A --+ flis a p r o d u c t i o n , t h e n w e w r i te

    ~ A - y ~ a ~ 7a n d s a y t h a t aA'y direc t ly derives a~ 'y . As e q u e n c e o f s tr i n g s

  • 8/9/2019 LR Parsing - Aho

    3/26

    s0 , S l , - - - , Sns u ch t h a t s , - ~ ~ s , f o r 1 ~< i ~< n i s s a i d t ob e a d e r w a l w n o f s ~ f r o m ~ 0 . W e s o m e t i m e sa l s o s a y s ~ i s derivable f r o m s 0.T h e s t a r t s y m b o l o f a g r a m m a r is c a ll ed asen ten t ,a l form. A s t r i n g d e r i v a b l e f r o m t h es t a r t s y m b o l i s a l s o a s e n t e n t m l f o r m o f t h eg r a m m a r . A s e n t e n t ia l f o r m c o n t a i n i n g o n l yt e r m i n a l s y m b o l s is s a id t o b e a sentenceg e n e r a t e d b y t h e g r a m m a r . The languagegenera ted by a grammar ( ; , o f t e n d e n o t e db y L ( G ) , i s t h e s e t o f s e n t e n c e s g e n e r a t e d b yG . E x a m p l e 2 .1 : T h e f o ll o w in g g r a m m a r ,h e r e a f t e r c a l l e d G ~, h a s L I S T a s it s s t a r ts y m b o l :

    L I S T --~ L I S T ' , ' E L E M E N TL I S T --* E L E M E N TE L E M E N T ~ ' a 'E L E M E N T --~ 'b 'T h e s e q ue n c e :

    L I S T ~ L I S T ' , ' E L E M E N TL I S T ' , a 'L I S T ' , ' E L E M E N T ' , a 'L I S T ' ,b,a'E L E M E N T ' ,b,a''a ,b ,a '

    i s a d e r i v a t i o n o f t h e s e n t e n c e 'a ,b ,a ' . L(G~)c o n s i s t s o f n o n e m p t y s t r i n g s o f a ' s a n d b ' s ,s e p a r a t e d b y c o m m a s .N o t e t h a t i n t h e d e r i v a t io n i n E x a m p l e2 .1 , t h e r i g h t m o s t n o n t e r m i n a l i n e a c h s en -t e n t i a l f o r m i s r e w r i t t e n t o o b t a i n t h e f o l-

    l o w i n g s e n t e n t ia l f o r m . S u c h a d e r i v a t i o n iss a i d t o b e a r~ghlm ost der~valzo~ a n d e a c h s e n-t e n t i a l f o r m i n s u c h a d e r i v a t i o n i s c a l l e d amght se~le~t~al form. F o r e x a m p l e ,L I S T ' ,b,a'

    i s a r i g h t s e n t e n t i a l f o r m o f C 1.I f s A w i s a r ig h t s e n t e n t i a l f o r m i n w h i c hw i s a s tr i n g o f t e r m i n a l s y m b o l s , a n d s A w ~s ~ w , t h e n ~ i s s a i d t o b e a handle o f s ~ w *F o r e x a m p l e , 'b ' i s t h e h a n d l e o f th e r i g h ts e n t e n t i a l f o r mL I S T ' ,b,a'

    i n E x a m p l e 2 . 1 . So me au t h o r s u s e a mo re r e s t mc t mg d eh n l t mn o fh an d l e

    L R P a r s i n g 101A p r e f i x o f a ~ i n t h e r i g h t s e n t e n t i a l f o r maf3w i s s a i d t o b e a wable pre f ix o f t h e g r a m -m a r . F o r e x a m p l e ,

    L I S T ' , 'i s a v i a b l e p r e f i x o f G 1 , s i n c e i t i s a p r e f i x o ft h e r i g h t s e n t e n t i a l f o r m ,

    L I S T ' , ' E L E M E N T( B o t h s a n d w a r e n u ll h e r e .)R e s t a t i n g t h i s d e f in i ti o n , a v i a b l e p r e fi x o fa g r a m m a r i s a n y p r e f ix o f a r i g h t s e n t e n t i a lf o r m t h a t d o e s n o t e x t e n d p a s t t h e r i g h t e n do f a h a n d l e i n t h a t r i g h t s e n t e n t i a l f o r m .T h u s w e k n o w t h a t t h e r e is a l w a y s s o m es t ri n g o f g r a m m a r s y m b o l s t h a t c a n b e a p-p e n d e d t o t h e e n d o f a v i a b l e p r e f ix to o b -t a i n a r i g h t s e n t e n t i a l f o r m . V i a b l e p r e f i x e sa r c i m p o r t a n t i n th e c o n s t r u c t i o n o f c o m -p i l e r s w i t h g o o d e r r o r - d e t e c t i n g c a p a b i l i t i e s ,a s l o n g a s t h e p o r t i o n o f t h e i n p u t w e h a v es e e n c a n b e d e r i v e d f r o m a v i a b l e p r e f i x ,w e c a n b e su r e t h a t t h e r e a r e n o e r r o r s t h a tc a n b e d e t e c t e d h a v i n g s c a n n e d o n l y t h a tp a r t o f t h e i n p u t .

    3 . D E R I V A T I O N T R E E SF r e q u e n t l y , o u r i n t e r e st i n a g r a m m a r isn o t o n l y i n t h e l a n g u a g e i t g e n e r a t e s , b u ta l so i n t h e s t r u c t u r e i t i m p o s e s o n t h e s e n -t e n c e s o f t h e l a n g u a g e . T h i s i s t h e c a s e b e -c a u s e g r a m m a t i c a l a n a l y s i s i s c l o s e l y c o n -n e c t e d w i t h o t h e r p r o c e s s e s , s u c h a s c o m p i l a -t io n a n d t r a n s l a ti o n , a n d t h e t r a n s l a t io n s o ra c t i o n s o f t h e o t h e r p r o c e s s e s a r e f r e q u e n t l yd e f i n e d in t e r m s o f t h e p r o d u c t i o n s o f t h eg r a m m a r . W i t h t h is in m i n d , w e t u r n o u ra t t e n t i o n t o t h e r e p r e s e n t a t i o n o f a d e r iv a -t i o n b y i t s demvatwn tree .

    F o r e a c h d e r i v a t i o n in a g r a m m a r w e c a nc o n s t r u c t a c o r r e s p o n d i n g d e r i v a t i o n t r e e .L e t u s c o n s i d e r t h e d e r i v a t i o n i n E x a m p l e2 .1 . T o m o d e l t h e f ir s t s t e p o f th e d e r i v a t i o n ,i n w h i c h L I S T i s r e w r i t t e n a s

    L I S T ' , ' E L E M E N Tu s i n g p r o d u c t i o n 1 , w e f i r s t c r e a t e a r o o tl a b el e d b y t h e s t a r t s y m b o l L I S T , a n d t h e nc r e a t e t h r e e d i r e c t d e s c e n d a n t s o f t h e r o o t ,l a b el e d L I S T , ' , ' , a n d E L E M E N T :

    Cor apu tmg Surveys, Vol 6, No 2, June 1974

  • 8/9/2019 LR Parsing - Aho

    4/26

    10 2

    (

    A . V . A h o a n d S . C . J o h n s o n

    ,ST ) ~) -""( W e f ol lo w h is t o r ic a l u s a g e a n d d r a w o u r" r o o t " n o d e a t t h e t o p . ) I n t h e s e c o n d s t e po f t h e d e r iv a t io n , E L E M E N T is r e w r i t te na s 'a ' . T o m o d e l t h i s s t e p , w e c r e a t e a d i r e c td e s c e n d a n t l a b e l e d 'a ' f o r t h e n o d e l a b e l e dE L E M E N T :

    L IST )( L I S T ~ ( ~ ~LE M E N T~)

    C o n t i n u i n g i n t h i s f a s h i o n , w e o b t a i n t h ef o l l o w i n g t r e e :

    N o t e t h a t i f a n o d e o f t h e d e r i v a t i o n t r e e isl a b e le d w i t h a n o n t e r m i n a l s y m b o l A a n d i t sd i r e c t d e s c e n d a n t s a r e l a b e l e d X 1, X 2, - . . ,X , , t h e n t h e p r o d u c t i o n .A - - ~ X 1 X 2 . . . X n

    m u s t b e in th e g r a m m a r .I f a ~, a 2, . . . , a m a r e t h e l a b e l s o f a ll t h el e a v e s o f a d e r i v a t i o n t r e e , i n t h e n a t u r a ll e f t - t o - r i g h t o r d e r , t h e n t h e s t r i n gal a2 am

    i s c a l l e d t h e f r o n t i e r o f t h e t r e e . F o r e x a m p l e ,' a , b , a ' i s t h e f r o n t i e r o f t h e p r e v i o u s t r e e .C l e a r l y , f o r e v e r y s e n t e n c e i n a l a n g u a g e

    t h e r e i s a t l e a s t o n e d e r i v a t i o n t r e e w i t ht h a t s e n t e n c e a s i ts fr o n t ie r . A g r a m m a r t h a ta d m i t s tw o o r m o r e d i s t i n c t d e r i v a t i o n t r e e sw i t h t h e s a m e f r o n t i e r i s s a i d t o b e a m b i g u -o u s .E x a m p l e 3 .1 : T h e g r a m m a r G 2 w i t h p r o -d u c t i o n s

    L I S T --* L I S T ' , ' L I S TL I S T --~ 'a 'L I S T - - * ' b 'i s a m b i g u o u s b e c a u s e t h e f o l l o w i n g t w od e r i v a t i o n t r e e s h a v e t h e s a m e f r o n t i e r .

    ( LIST) ST ~ ~ .,ST~@ < ,,s , ~ ,,ST @ )

    LISTG

    LIST )L,,T ~ ~ ,,,T~ ~ L,,T~ ~ )

    I n c e r t a i n s i t u a t i o n s a m b i g u o u s g r a m m a r sc a n b e u s e d to r e p r e s e n t p r o g r a m m i n gl a n g u ag e s m o r e e c o n o m i c a l l y th a n e q u i v a -l e n t u n a m b i g u o u s g r a m m a r s . H o w e v e r , i f a na m b i g u o u s g r a m m a r is u se d , t h e n s o m e o t h e rr u l e s s h o u l d b e s p e c i f i e d a l o n g w i t h t h eg r a m m a r t o d e t e r m i n e w h i c h o f s e v e ra ld e r i v a t i o n t r e e s i s t o b e a s s o c i a t e d w i t h ag i v e n i n p u t . W e s h a ll h a v e m o r e t o s a ya b o u t a m b i g u o u s g r a m m a r s in S ec t io n 7 .

    4. PARSERSW e c a n c o n s i d er a p a r s e r f o r a g r a m m a r t o b ea d e v ic e w h ic h , w h e n p r e s e n t e d w i t h a ni n p u t s t r i n g , a t t e m p t s t o c o n s t r u c t a d e r i v a -

    C o r a p u t m g S u r v e y s , V o l 6 , N o . 2 , J u n e 1 97 4

  • 8/9/2019 LR Parsing - Aho

    5/26

    tion tree whose frontier matches the input.If the parser can construct such a derivationtree, then it will have verified that the inputstring is a sentence of the language genera tedby the grammar. If the input is syntacticallyincorrect, then the tree construction processwill not succeed and the positions at whichthe process falters can be used to indicatepossible error locations.A parser can operate in many differentways. In this paper we shall restrict ourselvesto parsers that examine thc input stringfrom left to right, one symbol at a time.These parsers will attempt to construct thederivation tree "bottom-up"; i.e., from theleaves to the root. For historical reasons,these parsers are called L R p a r s e r s . The "L"stands for "left-to-right scan of the input",the "R" stands for "nghtmost derivation."We shall see that an LR parser operates byreconstructing the reverse of a rightmostderivation for the input. In this section weshall describe in an informal way how a cer-tain class of LR parsers, called LR(1)parse-% operate.An LR parser deals with a sequence ofpartially built trees during its tree construc-tion process. Wc shall loosely call this se-quence of trees a fo r e s t . In our framework theforest is built from left to right as the inputis read. At a particular stage in the construc-tion process, we have read a certain amountof the input, and we have a partially con-structed derivation tree. For example, sup-pose that we are parsing the input string' a , b ' according to the grammar ( i x . Afterreading the first 'a' we construct the tree:

    QThen we construct:

    ~LEMENT~

    L R P a r s i n g 103using the product ion, -.

    E L E M E N T - ~ ' a 'To reflect this parsing action, we say that 'a'is r e d u c e d t o ELEMENT. Next we use theproduction

    LIST --~ ELEMENTto obtain the tree:

    I( E L EM E N T ~Here, ELEMENT is reduced to LIST. Wethen read the next input symbol ', ', andadd it to the forest as a one node tree.

    LIST )I,( E L E M E N T ~

    We now have two trees. These trees willeventually become sub-trees in the finalderivation tree. We then read the next inputsymbol 'b' and create a single node tree forit as well'

    LIST )I

    ~ L E M E N T ~ QUsing the production,

    ELEMENT --~ 'b '

    C o m p u t i n g S u r v e y s , V o l 6 , N o 2 , J u n e 1 9 74

  • 8/9/2019 LR Parsing - Aho

    6/26

    10 4 A . V . A h o a n d S . C . J o h n s o nw e r e d u c e 'b ' t o E L E M E N T t o o b t a in :

    L I S T )I~ L E M E N T ~ ~ E L E M E N T ~

    F i n a l l y , u s i n g t h e p r o d u c t i o nL I S T --~ L I S T ' , ' E L E M E N T

    w e c o m b i n e t h e s e t h r e e t r e e s i n t o t h e f i n a lt r e e : (I

    / 0 , i t i s c a l l e d a nL R ( k ) p a r s e r . T o h e l p t o m a k e i t s p a r s i n gd e c i s i o n s , a n L R p a r s e r a t t a c h e s t o t h e r o o to f e a c h t r e e i n t h e f o r e s t a n u m b e r c a l l e d as ta te . T h e n u m b e r o n t h e r o o t o f t h e r ig h t -m o s t t r e e i s c a l l e d t h e c u r r e n t s t a t e . I n a d d i -t i o n , t h e r e i s a n r e , h a l s t a t e t o t h e l e f t o f t h ef o r e s t , w h i c h h e l p s d e t e r m i n e t h e v e r y f i r s t

    Comput ing Surveys, Vol 6, No 2, June 1974

  • 8/9/2019 LR Parsing - Aho

    7/26

    p a r s i n g a c t i o n . W e s h a l l w r i t e t h e s t a t e s i np a r e n t h e s e s a b o v e t h e a s s o c ia t e d ro o t s . F o re x a m p l e ,

    ( 0 )

    ( I )( U S T )I@ LEMENT~ ( 5 )

    r e p r e s e n t s a f o r e s t w i t h s t a t e s . S t a t e 5 i s t h ec u r r e n t s t a t e , a n d s t a t e 0 is t h e i n i t ia l s t a t e .T h e c u r r e n t s t a t e a n d t h e n e x t i n p u t s y m b o ld e t e r m i n e t h e p a r s i n g a c t io n o f a n L R ( 1 )p a r s e r .T h e f o l l o w i n g t a b l e s h o w s t h e s t a t e s o f a nL R ( 1 ) p a r s e r f o r G 1, a n d t h e a s s o c i a t e d p a r s -i n g a c t i o n s . I n t h i s t a b l e t h e r e i s a c o l u m nl a b e l e d ' $ ' w i t h s p e c i a l si g n if ic a n c e . T h e ' $ 's t a n d s f o r t h e r igh t endmarker , w h i c h i sa s s u m e d t o b e a p p e n d e d t o t h e e n d o f a lli n p u t s tr in g s . A n o t h e r w a y o f l o o k in g a t t h i si s t o t h i n k o f ' $ ' a s r e p r e s e n t i n g t h e c o n d i -t i o n w h e r e w e h a v e r e a d a n d s h i f t e d a l l o ft h e " r e a l " c h a r a c t e r s in th e i n p u t s t r in g .

    012C u r r e n t 3S t a t e 456

    FIG. I.

    Nex t In p u t Sy mb o l' a ' 'b' ', ' '$'

    sh i f ter rorer rore r ro rer rorsh i f te r ro r

    s h i f te r r o re r r o re r r o rerrorsh i f ter ror

    e r ro rsh i f tRed. 2Red. 3Red. 4er rorRed. 1

    er roracceptRed 2Red. 3Red. 4e r ro rRed 1Pars i ng Act ion Table fo r Gt

    T h e r e d u c e a c t i o n s a re re p r e s e n t e d a s" R e d . n " i n t h e a b o v e t a b l e ; t h e i n t e g e r nr e f e r s t o t h e p r o d u c t i o n s a s fo l lo w s :(1 ) L I S T - ~ L I S T ' , ' E L E M E N T(2 ) L I S T - -+ E L E M E N T(3 ) E L E M E N T --+ ' a '(4 ) E L E M E N T --~ ' b '

    W e s h a l l r e f e r t o t h e e n t r y f o r r o w s a n dc o l u m n c a s p a ( s , c ) . A f t e r m a k i n g e i t h e r a

    L R P a r s i n g 10 5s h i ft m o v e o r a re d u c e m o v e , t h e p a r s e rm u s t d e t e r m i n e w h a t s t a t e t o a t t a c h t o t h er o o t o f t h e t r e e t h a t h a s j u s t b e e n a d d e d t ot h e f o r e s t . I n a s h i f t m o v e , t h i s s t a t e i s d e -t e r m i n e d b y t h e c u r r e n t s t a t e a n d t h e i n p u ts y m b o l t h a t w a s j u s t s h i f t ed i n t o t h e f o r e st .

    F o r e x a m p l e , if w e h a v e j u s t s h i f t e d ' , 'i n t o t h e f o r e s t

    (1 )

    Io, t h e n s t a t e 1 a n d ' , ' d e t e r m i n e t h e s t a t e t o b ea t t a c h e d t o t h e n e w r i g h t m o s t r o o t ' , ' .I n a r e d u c e m o v e , s u p p o s e w e re d u c e b yp r o d u c t i o n

    A --* X 1 X 2 . . . X ,W h e n w e m a k e n o d e s X 1 , . - . , X ,, d i r e c td e s c e n d a n t s o f t h e r o o t A , w e r e m o v e t h es t a t e s t h a t w e r e a t t a c h e d t o X 1, . . . , X n .T h e s t a t e t h a t i s t o b e a t t a c h e d t o n o d e Ais d e t e r m i n e d b y t h e s t a t e t h a t i s n o w t h er i g h t m o s t s t a t e i n t h e f o r es t , a n d t h e n o n -t e r m i n a l A . F o r e x a m p l e , if w e h a v e j u s tr e d u c ed b y t h e p r o d u c t io n

    L I S T - ~ L I S T ' , ' E L E M E N Ta n d c r e a t e d t h e f o r e s t

    (0)

    (C " , s t ? "L E E N , )

    GLIST )I G

    t h e n s t a t e 0 a n d t h e n o n t e r m i n a l L I S T d e -t e r m i n e t h e s t a t e t o b e a t t a c h e d t o t h e r o o tL I S T . N o t e t h a t t h e s t a te s p r e v io u s l y a t -t a c h e d t o th e d i r e c t d e s c e n d a n t s o f t h e n e w

    Computing Surveys, Vol 6, No. 2, June 1974

  • 8/9/2019 LR Parsing - Aho

    8/26

  • 8/9/2019 LR Parsing - Aho

    9/26

  • 8/9/2019 LR Parsing - Aho

    10/26

    108 A . V . A h o a n d S . C . J o h n s o nActwn Stack Input

    5 0 1 5 4 '$ '6 0 1 5 6 '$ '7 0 1 '$ '8 0 1 '$ 'T h u s , t h e p a r s e r c o n t r o l is i n d e p e n d e n t o ft h e tr e e s, a n d d e p e n d s o n l y o n a s t a c k o fs t a te s . I n p r a c ti c e , w e m a y n o t n e e d to c o n -s t r u c t t h e d e r i v a t i o n t r e e e x p l i c it l y , if t h et r a n s l a t i o n b e i n g p e r f o r m e d i s s u f f i c i e n t l ys i m p l e . F o r e x a m p l e , i n S e c t i o n 1 0 , w e m e n -t io n a c l a s s o f u s e f u l t r a n s l a t i o n s t h a t c a nb e p e r fo r m e d b y a n L R p a r s e r w i t h o u t r e -q u i r i n g t h e f o r e s t t o b e m a i n t a i n e d .I f w e w i s h t o b u i l d t h e d e r i v a t i o n t r e e , w e

    c a n e a s il y d o s o b y s t a c k i n g , a l o n g w i t h e a c hs t a t e , t h e r o o t o f t h e t r e e a s s o c i a t e d w i t h t h a ts t a t e .

    5 . R E PR E S E N T IN G T H E P A R S IN G A C T I O N A N DG O T O T A B L E S

    S t o r i n g t h e f u l l a c t i o n a n d g o t o t a b l e ss t r a i g h t f o r w a r d l y a s m a t r i c e s i s e x t r e m e l yw a s t e f u l o f s p a c e f o r l a r g e p a r s e r s . F o r e x -a m p l e , t h e g o t o t a b l e i s t y p i c a l l y n e a r l y a l lb l a n k . I n t h i s s e c ti o n w e d is c u s s s o m e s i m p l ew a y s o f c o m p a c t i n g t h e s e t a b l e s w h i c h l e a dt o s u b s t a n t i a l s a v i n g s o f s p a c e ; in e f fe c t, w ea r e m e r e l y r e p r e s e n t in g a s p a r s e m a t r i x m o r ec o m p a c t l y , u s i n g a p a r t i c u l a r e n c o d in g .

    L e t u s b e g i n w i t h t h e s h i f t a c t i o n s . I f x isa t e r m i n a l s y m b o l a n d s i s a s t a te , t h e p a r s i n ga c t i o n o n x i n s t a t e s i s s h i f t i f a n d o n l y i fg o t o ( s , x ) is n o n b l a n k . W e w i ll en c o d e t h eg o t o i n t o t h e s h i f t a c t i o n , u s i n g t h e n o t a t i o n

    s h i f t 17a s a s h o r t h a n d f o r " s h i ft a n d a t t a c h s t a t e 17t o t h e n e w n o d e . " B y e n c o d in g t h e g o t o s o nt e r m i n a l s y m b o l s a s p a r t o f t h e a c t i o n t a b l e ,w e n e e d o n l y c o n s i d e r t h e g o t o s o n n o n -t e r m i n a l s y m b o l s . W e w il l e n c o d e t h e m b yc o l u m n s ; i . e . , b y n o n t e r m i n a l s y m b o l n a m e .I f , o n a n o n t e r m i n a l s y m b o l A , t h e r e a r en o n b l a n k e n t r i e s i n t h e g o t o t a b l e c o r r e -s p o n d i n g t o s t a t e s s ~, s2, , sn , a n d w e h a v es , ' = g o t o ( s , , A ) , f o r i = 1 , . - . , nt h e n w e sh a l l e n c o d e t h e c o l u m n f o r A i n ap s e u d o - p r o g r a m m i n g l a ng u a g e :

    A : i f ( s t a t e = sl ) goto = s l 'i f ( s t a te = s n ) g o t o = s ~'

    T h e g o t o t a b l e o f G 1 w o u l d b e r e p r e s e n t e d i nt h i s f o r m a t a s :L I S T : i f ( s t a t e = 0 ) g o t o = 1E L E M E N T : i f ( s t a te = 0 ) g o t o = 2i f ( s t a t e = 5 ) g o t o = 6

    I t t u r n s o u t t h a t [ A ho a n d U l l m a n ( 1 97 2 b )]w h e n e v e r w e d o a g o t o o n A , t h e s t a t e w i l la l w a y s b e o n e o f s l, , sn , e v e n i f t h e i n p u ts t r i n g i s i n e r ro r . T h u s , o n e o f t h e s e b r a n c h e sw i ll a l w a y s b e t a k e n . W e s h a ll r e t u r n t o t h i sp o i n t l a t e r i n t h i s s e c t io n .W e s h a l l e n c o d e p a r s i n g a c t i o n s i n t h es a m e s p i ri t, b u t b y r o w s o f t h e t a b l e . T h ep a r s i n g a c t i o n s f o r a s t a t e s w i l l a l s o b er e p r e s e n t e d b y a s e q u e n c e o f p s e u d o - p r o -g r a m m i n g l an g u a g e s t a t e m e n t s . I f th e i n p u ts y m b o l s a l , . . . , a= h a v e t h e a s s o c i a te da c t i o n s a c t i o n l , . . . , a c ti o n n , t h e n w e w i llw r i t e :s : i f ( i n p u t = a l ) a c t i o n 1

    i f ( i n p u t = a n) a c t i o n nA s w e m e n t i o n e d e a r li e r, w e s h a ll a t t a c hg o t o ( s , a , ) o n t o t h e a c t i o n i f a c ti on ~ is s h i ft .S i m i l a rl y , i f w e h a v e a r e d u c t i o n b y t h ep r o d u c t i o n A --* a , w e w i ll u s u a l l y w r i t e

    r e d u c e b y A --~ aa s t h e a c t i o n .

    F o r e x a m p l e , t h e p a r s i n g a c t i o n s fo r s t a t e1 i n t h e p a r s e r f o r G ~ a r e r e p r e s e n t e d b y :1 : i f ( i n p u t = ' a ' ) e r r o ri f ( i n p u t = 'b ' ) e r r o ri f ( i n p u t = ' , ' ) s h i f t 5i f ( in p u t = ' $ ') a c c e p t

    A t f i r s t g l a n c e t h i s i s n o s a v i n g o v e r t h et a b l e , s i n c e t h e p a r s i n g a c t i o n t a b l e i su s u a l l y n e a r l y f u l l . W e m a y m a k e a l a r g es a v i n g , h o w e v e r , b y i n t r o d u c i n g t h e n o t i o no f a d e f a u l t a c t i o n in th e s t a t e m e n t s . Ad e f a u l t a c t i o n is s i m p l y a p a r s i n g a c t i o nw h i c h i s d o n e i r r e s p e c t i v e o f t h e i n p u t c h a r -a c t e r ; t h e r e m a y b e a t m o s t o n e o f t h e s e i ne a c h s t a t e , a n d i t w ill b e w r i t t e n l a st . T h u s ,i n s t a t e 1 w e h a v e t w o e r r o r a c t io n s , a s h i f t

    C o m p u t i n g S u r v e y s , Vol 6 , N o 2 , J u n e 1 9 7 4

  • 8/9/2019 LR Parsing - Aho

    11/26

    a c t io n , a n d a n a c c e p t a c t i o n , w e s h al l m a k et h e e r r o r a c t i o n t h e d e f a u l t . W e w il l w r i t e :1 : i f ( i n p u t = ' , ') s h i f t 5i f ( in p u t = $ ) a c c e p t

    e r r o rT h e r e i s a n a d d i t i o n a l s a v i n g w h i c h i sp o s s i b l e . S u p p o s e a s t a t e h a s b o t h e r r o r a n dr e d u c e e n t ri e s. T h e n w e m a y r e p l a c e a lle r r o r e n tr i e s in t h a t s t a t e b y o n e o f t h e r e -d u c e e n t r i e s . T h e r e s u l t i n g p a r s e r m a y m a k ea s e q u e n c e o f r e d u c t i o n s w h e r e t h e o r i g i n a lp a r s e r a n n o u n c e d e r r o r b u t t h e n e w p a r s e rw i l l a n n o u n c e e r r o r b e f o r e s h i f t i n g t h e n e x ti n p u t s y m b o l . T h u s b o t h p a r s e r s a n n o u n c e

    e r r o r a t t h e s a m e p o s i t i o n i n t h e i n p u t , b u tt h e n e w p a r s e r m a y t a k e s l i g h t l y l o n g e r b e -f o r e d o i n g s o .T h e r e is a b e n e f i t to b e h a d f r o m t h i s m o d i -f i c a ti o n ; t h e n e w p a r s i n g a c t i o n t a b l e w i l l r e -q u i r e l e ss s p a c e t h a n t h e o r i g i n al . F o re x a m p l e , s t a t e 2 o f t h e p a r s i n g a c t i o n t a b l ef o r G 1 w o u l d o r i g i n a ll y b e r e p r e s e n t e d b y :2 : i f ( i n p u t = ' a ' ) e r r o ri f ( i n p u t = ' b ' ) e r r o r

    i f ( in p u t = ' , ' ) r e d u c e 2i f ( in p u t = ' $ ') r e d u c e 2A p p l y i n g t h is t r a n s f o r m a t i o n , s t a t e 2 w o u l db e s i m p l y r e p r e s e n t e d a s :

    2 : r e d u c e 2T h u s i n a s t a t e w i t h r e d u c e a c t i o n s , w ew i l l a l w a y s h a v e t h e s h i f t a n d a c c e p t a c t i o n sp r e c e d e t h e r e d u c e a c t i o n s . O n e o f t h e r e d u c e

    a c t i o n s w ill b e c o m e a d e f a u l t a c t i o n , a n d w ew i l l i g n o r e t h e e r r o r e n t r i e s . I n a s t a t e w i t h -o u t r e d u c e a c t i o n s , t h e d e f a u l t a c t i o n w i l l b ee r r o r . W e s h a l l d i s c u s s o t h e r m e a n s o f c u t -t i n g d o w n o n t h e s iz e o f a p a r s e r i n S e c t i o n 8 .

    6 . C O N S T R U C T I O N O F A P AR SE R F R O M AG R A M M A R

    H o w d o w e c o n s t r u c t t h e p a r s i n g a c t i o n a n dg o t o t a b l e s o f a n L R ( 1 ) p a r s e r f o r a g i v e ng r a m m a r ? I n t h i s s e c t i o n w e o u t l i n e am e t h o d t h a t w o r k s f o r a l a r g e c la s s o fg r a m m a r s c a l l e d t h e l o o k a h e a d L R ( 1 )( L A L R ( 1 ) ) g r a m m a r s .T h e b e h a v i o r o f a n L R p a r s e r, a s d e s c r ib e d

    L R P a r s i ng 1 0 9i n t h e l a s t s e c t i o n , i s d i c t a t e d b y t h e c u r r e n ts t a t e . T h i s s t a t e r e f l e c ts t h e p r o g r e s s o f t h ep a r s e , i . e . , i t s u m m a r i z e s i n f o r m a t i o n a b o u tt h e i n p u t s t r i n g r e a d t o t h i s p o i n t s o t h a tp a r s i n g d e c i s i o n s c a n b e m a d e .

    A n o t h e r w a y t o v i e w a s t a t e i s t o c o n s id e rt h e s t a t e a s a r e p r e s e n t a t i v e o f a n e q u i v a -l e n c e c l a s s o f v i a b l e p r e f i x e s . A t e v e r y s t a g eo f t h e p a r s i n g p r o c e s s , t h e s t r i n g f o r m e d b yc o n c a t e n a t in g t h e g r a m m a r s y m b o l s on th er o o t s o f t h e e x i s ti n g s u b t r e e s m u s t b e a v i-a b l e p r e f i x ; t h e c u r r e n t s t a t e i s t h e r e p r e -s e n t a t i v e o f t h e c la s s c o n t a i n i n g t h a t v i a b l ep r e f i x .6.1 Sets o f ItemsI n t h e s a m e w a y t h a t w e n e e d e d t o d is c us sp a r t i a l l y b u i l t t re e s w h e n t a l k i n g a b o u t p a r s -i n g, w e w i ll n e e d t o t a l k a b o u t " p a r t i a l l yr e co g n iz e d p r o d u c t i o n s " w h e n w e t a l k a b o u tb u i l d i n g p a r s e r s . W e i n t r o d u c e t h e n o t i o n o fi t e m * t o d e a l w i t h t h i s c o n c e p t . A n i t e m i ss i m p l y a p r o d u c t i o n w i t h a d o t ( . ) p l a c e ds o m e w h e r e i n t h e r i g h t - h a n d s id e ( p o s s i b lya t e i t h e r e n d ) . F o r e x a m p l e ,

    [ L I S T ~ L I S T ' , ' E L E M E N T ][ E L E M E N T - ~ . 'a']a r e b o t h i t e m s o f G1.W e e n c l o s e i t e m s i n s q u a r e b r a c k e t s t od i s ti n g u i sh t h e m m o r e c le a r l y f r o m p r o d u c -t i o n s .I n t u i t i v e l y , a s e t o f i t e m s c a n b e u s e d t or e p r e s e n t a s t a g e i n t h e p a r s i n g p r o c e s s ; f o re x a m p l e , t h e i t e m

    [A --~ a . f~]i n d i c a te s t h a t a n i n p u t s t r i n g d e r i v a b l e f r o ma h a s j u s t b e e n s e e n , a n d , i f w e n e x t s e e a ni n p u t s t r in g d e r i v a b l e f r o m f3, w e m a y b ea b l e t o r e d u c e b y t h e p r o d u c t i o n A --* a ft .S u p p o s e t h e p o r t i o n o f t h e i n p u t t h a t w eh a v e s e e n t o t h i s p o i n t h a s b e e n r e d u c e d t ot h e v i a b l e p r e f ix "y a. T h e n t h e i t e m [ A --*a . ~ ] i s s a i d t o b e v a l i d f o r ~ a i f ~ A i s a l s o av i a b l e p r e fi x . I n g e n e r a l , m o r e t h a n o n e i t e mi s v a l i d f o r a g i v e n v i a b l e p r e f i x ; t h e s e t o fa l l i t e m s w h i c h a r e v a l i d a t a p a r t i c u l a r* Some au thor s have used the te rm "conf igura-t ion" fo r i tem.

    Computing Surveys, Vol 6, No 2, June 1974

  • 8/9/2019 LR Parsing - Aho

    12/26

    11 0 A. V. Aho and S. C. Johnsons t a g e o f t h e p a r s e c o r r e s p o n d s t o t h e c u r r e n ts t a t e o f t h e p a r s e r .A s a n e x a m p l e , l e t u s e x a m i n e t h e v i a b l ep r e f i x

    L I S T ' , 'i n G 1 . T h e i t e m

    [ L I S T --~ L I S T ' , ' . E L E M E N T ]i s v a l i d f o r t h i s p r e f i x , s i n c e , s e t t i n g ~, t o t h ee m p t y s t r i n g a n d a t o L I S T ' , ' m t h e d e fi ni -t i o n a b o v e , w e s ee t h a t ~ L I S T ( w h ic h isl u s t L I S T ) i s a v i a b l e p r e f i x . I n o t h e r w o r d s ,w h e n t h i s i t e m i s v a l i d , w e h a v e s e e n a p o r -t i o n o f t h e i n p u t t h a t c a n b e re d u c e d t o t h ev i a b l e p r e f i x , a n d w e e x p e c t t o s e e n e x t ap o r t i o n o f t h e i n p u t t h a t c a n b e r e d u c e d t oE L E M E N T .T h e i t e m

    [ L I S T --* . E L E M E N T ]is n o t v a l i d fo r L I S T ' , ' h o w e v e r , s in c es e t t in g ~/ t o L I S T ' , ' a n d a t o t h e e m p t ys t r in g w e o b t a i n

    L I S T ' , ' L I S Tw h i c h is n o t a v i a b l e p r e f i x .T h e r e a d e r c a n ( a n d s h o u l d ) v e r i f y t h a tt h e s t a t e c o r r e s p o n d i n g t o t h e v i a b l e p r e f i xL I S T ' , ' i s a s s o c ia t e d w i t h t h e s e t o f i t e m s :[ L IS T - ~ L I S T ' , ' . E L E M E N T ][ E L E M E N T --* . 'a '][ E L E M E N T - ~ . 'b']

    I f ~ , i s a v i a b l e p r e f i x , w e s h a l l u s e V ( ' ~ ) t od e n o t e the set of items that are valid for % I f~ / i s n o t a v i a b l e p r e f i x , V ( ~ , ) w i l l b e e m p t y .W e s h a l l a s s o c i a t e a s t a t e o f t h e p a r s e r w i t he a c h s e t of v a l i d i te m s a n d c o n s t r u c t t h ee n t r i e s i n t h e p a r s i n g a c t i o n f o r t h a t s t a t ef r o m t h e s e t o f i t e m s . T h e r e i s a f in i te n u m -b e r o f p r o d u c t i o n s , t h u s o n l y a f i n it e n u m b e ro f i t e m s , a n d t h u s a fi n it e n u m b e r o f p o s s i -b le s t a te s a s s o c ia t e d w i th e v e r y g r a m m a r G .6.2 Co nstructing the C o l lec t ion o f A ccessib le

    Se ts o f I tem sW e s h a l l n o w d e s c r i b e a c o n s t r u c t i v e p r o -c e d u r e f o r g e n e r a t i n g a l l o f t h e s t a t e s a n d ,a t t h e s a m e t i m e , g e n e r a t i n g t h e p a r s i n ga c t i o n a n d g o t o t a b l e . A s a r u m f i n g e x -

    a m p l e , w e s h a ll c o n s t r u c t p a r s i n g a c t i o n a n dg o t o t a b l e s f o r G1.F i rs t , w e a u g m e n t t h e g r a m m a r w i t h t h ep r o d u c t i o nA C C E P T --~ L I S T

    w h e r e i n g e n e r a l L I S T w o u l d b e t h e s t a r ts y m b o l o f t h e g r a m m a r ( h e re G 1). A r e d u c -t i o n b y t h is p r o d u c t i o n c o r r e s p o n d s t o t h ea c c e p t a c t i o n b y t h e p a r s e r .N e x t w e c o n s t r u c t I0 = V ( " ) , t h e s e t o fi t e m s v a l i d f o r t h e v i a b l e p r e f i x c o n s i s t i n go f t h e e m p t y s tr i n g . B y d e f in i ti o n , f o r G1 t h i ss e t m u s t c o n t a i n t h e i t e m[ A C C E P T --~ . L I S T ]

    T h e d o t i n f r o n t of t h e n o n t e r m i n a l L I S Tm e a n s t h a t , a t t h is p o i n t , w e c a n e x p e c t t of i n d a s t h e r e m a i n i n g i n p u t a n y s e n t e n c ed e r i v a b le f r o m L I S T . T h u s , I 0 m u s t a l s oc o n t a i n t h e t w o i t e m s[ L I S T --~ . L I S T ' , ' E L E M E N T ][ L I S T --~ . E L E M E N T ]

    o b t a i n e d f r o m t h e t w o p r o d u c t i o n s f o r th en o n t e r m i n a l L I S T . T h e s e c o n d o f t h e i te m sh a s a d o t i n f r o n t of t h e n o n t e r m i n a l E L E -M E N T , s o w e s h o u l d a ls o a d d t o t h e in i ti a ls t a t e t h e i t e m s[ E L E M E N T --~ . 'a '][ E L E M E N T - ~ . 'b ']

    c o r r e s p o n d i n g t o t h e t w o p r o d u c t i o n s f o re l e m e n t . T h e s e f i v e i t e m s c o n s t i t u t e I 0.W e s h a l l a s s o c i a t e s t a t e 0 w i t h I 0 .N o w s u p p o s e t h a t w e h a v e c o m p u t e dV ( ~ ) , t h e s e t o f i t e m s w h i c h a r e v a l i d f o rs o m e v i a b l e p re f ix % L e t X b e a t e r m i n a l o rn o n t e r m i n a l s y m b o l . W e c o m p u t e V(~X)f r o m V ( ' y ) a s f o l l o w s :( 1) F o r e a c h i t e m o f t h e f o r m [A - -*a . X ~ ] i n V ( 'y ) , w e a d d t o V('yX)t h e i t e m [ A ~ aX . ~].(2 ) W e c o m p u t e t h e closure o f t h e s e t o fi t e m s i n V ( ~ , X ) ; t h a t i s , f o r e a c h i t e mo f t h e f o r m [ B --~ a . C ~ ] i n V ( ~ , X ) ,

    w h e r e C is a n o n t e r m i n a l s y m b o l , w ea d d t o V(~X) t h e i t e m s[C ~ . ~1 ]

    [ C ~ . a n ]

    C o m p u t i n g S u r v e y s , V o l . 6 , N o . 2 , J u n e 1 9 7 4

  • 8/9/2019 LR Parsing - Aho

    13/26

    w h e r e C - ~ a x , . . . , C - ~ a n a r e a l lt h e p r o d u c t i o n s i ll G w i t h C o n t h el e f t s i d e . I f o n e o f t h e s e i t e m s i s a l -r e a d y i n V ( ' I X ) w e d o n o t d u p l i c a t et h i s i te m . W e c o n t in u e t o a p p l y t h isp r o c e s s u n t i l n o n e w i t e m s c a n b ea d d e d t o V ( ' r X ) .

    I t c a n b e s h o w n t h a t s t e p s ( 1) a n d (2 )c o m p u t e e x a c t l y th e i t e m s t h a t a r e v a l i d fo r~ , X [ A h o a n d U l l m a n ( 1 9 7 2 a ) ] .F o r c x a m p l e , l e t u s c o m p u t e 1 1 =V ( L I S T ) , t h e s e t o f i t e m s t h a t a r e v a l i d fo rt h e v i a b le p re f i x L I S T . W e a p p l y t h e a b o v ec o n s t r u c t i o n w i t h ~, = " a n d X = L I S T , a n du s e t h e f i v c i t e m s i n I 0 .I n s t e p (1 ) o f t h e a b o v e c o n s t r u c t i o n , w ea d d t h e i t e m s

    [ A C C E P T - ~ L I S T .][ L I S T --~ L I S T . ' , ' E L E M E N T ]

    t o 1 1. S i n c e n o i t e m i n 1 1 h a s a n o n t e r m i n a ls y m b o l i m m e d i a t e l y t o t h e r i g h t o f t h e d o t ,t h e c l o s u r e o p e r a t i o n a d d s n o n e w i t e m s t o11. T h e r e a d e r s h o u l d v e r i f y t h a t t h e s e t w oi t e m s a r e t h e o n l y i te m s v a l i d f o r t h e v i a b l ep r e f i x . W e s h a l l a s s o c i a t e s t a t e 1 w i t h 1 1.N o t i c e t h a t t h e a b o v e c o n s t r u c t i o n i s c o m -p l e t e l y i n d e p e n d e n t o f ~/; i t n e e d s o n l y t h ei t e m s i n V ( ~ ), a n d X . F o r e v e r y s e t o f i t e m sI a n d e v e r y g r a m m a r sy m b o l X t h e a b o v ec o n s t r u c t i o n b u i l d s a n e w s e t o f i t e m s w h i c hw e s h a l l c a l l G O T O ( I , X ) ; t h i s i s e s s e n t i a l l yt h e s a m e g o t o fu n c t i o n e n c o u n t e r e d i n t h el a s t t w o s e c t i o n s . T h u s , i n o u r e x a m p l e , w eh a v e c o m p u t e d

    G O T O ( I0 , L I S T ) = 11W e c a n e x t e n d t h i s G O T O f u n c t i o n t os t ri n g s o f g r a m m a r s y m b o l s a s fo l lo w s :

    G O T O ( I , " ) = IG O T O ( I , - r X ) = G O T O ( G O T O ( I , ~ ), X )

    w h e r e " r is a s t r i n g o f g r a m m a r s y m b o l s a n dX is a n o n t e r m m a l o r t e r m i n a l s y m b o l . I fI = V ( a ) , t h e n I = G O T O ( I o , a ) . T h u sG O T O ( I 0 , a ) ~ ~b i f a n d o n l y i f a i s a v i a b l ep r e f i x , w h e r e I 0 = V ( " ) .T h e s e t s o f i t e m s w h i c h c a n b e o b t a i n e df r o m I o b y G O T O ' s a r e c a l le d t h e accesszblesets of ~tems. W e b u i l d u p t h e s e t o f a c c e s si -

    L R P a r s i n g * 1 11b le se ts of i te m s b y c o m p u t i n g G O T O ( I , X ) ,f o r al l a c c e s s ib l e s e t s o f i t e m s I a n d g r a m -m a r s ym b o l s X , w h e n e v e r t h e G O T O c o n-s t ru c t io n c o m e s u p w i th a n e w n o n e m p t y s e to f i t e m s , t h i s s e t o f i t e m s i s a d d e d t o t h e s e to f a c c e s s ib l e s e ts o f i t e m s a n d t h e p r o c e s sc o n t i n u e s . S i n c e t h e n u m b e r o f s e t s o f i t e m si s f i n i t e , t h e p r o c e s s e v e n t u a l l y t e r m i n a t e s .T h e o r d e r in w h i c h t h e s e ts o f i t e m s a r ec o m p u t e d d o e s n o t m a t t e r , n o r d o es th en a m e g i v e n to e a c h s e t o f i te m s . W e w i lln a m e t h e s e t s o f i t e m s I 0 , 11 , 12, . . . i n t h eo r d e r in w h i c h w e c r e a t e t h e m . W e s h a llt h e n a s s o c i a t e s t a t e i w i t h I , .L e t u s r e t u r n t o G1. W e h a v e c o m p u t e dI 0 , w h i c h c o n t a i n e d t h e i t e m s[ A C C E P T - - ~ . L I S T ][ L IS T - ~ . L I S T ' , ' E L E M E N T ][ L I S T --~ . E L E M E N T ][ E L E M E N T --* . 'a'][ E L E M E N T - - * . 'b']W e n o w w is h t o c o m p u t e G O T O ( I o , X ) f o ra ll g r a m m a r s y m b o l s X . W e h a v e a l r e a d yc o m p u t e d

    G O T O ( I o , L I S T ) = I1T o d e te r m i n e G O T O ( I0 , E L E M E N T ) , w el o o k f o r a ll it e m s i n I0 w i t h a d o t i m m e d i -a t e l y be fo re E L E M E N T . W e t h en t a k et h e s e i t e m s a n d m o v e t h e d o t t o t h e r i g h t o fE L E M E N T . W e o b t a in th e s in g le i t e m

    [ L I S T --* E L E M E N T .]T h e c l o s u r e o p e r a t i o n y i e l d s n o n e w i t e m ss in c e t h is i t e m h a s n o n o n t e r m i n a l t o t h er i g h t o f t h e d o t . W e c a l l t h e s e t w i t h t h i si t e m I 2 . C o n t i n u i n g i n t h i s f a s h i o n w e f i n dt h a t :

    G O T O ( I 0 , ' a ' ) c o n t a i n s o n l y[ E L E M E N T --~ 'a ' .]G O T O ( I 0 , 'b ') c o n t a i n s o n l y[ E L E M E N T --~ 'b' .]a n d G O T O ( I 0 , ' , ' ) a n d G O T O ( I 0 , 'S ') a r ee m p t y . L e t u s c al l t h e t w o n o n e m p t y s e tsI 3 a n d I 4. W e h a v e n o w c o m p u t e d a ll se t s o fi t e m s t h a t a r e d i r e c t l y a c c e s s ib l e f r o m I 0 .W e n o w c o m p u t e a ll s e ts o f i t e m s t h a t a r ea c c e s s ib l e f r o m t h e s e t s o f i t e m s j u s t c o m -p u t e d . W e c o n t i n u e c o m p u t i n g a c c e s s i b l es e t s o f i t e m s u n t i l n o m o r e n e w s e t s o f i t e m s

    C o m p u t i n g S u r v e y s , V o t 6 , N o 2 , J u n e 1 9 74

  • 8/9/2019 LR Parsing - Aho

    14/26

    112 A . V . A h o a n d S . C . J o h n s o nare found. The following table shows thecollection of accessible sets of items for G~:

    Io: ]ACCEPT --~ . LIST][LIST - -*. LIST ' , ' ELE MEN T][LIST --~ . E LE ME N T][ELEMENT --~ . 'a '][ELEMENT --~ . 'b']

    Ix: [A CC EP T --~ LI ST . ][LIST --~ L IS T. ' , ' E L E M E N T ]12: [LIS T --~ E L E M E N T .]13 : [EL EM EN T -~ 'a ' .1I4: [ELEMENT --* 'b ' . ]15 : [LIST --~ LIST ' , ' . ELEMENT][ELEMENT -~ . 'a '][ELEMENT --* . 'b']I6: [LIST --~ LIS T ', ' E LE ME N T .]

    The GOTO function on this collection canbe portrayed as a directed graph in whichthe nodes are labeled by the sets of itemsand the edges by grammar symbols, as fol-lows:

    E L E M E N T ~ @

    k ,Q~ Q'b' i

    ELEMENT

    Here, we used i in place of I,.For example, we observeGOTO(0, ") = 0GOTO(0, LIST ', ') = 5GOTO(0, LIST ' ,' EL EM EN T) = 6

    Observe that there is a path from vertex 0to a given node if and only if that path spellsout a viable prefix. Thus, GOTO(0, ' a b ' ) isempty, since ' ab ' is not a viable prefix.

    C o m p u t i n g S u r v e y s , V o l . 6 , N o 2 , J u n e 1 97 4

    6.3 Construct ing the Parsing Act ion and G ot oTables f rom the Co l lect ion o f S ets o f I tem sThe parsing action table is constructedfrom the collection of accessible sets of items.From the items in each set of items I, we

    generate parsing actions. An item of theform[A --* a . 'a ' El

    in I, generates the parsing actioni f (input = ' a ' ) sh i f t t

    where GOTO(I,, ' a ' ) = I t .An item with the dot at the right end ofthe production is called a c o m p l e t e d i t e m . Acompleted item [A -~ a .] indicates that wemay reduce by produ ction A --~ a. H owever,with an LR(1) parser we must determineon what input symbols this reduction is

    ' ' 'a 'possible. If ' a l ' , a 2 , " . , ,, are thesesymbols and 'a l ' , a2 , , an are not asso-ciated with shift or accept actions, then wewould generate the sequence of parsing ac-tions:if(input = ' a l ' ) reduce by: A --+if(input = 'a2 ' ) reduce by: A --*if(input = ' an ' ) r e d u c e b y : A --~

    As we mentioned in the last section, if theset of items contains only one completeditem, we can replace this sequence of parsingactions by the default reduce actionr e d u c e b y : A ~

    This parsing action is placed after all shiftand accept actions generated by this set ofitems.If a set of items contains more than onecompleted item, then we must generateconditional reduce actions for all completeditems except one. In a while we shall ex-plain how to compute the set of input sym-bols on which a given reduction is permissi-ble.If a comple ted item is of the form[AC CE PT --~ S. ]

    then we generate the accept actionif(input = '$') a c c e p t

  • 8/9/2019 LR Parsing - Aho

    15/26

    w h e r e ' $ ' is t h e r i g h t e n d m a r k e r f o r t h e i n p u ts t r i n g .F i n a l l y , i f a s e t o f i t e m s g e n e r a t e s n o r e -d u c e a c t i o n , w e g e n e r a t e t h e d e f a u l t e r r o rs t a t e m e n t . T h i s s t a t e m e n t i s p l a c e d a f t e ra l l s h i f t a n d a c c e p t a c t i o n s g e n e r a t e d f r o mt h e s e t o f i te m s .R e t u r n i n g t o o u r e x a m p l e f o r G 1 , f r o mI 0 w e w o u l d g e n e r a t e t h e p a r s i n g a c t i o n s :

    i f ( i n p u t = ' a ' ) s h i f t 3i f ( i n p u t = ' b ' ) s h i f t 4e r r o r

    N o t i c e t h a t t h e s e a r e e x a c t l y t h e s a m e p a rs -i n g a c t i o n s a s t h o s e f o r s t a t e 0 i n t h e p a r s e ro f S e c t i o n 4 . S i m i l a r l y , I 3 g e n e r a t e s t h e a c -t i o n

    r e d u c e b y : E L E M E N T - ~ ' a 'T h e g o t o t a b l e i s u s e d t o c o m p u t e t h e n e ws t a t e a f t e r a r e d u c t i o n . F o r e x a m p l e , w h e nt h e r e d u c t i o n i n s t a t e 3 i s p e r f o r m e d w e a l -w a y s h a v e s t a t e 0 t o t h e l e f t o f ' a ' . T h e n e ws t a t e i s d e t e r m i n e d b y s i m p l y n o t i n g t h a t

    G O T O ( I 0 , E L E M E N T ) = I2T h i s g i v e s r i se t o t h e c o d e

    i f (s t a te = 0 ) g o t o = 2f or E L E M E N T in t h e g o to t ab le .I n g e n e r a l, if n o n t e r m i n a l A h a s p r e c i s e l yt h e f o ll ow i ng G O T O ' s in t h e G O T O g r a p h :

    G O T O ( I ~ , A ) = I ,G O T O ( I , , A ) = I t ,G O T O ( I ,~ , A ) = I t~

    t h e n w e w o u l d g e n e r a t e t h e f o l lo w i n g r e p r e -s e n t a t i o n f o r c o l u m n A o f t h e g o t o t a b l e :A : i f ( s t a te = s l ) g o t o = t li f ( s t a t e = s 2) g o t o = t~

    i f ( s t a t e = s , , ) g o t o = t~T h u s , t h e g o t o t a b l e i s s i m p l y a r e p r e s e n t a -t i o n o f t h e G O T O f u n c t i o n o f t h e l a s t se c-t i o n , a p p l i e d t o t h e n o n t e r m i n a l s y m b o l s .W e m u s t n o w d e t e r m i n e t h e i n p u t s y m -b o l s o n w h i c h e a c h r e d u c t i o n i s a p p l i c a b l e .T h i s w i ll e n a b l e u s to d e t e c t a m b i g u i t i e s a n dd i f f i c u l t - t o - p a r s e c o n s t r u c t s i n t h e g r a m m a r ,

    L R P a r s i n g 113a n d t o d e c i d e b e t w e e n r e d u c t i o n s if m o r et h a n o n e i s p o s s i b l e i n a g i v e n s t a t e . I ng e n e r a l, t h i s is a c o m p l e x t a s k ; t h e m o s tg e n e r a l s o l u t i o n o f t h i s p r o b l e m w a s g i v e n b y[ K n u t h ( 1 9 6 5 ) ] , b u t h i s a l g o r i t h m s u f f e r sf r o m l a r g e t i m e a n d m e m o r y r e q u i r e m e n t s .S e v e r a l s i m p l i f i c a t i o n s h a v e b e e n p r o p o s e d ,n o t a b l y b y [ D e R e m e r ( 1 9 6 9 a n d 1 97 1 )],w h i c h l a c k t h e f u l l g e n e r a l i t y o f K n u t h ' st e c h n i q u e , b u t c a n c o n s t r u c t p r a c t i c a l p a r -s e r s i n r e a s o n a b l e t i m e f o r a l a r g e c l a s s o fl a n g u a g e s . W e s h a l l d e s c r i b e a n a l g o r i t h mt h a t is a s im p l i f i c a ti o n o f K n u t h ' s a l g o r i t h mw h i c h r e s o l v e s a l l c o n f l i c t s t h a t c a n b e r e -s o l v e d w h e n t h e p a r s e r h a s t h e s t a t e s a sg i v e n a b o v e .6.4 C omputing Lookahead SetsS u p p o s e [ A - ~ = . B] i s a n i t e m t h a t i sv a l i d f o r s o m e v i a b l e p re f ix ~ a . W e s a y t h a ti n p u t s y m b o l ' a ' is a p p l i c a b l e fo r [A ---* ~ ~]i f , f o r s o m e s t r i n g o f t e r m i n a l s ' w ' , b o t h" y = ~ ' a w ' a n d ~ , A ' a w ' a r e r i g h t s e n t e n t i a lf o r m s . T h e r i g h t e n d m a r k e r ' $ ' i s a p p l i c a b l ef o r [ A - --* = . ~ ] i f b o t h ~ ,= B a n d ~ A a r er i g h t s e n t e n t i a l f o r m s .T h i s d e f i n i ti o n h a s a s i m p l e i n t u i t i v e e x -p l a n a t i o n w h e n w e c o n s id e r c o m p l e t e d i te m s .S u p p o s e i n p u t s y m b o l ' a ' i s a p p l i c a b l e f o rc o m p l e t e d i t e m [A --* ~ . ]. I f a n L R ( 1 )p a r s e r m a k e s t h e r e d u c t i o n s p e c if ie d b y t h i si t e m o n t h e a p p l i c a b l e i n p u t s y m b o l ' a ',t h e n t h e p a r s e r w i l l b e a b l e t o m a k e a t l e a s to n e m o r e s h i f t m o v e w i t h o u t e n c o u n t e r i n ga n e r r o r .T h e s e t o f s y m b o l s t h a t a r e a p p l i c a b l e fo re a c h i t e m w i l l b e c a l l e d t h e l o o k a h e a d s e tf o r t h a t i t e m . F r o m n o w o n w e s h a l l i n -c l u d e t h e l o o k a h e a d s e t a s p a r t o f a n i t e m .T h e p r o d u c t i o n w i t h t h e d o t s o m e w h e r e int h e r i g h t s i d e w i l l b e c a l l e d t h e core o f t h ei t e m . F o r e x a m p l e ,

    ( [ E L E M E N T - o ' a ' .] , { ' , ' , '$ '} )i s a n i t e m o f G 1 w i t h c o r e

    [ E L E M E N T --* ' a ' . ]a n d l o o k a h e a d s e t { ', ', '$ ' } .W e s h a l l n o w d e s c r i b e a n a l g o r i t h m t h a tw i ll c o m p u t e t h e s e ts o f v a l i d i t e m s f o r ag r a m m a r w h e r e t h e i t e m s i n c l u d e t h e i r

    C o m p u t i n g S u r v e y s , V o l 6 , N o . 2 , J u n e 1 97 4

  • 8/9/2019 LR Parsing - Aho

    16/26

  • 8/9/2019 LR Parsing - Aho

    17/26

    I4 : [ E L E M E N T ~ ' b ' . ] , { ' , ' , ' $ ' }15: [LIS T --~ LI S T ' , ' . EL E M E N T] , { ' , ', '$ '}[ EL E M E N T ~ 'a '] , ' , ' , '$ '}[ E L E M E N T ~ . ' b' ], ' , ', '$ '}16" [LIS T ~ LIS T ' , ' E L E M E N T . ], ' , ' , '$ '}

    A l t h o u g h t h e s i t u a t io n d o e s n o t o c c u rh e r e , if w e g e n e r a t e a s e t o f i t e m s I t s u c h t h a tI t h a s t h e s a m e s e t o f c o r e s a s s o m e o t h e rs e t o f i t em s I , a l r e a d y g e n e r a t e d , b u t I ,I t , t h e n w e c o m b i n e I8 a n d I t i n to a n e w s e to f i t e m s I b y m e r g i n g t h e l o o k a h e a d s e ts o fi t e m s w i t h t h e s a m e c o r e s . W e m u s t t h e nc o m p u t e G O T O ( I , X ) f o r a ll g r a m m a r s y m -b o l s X .T h e l o o k a h e a d s e t s o n t h e c o m p l e t e di t e m s g i v e t h e t e r m i n a l s y m b o l s f o r w h i c ht h e r e d u c t i o n s s h o u l d b e p e r f o r m e d . T h e r ei s a p o s s i b i l i t y t h a t t h e r e a r e a m b i g u i t i e s i nt h e g r a m m a r , o r th e g r a m m a r is t oo c o m p l e xt o a l l o w a p a r s e r t o b e c o n s t r u c t e d b y t h i st e c h n i q u e ; t h i s c a u s e s c o n f l ic t s t o b e d is -c o v e r e d i n th e a c t i o n s o f t h e p a r s e r . F o r e x -a m p l e , s u p p o s e t h e r e is a s e t o f i t e m s I~ i nw h i c h 'a ' g i v e s r i s e t o t h e p a r s i n g a c t i o ns h i f t b e c a u s e G O T O ( I s , 'a ') e x i s t s . S u p p o s ea l s o t h a t t h e r e i s a c o m p l e t e d i t e m

    ( [ A - - . a . ] , L )i n I , , a n d t h a t t h e t e r m i n a l s y m b o l ' a ' is i nt h e l o o k a h e a d s e t L . T h e n w e h a v e n o w a yo f k n o w i n g w h i c h a c t i o n i s c o r r e c t i n s t a t e sw h e n w e s ee a n ' a ' ; w e m a y s h i ft ' a ' , o r w em a y r e d u c e b y A --~ a . O u r o n l y r e c o u r s e i st o r e p o r t a shif t-reduce c o n f l i c t .

    I n t h e s a m e w a y , i f t h e r e a r e t w o r e d u c -t i o n s p o s s i b le i n a s t a t e b e c a u s e t w o c o m -p l e t e d it e m s c o n t a i n t h e s a m e t e r m i n a l s y m -b o l i n t h e i r l o o k a h e a d s e t s , t h e n w e c a n n o tt e l l w h i c h r e d u c t i o n w e s h o u l d d o ; w e m u s tr e p o r t a reduce-reduce c o n f l i c t .I n s t e a d o f r e p o r t i n g a c o n fl ic t w e m a ya t t e m p t t o p r o c e e d b y c a r r y i n g o u t a l l c o n -f l i c t i n g p a r s i n g a c t i o n s , e i t h e r b y p a r a l l e ls i m u l a t i o n [ E a r l e y ( 19 70 )] o r b y b a c k t r a c k -i n g [ P a g e r ( 1 9 7 2 b ) ] .A s e t o f i t e m s i s consis tent o r adequate i f i td o e s n o t g e n e r a t e a n y s h i f t - r e d u c e o r r e d u c e -r e d u c e c o n f l ic t s . A c o l l e c t i o n o f s e t s o f i t e m sis v a h d i f a l l i t s s e t s o f i t e m s a r e c o n s i s t e n t ;o u r c o l l e c t io n o f s e t s o f i t e m s f o r G1 i s v a l i d .W e s u m m a r i z e th e p a r s i n g a c t io n a n d g o t o

    L R P a r s in g 115t a b l e c o n s t r u c t i o n p r o c e s s :(1 ) G i v e n a g r a m m a r G , a u g m e n t t h eg r a m m a r w i t h a n e w i n it ia l p r o d u c -t i o n

    A C C E P T ~ Sw h e r e S i s t h e s t a r t s y m b o l o f G .(2 ) L e t I b e t h e s e t w i t h t h e o n e i t e m

    ( [ A C C E P T - - ~ . S ], { ' $ ' })

    (3 )

    ( 4 )

    L e t I 0 b e t h e c l o s u r e o f I .L e t C , th e c u r r e n t c o l le c t io n o f a c -c e s s i b l e s e t s o f i t e m s , i n i t i a l l y c o n t a i no n l y I 0 .F o r e a c h I i n C , a n d f o r e a c h g r a m m a rs y m b o l X , c o m p u t e I ' = G O T O ( I , X ) .T h r e e c a s e s c a n o c c u r :a . I ' = I " f o r s o m e I " a l r e a d y in C .I n t h i s c a s e , d o n o t h i n g .b . I f t h e s e t o f c o r e s o f I ' i s d i s t i n c tf r o m t h e s e t o f c o r e s o f a s e t o fi t e m s a l r e a d y i n C , t h e n a d d I ' t o C .c . I f t h e s e t o f c o r e s o f I ~ i s t h e s a m ea s t h e s e t o f c o r es o f s o m e I " a l -r e a d y i n A b u t I ' ~ I " , t h e n l e tI " b e t h e s e t o f i t e m s

    ( [ A - ~ a . / ~ ] , L 1 (J L2)s u c h t h a t( [A - -* a . f ~], 51 ) i s i n I ' an d( [A - -~ a . ~ ], L~) is i n I " .R e p l a c e I " b y I " i n C .( 5 ) R e p e a t s t e p 4 u n t i l n o n e w s e t s o f

    i t e m s c a n b e a d d e d t o C . C i s c a l l e dt h e LALR(1) co l l ec t ion o f s e t s o f i t e m sf o r G .(6 ) F r o m C t r y t o c o n s t r u c t t h e p a r s i n ga c t i o n a n d g o t o t a b l e s a s i n S e c t i o n6 .3 .I f th i s t e c h n i q u e s u c c e ed s in p r o d u c i n g ac o l l e c ti o n o f s e t s o f i t e m s f o r a g i v e n g r a m -m a r i n w h i c h a l l s e t s o f i t e m s a r e c o n s i s t e n t ,t h e n t h a t g r a m m a r is s a id t o b e a n L A L R ( 1 )g r a m m a r . L A L R ( 1 ) g r a m m a r s i nc lu d e m a n yi m p o r t a n t c la s se s o f g r a m m a r s , i n c l u d i n gt h e L L ( 1 ) g r a m m a r s [ L e w is a n d S t e a r n s( 19 6 8) ], t h e s i m p l e m i x e d s t r a t e g y p r e c e -d e nc e g r a m m a r s [ M c K e e m a n , H o r n in g , a n dW o r t m a n ( 19 70 )] , a n d t h o s e p a r s a b l e b yo p e r a t o r p r e c e d e n c e t e c h n i q u e s . T e c h n i q u e s

    C o mp ut i ng S ur vey s , Vol 6 , No . 2 , June 1974

  • 8/9/2019 LR Parsing - Aho

    18/26

    116 A. V. Aho and S. C. Johnsonf o r p r o v i n g t h e s e i n c l u s i o n s c a n b e f o u n d i n[ A h o a n d U l l m a n ( 1 9 7 2 a a n d 1 9 7 3 a ) ] .S t e p ( 4 ) c a n b e r a t h e r t i m e - c o n s u m i n g t oi m p l e m e n t . A s i m p l e r , b u t l e s s g e n e r a l ,a p p r o a c h w o u l d b e t o p r o c e e d a s f o ll o w s. L e tF O L L O W ( A ) b e t h e se t o f t e r m i n a l s y m b o l st h a t c a n f ol lo w n o n t e r m i n a l s y m b o l A i n as e n t e n t i a l f o r m . I f A c a n b e t h e r i g h t m o s ts y m b o l o f a s e n t e n t i a l f o r m , t h e n ' $ ' is in -c lu d ed in F O L L O W ( A ) . W e c a n c o m p u t e th es e t s o f i t e m s w i t h o u t l o o k a h e a d s a s in S e c t i o n6 .2 . T h e n i n e a c h c o m p l e t e d i t e m [A --~ a . ]w e c a n a p p r o x i m a t e t h e l o o k a h e a d s e t L f o rt h is i t e m b y F O L L O W ( A ) ( I n ge n er al , L isa s u b se t o f F O L L O W ( A ) . ) T h e r e su l ti n gc o l l e c t i o n o f s e t s o f i t e m s i s c a l l e d t h eSLR(1) collection. I f a l l s e t s o f i t e m s i n t h eS L R ( 1 ) c o l l e c ti o n a r e c o n s i s te n t , t h e n t h eg r a m m a r i s s a i d t o b e simple L R ( 1 ) [ D e -R e m e r ( 1 97 1) ]. A l t h o u g h n o t e v e r y L A L R ( 1 )g r a m m a r i s s im p l e L R ( 1 ) , e v e r y l a n g u a g eg e n e r a t e d b y a n L A L R ( 1 ) g r a m m a r i s a l s og e n e r a t e d b y a si m p le L R ( 1 ) g r a m m a r( [ A h o a n d U l l m a n ( 1 9 7 3 a ) ] c o n t a i n s m o r ed e t a i l s ) .

    7 . P A R S I N G A M B IG U O U S G R A M M A R SI t i s u n d e s i r a b le t o h a v e u n d e t e c t e d a m b i g u i -t i e s i n t h e d e f i n i t i o n o f a p r o g r a m m i n gl an g u a g e. H o w e v e r , a n a m b i g u o u s g r a m m a rc a n o f t e n b e u s e d t o s p e c if y c e r t a in l a n g u a g ec o n s t r u c t s m o r e e a s il y t h a n a n e q u i v a l e n tu n a m b i g u o u s g r a m m a r . W e s h a l l a l s o s e et h a t w e c a n c o n s t r u c t m o r e e f f ic i en t p a r s e r sd i r e c t l y f r o m c e r t a i n a m b i g u o u s g r a m m a r st h a n f r o m e q u iv a l e n t u n a m b i g u o u s g r a m -m a r s .I f w e a t t e m p t t o c o n s t r u c t a p a r s e r f o ra n a m b i g u o u s g r a m m a r , t h e L A L R ( 1 )p a r s e r c o n s t r u c t i o n t e c h n i q u e w i l l g e n e r a t ea t l e a s t o n e i n c o n s i s t e n t s e t o f i t e m s . T h u s ,t h e p a r s e r g e n e r a t i o n t e c h n i q u e c a n b e u s e dt o d e t e r m i n e t h a t a g r a m m a r i s u n a m b i g u -o u s . T h a t i s t o s a y , i f n o i n c o n s i s t e n t s e t s o fi t e m s a r e g e n e ra t e d , t h e g r a m m a r i s g u a r a n -t e e d t o b e u n a m b i g u o u s . H o w e v e r , i f a ni n c o n s i s t e n t s e t o f i t e m s i s p r o d u c e d , t h e na ll w e c a n c o n c lu d e is t h a t t h e g r a m m a r isn o t L A L R ( 1 ) . T h e g r a m m a r m a y o r m a yn o t b e a m b i g u o u s . ( T h e r e is n o g e n er a l

    a l g o r i t h m t o d e t e r m i n e i f a c o n t e x t - f r e eg r a m m a r is a m b i g u o u s ( se e, f o r e x a m p l e[ A h o a n d U l l m a n ( 1 9 7 2 a ) ] ) .I n c o n s i s t e n t s e t s o f i t e m s a r e u s e f u l i np i n p o i n t i n g d i f f i c u l t -t o - p a r s e o r a m b i g u o u sc o n s t r u c t io n s in a g i v en g r a m m a r . F o re x a m p l e , a p r o d u c t i o n o f t h e f o r m

    A --~ A Ai n a n y g r a m m a r w i l l m a k e t h a t g r a m m a ra m b i g u o u s a n d c a u s e a p a r s i n g a c t i o n c o n -f l i c t t o a r i s e f r o m s e t s o f i t e m s c o n t a i n i n gt h e i t e m s w i t h t h e c o r es

    [A --~ A A .][A --~ A . A]

    C o n s t r u c t i o n s w h i c h a r e s u f f ic i e n t ly c o m -p l e x t o r e q u i r e m o r e t h a n o n e s y m b o l o fl o o k a h e a d a l s o r e s u l t i n p a r s i n g a c t i o n c o n -f l i c t s . F o r e x a m p l e , t h e g r a m m a rS --~ A 'a'A --) 'a' I"

    i s a n L A L R ( 2 ) b u t n o t L A L R ( 1 ) g r a m m a r .E x p e r i e n c e w i t h a n L A L R ( 1 ) p a r s erg e n e r a t o r c a l l e d Y A C C a t B e l l L a b o r a t o r i e sh a s s h o w n t h a t a f e w i t e r a ti o n s w i t h t h ep a r s e r g e n e r a t o r a r e u s u a l l y s u f f ic i e n t t o r e -s o l v e t h e c o n f l ic t s i n a n L A L R ( 1 ) c o ll e c-t i o n o f s e t s o f i t e m s f o r a r e a s o n a b l e p r o -g r a m m i n g l a n g u a g e .Example 7 . 1 : C o n s i d e r t h e f o l l o w i n g p r o -d u c t i o n s fo r " i f - t h e n " a n d " i f - t h e n - e l s e "s t a t e m e n t s :

    S - -~ ' i f b then ' SS - ~ ' if b t h e n ' S ' e l se ' SI f t h e s e t w o p r o d u c t i o n s a p p e a r i n a g r a m -m a r , t h e n t h a t g r a m m a r w ill b e a m b i g u o u s ;t h e s t r i n g

    ' if b t h e n i f b t h e n ' S ' e l s e ' Sc a n b e p a r s e d i n t w o w a y s a s s h o w n :

    C o m p u t i n g S u r v e y s , V o l . 6 , N o 2, June 1974

  • 8/9/2019 LR Parsing - Aho

    19/26

    I n m o s t p r o g r a m m i n g l a ng u a g es , t h e f i r stp h r a s i n g i s p r e f e r r e d . T h a t i s , e a c h n e w' e l s e ' i s t o b e a s s o c i a t e d w i t h t h e c l o s e st" u n e l s e d " ' th e n ' .A g r a m m a r u s in g th e s e a m b i g u o u s p r o d u c -t i o n s t o s p e c i f y i f -t h e n - e l s e s t a t e m e n t s w i llb e s m a l l e r a n d , w e f e e l , e a s i e r t o c o m p r e -h e n d t h a n a n e q u i v a le n t u n a m b i g u o u sg r a m m a r . I n a d d i t io n i f a g r a m m a r h a s o n lya m b i g u i t ie s o f t h is t y p e , t h e n w e c a n c o n -s t r u c t a "M id L A L R ( 1 ) p a r s e r f o r t h e g r a m -m a r m e r e l y b y r e s o l v i n g e a c h s h i f t - re d u c ec o n f li c t i n f a v o r o f s h i f t [ A h o , J o h n s o n , a n dU l l m a n ( 1 9 7 3 ) ] .

    E x a m p l e 7 . 2 : C o n s i d e r t h e a m b i g u o u sg r a m m a r *S ~ ' i f b t h e n ' SS - ~ ' i f b t h e n ' S ' e l s e ' SS --~ 'a '

    i n w h i c h e a c h e ls e is t o b e a s s o c i a t e d w i t ht h e l a s t u n e l s e d ' t h e n ' . T h e L A L R ( 1 ) c o l -l e c t io n o f s e t s o f i t e m s f o r t h i s g r a m m a r is asf o l l o w s :/0 : [A C CE PT -- , S] , { '$ '}[3 --* . 'if b th en ' 3], {'3'}[3 ~ . ' i f b the n' S 'e lse ' 3] , { '$ '}[3 --* 'a'], {'$'}I1 [ A C C E P T - - , S .1 , { 'S ' }I~ : I S ~ ' i f b t h e n ' . 3 [ , { ' e l s e ' , ' $ '1[ 3 ~ ' i f b t h e n ' S ' e l s e ' S ] , [ ' e l s e ' , ' $ '}I S ~ ' i f b t h e n ' S ] , [ ' e l s e ' , ' $ '}I S ~ . ' i f b t h e n ' S ' e l s e ' S ] , [ ' e l s e ' , ' $ '}

    [3 ~ . ' a ' ] , [ ' e l s e ' , ' $ ' }I n : [S ~ ' a ' ] , { ' e l s e ' , ' $ ' }14 : [ S - -* ' i f b t h e n ' S . 1 , [ ' e l s e ' , ' $ ' }I S ~ ' i f b t h e n ' S . ' e l s e ' S ] , { ' e l s e ' , ' $ '}* T h e f o l lo w i n g g r a m m a r i s a n e q u i v a l e n t u n a m -b i g u o u s g r a m m a r :

    S - - * ' i f b t h e n ' SS - - * ' I f b t h e n ' S ~ ' e l s e ' SS --* 'a '$ I - - * ' i f b t h e n ' $ 1 ' e l s e ' S xSI --* 'a'

    L R P a r s i n g 1171 5: [ S ~ ' i f b t h e n ' S ' e l s e ' S ] , [ ' e l s e ' , ' $ ' }I S ~ . ' i f b t h e n ' S ] , { ' e l s e ' , ' $ ' }[ 3 ~ . ' i f b t h e n ' S ' e l s e ' 3 ] , [ ' e l s e ' , ' $' }

    [3 ~ . ' a ' ] , { ' e l s e ' , ' $ ' }I s ' [ 3 ~ ' i f b t h e n ' S ' e l s e ' S . ] , { ' e l s e ' , ' $ '}

    I 4 c o n t a i n s a s h i f t - r e d u c e c o n f l ic t . O n t h ei n p u t ' e ls e ' , I 4 s a y s t h a t e i t h e r a s h if t m o v et o / 5 i s p e r m i s s i b l e , o r a r e d u c t i o n b y p r o -d u c t i o nS - -~ 'i f b t h e n ' S

    i s p o s s i b l e . I f w e c h o o s e t o s h i f t , w e s h a l la s s o c i a te t h e i n c o m i n g ' e ls e ' w i t h t h e l a s tu n e l s e d ' t h e n ' . T h i s i s e v i d e n t b e c a u s e t h ei t e m w i t h t h e c o r e

    I S --~ ' i f b t h e n ' S . ' e l s e ' S ]i n I 4 g i v e s r i s e t o t h e s h i f t a c t i o n .T h e c o m p l e t e p a r s i n g a c t i o n t a b l e , w i t ht h e c o n f l i c t r e s o l v e d , a n d t h e g o t o t a b l e c o n -s t r u c t e d f r o m t h i s c o l l e c t i o n o f s e t s o f i t e m sa r e s h o w n b e l o w :P a r s v n g A c t i o n T a b l e

    0 : i f ( i n p u t = ' if b t h e n ' ) s h i f t 2i f ( i n p u t = ' a ' ) s h i f t 3e r r o r1 : i f ( i n p u t = $ ) a c c e p te r r o r2 : i f ( i n p u t = ' if b t h e n ' ) s h i f t 2i f ( i n p u t = ' a ' ) s h i f t 3e r r o r

    3" r e d u c e b y : S - - + ' a '4 : i f ( i n p u t = ' e ls e ' ) s h i f t 5

    r e d u c e b y : S - -~ ' i f b t h e n ' S5 : i f ( i n p u t = ' if b t h e n ' ) s h i f t 2i f ( in p u t = ' a ' ) s h i f t 3e r r o r6 : r e d u c e b y : S - -~ ' if b t h e n ' S ' e l s e ' S

    G o t o T a b l eS : i f ( s t a t e = 0 ) g o t o = 1i f ( s t a t e = 2 ) g o t o = 4

    g o t o = 6G i v e n a n a m b i g u o u s g r a m m a r , w i t h t h ea p p r o p r i a t e r u le s f o r r e s o lv i n g t h e a m b i g u i -t i e s w e c a n o f t e n d i r e c t l y p r o d u c e a s m a l l e rp a r se r f r o m th e a m b i g u o u s g r a m m a r t h a nf r om t h e e q u i v a le n t u n a m b i g u o u s g r a m m a r .

    Computing Surveys, Vol 6, No 2, June 1974

  • 8/9/2019 LR Parsing - Aho

    20/26

    1 18 A . V . A h o a n d S . C . J o h n s o nH o w e v e r , s o m e o f t h e " o p t i m i z a t i o n s " d i s-c u s s e d i n t h e n e x t s e c t io n w il l m a k e t h e p a r -s e r f o r t h e u n a m b i g u o u s g r a m m a r a s s m a lla s t h a t f o r t h e a m b i g u o u s g r a m m a r .E x a m p l e 7 .3 : C o n s i d e r t h e f o l lo w i n g g r a m -m a r G 3 f o r a r i t h m e t i c e x p r e s s io n s :

    E - ~ E ' + ' EE - ~ E ' , ' EE --* ' ( ' E ' ) 'E - -~ 'a '

    w h e r e 'a ' s t a n d s f o r a n y i d e n t i fi e r . A s s u m i n gt h a t + a n d a r e b o t h le f t a s s o c i a t iv e a n d h a s h i g h e r p re c e d e n c e t h a n + , t h e r e a ret w o t h i n g s w r o n g w i t h th i s g r a m m a r . F i r st ,i t is a m b i g u o u s i n t h a t t h e o p e r a n d s o f t h eb i n a r y o p e r a t o r s ' + ' a n d ' . ' c a n b e a s s o c i -a t e d i n a n y a r b i t r a r y w a y . F o r e x a m p l e ,'a + a -4- a' c a n b e p a r s e d a s

    o r a s

    T h e f i rs t p a r s i n g g i v e s t h e u s u a l l e f t - t o - r i g h ta s s o c i a t i v i t y , t h e s e c o n d a r i g h t - t o - l e f ta s s o c i a t i v i t y .I f w e r e w r o t e t h e g r a m m a r a s G 4:

    E ---~ E ' A - ' TE -- -~ E ' . ' TE - - - ~ TT ~ ' ( ' E ' ) 'T ~ ' a '

    t h e n w e w o u l d h a v e e l i m i n a t e d t h i s a m -b i g u i t y b y i m p o s i n g t h e n o r m a l l e f t - t o - r i g h ta s s o c i a t i v i t y f o r + a n d . . H o w e v e r , t h i sn e w g r a m m a r h a s s t i l l o n e m o r e d e f e c t ; +a n d h a v e t h e s a m e p r e c e d e n c e , s o t h a t a ne x p r e s s io n o f t h e f o r m ' a + a , a ' w o u l d b ee v a l u a t e d a s ( a + a ) . a . T o e l i m i n a t e t h i s ,w e m u s t f u r t h e r r e w r i t e t h e g r a m m a r a sas:

    E - ) E ' + ' TE - ~ TT --~ T '* ' FT - " ) FF - - ' ( ' E ' ) 'F ---) 'a '

    W e c a n n o w c o n s t r u c t ~ p a r s e r f o r G 5q u i t e e a s i ly , a n d f i n d t h a t w e h a v e 1 2 s t a t e s ;i f w e c o u n t t h e n u m b e r o f p a r s i n g a c t i o n s int h e p a r s e r ( i.e ., t h e s u m o f t h e n u m b e r o fs h i f t a n d r e d u c e a c t i o n s i n a l l s t a t e s t o -g e t h e r w i t h t h e g o t o a c t io n s ) w e s ee t h a t t h ep a r s e r f o r G5 h a s 3 5 a c t i o n s .I n c o n t r a s t , t h e p a r s e r f o r G3 h a s o n l y 1 0s t a t e s , a n d 2 9 a c t i o n s . A c o n s i d e r a b l e p a r to f t h e s a v i n g c o m e s f r o m t h e e l i m i n a t io n o ft h e n o n t e r m i n a l s T a n d F f r o m G s, a s w e l l a st h e e l i m i n a t i o n o f t h e p r o d u c t i o n s E --~ Ta n d T - * F .L e t u s d i s cu s s t h e r e s o l u t i o n o f p a r s i n ga c t i o n c o n f l ic t s i n G3 i n s o m e w h a t m o r e d e -t a il . T h e r e a r e t w o s e t s o f i t e m s i n t h eL A L R ( 1 ) c o l l e c ti o n o f se t s o f i t e m s f o r G 3w h i c h g e n e r a t e c o n f l i ct s i n t h e i r p a r s i n g a c -t i o n s :

    [ E ---) E . ' + ' E l , { ' + ' , ' . ' , ' ) ', '$ '}[ E --~ E . ' , ' E l , { ' + ' , ' , ' , ') ' , ' $' }[ E --~ E ' + ' E . ], { ' + ' , ' . ' , ' ) ' , ' $' }a n d [ E --* E . ' + ' E l , { ' + ' , ' . ' , ') ' , '$ '}[ E - ~ E . ' , ' E l , { ' + ' , ' . ' , ') ' , '$ '}[ E - -* Z ' . ' E .1 , { ' + ' , ' . ' , ' ) ' , '$ ' }

    C o m p u t i n g S u r ~ e y s , V o l 6 , N o . 2, J u n e 1 97 4

  • 8/9/2019 LR Parsing - Aho

    21/26

  • 8/9/2019 LR Parsing - Aho

    22/26

    1 20 A . V . A h o a n d S . C . J o h n s o n8.2 Subsuming StatesA s l ig h t g e n e r a l i z a t i o n o f t h e t r a n s f o r m a -t i o n i n S e c t i o n 8 . 1 i s t o e l i m i n a t e a s t a t ew h o s e p a r s i n g a c t i o n s a r e a s u f f ix o f t h ea c t i o n s o f a n o t h e r s t a t e . W e t h e n l a b e l t h eb e g i n n i n g o f t h e s u f fi x b y t h e e l i m i n a t e ds t a t e . F o r e x a m p l e , if w e h a v e :

    n : i f ( i n p u t = ' x ') s h i f t pi f ( i n p u t = ' y ' ) s h i f t qe r r o r

    a n d m : i f ( i n p u t = ' y ' ) s h i f t qe r r o r

    t h e n w e m a y e l i m i n a t e s ta t e m b y a d d i n g t h el a b e l in t o t h e m i d d l e o f s t a t e n :

    n : i f ( i n p u t = ' x ' ) s h i f t pm : i f ( i n p u t = ' y ' ) s h i f t qe r r o r

    P e r m u t i n g t h e o r d e r o f t h es e s t a t e m e n t sc a n i n c r e a s e t h e a p p l i c a b i l i t y o f th i s o p -t i m i z a t io n . ( S ee I c h b i a h a n d M o r s e ( 19 7 0)f o r s u gg e s ti o n s o n t h e i m p l e m e n t a t i o n o f t h iso p t i m i z a t i o n . )8.3 El imination o f Reductions by Single

    ProductionsA s i n g l e p r o d u c t i o n i s o n e o f t h e f o r mA - * X , w h e r e A i s a n o n t e r m i n a l a n d X isa g r a m m a r s y m b o l . I f t h i s p r o d u c t i o n i s n o to f a n y i m p o r t a n c e i n t h e t r a n s l a ti o n , t h e nw e s a y t h a t t h e s i n g l e p r o d u c t i o n i s se -m a n t i c a l l y m s z g n ~ f i c a n t . A c o m m o n s i tu a -t i o n i n w h i c h s i n g l e p r o d u c t i o n s a r i s e o c c u r sw h e n a g r a m m a r i s u s e d t o d e s c ri b e t h ep r e c e d e n c e l e v e ls a n d a s s o c i a ti v i ti e s o fo p e r a t o r s ( se e g r a m m a r G5 o f E x a m p l e 7 .3 ).W e c a n a l w a y s c a u s e a n L R p a r s e r t o a v o i dm a k i n g t h e s e r e d u c t i o n s ; b y d o i n g s o w em a k e t h e L R p a r s e r f a s te r , a n d r e d u c e t h en u m b e r o f s ta t e s. ( W i t h so m e g r a m m a r s , t h es iz e o f t h e " o p t i m i z e d " f o r m o f t h e p a r s i n ga c t i o n t a b l e m a y b e g r e a t e r t h a n t h e u n -o p t i m i z e d o n e .)

    W e s h a l l g i v e a n e x a m p l e i n t e r m s o f G 1w h i c h c o n t a i n s t h e s i n g l e p r o d u c t i o nL I S T --~ E L E M E N T

    W e s h a l l e l i m i n a t e r e d u c t i o n s b y t h i s p r o -d u c t i o n f r o m t h e p a r s e r f o r G , f o u n d i n S e c-

    t i o n 5 . T h e o n l y s t a t e w h i c h c a l l s f o r a r e -d u c t i o n b y t h i s p r o d u c t i o n i s s t a t e 2 . M o r e -o v e r , t h e o n l y w a y i n w h i c h w e c a n g e t t os t a t e 2 i s b y t h e g o t o a c t i o nE L E M E N T : i f ( s ta t e = 0 ) g o t o = 2A f t e r t h e p a r s e r d o e s t h e r e d u c t i o n i n s t a t e2 , i t i m m e d i a t e l y r e f e r s t o t h e g o t o a c t i o n

    L I S T : g o t o = 1a t w h i c h t i m e t h e c u r r e n t s t a t e b e c o m e s 1 .T h u s , t h e r i g h t m o s t t r e e is o n l y l a b e le d w i t hs t a t e 2 f o r a s h o r t p e r i o d o f t i m e ; s t a t e 2r e p r e s e n t s o n l y a s t e p o n t h e w a y t o s t a t e I .W e m a y e l im i n a t e th i s r e d u c t i o n b y t h e s in -g l e p r o d u c t i o n b y c h a n g i n g t h e g o t o a c t i o nu n d e r E L E M E N T t o :

    E L E M E N T : i f ( s t a te = 0 ) g o t o - - 1s o t h a t w e b y p a s s s t a t e 2 a n d g o d i r e c t l y tos t a t e 1 . W e n o w f i n d t h a t s t a t e 2 c a n n e v e rb e r e a c h e d b y a n y p a r s i n g a c t i o n , s o i t c a nb e e l i m i n a t e d . M o r e o v e r , i t t u r n s o u t h e r e( a n d f r e q u e n t l y i n p r a c t i c e as w e ll ) t h a t t h eg o to a c ti on s f o r L I S T a n d E L E M E N T b e-c o m e c o m p a t i b l e a t t h i s p o i n t ; t h a t i s , t h ea c t i o n s d o n o t d i f f e r o n t h e s a m e s t a t e . I t isa l w a y s p o s s i b l e t o m e r g e c o m p a t i b l e g o t oa c t i o n s fo r n o n t e r m i n a l s ; t h e r e s u l t i n g p a r s e rh a s o n e l e s s s t a t e , a n d o n e l e s s g o t o a c t i o n .E x a m p l e 8 . 1: T h e f o l lo w i n g is a r e p r e s e n t a -t i o n o f t h e p a r s in g a c t i o n a n d g o t o t a b l es f o ra n L R ( 1 ) p a r s e r f o r G 1. I t r e s u l t s f r o m t h ep a r s i n g a c t i o n a n d g o t o t a b l e s i n S e c t i o n 5b y a p p l y i n g s t a t e m e r g e r ( S e c t i o n 8 . 1 ), a n de l i m i n a t i n g t h e r e d u c t i o n b y t h e s i n g l e p r o -d u c t i o n .P a r s i n g A c t i o n T a b l e0 . 5 . i f ( i n p u t = 'a ' ) s h i f t 3i f ( i n p u t = ' b ' ) s h i f t 4e r r o r1 : i f ( i n p u t = ' , ' ) s h i f t 5i f ( i n p u t = $ ) a c c e p te r r o r3 " r e d u c e b y : E L E M E N T --* ' a '4 r e d u c e b y : E L E M E N T --~ ' b '6 " r e d u c e b y : L I S T - ~ L I S T ' ,' E L E M E N T

    G o t o T a b l eL I S T : E L E M E N T : i f ( s t a t e = 0 ) g o t o = 1

    g o t o = 6

    C omput i ng S ur veys , Vol . 6 , No . 2 , June 1974

  • 8/9/2019 LR Parsing - Aho

    23/26

    T h e s e t a b l e s a r e i d e n t i c a l w i t h t h o s e f o rt h e a m b i g u o u s v e r s i o n o f G 1, a f t e r t h e e q u a ls t a t e s h a v e b e e n i d e n t i f i e d . T h e s e t a b l e sd i ff e r o n l y i n t h a t t h e n o n t e r m i n a l s y m b o l sL I S T a n d E L E M E N T h a v e b e e n e x pl ic i t lym e r g e d i n t h e a m b i g u o u s g r a m m a r , w h i let h e d i s t i n c t i o n is st il l n o m i n a l l y m a d e i n t h et a b l e s a b o v e .I n t h e g e n e ra l ca se , t h e r e m a y b e a n u m b e ro f s t a t e s w h i c h c a l l f o r r e d u c t i o n s b y t h es a m e s i n g l e p r o d u c t i o n , a n d t h e r e m a y b eo t h e r p a r s i n g a c t i o n s in t h e s t a t e s w h i c h c a l lf o r th e s e r e d u c t i o n s . I t i s n o t a l w a y s p o s si -b l e , i n g e n e r a l , t o p e r f o r m t h e s e m o d i f i c a -t i o n s w i t h o u t i n c r e a s i n g t h e n u m b e r o fs t a t e s ; c o n d i t i o n s w h i c h m u s t b e s a t is f ie d i no r d e r t o p r o f i t a b l y c a r r y o u t t h i s p r o c e s sa r e g i v e n in [ A h o a n d U l l m a n ( 1 97 3 b )] . I ti s e n o u g h f o r o u r p u r p o s e s t o n o t i c e t h a t i fa r e d u c t i o n b y a s in g l e p r o d u c t i o n A - -* Xi s t o b e e l i m i n a t e d , a n d i f t h i s r e d u c t i o n i sg e n e r a t e d b y e x a c t l y o n e se t o f i t em s c o n -t a i n i n g t h e i t e m w i t h t h e c o r e

    [A ~ X .]t h e n t h i s s in g l e p r o d u c t i o n c a n b e e l i m i n a t e d .I t t u r n s o u t t h a t t h e s i n g l e p r o d u c t i o n sw h i c h a r is e in t h e r e p r e s e n t a t i o n o f o p e r a t o rp r e c e d e n c e o r a s s o c i a t i v i t y c a n a l w a y s b ee l i m i n a t e d ; t h e r e s u l t i s t y p i c a l l y t h e s a m ea s i f a n a m b i g u o u s g r a m m a r w e r e w r it t e n ,a n d t h e c o n f l i c t s r e s o l v e d a s d i s c u s s e d i nS e c t io n 6 . H o w e v e r , t h e a m b i g u o u s g r a m m a rg e n e r a t e s t h e r e d u c e d p a r s e r i m m e d i a t e l y ,w i t h o u t n e e d i n g t h i s o p t i m i z i n g a l g o r i th m[ A h o , J o h n s o n , a n d U l l m a n ( 1 9 7 3 ) ] .O t h e r a p p r o a c h e s t o o p t i m i z a t i o n o f L Rp a r s e r s a r e d i s c u s s e d b y [ A h o a n d U l l m a n(1 9 7 2 b ) ] , [ A n d e r s o n (1 9 7 2 ) ] , [ J o l l i a t ( 1 9 7 3 ) ] ,a n d [ P a g e r ( 1 9 7 0 ) ] . [ A n d e r s o n , E v e , a n dH o r n i n g ( 1 9 7 3 ) ] , [ D e m e r s ( 1 9 7 3 ) ] , a n d[ P a g e r ( 1 9 7 4 ) ] a l s o d i s c u s s t h e e l i m i n a t i o n o fr e d u c t i o n s b y s i n g l e p r o d u