110 likes | 556 Views
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”
E N D
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 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 http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction
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 id + id + id * id $ State stack Symbol stack + 6 SLR Parser E 1 Output $ 0 F I10 I7 T ::= T * • F F ::= • ( E ) F ::= • id Parsing Table ( I4 I5 id http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction
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: 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 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(I, X) For all A::=α • X β in I add A::=α X • β to J Return CLOSURE(J) GOTO identifies groups of “kernel items”, whose dots are not at extreme left. CLOSURE adds “nonkernel items”, whose dots are at extreme left. http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction
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(SETI, a) • where [A::=α • X β] is in SETI • 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 SETI • Actions (for state I + nonterminal symbol X) are of the form • go to state N where GOTO(SETI, X) = SETN http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction
SLR Parsing Table Example http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction