
Understanding CLR(1) Parsing Tables and LR(1) Automaton
Explore the construction of CLR(1) parsing tables, LR(1) sets of items, LR(1) versus SLR parser, and review of LR(1) automaton. Learn why LR(1) parsers are preferred over SLR parsers despite the trade-offs involved.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Constructing CLR(1) parsing table Consider the following augmented grammar S S C C C c C | d
LR(1) vs SLR parser LR(1) parser is better than SLR parser because LR(1) accepts more languages than SLR SLR(1) is weak because it has no lookahead information But the cost is LR(1) has more states than SLR and thus it consumes more space LR(1) is impractical because its lookahead information makes the automaton too big
Review of LR(1) Each state in an LR(1) automaton is a combination of an LR(0) state and lookahead information. Two LR(1) items have the same core if they are identical except for lookahead.
Canonical LR(1) parsing I1 S I9 R I6 S L= R, $ R L, $ L *R, $ L id, $ S' S, $ S L=R, $ S R, $ L *R, =/$ L id, =/$ R L, $ S' S , $ S L=R , $ I0 id L L id , $ I3' = S L =R, $ R L , $ I2 * L R L , $ I7' idI3' * L * R, $ R L, $ L id, $ L *R, $ L * R, =/$ R L, =/$ L id, =/$ L *R, =/$ L id R I5 I5' L *R , $ I3 L id , =/$ R id I8' * L R * I4 S R , =/$ L *R , =/$ I8 I7 R L , =/$
Canonical LR(1) parsing The table is created in the same way as SLR, except we now use the possible lookahead tokens saved in each state, instead of the FOLLOW sets. Note that the conflict that had appeared in the SLR parser is now gone. However, the LR(1) parser has many more states. This is not very practical. It may be possible to merge states!
LALR(1) parsing This is the result of an effort to reduce the number of states in an LR(1) parser. We notice that some states in our LR(1) automaton have the same core items and differ only in the possible lookahead information. Furthermore, their transitions are similar. States I3 and I3', I5 and I5', I7 and I7', I8 and I8' We shrink our parser by merging such states. SLR : 10 states, LR(1): 14 states, LALR(1) : 10 states
LALR(1) parsing I1 I9 S R I6 S L= R, $ R L, $ L *R, $ L id, $ S' S, $ S L=R, $ S R, $ L *R, =/$ L id, =/$ R L, $ S' S , $ S L=R , $ I0 id L I3 = S L =R, $ R L , $ I2 * L * L * R, =/$ R L, =/$ L id, =/$ L *R, =/$ id R I5 I3 I7 L id , =/$ R L , =/$ id L R * I4 S R , =/$ L *R , =/$ I8
Conflicts in LALR(1) parsing Note that the conflict that had vanished when we created the LR(1) parser has not reappeared. Can LALR(1) parsers introduce conflicts that did not exist in the LR(1) parser? Unfortunately YES. BUT, only reduce/reduce conflicts.
Conflicts in LALR(1) parsing LALR(1) parsers cannot introduce shift/reduce conflicts. Such conflicts are caused when a lookahead is the same as a token on which we can shift. They depend on the core of the item. But we only merge states that had the same core to begin with. The only way for an LALR(1) parser to have a shift/reduce conflict is if one existed already in the LR(1) parser. LALR(1) parsers can introduce reduce/reduce conflicts. Here's a situation when this might happen: merges with A B , y A C , x A B , x/y A C , x/y to give: A B , x A C , y