LR() nlysis Drwcks of LR(). Look-hed symols s eplined efore, concerning LR(), it is possile to consult the net set to determine, in the reduction sttes, for which symols it would e possile to perform reductions. The key concept is the look-hed symol. Using this symol will llow us to er in mind, together with the input symol tht is eing studied, the net one tht ppers in the input. ecuse this symol will lso e used in lter step, it receives the nme of look-hed symol. In LR(), when we reduced rule R in stte s only for the elements in the net set, tht ction is like considering ll the look-hed symols for R in ll the possile sttes. LR() nlysis Drwcks of LR(): emple Rememer the ugmented grmmr: () () () () () () LR() nlysis Drwcks of LR(): emple We hve seen n emple grmmr which is not LR(): () () () () () this grmmr genertes the following lnguge: {} { n n n } LR() nlysis Drwcks of LR(): emple Deterministic finite utomt with the trnsitions s s s s s s s s s s
LR() nlysis Drwcks of LR(): emple nd this ws the nlysis tle for the LR() grmmr, with the conflict s s cc () () () () () () s /s s r r ction LR() nlysis Drwcks of LR(): emple The following ws the ugmented grmmr: () () + () * () i LR() nlysis Drwcks of LR(): emple The lst emple seen ws n emple of n miguous grmmr which ws not LR(). () + () * () i LR() nlysis Drwcks of LR(): emple Rememer the trnsition digrm in the LR() utomt () () + () * () i
LR() nlysis Drwcks of LR(): emple nd the LR() mtri showing the conflicts * + s () () + () * () i s r/s s r/s s s cc r r/s r/s r ction The grmmr is not LR() LR() nlysis ttes of the utomt in n LR() nlyser: introductory emple The emple used to illustrte LR() nd LLR() grmmrs will e the following: {, n n n } () () () () () This is not the simplest grmmr we cn uild for this lnguge, ut it is good emple, ecuse: It is not LR() It is LR() It is LLR, ut the utomt otined with this method will e different thn the one otined for n LR() prser We shll see, in this introductory emple, the initil stte s nd the trnsition from s to s. LR() nlysis Introduction to LR(k) with k= In the nlysis, t ech step, we shll tke into considertion: The current symol tht is eing treted The k symols tht cn follow it (k look-hed symols). ch stte in the utomt will hve severl copies, s mny s possile sequences of k symols tht cn follow the current stte. LR() nlysis Look-hed symols in n LR() nlyser: introductory emple For ech stte, we shll need to clculte the set of possile look-hed symols. This process cn e incorported to the construction of the utomt. It will suffice with: pecifying procedure for clculting ll the look-hed symols in ll the configurtions in ech stte. The new sttes will dd eplicitly, to ech configurtion, the set of lookhed symols tht re vlid, using the following nottion: s i = { <configurtion> {<look-hed> j }, <configurtion> i {<look-hed> ji }, <configurtion> n {<look-hed> jn } } i
LR() nlysis Look-hed symols in n LR() nlyser: introductory emple Considering the initil stte, s in LR() nd LR(), it ws clculted s the closure of the following configurtion: is the old iom, the non-terminl symol to which we should derive the complete derivtion tree fter we finish the nlysis. Therefore, the only look-hed symol tht we should epect t this position is the endof-progrm symol () Therefore, tht configurtion in the initil stte will hve the set of lookhed symols {} The net digrm shows this emple. LR() nlysis Look-hed symols in n LR() nlyser: introductory emple We cn continue the closure with the rules for : s ={ {}, {}, {}, {}, {}} s efore, ecuse ws t the end of the rule, fter the we my find nything tht we could find fter the (the look-hed symols for tht configurtion): {} The sme rgumenttion cn e followed to show tht the lookhed symols for the lst configurtion to e dded to this stte is lso {}: s ={ {}, {}, {}, {}, {}, {}} LR() nlysis Look-hed symols in n LR() nlyser: introductory emple Furthermore, the initil stte will contin the closure of the configurtion For clculting closures, we need to clculte the look-hed symols for ech of the new configurtions dded: This cse will e solved with the help of the first nd net sets. We shll use the prts of the right-hnd sides of the non-terminl symol tht follows the dot. For instnce, in the initil stte s s = { {}, {}, {}} the two new configurtions dded cn hve, s look-hed, nything tht could pper fter the in ecuse the ws t the end of the rule, nd the look-hed symol for tht rule ws, the look-hed symols for the new configurtions re the sme. LR() nlysis Look-hed symols in n LR() nlyser: introductory emple Deterministic Finite utomt with trnsitions s
LR() nlysis Look-hed symols in n LR() nlyser: introductory emple Deterministic Finite utomt with trnsitions s {} {} {} {} {} {} LR() nlysis Look-hed symols in n LR() nlyser: introductory emple The closure of {} implies the inclusion of two new configurtions: s this is the cse, we proly hve to chnge the sets of lookhed symols, s follows: The closure of {} forces us to keep the hypothesis tht we might now find ny right-hnd side of. On the other hnd, fter processing we shll lso need to shift nd, only fter we hve found the, we shll e prepred to reduce only in the presence of the look-hed symols in the set {}. This mens tht the three configurtions dded to the closure of {}, tht is,, or, will need to keep s look-hed symol, ecuse tht ppers right fter the ( {}). LR() nlysis Look-hed symols in n LR() nlyser: introductory emple Let us continue the emple with trnsition etween two sttes: gin, the closures will e solved with the help of the first nd net sets. For instnce, from the initil stte s s = { {}, {}, {}, {}, {}, {}} When we clculte s s the resulting stte of go_to(s,), the underlined configurtion cn e shifted to {} LR() nlysis Look-hed symols in n LR() nlyser: introductory emple In summry, with respect to the stte s, ecuse of the symol, oth hve the look-hed symol : {} {} The presence of {} in s forces us to etend the closure with ll the right-hnd sides of. The sitution in this cse is different, ecuse there is no symol fter the in the rule ( (*){}) In this cse, s in s efore, the closure does not modifies the set of look-hed symols, nd the element which will e look-hed symol of the configurtion will e {}
LR() nlysis Look-hed symols in n LR() nlyser: introductory emple o we cn conclude tht s = { {}, {}, {}, {} } s we cn see in the following digrm: LR() nlysis Look-hed symols in n LR() nlyser: introductory emple Deterministic Finite utomt with trnsitions s {} {} {} {} {} {} s {} {} {} {} LR() nlysis Look-hed symols in n LR() nlyser: introductory emple Deterministic Finite utomt with trnsitions s {} {} {} {} {} {} LR() nlysis Look-hed symols in n LR() nlyser: formlistion Formlly, to clculte the look-hed symols for the sttes trnsitions digrm, The initil configurtion in the initil stte hve the following look-hed symols: {} For clculting s j =closure(s i ) for the elements s i of the form P α Nβ{σ,...,σ n },, N we need to dd net(n), clculted using the previous rule, s look-hed symol for ll the new configurtions N... This net(n) set will e first(β{σ,...,σ n }) net(p) if β{σ,...,σ n } * λ Rememer, from the clcultion of the net set, tht, if β cn derive the empty word, we hve to dd not only first(β) to the set net(n), ut lso first(σ ), where σ is the symol tht follows β in the right-hnd side of the rule, nd net(p), if β is the lst symol in the right-hnd side!!!!
LR() nlysis Introductory eercise uild the LR() nlysis tle for the following grmmr tht genertes lnguge L={, n n n } () () () () () Use them to nlyse the following two strings: The first step is to otin the ugmented grmmr: () () () () () LR() nlysis Constructing LR() nlysis tles Deterministic Finite utomt with trnsitions s {} {} {} {} {} {} first()={} () LR() nlysis Constructing LR() nlysis tles Deterministic Finite utomt with trnsitions LR() nlysis Constructing LR() nlysis tles Deterministic Finite utomt with trnsitions s {} s {} {} {} {} {} {} first()={} s {} s {} {} {} {} {} {} s {} first()={}
LR() nlysis Constructing LR() nlysis tles Deterministic Finite utomt with trnsitions s {} s {} s {} {} {} {} {} {} s {} LR() nlysis Constructing LR() nlysis tles Deterministic Finite utomt with trnsitions s {} s {} s {} {} {} {} {} {} s {} s {} {} {} {} {} {} first()={} first()={} first()={} first()={} LR() nlysis Constructing LR() nlysis tles Deterministic Finite utomt with trnsitions s {} s {} s {} {} {} {} {} {} s {} {} {} LR() nlysis Constructing LR() nlysis tles Deterministic Finite utomt with trnsitions s {} s {} s {} {} {} {} {} {} s {} s s {} {} {} {} {} {} {} first()={} first()={} first()={} first()={} s s s
LR() nlysis Constructing LR() nlysis tles Deterministic Finite utomt with trnsitions s {} s {} s {} {} {} {} {} {} s {} s s {} {} {} {} {} s {} {} {} {} {} {} LR() nlysis Constructing LR() nlysis tles Deterministic Finite utomt with trnsitions s {} s {} s {} {} {} {} {} {} s s {} s {} {} {} {} s {} s s {} {} {} {} {} {} {} s {} first()={} first()={} first()={} first()={} first()={} first()={} first()={} first()={} LR() nlysis Constructing LR() nlysis tles Deterministic Finite utomt with trnsitions s {} s {} s {} {} {} {} {} {} s {} s s {} {} {} {} {} s {} {} {} {} {} {} s {} LR() nlysis Constructing LR() nlysis tles Deterministic Finite utomt with trnsitions s {} s {} s {} s {} {} {} {} {} {} s s {} s {} {} {} {} s {} s s {} {} {} {} {} {} {} s {} first()={} first()={} first()={} first()={} first()={} first()={} first()={} first()={} s s
LR() nlysis Constructing LR() nlysis tles Deterministic Finite utomt with trnsitions s {} s {} s {} s {} {} {} {} {} {} s s {} s {} {} {} {} s {} s s {} {} {} {} {} {} {} s {} LR() nlysis Constructing LR() nlysis tles Deterministic Finite utomt with trnsitions s {} s {} s {} s {} {} {} {} {} {} s s {} s {} {} {} {} s {} s s {} {} {} {} {} {} {} s {} first()={} first()={} first()={} first()={} {} first()={} first()={} first()={} first()={} {} s s LR() nlysis Constructing LR() nlysis tles Deterministic Finite utomt with trnsitions s {} s {} s {} s {} {} {} {} {} {} s s {} s s {} {} {} {} {} s s {} {} {} {} {} {} {} s {} LR() nlysis Constructing LR() nlysis tles Deterministic Finite utomt with trnsitions s {} s {} s {} s {} {} {} {} {} {} s s {} s {} {} {} {} s {} s s {} {} {} {} {} {} {} s {} first()={} first()={} first()={} first()={} {} first()={} first()={} first()={} first()={} {} s s
LR() nlysis Constructing LR() nlysis tles Deterministic Finite utomt with trnsitions s {} s {} {} {} {} {} {} s s {} {} {} s {} s s {} {} {} {} {} s {} s s {} {} {} {} {} s {} first()={} first()={} first()={} first()={} {} s {} s LR() nlysis Constructing LR() nlysis tles: method hifts in the tle: It is the sme s in LR() They cn e otined y following the trnsitions in the tle. If the utomt cn go from s i to s j y mens of symol X, then we shll dd the following ction: sj i X yntctic_tle[i,x]= j i X Reductions in the tle: In the cells for the sttes which contin reduction configurtions, of the form γ {σ,...,σ n } we hve to dd the reduction of the rule γ only in the columns for their look-hed non-terminl symols, i.e., {σ,...,σ n }. Therefore, this step is different to tht in LR() LR() nlysis Constructing LR() nlysis tles Deterministic Finite utomt with trnsitions s s {} {} {} {} {} {} {} s s {} {} {} s {} s s {} {} {} {} {} s s {} {} s {} s {} {} {} {} s {} first()={} first()={} first()={} first()={} {} s {} s LR() nlysis Constructing LR() nlysis tles: method ccepttion: It is the sme s in LR() nlysers If stte s i hs trnsition with the terminl symol to the finl stte with the configurtion iom iom, we hve to dd the ccept ction to yntctic_tle[i,]. Is it possile to find lterntive techniques for ccepttion in LR() prsers. rror: It is the sme s in LR() ll the empty cells hve ssocited the error ction.
nlysis tle The following is n emple of nlysis with two strings: LR() nlysis Constructing LR() nlysis tles: method s s s cc r s s r ction LR() nlysis: eercise {, n n n } s s cc r s s s () () () () () () r ction s s LR() nlysis: eercise {, n n n } s cc r s s s () () () () () () r ction LR() nlysis: eercise {, n n n } s cc r s s s () () () () () () r ction
s s LR() nlysis: eercise {, n n n } s cc r s s s () () () () () () r ction LR() nlysis: eercise {, n n n } s cc r s s s () () () () () () r ction s s LR() nlysis: eercise {, n n n } s cc r s s s () () () () () () r ction LR() nlysis: eercise {, n n n } s cc r s s s () () () () () () r ction
s s LR() nlysis: eercise {, n n n } s cc r s s s () () () () () () r ction LR() nlysis: eercise {, n n n } s cc r s s s () () () () () () r ction s s LR() nlysis: eercise {, n n n } s cc r s s s () () () () () () ction LR() nlysis: eercise {, n n n } s cc r s s s () () () () () () r ction
s s LR() nlysis: eercise {, n n n } s cc r s s s () () () () () () r ction LR() nlysis: eercise {, n n n } s cc r s s s () () () () () () r ction s s LR() nlysis: eercise {, n n n } s cc r s s s () () () () () () r ction LR() nlysis: eercise {, n n n } s cc r s s s () () () () () () r ction
LR() nlysis: eercise {, n n n } s s cc r s s s () () () () () () r ction LR(k) nlysis Generlistion to k look-hed symols (k > ) This sme technique cn e etended to consider ny numer of lookhed symols. This generlistion is out of the scope of this course. LR() nlysis vlution Power: It cn e shown tht LR() is the most powerful nlysis lgorithm mongst those tht nlyse the string from left to right using just look-hed symol. fficiency: s cn e oserved from the emples, there re considerly more sttes in LR() nlysers thn in LR() nlysers.