Exercises II. Exercise: Lexical Analysis
|
|
- Clarissa Mae Martin
- 5 years ago
- Views:
Transcription
1 xercises II Text adapted from : Alessandro Artale, Free University of Bolzano les adapted from : nrico Cimitan, Università di Padova xercise: Lexical Analysis Describe the notions of token, token name, lexeme, and attribute, and prove examples of their use 1
2 xercise: Lexical Analysis Input : y = 42 The lexemes are y, =, 42 The tokens are, y, assign, num, 42 The token names are, assign, num The attributes for first and third tokens are y, 42 ; second token does not need an attribute xercise: Lexical Analysis To describe the set of lexemes we need patterns xample of patterns expressed by means of Rs Idenifier: [a-za-z][a-za-z_0-9]* (N.B. keywords ignored) Number : [0-9] + 2
3 xercise: Lexical Analysis During LA there are two kinds of conflicts everal portions of a lexeme are recognized by the same Rs The same lexeme is recognized by several Rs Describe how to resolve these conflicts xercise: Lexical Analysis The conflict between several portions of different lengths is resolved by taking the longest match The conflict between several Rs on the same lexeme is resolved by taking the Rs with highest precedence 3
4 xercise: Finite Automata Describe a lexer that recognizes entifiers and numbers (integers), and show the finite automaton xercise: Finite Automata Flex code ws [\n \t]+ %% [0-9]+{ws} {printf ( int\n );} [a-za-z][a-za-z_0-9]*{ws {printf( \n );} %% 4
5 xercise: Finite Automata ɛ [a-za-z] ws print start [a-za-z_0-9] ɛ [0-9] ws print «integer» [0-9] xercise: Top-Down Parsing Conser the following grammar with terminals T = { [, ], a, b, c, +, - } : [ X ] a X + Y Y b ɛ Y - X c ɛ Prove the parsing table for the LL(1) top down parser 5
6 Recall: FIRT() FIRT(α) = the set of terminals that begin all strings derived from α FIRT(a) = {a} if a T FIRT(ε) = {ε} FIRT(A) = A α FIRT(α) for A α P FIRT(X 1 X 2 X k ) : if for all j = 1,, i-1 : ε FIRT(X j ) then add FIRT(X i )\{ε} to FIRT(X 1 X 2 X k ) if for all j = 1,, k : ε FIRT(X j ) then add ε to FIRT(X 1 X 2 X k ) xercise: Top-Down Parsing FIRT(a) = {a}, if a T = { [, ], a, b, c, +, - } FIRT(Y) = { -, ε } FIRT() = { [, a } FIRT(X) = {+} FIRT(Y)\{ε} {b} {ε} (since Y derives ε) = { +, -, b, ε } 6
7 xercise: Top-Down Parsing A α FIRT(α) [ X ] [ a a X + Y + X Y b - b X ɛ ε Y - X c - Y ɛ ε Recall: FOLLOW() FOLLOW(A) = the set of terminals that can immediately follow nonterminal A FOLLOW(A) = for all (B α A β) P do add FIRT(β)\{ε} to FOLLOW(A) for all (B α A β) P and ε FIRT(β) do add FOLLOW(B) to FOLLOW(A) for all (B α A) P do add FOLLOW(B) to FOLLOW(A) if A is the start symbol then add $ to FOLLOW(A) 7
8 xercise: Top-Down Parsing FOLLOW(X) = { ], c } FOLLOW(Y) = FOLLOW(X) {b} = { ], c, b } FOLLOW() = {$} FIRT(X)\{ε} {]} ( [X] and X ɛ) FIRT(X)\{ε} {c} (Y -Xc and X ɛ) FIRT(Y)\{ε} FOLLOW(X) (X +Y and Y ɛ) (since FIRT(Y) FIRT(X) ) = {$} FIRT(X) \{ε} {]} {c} FOLLOW(X) = = { $, +, -, b, ], c } xercise: Top-Down Parsing A X Y FOLLOW(A) $ + - b ] c ] c ] c b 8
9 Recall: Constructing an LL(1) Predictive Parsing Table for each production A α do for each a FIRT(α) do add A α to M[A, a] enddo if ε FIRT(α) then for each b FOLLOW(A) do add A α to M[A, b] enddo endif enddo Mark each undefined entry in M error xercise: Top-Down Parsing FIRT & FOLLOW as computed before : A α FIRT(α) A FOLLOW(A) [ X ] [ a a $ + - b ] c X + Y + X Y b - b X ] c X ɛ ε Y - X c - Y ɛ ε Y ] c b 9
10 xercise: Top-Down Parsing [ ] a b c + - $ [ X ] a X X ɛ (FOLLOW) X Y b X ɛ (FOLLOW) X + Y X Y b Y Y ɛ (FOLLOW) Y ɛ (FOLLOW) Y ɛ (FOLLOW) Y - X c Productions marked as FOLLOW are inserted in the second phase of the algorithm xercise: Top-Down Parsing Prove the stack and the moves of the LL(1) parser on input [ a b ] 10
11 xercise: Top-Down Parsing tack $ $ ] X [ $ ] X $ ] X a $ ] X $ ] b Y $ ] b $ ] $ Input [ a b ] $ [ a b ] $ a b ] $ a b ] $ b ] $ b ] $ b ] $ ] $ $ Production applied [ X ] (match!) a (match!) X Y b Y ɛ (match!) (match!) (end! accept.) xercise: Top-Down Parsing xplain the backtracking technique for top-down parsing and prove an example consering the grammar [ X ] a X + Y Y b ɛ Y - X c ɛ 11
12 xercise: Top-Down Parsing Backtracking is exploited in top-down parsers that do not use predictive parsing The parser keeps a record of all previous decisions for production application, and sets up a trial-and-error strategy Note that choosing a wrong production leads the repeated reading of a portion of the input xercise: Top-Down Parsing tack $ $ ] X [ $ ] X $ ] X ] X [ Input [ a b ] $ [ a b ] $ a b ] $ a b ] $ Production applied [ X ] (match!) [ X ] Not matching! Back to last choice that can be changed (8 ) 8 choice (1/2) 8 choice (1/2) 8 marks choices where alternatives are available 12
13 xercise: Top-Down Parsing tack $ $ ] X [ $ ] X $ ] X a $ ] X $ ] Y + Input [ a b ] $ [ a b ] $ a b ] $ a b ] $ b ] $ b ] $ Production applied [ X ] (match!) a (match!) X + Y Not matching! Back to last choice that can be changed (8 ) 8 choice (1/2) choice (2/2) 8 choice (1/3) xercise: Top-Down Parsing tack $ $ ] X [ $ ] X $ ] X a $ ] X $ ] b Y $ ] b c X - Input [ a b ] $ [ a b ] $ a b ] $ a b ] $ b ] $ b ] $ b ] $ Production applied [ X ] (match!) a (match!) X Y b Y - X c Not matching! Back to last choice that can be changed (8 ) 8 choice (1/2) choice (2/2) 8 choice (2/3) 8 choice (1/2) 13
14 xercise: Top-Down Parsing tack $ $ ] X [ $ ] X $ ] X a $ ] X $ ] b Y $ ] b $ ] $ Input [ a b ] $ [ a b ] $ a b ] $ a b ] $ b ] $ b ] $ b ] $ ] $ $ Production applied [ X ] (match!) a (match!) X Y b Y ɛ (match!) (match!) (end! accept.) 8 choice (1/2) choice (2/2) 8 choice (2/3) choice (2/2) xercise: Bottom-Up Parsing Conser the grammar L ; L T VL T array Idx of T int VL, VL Idx num Prove the parsing table for the LR bottom-up parser 14
15 Recall: Function closure() 1. tart with closure(i) = I 2. If [A α Bβ] closure(i) then for each production B γ in the grammar, add the item [B γ] to I if not already in I 3. Repeat 2 until no new items can be added Recall: Function goto() 1. For each [A α Xβ] I, add [A αx β] to goto(i, X), if not already there 2. Compute closure() of the resulting set 15
16 Recall: build LR(0) collection teps: - augment grammar with initial production - start from the state containing the closure of the set containing only the item derived from the initial production: closure( {[ ʹ ]} ) - add iteratively the states that are reachable from the existing states using goto(i,x), for some state I already added and some symbol (terminal or nonterminal) X Recall: build the collection Procedure: C = { closure( {[ ʹ ]} ) } repeat for each set of items I in C and each grammar symbol X such that goto(i, X) is not empty and not in C do add goto(i, X) to C until no new sets of items can be added to C 16
17 xercise: Bottom-Up Parsing Augmented grammar L ʹ L L ; L T VL T array Idx of T int VL, VL Idx num In the next sle, 8 means kernel item xercise: Bottom-Up Parsing tate 0 8 L ʹ L L ; L L T VL T array Idx of T T int tate 1 8 T array Idx of T Idx num tate 2 8 T int tate 3 8 L ʹ L tate 4 8 L ; L 8 L tate 5 8 T VL VL, VL VL tate 6 8 T array Idx of T tate 7 8 Idx num tate 8 8 L ; L L ; L L T VL T array Idx of T T int tate 9 8 T VL tate 10 8 VL, VL 8 VL tate 11 8 T array Idx of T T array Idx of T T int tate 12 8 L ; L tate 13 8 VL, VL VL, VL VL tate 14 8 T array Idx of T tate 15 8 VL, VL 17
18 start xercise: Bottom-Up Parsing Idx 6 1 num array 7 int 2 int array L ; 4 T T VL 5 9 of 11 T array 14 int L 12 15, VL Recall: LR parsing table 1. Augment the grammar with L L P done 2. Construct the set C={I 0,I 1,,I n } of LR(0) states P done 3. If [A α aβ] I i and goto(i i, a)=i j then set action[i, a]=shift j 4. If [A α ] I i then set action[i, a]=reduce A α for all a FOLLOW(A) (apply only if A L ) O need FOLLOW! 5. If [L L ] is in I i then set action[i, $]=accept 6. If goto(i i, A)=I j then set goto[i, A]=j 7. Repeat 3-6 until no more entries added 8. The initial state i is the I i holding item [ ] 18
19 xercise: Bottom-Up Parsing L ʹ L L ; L T VL T array Idx of T int VL, VL Idx num A FOLLOW(A) Idx of T FIRT(VL)= L $ ; FOLLOW(L)= $ VL FOLLOW()= ; $ 1. L ʹ L 2. L ; L 3. L 4. T VL 5. T array Idx of T 6. T int 7. VL, VL 8. VL 9. Idx num states action array int num, ; of $ L VL T Idx 0 s1 s s7 6 2 r6 3 acc 4 s8 r3 5 s s11 7 r9 8 s1 s r4 r4 10 s13 r8 r8 sx : shift & go to state x 11 s1 s2 14 ry : reduce using y 12 r2 Red entries: LR 13 s10 15 reduce actions derived from 14 r5 lookahead (FOLLOW) 15 r7 r7 goto 19
20 xercise: Bottom-Up Parsing how the stack and the moves of the LR parser on input array 5 of int x xercise: Bottom-Up Parsing tack $ 0 $ 0 $ 0 array 1 $ 0 array $ 0 array 1 Idx 6 $ 0 array 1 Idx 6 of 11 $ 0 array 1 Idx 6 of 11 int 2 $ 0 array 1 Idx 6 of 11 T 14 $ 0 T 5 $ 0 T 5 x 10 $ 0 T 5 VL 9 $ 0 4 $ 0 L 3 Input array 5 of int x $ array 5 of int x $ 5 of int x $ of int x $ of int x $ int x $ x $ x $ x $ $ $ $ $ Action start from state 0 shift & goto 1 shift & goto 7 reduce with 9: Idx num goto(1,idx)=6 shift & goto 11 shift & goto 2 reduce with 6: T int goto(11,t)=14 reduce with 5: T array Idx of T goto(0,t)=5 shift & goto 10 reduce with 8: VL goto(5,vl)=9 reduce with 4: T VL goto(0,)=4 reduce with 3: L goto(0,l)=3 action(3,$)= accept! 20
21 xercise: emantic Analysis 4) Conser the DD in the next sles, where newlabel() generates a fresh symbolic label newtemp() generates a fresh variable name gen() generates strings, is string concatenation code is the attribute containing 3AC.place is the name of the variable associated to the token relop.op is a comparison operator (<, <=, =, ) xercise 4: emantic Analysis Productions Prog 1 ; 2 if Test then{ 1 } = emantic rules.next = newlabel(); Prog.code =.code gen(.next : ) 1.next = newlabel(); 2.next =.next;.code = 1.code gen( 1.next : ) 2.code Test.true = newlabel(); Test.false =.next; 1.next =.next;.code = Test.code gen(test.true : ) 1.code.code =.code gen(.place =.place) 21
22 xercise 4: emantic Analysis Productions Test 1 relop emantic rules Test.code = gen( if 1.place relop.op 2.place goto Test.true) gen( goto Test.false).place = newtemp();.code = 1.code gen(.place = 1.place +.place).place =.place;.code = xercise 4: emantic Analysis Conser the input: if y > w then { y = x + z}; x = z + v 22
23 xercise 4: emantic Analysis 4.1) how the annotated parse tree (without the code attribute) for the input together with the values of the attributes xercise 4: emantic Analysis Prog ; if Test relop then { } = + = + if y > w then { y = x + z } ; x = z + v 23
24 xercise 4: emantic Analysis Prog.next = LABL3.next = LABL2 ;.next = LABL3 if then Test.true = LABL1 Test.false = LABL2 {.next = LABL2 }.place = y =.place =t1.place = x =.place = z +.place =t2.place = v.place =x +.place = z.place = y.place = w.place = z relop.op = > Id.place = x if y > w then { y = x + z } ; x = z + v xercise 4: emantic Analysis Prog ; if Test then { } : = relop : = + + if y > w then { y : = x + z } ; x : = z + v 24
25 xercise 4: emantic Analysis code next = LABL3 code Prog ; code next = LABL3 place = t2 code Place = x = + place = z code Place = z Place = v xercise 4: emantic Analysis Prog ; if Test relop then { } = + = + if y > w then { y = x + z } ; x = z + v 25
26 xercise 4: emantic Analysis true = LABL1 false = LABL2 code code next = LABL2 code next = LABL2 if Test then { } = place = t1 code relop Place = y + Place = z op = > place = x code Place = y Place = w Place = x xercise 4: emantic Analysis 4.2) how the three-address code produced by the semantic actions for the given input 26
27 xercise 4: emantic Analysis Prog Test if y > x goto LABL1 goto LABL2 LABL1 : t1 = x + z y = t1 LABL2 : t2 = z + v x = t2 LABL3 : xercise 4: emantic Analysis 4.3) Give the definition of inherited attribute. For the rule if Test then { 1 } show what are the synthesized attributes and what are the inherited attributes. 27
28 xercise 4: emantic Analysis In semantic analysis, an attribute of a nonterminal A 1 is called inherited if it is derived only from operations on attributes of A 1 s parent or siblings in the parsing tree; i.e., if it is computed in the semantic rule associated to a production with A 1 in the right-hand se. On the other hand, synthesized attributes of A 1 are generated only from the below, i.e. from the attributes of the children of A 1. In other words, in the rule where A 1 appears in the left-hand se. xercise 4: emantic Analysis Production if Test then { 1 } emantic rules Test.true = newlabel(); ynthesized (convention) Test.false =.next; Inherited 1.next =.next; Inherited.code = Test.code gen(test.true : ) 1.code ynthesized 28
Formal Languages and Compilers Lecture VII Part 3: Syntactic A
Formal Languages and Compilers Lecture VII Part 3: Syntactic Analysis Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/
More informationContext-free grammars
Context-free grammars Section 4.2 Formal way of specifying rules about the structure/syntax of a program terminals - tokens non-terminals - represent higher-level structures of a program start symbol,
More informationMIT Parse Table Construction. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology
MIT 6.035 Parse Table Construction Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology Parse Tables (Review) ACTION Goto State ( ) $ X s0 shift to s2 error error goto s1
More informationPrinciples of Programming Languages
Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 8! Bo;om- Up Parsing Shi?- Reduce LR(0) automata and
More informationS Y N T A X A N A L Y S I S LR
LR parsing There are three commonly used algorithms to build tables for an LR parser: 1. SLR(1) = LR(0) plus use of FOLLOW set to select between actions smallest class of grammars smallest tables (number
More informationCompiler Construction: Parsing
Compiler Construction: Parsing Mandar Mitra Indian Statistical Institute M. Mitra (ISI) Parsing 1 / 33 Context-free grammars. Reference: Section 4.2 Formal way of specifying rules about the structure/syntax
More informationBottom-Up Parsing LR Parsing
Bottom-Up Parsing LR Parsing Maryam Siahbani 2/19/2016 1 What we need for LR parsing LR0) states: Describe all possible states in which parser can be Parsing table ransition between LR0) states Actions
More informationLR Parsers. Aditi Raste, CCOEW
LR Parsers Aditi Raste, CCOEW 1 LR Parsers Most powerful shift-reduce parsers and yet efficient. LR(k) parsing L : left to right scanning of input R : constructing rightmost derivation in reverse k : number
More informationParser Generation. Bottom-Up Parsing. Constructing LR Parser. LR Parsing. Construct parse tree bottom-up --- from leaves to the root
Parser Generation Main Problem: given a grammar G, how to build a top-down parser or a bottom-up parser for it? parser : a program that, given a sentence, reconstructs a derivation for that sentence ----
More informationUNIT-III BOTTOM-UP PARSING
UNIT-III BOTTOM-UP PARSING Constructing a parse tree for an input string beginning at the leaves and going towards the root is called bottom-up parsing. A general type of bottom-up parser is a shift-reduce
More informationBottom-up parsing. Bottom-Up Parsing. Recall. Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form
Bottom-up parsing Bottom-up parsing Recall Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form If α V t,thenα is called a sentence in L(G) Otherwise it is just
More informationAbstract Syntax Trees & Top-Down Parsing
Review of Parsing Abstract Syntax Trees & Top-Down Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree
More informationLR Parsing, Part 2. Constructing Parse Tables. An NFA Recognizing Viable Prefixes. Computing the Closure. GOTO Function and DFA States
TDDD16 Compilers and Interpreters TDDB44 Compiler Construction LR Parsing, Part 2 Constructing Parse Tables Parse table construction Grammar conflict handling Categories of LR Grammars and Parsers An NFA
More informationSyntax Analysis. Amitabha Sanyal. (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay
Syntax Analysis (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay September 2007 College of Engineering, Pune Syntax Analysis: 2/124 Syntax
More information3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino
3. Syntax Analysis Andrea Polini Formal Languages and Compilers Master in Computer Science University of Camerino (Formal Languages and Compilers) 3. Syntax Analysis CS@UNICAM 1 / 54 Syntax Analysis: the
More informationCompiler Design 1. Bottom-UP Parsing. Goutam Biswas. Lect 6
Compiler Design 1 Bottom-UP Parsing Compiler Design 2 The Process The parse tree is built starting from the leaf nodes labeled by the terminals (tokens). The parser tries to discover appropriate reductions,
More informationAbstract Syntax Trees & Top-Down Parsing
Abstract Syntax Trees & Top-Down Parsing Review of Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree
More informationAbstract Syntax Trees & Top-Down Parsing
Review of Parsing Abstract Syntax Trees & Top-Down Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree
More informationBuilding Compilers with Phoenix
Building Compilers with Phoenix Syntax-Directed Translation Structure of a Compiler Character Stream Intermediate Representation Lexical Analyzer Machine-Independent Optimizer token stream Intermediate
More informationPART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309
PART 3 - SYNTAX ANALYSIS F. Wotawa (IST @ TU Graz) Compiler Construction Summer term 2016 64 / 309 Goals Definition of the syntax of a programming language using context free grammars Methods for parsing
More informationFormal Languages and Compilers Lecture VII Part 4: Syntactic A
Formal Languages and Compilers Lecture VII Part 4: Syntactic Analysis Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/
More informationMore Bottom-Up Parsing
More Bottom-Up Parsing Lecture 7 Dr. Sean Peisert ECS 142 Spring 2009 1 Status Project 1 Back By Wednesday (ish) savior lexer in ~cs142/s09/bin Project 2 Due Friday, Apr. 24, 11:55pm My office hours 3pm
More informationCompiler Construction 2016/2017 Syntax Analysis
Compiler Construction 2016/2017 Syntax Analysis Peter Thiemann November 2, 2016 Outline 1 Syntax Analysis Recursive top-down parsing Nonrecursive top-down parsing Bottom-up parsing Syntax Analysis tokens
More informationA left-sentential form is a sentential form that occurs in the leftmost derivation of some sentence.
Bottom-up parsing Recall For a grammar G, with start symbol S, any string α such that S α is a sentential form If α V t, then α is a sentence in L(G) A left-sentential form is a sentential form that occurs
More informationParsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing
Roadmap > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing The role of the parser > performs context-free syntax analysis > guides
More informationFormal Languages and Compilers Lecture VI: Lexical Analysis
Formal Languages and Compilers Lecture VI: Lexical Analysis Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/ artale/ Formal
More informationCS143 Midterm Sample Solution Fall 2010
CS143 Midterm Sample Solution Fall 2010 Please read all instructions (including these) carefully. There are 4 questions on the exam, all with multiple parts. You have 75 minutes to work on the exam. The
More informationBottom-Up Parsing. Parser Generation. LR Parsing. Constructing LR Parser
Parser Generation Main Problem: given a grammar G, how to build a top-down parser or a bottom-up parser for it? parser : a program that, given a sentence, reconstructs a derivation for that sentence ----
More informationLR Parsing LALR Parser Generators
LR Parsing LALR Parser Generators Outline Review of bottom-up parsing Computing the parsing DFA Using parser generators 2 Bottom-up Parsing (Review) A bottom-up parser rewrites the input string to the
More informationTime : 1 Hour Max Marks : 30
Total No. of Questions : 6 P4890 B.E/ Insem.- 74 B.E ( Computer Engg) PRINCIPLES OF MODERN COMPILER DESIGN (2012 Pattern) (Semester I) Time : 1 Hour Max Marks : 30 Q.1 a) Explain need of symbol table with
More informationLR Parsing - The Items
LR Parsing - The Items Lecture 10 Sections 4.5, 4.7 Robb T. Koether Hampden-Sydney College Fri, Feb 13, 2015 Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 1 / 31 1 LR
More informationLR Parsing LALR Parser Generators
Outline LR Parsing LALR Parser Generators Review of bottom-up parsing Computing the parsing DFA Using parser generators 2 Bottom-up Parsing (Review) A bottom-up parser rewrites the input string to the
More informationBottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as Shift-Reduce parsing
Also known as Shift-Reduce parsing More powerful than top down Don t need left factored grammars Can handle left recursion Attempt to construct parse tree from an input string eginning at leaves and working
More informationCompilers. Bottom-up Parsing. (original slides by Sam
Compilers Bottom-up Parsing Yannis Smaragdakis U Athens Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Bottom-Up Parsing More general than top-down parsing And just as efficient Builds
More informationBottom-Up Parsing. Lecture 11-12
Bottom-Up Parsing Lecture 11-12 (From slides by G. Necula & R. Bodik) 2/20/08 Prof. Hilfinger CS164 Lecture 11 1 Administrivia Test I during class on 10 March. 2/20/08 Prof. Hilfinger CS164 Lecture 11
More informationBottom up parsing. The sentential forms happen to be a right most derivation in the reverse order. S a A B e a A d e. a A d e a A B e S.
Bottom up parsing Construct a parse tree for an input string beginning at leaves and going towards root OR Reduce a string w of input to start symbol of grammar Consider a grammar S aabe A Abc b B d And
More informationBottom-Up Parsing. Lecture 11-12
Bottom-Up Parsing Lecture 11-12 (From slides by G. Necula & R. Bodik) 9/22/06 Prof. Hilfinger CS164 Lecture 11 1 Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient
More informationConcepts Introduced in Chapter 4
Concepts Introduced in Chapter 4 Grammars Context-Free Grammars Derivations and Parse Trees Ambiguity, Precedence, and Associativity Top Down Parsing Recursive Descent, LL Bottom Up Parsing SLR, LR, LALR
More informationLet us construct the LR(1) items for the grammar given below to construct the LALR parsing table.
MODULE 18 LALR parsing After understanding the most powerful CALR parser, in this module we will learn to construct the LALR parser. The CALR parser has a large set of items and hence the LALR parser is
More informationLR Parsing Techniques
LR Parsing Techniques Introduction Bottom-Up Parsing LR Parsing as Handle Pruning Shift-Reduce Parser LR(k) Parsing Model Parsing Table Construction: SLR, LR, LALR 1 Bottom-UP Parsing A bottom-up parser
More informationMonday, September 13, Parsers
Parsers Agenda Terminology LL(1) Parsers Overview of LR Parsing Terminology Grammar G = (Vt, Vn, S, P) Vt is the set of terminals Vn is the set of non-terminals S is the start symbol P is the set of productions
More informationLexical and Syntax Analysis. Bottom-Up Parsing
Lexical and Syntax Analysis Bottom-Up Parsing Parsing There are two ways to construct derivation of a grammar. Top-Down: begin with start symbol; repeatedly replace an instance of a production s LHS with
More informationLALR Parsing. What Yacc and most compilers employ.
LALR Parsing Canonical sets of LR(1) items Number of states much larger than in the SLR construction LR(1) = Order of thousands for a standard prog. Lang. SLR(1) = order of hundreds for a standard prog.
More information3. Parsing. Oscar Nierstrasz
3. Parsing Oscar Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes. http://www.cs.ucla.edu/~palsberg/ http://www.cs.purdue.edu/homes/hosking/
More informationLR Parsing. Leftmost and Rightmost Derivations. Compiler Design CSE 504. Derivations for id + id: T id = id+id. 1 Shift-Reduce Parsing.
LR Parsing Compiler Design CSE 504 1 Shift-Reduce Parsing 2 LR Parsers 3 SLR and LR(1) Parsers Last modifled: Fri Mar 06 2015 at 13:50:06 EST Version: 1.7 16:58:46 2016/01/29 Compiled at 12:57 on 2016/02/26
More informationLR(0) Parsing Summary. LR(0) Parsing Table. LR(0) Limitations. A Non-LR(0) Grammar. LR(0) Parsing Table CS412/CS413
LR(0) Parsing ummary C412/C41 Introduction to Compilers Tim Teitelbaum Lecture 10: LR Parsing February 12, 2007 LR(0) item = a production with a dot in RH LR(0) state = set of LR(0) items valid for viable
More informationLecture Bottom-Up Parsing
Lecture 14+15 Bottom-Up Parsing CS 241: Foundations of Sequential Programs Winter 2018 Troy Vasiga et al University of Waterloo 1 Example CFG 1. S S 2. S AyB 3. A ab 4. A cd 5. B z 6. B wz 2 Stacks in
More informationCA Compiler Construction
CA4003 - Compiler Construction David Sinclair A top-down parser starts with the root of the parse tree, labelled with the goal symbol of the grammar, and repeats the following steps until the fringe of
More informationMODULE 14 SLR PARSER LR(0) ITEMS
MODULE 14 SLR PARSER LR(0) ITEMS In this module we shall discuss one of the LR type parser namely SLR parser. The various steps involved in the SLR parser will be discussed with a focus on the construction
More informationA programming language requires two major definitions A simple one pass compiler
A programming language requires two major definitions A simple one pass compiler [Syntax: what the language looks like A context-free grammar written in BNF (Backus-Naur Form) usually suffices. [Semantics:
More informationHW 3: Bottom-Up Parsing Techniques
HW 3: BottomUp Parsing Techniques CSCI 434T Fall, 2015 Overview Source Program Lexical Syntax Semantic Intermediate Code Generation Machine Independent Optimization Code Generation Target Program This
More informationCS 4120 Introduction to Compilers
CS 4120 Introduction to Compilers Andrew Myers Cornell University Lecture 6: Bottom-Up Parsing 9/9/09 Bottom-up parsing A more powerful parsing technology LR grammars -- more expressive than LL can handle
More informationWednesday, August 31, Parsers
Parsers How do we combine tokens? Combine tokens ( words in a language) to form programs ( sentences in a language) Not all combinations of tokens are correct programs (not all sentences are grammatically
More informationTop down vs. bottom up parsing
Parsing A grammar describes the strings that are syntactically legal A recogniser simply accepts or rejects strings A generator produces sentences in the language described by the grammar A parser constructs
More informationCSE302: Compiler Design
CSE302: Compiler Design Instructor: Dr. Liang Cheng Department of Computer Science and Engineering P.C. Rossin College of Engineering & Applied Science Lehigh University March 20, 2007 Outline Recap LR(0)
More informationCOP4020 Programming Languages. Syntax Prof. Robert van Engelen
COP4020 Programming Languages Syntax Prof. Robert van Engelen Overview n Tokens and regular expressions n Syntax and context-free grammars n Grammar derivations n More about parse trees n Top-down and
More informationSyntax-Directed Translation Part II
Syntax-Directed Translation Part II Chapter 5 Slides adapted from : Robert van Engelen, Florida State University Alessandro Artale, Free University of Bolzano Syntax-Directed Translation Schemes Syntax-directed
More informationCS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)
CS1622 Lecture 9 Parsing (4) CS 1622 Lecture 9 1 Today Example of a recursive descent parser Predictive & LL(1) parsers Building parse tables CS 1622 Lecture 9 2 A Recursive Descent Parser. Preliminaries
More informationIntroduction to Parsing. Lecture 5. Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)
Introduction to Parsing Lecture 5 (Modified by Professor Vijay Ganesh) 1 Outline Regular languages revisited Parser overview Context-free grammars (CFG s) Derivations Ambiguity 2 Languages and Automata
More information2068 (I) Attempt all questions.
2068 (I) 1. What do you mean by compiler? How source program analyzed? Explain in brief. 2. Discuss the role of symbol table in compiler design. 3. Convert the regular expression 0 + (1 + 0)* 00 first
More informationOutline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5. Derivations.
Outline Regular languages revisited Introduction to Parsing Lecture 5 Parser overview Context-free grammars (CFG s) Derivations Prof. Aiken CS 143 Lecture 5 1 Ambiguity Prof. Aiken CS 143 Lecture 5 2 Languages
More informationWednesday, September 9, 15. Parsers
Parsers What is a parser A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda
More informationParsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:
What is a parser Parsers A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda
More informationSLR parsers. LR(0) items
SLR parsers LR(0) items As we have seen, in order to make shift-reduce parsing practical, we need a reasonable way to identify viable prefixes (and so, possible handles). Up to now, it has not been clear
More informationSimple LR (SLR) LR(0) Drawbacks LR(1) SLR Parse. LR(1) Start State and Reduce. LR(1) Items 10/3/2012
LR(0) Drawbacks Consider the unambiguous augmented grammar: 0.) S E $ 1.) E T + E 2.) E T 3.) T x If we build the LR(0) DFA table, we find that there is a shift-reduce conflict. This arises because the
More informationTypes of parsing. CMSC 430 Lecture 4, Page 1
Types of parsing Top-down parsers start at the root of derivation tree and fill in picks a production and tries to match the input may require backtracking some grammars are backtrack-free (predictive)
More informationSyntax Analyzer --- Parser
Syntax Analyzer --- Parser ASU Textbook Chapter 4.2--4.9 (w/o error handling) Tsan-sheng Hsu tshsu@iis.sinica.edu.tw http://www.iis.sinica.edu.tw/~tshsu 1 A program represented by a sequence of tokens
More informationZhizheng Zhang. Southeast University
Zhizheng Zhang Southeast University 2016/10/5 Lexical Analysis 1 1. The Role of Lexical Analyzer 2016/10/5 Lexical Analysis 2 2016/10/5 Lexical Analysis 3 Example. position = initial + rate * 60 2016/10/5
More informationPESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science and Engineering
TEST 1 Date : 24 02 2015 Marks : 50 Subject & Code : Compiler Design ( 10CS63) Class : VI CSE A & B Name of faculty : Mrs. Shanthala P.T/ Mrs. Swati Gambhire Time : 8:30 10:00 AM SOLUTION MANUAL 1. a.
More informationCSC 4181 Compiler Construction. Parsing. Outline. Introduction
CC 4181 Compiler Construction Parsing 1 Outline Top-down v.s. Bottom-up Top-down parsing Recursive-descent parsing LL1) parsing LL1) parsing algorithm First and follow sets Constructing LL1) parsing table
More information8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing
8 Parsing Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces strings A parser constructs a parse tree for a string
More informationCSCI312 Principles of Programming Languages
Copyright 2006 The McGraw-Hill Companies, Inc. CSCI312 Principles of Programming Languages! LL Parsing!! Xu Liu Derived from Keith Cooper s COMP 412 at Rice University Recap Copyright 2006 The McGraw-Hill
More informationTable-driven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and non-terminals.
Bottom-up Parsing: Table-driven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and non-terminals. Basic operation is to shift terminals from the input to the
More informationExample CFG. Lectures 16 & 17 Bottom-Up Parsing. LL(1) Predictor Table Review. Stacks in LR Parsing 1. Sʹ " S. 2. S " AyB. 3. A " ab. 4.
Example CFG Lectures 16 & 17 Bottom-Up Parsing CS 241: Foundations of Sequential Programs Fall 2016 1. Sʹ " S 2. S " AyB 3. A " ab 4. A " cd Matt Crane University of Waterloo 5. B " z 6. B " wz 2 LL(1)
More informationUnit 13. Compiler Design
Unit 13. Compiler Design Computers are a balanced mix of software and hardware. Hardware is just a piece of mechanical device and its functions are being controlled by a compatible software. Hardware understands
More informationLexical Analysis. Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata
Lexical Analysis Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata Phase Ordering of Front-Ends Lexical analysis (lexer) Break input string
More informationshift-reduce parsing
Parsing #2 Bottom-up Parsing Rightmost derivations; use of rules from right to left Uses a stack to push symbols the concatenation of the stack symbols with the rest of the input forms a valid bottom-up
More information4. Lexical and Syntax Analysis
4. Lexical and Syntax Analysis 4.1 Introduction Language implementation systems must analyze source code, regardless of the specific implementation approach Nearly all syntax analysis is based on a formal
More informationSyntactic Analysis. Chapter 4. Compiler Construction Syntactic Analysis 1
Syntactic Analysis Chapter 4 Compiler Construction Syntactic Analysis 1 Context-free Grammars The syntax of programming language constructs can be described by context-free grammars (CFGs) Relatively simple
More informationIn One Slide. Outline. LR Parsing. Table Construction
LR Parsing Table Construction #1 In One Slide An LR(1) parsing table can be constructed automatically from a CFG. An LR(1) item is a pair made up of a production and a lookahead token; it represents a
More informationWWW.STUDENTSFOCUS.COM UNIT -3 SYNTAX ANALYSIS 3.1 ROLE OF THE PARSER Parser obtains a string of tokens from the lexical analyzer and verifies that it can be generated by the language for the source program.
More informationLL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012
Predictive Parsers LL(k) Parsing Can we avoid backtracking? es, if for a given input symbol and given nonterminal, we can choose the alternative appropriately. his is possible if the first terminal of
More informationParsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468
Parsers Xiaokang Qiu Purdue University ECE 468 August 31, 2018 What is a parser A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure
More information1 Introduction. 2 Recursive descent parsing. Predicative parsing. Computer Language Implementation Lecture Note 3 February 4, 2004
CMSC 51086 Winter 2004 Computer Language Implementation Lecture Note 3 February 4, 2004 Predicative parsing 1 Introduction This note continues the discussion of parsing based on context free languages.
More informationSyntactic Analysis. Top-Down Parsing
Syntactic Analysis Top-Down Parsing Copyright 2017, Pedro C. Diniz, all rights reserved. Students enrolled in Compilers class at University of Southern California (USC) have explicit permission to make
More informationSyn S t yn a t x a Ana x lysi y s si 1
Syntax Analysis 1 Position of a Parser in the Compiler Model Source Program Lexical Analyzer Token, tokenval Get next token Parser and rest of front-end Intermediate representation Lexical error Syntax
More informationSyntax Analysis Part I
Syntax Analysis Part I Chapter 4: Context-Free Grammars Slides adapted from : Robert van Engelen, Florida State University Position of a Parser in the Compiler Model Source Program Lexical Analyzer Token,
More informationRegular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications
Agenda for Today Regular Expressions CSE 413, Autumn 2005 Programming Languages Basic concepts of formal grammars Regular expressions Lexical specification of programming languages Using finite automata
More informationCOMPILER DESIGN - QUICK GUIDE COMPILER DESIGN - OVERVIEW
COMPILER DESIGN - QUICK GUIDE http://www.tutorialspoint.com/compiler_design/compiler_design_quick_guide.htm COMPILER DESIGN - OVERVIEW Copyright tutorialspoint.com Computers are a balanced mix of software
More informationCSCI Compiler Design
University of Southern California CSCI565 Compiler Design Mterm Exam Solution - Spring 2014 CSCI 565 - Compiler Design Spring 2014 Mterm Exam - Solution Problem 1: Context-Free-Grammars and Parsing Algorithms
More informationAdministrativia. WA1 due on Thu PA2 in a week. Building a Parser III. Slides on the web site. CS164 3:30-5:00 TT 10 Evans.
Administrativia Building a Parser III CS164 3:30-5:00 10 vans WA1 due on hu PA2 in a week Slides on the web site I do my best to have slides ready and posted by the end of the preceding logical day yesterday,
More informationFaculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology
Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology exam Compiler Construction in4303 April 9, 2010 14.00-15.30 This exam (6 pages) consists of 52 True/False
More informationCOMPILER (CSE 4120) (Lecture 6: Parsing 4 Bottom-up Parsing )
COMPILR (CS 4120) (Lecture 6: Parsing 4 Bottom-up Parsing ) Sungwon Jung Mobile Computing & Data ngineering Lab Dept. of Computer Science and ngineering Sogang University Seoul, Korea Tel: +82-2-705-8930
More informationCS606- compiler instruction Solved MCQS From Midterm Papers
CS606- compiler instruction Solved MCQS From Midterm Papers March 06,2014 MC100401285 Moaaz.pk@gmail.com Mc100401285@gmail.com PSMD01 Final Term MCQ s and Quizzes CS606- compiler instruction If X is a
More informationCSCI312 Principles of Programming Languages!
CSCI312 Principles of Programming Languages!! Chapter 3 Regular Expression and Lexer Xu Liu Recap! Copyright 2006 The McGraw-Hill Companies, Inc. Clite: Lexical Syntax! Input: a stream of characters from
More informationSYNTAX ANALYSIS 1. Define parser. Hierarchical analysis is one in which the tokens are grouped hierarchically into nested collections with collective meaning. Also termed as Parsing. 2. Mention the basic
More informationIntroduction to Parsing. Lecture 8
Introduction to Parsing Lecture 8 Adapted from slides by G. Necula Outline Limitations of regular languages Parser overview Context-free grammars (CFG s) Derivations Languages and Automata Formal languages
More informationUNIT III & IV. Bottom up parsing
UNIT III & IV Bottom up parsing 5.0 Introduction Given a grammar and a sentence belonging to that grammar, if we have to show that the given sentence belongs to the given grammar, there are two methods.
More informationReview: Shift-Reduce Parsing. Bottom-up parsing uses two actions: Bottom-Up Parsing II. Shift ABC xyz ABCx yz. Lecture 8. Reduce Cbxy ijk CbA ijk
Review: Shift-Reduce Parsing Bottom-up parsing uses two actions: Bottom-Up Parsing II Lecture 8 Shift ABC xyz ABCx yz Reduce Cbxy ijk CbA ijk Prof. Aiken CS 13 Lecture 8 1 Prof. Aiken CS 13 Lecture 8 2
More informationTop-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7
Top-Down Parsing and Intro to Bottom-Up Parsing Lecture 7 1 Predictive Parsers Like recursive-descent but parser can predict which production to use Predictive parsers are never wrong Always able to guess
More information