Download presentation
Presentation is loading. Please wait.
Published byPhoebe Blankenship Modified over 9 years ago
1
LANGUAGE TRANSLATORS: WEEK 17 scom.hud.ac.uk/scomtlm/cis2380/ See Appel’s book chapter 3 for support reading Last Week: Top-down, Table driven parsers Next Few Weeks: Bottom-up, Table driven, Shift-Reduce (SR) Parser n How SR Parsers Work n How to Create SR Parsers n Practical: How to use JavaCup (which creates SR Parses) This Week’s TUTORIALS: How SR Parsers Work + JavaCup
2
Parsing Direction Generating Symbol begin X = X + 1 ; end TOP DOWN BOTTOM UP
3
Pushdown Automata Table driven Parsers are “Pushdown Automata” (PDA) PDAs are composed of a control, a stack and a table The control decides what to do using the table, the current input, the top of the stack, and the current state (if any). stack operations (for stack K) include: n “Pop(K)” – removes (discards) the top of K n “Push(W,K)” – pushes symbols of string W from right to left on to the top of K n “Top(K)” – reads the top of the stack table operations are restricted to table look-up table stack control Output Input Sequence of Symbols
4
Table-Driven Parser Table driven Parsing – Summary n Parsing is carried out by a PDA n PDA’s table is automatically created from the language’s grammar. n Language – independent method (generate a different table for each language): the control and stack are fixed, depending on the type of Table-driven parser. table Input Sequence of Tokens stack control Parse Tree or Error Different Language? Change Table
5
Top Down Parsing – Summary Control Loop K is the stack n is the next token case Top(K) -- Non-Terminal: If lookup(Top(K),n) returns rule X ::= W then {Pop(K); Push(W, K);} else error -- Token: If n == Top(K) then {Pop(K); consume n;} else error end case table stack control Table Tokens Non- Terminals Entries are Refs. To Rules X ::= W Stack contains NTs/Ts symbols (Non- Terminals /Tokens) n
6
Top Down Parsing – Summary n Initialisation: stack = Push(start symbol, empty stack) n is the first token in the input stream n End condition stack = empty stack Input stream is empty
7
Table-Driven Parser Introducing: Bottom-up, Table-driven, Shift- Reduce (SR) Parser n Same “Table- Driven” idea – so architecture is the same n Table Auto-generated from Grammar as Top Down Parser But: n Control, Stack + Table more elaborate n Uses an explicit state n More robust + popular than Top Down n Popular Parser Generators available (e.g. JavaCup, YACC) table Input Sequence of Tokens stack control Parser- Generator (JAVACUP) BNF Grammar Parse Tree or Error
8
Introducing SR Parsing (Appel, p60) Control Loop K is the stack n is the next token s is Top(K), the current state case lookup(n,s) of s x: Push(n,K); Push(x,K) r y: Reduce K with rule y g z: Push(z,K) end case NB: any null entries give syntax errors table stack control Table Tokens+Non-Terminals State Numbers Shift (s), Reduce(r), Goto (g) actions on the Stack / State Stack state No. NTs/Ts state No. NTs/Ts state No. ETC.. …. n
9
Introducing SR Parsing n Initialisation: stack = Push(state 1, empty stack) n is the first token in the input stream n End condition Reach entry “accept” in the table.
10
“Shift-Reduce” Parsers - General Workings Shift x: (means “Shift symbol, move to state x”) Put symbol onto the top of the stack; Put the new state number x on top of the stack Reduce y: (means “Reduce with rule y”) matching the RHS of rule y with the top of the stack and REMOVE all the matched top; Push the LHS of rule y onto the top of the stack; Input LHS of rule k + state below it to the Table to find out the next state s, then push s onto the stack.
11
LR Parsers - Summary In this lecture n we have reviewed how TD table driven parsers work n we have seen HOW BU table driven parsers work During the week – go through trace in handout example NB u The SR parser is called “LR” means parse string from Left to right, but build up the parse tree from the Right of the string first. u “Most” parsers are “LR(1)” - the “1” means they look at the 1 next token in the string.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.