Download presentation
Presentation is loading. Please wait.
Published byBrandon Moody Modified over 8 years ago
1
Three kinds of bottom-up LR parser SLR “Simple LR” –most restrictions on eligible grammars –built quite directly from items as just shown LR “Canonical LR” –fewest restrictions on eligible grammars, but still not unrestricted –parsing table is typically enormous LALR “Lookahead LR” –intermediate in restrictions on eligible grammars –parsing table no bigger than with SLR –parsing method of choice for most compilers –YACC builds LALR parsers http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction1 A grammar is said to be SLR, LALR, LR if it is possible to build a parser for it using the SLR, LALR or LR method
2
Simple LR Sets of “items” correspond to states of an automaton Two stacks are used in tandem, one for states and one for grammar symbols Symbol stack actually unnecessary! Parsing table contains Actions & Gotos A Shift action specifies the new state to go to A Reduce action indicates which production is used to reduce Appropriate ( |rhs| ) number of symbols & states popped from stacks lhs symbol pushed onto symbol stack state for that symbol from state now at stack top is pushed onto state stack http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction2 id+ + * $ SLR Parser Parsing Table + E $ Output 6 1 0 State stack Symbol stack I 7 T ::= T * F F ::= ( E ) F ::= id F ( id I 10 I4I4 I5I5
3
Constructing the collection of sets of the automaton - 1 Two subroutines, CLOSURE (of SetOfItems) and GOTO (of setOfItems & symbol), are used in computing the set of sets of items: http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction3 CLOSURE(I) Add all items in I to J If A::=α B β is in I & B::=γ is a production then add B::= γ to set J Repeat until J stabilises Return J GOTO(I, X) For all A::=α X β in I add A::=α X β to J Return CLOSURE(J) ITEMS(G’) Add CLOSURE({S’::= S}) to C For all I in C For each grammar symbol X unless GOTO(I,X) is empty add GOTO(I,X) to set C Repeat until C stabilises Return C GOTO identifies groups of “kernel items”, whose dots are not at extreme left. CLOSURE adds “nonkernel items”, whose dots are at extreme left.
4
Constructing the SLR parsing table Entries are indexed by a state (at top of state stack) and a grammar symbol Actions (for state I + terminal symbol X) are of the form s6, meaning ‘shift symbol and push state #6’ using GOTO(SET I, a) where [A::=α X β] is in SET I r8, meaning ‘reduce according to production #8’ where production #8 reads ‘A::=α ’ and A is not S’ and X is in FOLLOW(A) accept, if X = $ and [S’::=S ] is in SET I Actions (for state I + nonterminal symbol X) are of the form go to state N where GOTO(SET I, X) = SET N http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction4
5
SLR Parsing Table Example http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction5
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.