Download presentation
Presentation is loading. Please wait.
1
CH4.1 CSE244 More on LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs, CT 06269-1155 aggelos@cse.uconn.edu http://www.cse.uconn.edu/~akiayias
2
CH4.2 CSE244 LR(1) Items DEF. A LR(1) item is a production with a marker together with a terminal: E.g. [] DEF. A LR(1) item is a production with a marker together with a terminal: E.g. [ S aA.Be, c ] intuition: it indicates how much of a certain production we have seen already (aA) + what we could expect next (Be) + a lookahead that agrees with what should follow in the input if we ever do Reduce by the production S aABe By incorporating such lookahead information into the item concept we will make more wise reduce decisions. Direct use of lookahead in an LR(1) item is only performed in considering reduce actions. (I.e. when marker is in the rightmost). Core of an LR(1) item [] is the LR(0) item Core of an LR(1) item [ S aA.Be, c ] is the LR(0) item S aA.Be Different LR(1) items may share the same core.
3
CH4.3 CSE244 Usefulness of LR(1) items E.g. if we have two LR(1) items of the form [ A ., a ] [ B ., b ] we will take advantage of the lookahead to decide which reduction to use (the same setting would perhaps produce a reduce/reduce conflict in the SLR approach). How the Notion of Validity changes: An item [ A 1. 2, a ] is valid for a viable prefix 1 if we have a rightmost derivation that yields Aaw which in one step yields 1 2 aw
4
CH4.4 CSE244 Constructing the Canonical Collection of LR(1) items Initial item: [ S’ .S, $] Closure. if [A .B , a] belongs to the set of items, and B is a production of the grammar, then: we add the item [B . , b] for all b FIRST( a) Goto. Goto. A state containing [A .X , a] will move to a state containing [A X. , a] with label X Every state is closed according to Closure. Every state has transitions according to Goto.
5
CH4.5 CSE244 Constructing the LR(1) Parsing Table Shift actions: If is in state I k and I k moves to state I m with label then we add the action action[k, ] = “shift m” Shift actions: If [A .b , a] is in state I k and I k moves to state I m with label b then we add the action action[k, b] = “shift m” Reduce actions: If is in state I k then we add the action: “Reduce ” into action[A, ] Observe that we don’t use information from FOLLOW(A) anymore. Reduce actions: If [A ., a] is in state I k then we add the action: “Reduce A ” into action[A, a] Observe that we don’t use information from FOLLOW(A) anymore. Goto part of the table is as before.
6
CH4.6 CSE244Example S’ S S L = R | R L * R | id R L FIRST S * id L * id R * id
7
CH4.7 CSE244 Compare to S’ S S L = R | R L * R | id R L I 0 = { S’ .S, S .L = R, S .R, L .* R, L . id, R .L } I 1 = { S’ S. } I 2 = { S L. = R, R L. } I 3 = { S R. } I 4 = { L *.R, R .L, L .* R, L . id } I 5 = { L id. } I 6 = { S L =. R, R .L, L .* R, L . id } I 7 = { L *R. } I 8 = { R L. } I 9 = { S L = R. } action[2, = ] ?s 6 (because of S L. = R ) r R L (because of R L. and = follows R )
8
CH4.8 CSE244 LALR Parsing Canonical sets of LR(1) items Number of states much larger than in the SLR construction LR(1) = Order of thousands for a standard prog. Lang. SLR = order of hundreds for a standard prog. Lang. LALR (lookahead-LR) A tradeoff: Collapse states of the LR(1) table that have the same core. LALR never introduces a Shift/Reduce Conflict. It might introduce a Reduce/Reduce Conflict (that did not exist in the LR(1))… Still much better than SLR (larger set of languages) … but smaller than LR(1)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.