Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Syntax Analysis - Part IV: Bottom-Up Parsing and LR Grammars, Study Guides, Projects, Research of Electrical and Electronics Engineering

A lecture note from the university of michigan's eecs 483 course, focusing on syntax analysis, specifically bottom-up parsing and lr grammars. It covers topics such as eliminating left recursion, bottom-up parsing, shift-reduce parsing, and lr parsing tables. The document also includes examples and explanations of various concepts.

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 09/17/2009

koofers-user-0zb
koofers-user-0zb 🇺🇸

1

(1)

10 documents

1 / 36

Toggle sidebar

Related documents


Partial preview of the text

Download Syntax Analysis - Part IV: Bottom-Up Parsing and LR Grammars and more Study Guides, Projects, Research Electrical and Electronics Engineering in PDF only on Docsity! Syntax Analysis – Part IV Bottom-Up Parsing EECS 483 – Lecture 7 University of Michigan Wednesday, September 29, 2004 - 1 - Announcements: Turning in Project 1 Anonymous ftp to www.eecs.umich.edu » login: anonymous » pw: your email addr cd groups/eecs483 put uniquename.l put uniquename.y Note, you won’t be able to “get” or “rm” any files in the directory – try if you wish If you make a mistake, then put uniquename2.l and send Yuan mail Grading signup sheet available soon (Fri) - 4 - Bottom-Up Parsing (2) Right-most derivation – Backward » Start with the tokens » End with the start symbol » Match substring on RHS of production, replace by LHS S S + E | E E num | (S) (1+2+(3+4))+5 (E+2+(3+4))+5 (S+2+(3+4))+5 (S+E+(3+4))+5 (S+(3+4))+5 (S+(E+4))+5 (S+(S+4))+5 (S+(S+E))+5 (S+(S))+5 (S+E)+5 (S)+5 E+5 S+E S - 5 - Bottom-Up Parsing (3) S S + E ( S ) S + E E S + E 2E 1 ( S ) S + E 4E 3 S S + E | E E num | (S) 5(1+2+(3+4))+5 (E+2+(3+4))+5 (S+2+(3+4))+5 (S+E+(3+4))+5 Advantage of bottom-up parsing: can postpone the selection of productions until more of the input is scanned - 6 - Top-Down Parsing S S + E ( S ) S + E E S + E 2E 1 ( S ) S + E 4E 3 S S + E | E E num | (S) 5S S+E E+E (S)+E (S+E)+E (S+E+E)+E (E+E+E)+E (1+E+E)+E (1+2+E)+E ... In left-most derivation, entire tree above token (2) has been expanded when encountered - 9 - Shift-Reduce Parsing Parsing actions: A sequence of shift and reduce operations Parser state: A stack of terminals and non- terminals (grows to the right) Current derivation step = stack + input Derivation step stack Unconsumed input (1+2+(3+4))+5 (1+2+(3+4))+5 (E+2+(3+4))+5 (E +2+(3+4))+5 (S+2+(3+4))+5 (S +2+(3+4))+5 (S+E+(3+4))+5 (S+E +(3+4))+5 ... - 10 - Shift-Reduce Actions Parsing is a sequence of shifts and reduces Shift: move look-ahead token to stack Reduce: Replace symbols β from top of stack with non-terminal symbol X corresponding to the production: X β (e.g., pop β, push X) stack input action ( 1+2+(3+4))+5 shift 1 (1 +2+(3+4))+5 stack input action (S+E +(3+4))+5 reduce S S+ E (S +(3+4))+5 - 11 - Shift-Reduce Parsing S S + E | E E num | (S) derivation stack input stream action (1+2+(3+4))+5 (1+2+(3+4))+5 shift (1+2+(3+4))+5 ( 1+2+(3+4))+5 shift (1+2+(3+4))+5 (1 +2+(3+4))+5 reduce E num (E+2+(3+4))+5 (E +2+(3+4))+5 reduce S E (S+2+(3+4))+5 (S +2+(3+4))+5 shift (S+2+(3+4))+5 (S+ 2+(3+4))+5 shift (S+2+(3+4))+5 (S+2 +(3+4))+5 reduce E num (S+E+(3+4))+5 (S+E +(3+4))+5 reduce S S+E (S+(3+4))+5 (S +(3+4))+5 shift (S+(3+4))+5 (S+ (3+4))+5 shift (S+(3+4))+5 (S+( 3+4))+5 shift (S+(3+4))+5 (S+(3 +4))+5 reduce E num ... - 14 - LR Parsing Engine Basic mechanism » Use a set of parser states » Use stack with alternating symbols and states E.g., 1 ( 6 S 10 + 5 (blue = state numbers) » Use parsing table to: Determine what action to apply (shift/reduce) Determine next state The parser actions can be precisely determined from the table - 15 - LR Parsing Table Terminals Algorithm: look at entry for current state S and input terminal C » If Table[S,C] = s(S’) then shift: push(C), push(S’) » If Table[S,C] = X α then reduce: pop(2*|α|), S’= top(), push(X), push(Table[S’,X]) Non-terminals Next action and next state Next stateState Action table Goto table - 16 - LR Parsing Table Example We want to derive this in an algorithmic fashion Input terminal Non-terminals ( ) id , $ S L 1 s3 s2 g4 2 S id S id S id S id S id 3 s3 s2 g7 g5 4 accept 5 s6 s8 6 S (L) S (L) S (L) S (L) S (L) 7 L S L S L S L S L S 8 s3 s2 g9 9 L L,S L L,S L L,S L L,S L L,S St at e - 19 - Example LR(0) State An LR(0) item is a production from the language with a separator “.” somewhere in the RHS of the production Sub-string before “.” is already on the stack (beginnings of possible γ’s to be reduced) Sub-string after “.”: what we might see next E num . E ( . S) state item - 20 - Class Problem For the production, E num | (S) Two items are: E num . E ( . S ) Are there any others? If so, what are they? If not, why? - 21 - LR(0) Grammar Nested lists » S (L) | id » L S | L,S Examples » (a,b,c) » ((a,b), (c,d), (e,f)) » (a, (b,c,d), ((f,g))) Parse tree for (a, (b,c), d) S ( L ) L , S L , S ( S )S a L , S S b c d - 24 - The Goto Operation Goto operation = describes transitions between parser states, which are sets of items Algorithm: for state S and a symbol Y » If the item [X α . Y β] is in I, then » Goto(I, Y) = Closure( [X α Y . β ] ) S’ . S $ S . (L) S . id Goto(S, ‘(‘) Closure( { S ( . L) } ) - 25 - Class Problem E’ E E E + T | T T T * F | F F (E) | id 1. If I = { [E’ . E]}, then Closure(I) = ?? 2. If I = { [E’ E . ], [E E . + T] }, then Goto(I,+) = ?? - 26 - Goto: Terminal Symbols S’ . S $ S . (L) S . id S ( . L) L . S L . L, S S . (L) S . id S id . id id ( Grammar S (L) | id L S | L,S ( In new state, include all items that have appropriate input symbol just after dot, advance do in those items and take closure - 29 - Full DFA S’ . S $ S . (L) S . id S ( . L) L . S L . L, S S . (L) S . id S id .id ( id ( S (L . )L L L . , S L S . L L , . S S . (L) S . id L L,S . S (L) . S’ S . $ final state 1 2 8 9 6 3 7 S ) S $ id L , 5 S 4 Grammar S (L) | id L S | L,S - 30 - S (L) | id L S | L,SParsing Example ((a),b) derivation stack input action ((a),b) 1 ((a),b) shift, goto 3 ((a),b) 1(3 (a),b) shift, goto 3 ((a),b) 1(3(3 a),b) shift, goto 2 ((a),b) 1(3(3a2 ),b) reduce S id ((S),b) 1(3(3(S7 ),b) reduce L S ((L),b) 1(3(3(L5 ),b) shift, goto 6 ((L),b) 1(3(3L5)6 ,b) reduce S (L) (S,b) 1(3S7 ,b) reduce L S (L,b) 1(3L5 ,b) shift, goto 8 (L,b) 1(3L5,8 b) shift, goto 9 (L,b) 1(3L5,8b2 ) reduce S id (L,S) 1(3L8,S9 ) reduce L L,S (L) 1(3L5 ) shift, goto 6 (L) 1(3L5)6 reduce S (L) S 1S4 $ done - 31 - Reductions On reducing X β with stack αβ » Pop β off stack, revealing prefix α and state » Take single step in DFA from top state » Push X onto stack with new DFA state Example derivation stack input action ((a),b) 1 ( 3 ( 3 a),b) shift, goto 2 ((a),b) 1 ( 3 ( 3 a 2 ),b) reduce S id ((S),b) 1 ( 3 ( 3 S 7 ),b) reduce L S - 34 - LR(0) Summary LR(0) parsing recipe: » Start with LR(0) grammar » Compute LR(0) states and build DFA: Use the closure operation to compute states Use the goto operation to compute transitions » Build the LR(0) parsing table from the DFA This can be done automatically - 35 - Homework Problem Generate the DFA for the following grammar S E + S | E E num
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved