Q# Diferences between LR(0), LR(1) ,LR(k),LALR(1) and SLR(1): LR(0) and SLR(1): Both LR(0) and SLR(1) parsers are bottom-up, directional, predictive parsers. parsers. This means that The parsers attempt to apply productions in reverse reverse to reduce the input sentence back to the start symbol (bottom-up ( bottom-up)) The parsers scan the input from from lefttori!ht (directional (directional)) The parsers attempt to predict "hat reductions to apply "ithout necessarily seein! all of the input ( predictive) predictive) Both LR(0) and SLR(1) are shit/reduce arsers# meanin! that they process the tokens of the input stream by placin! them on a stack# and at each each point point either either shitin! a tok token by pushi pushin! n! it onto onto the the stac stack k or reducin! some se$uence of terminals and nonterminals atop the stack back to some nonterminal symbol. %t can be sho"n that any !rammar can be parsed bottomup usin! a shift&reduce parser# but that parser parser mi!ht mi!ht not be deterministic. deterministic. That is# the parser may have to '!uess' "hether to apply a shift or reduction# and may end up havin! to backtrack to realie that it made the "ron! choice. o matter ho" po"erful a deterministic shift&reduce parser you construct# it "ill never be able to parse all !rammars. *hen a deterministic shift&reduce parser is used to parse a !rammar shit/reduce that it cannot handle# it results in c"nicts or reduce/reduce c"nicts# "here the parser may enter a stat state e in "hic "hich h it cann cannot ot tell tell "hat "hat acti action on to take. take. %n a sh shif ift& t&rreduc educe e con+ict# it cannot tell "hether it should add another symbol to the stack or perform some reduction on the top symbols of the stack. %n a reduce&reduce con+ict# the parser kno"s that it needs to replace the top symbols of the stack "ith some nonterminal# but it can,t tell "hat reduction to use. •
• •
-n LR(0) parser is a shift&reduce parser that uses ero tokens of look ahead to determine "hat action to take (hence the 0). This means that in any con!urat uratiion of the pars arser# the pars arser must have ave an unambi!uous action to choose either it shifts a specic symbol or applies a specic reduction. %f there are ever t"o or more choices to make# the parser fails and "e say that the !rammar is not LR(0). %n SLR(1) ('Simp ('Simplie lied d LR(1)'), the parser is allo"ed to look at one token of look ahead "hen decidin! "hether it should shift or reduce. %n particular# "hen the parser "ants to try reducin! somethin! of the form - / " (for nonterminal - and strin! ")# it looks at the net token of input. %f that token could le!ally appear after the nonterminal - in some some deri deriva vati tion on## the the pars parser er reduc educes es.. the ther" r"is ise# e# it does does not. not. The The
intuition here is that in some cases it makes no sense to attempt a reduction# because !iven the tokens "e,ve seen so far and the upcomin! token# there is no possible "ay that the reduction could ever be correct. The only di2erence bet"een LR(0) and SLR(1) is this etra ability to help decide "hat action to take "hen there are con+icts. Because of this# any !rammar that can be parsed by an LR(0) parser can be parsed by an SLR(1) parser. 3o"ever# SLR(1) parsers can parse a lar!er number of !rammars than LR(0). LR(k)
an LR(k) parser is a bottomup parser that "orks by maintainin! a stack of terminals an non terminals. The parser is controlled by a nite automaton that determines# based on the current state of the parser and the net k tokens of input# "hether to shift a ne" token onto the stack or reduce the top symbols of the stack by applyin! a production in reverse. %n order to keep track of enou!h information to make a determination about "hether to shift or reduce# LR(k) parsers have each state correspond to a 'con!uratin! set#' a set of productions annotated "ith the follo"in! information4 3o" much of the production has been seen so far# and *hat tokens to epect after the production has been completed (the look ahead) $r"b%e& 5roblems arise in an LR(k) parser "hen there are con+icts about "hat the parser should do in a !iven state. ne type of con+ict# a shift/reduce conict # comes up "hen the parser is in a state "here a production has been completed# but the look ahead symbols for that production con+ict are also used by another uncompleted production in the state. This means that the parser can,t tell "hether to perform the reduction or not. - second type of con+ict is a reduce/reduce conict # "here the parser kno"s it has to do a reduction# but t"o or more reductions are possible and it can,t tell "hich to do. • •
LALR(1)
SLR(1) is still a fairly "eak parsin! method. 6ore commonly# you "ill see L-LR(1) ('Look ahead LR(1)') parsers bein! used. They too "ork by tryin! to resolve con+icts in an LR(0) parser# but the rules they use for resolvin! con+icts are far more precise than those used in SLR(1)# and conse$uently a much lar!er number of !rammars are L-LR(1) than are SLR(1). To be a bit more specic# SLR(1) parsers try to resolve con+icts by lookin! at the structure of the !rammar to learn more information
about "hen to shift and "hen to reduce. L-LR(1) parsers look at both the !rammar and the LR(0) parser to !et even more specic information about "hen to shift and "hen to reduce. Because L-LR(1) can look at the structure of the LR(0) parser# it can more precisely identify "hen certain con+icts are spurious. The Linu utilities yacc and bison# by default# produce L-LR(1) parsers. %n SLR(1) or L-LR(1)# the parser then makes the decision to shift or reduce based on the net token of look ahead.
Thank you for interesting in our services. We are a non-profit group that run this website to share documents. We need your help to maintenance this website.