CSC 4181 Compiler Construction. Parsing. Outline. Introduction


 Conrad Knight
 4 years ago
 Views:
Transcription
1 CC 4181 Compiler Construction Parsing 1 Outline Topdown v.s. Bottomup Topdown parsing Recursivedescent parsing LL1) parsing LL1) parsing algorithm First and follow sets Constructing LL1) parsing table Error recovery Bottomup parsing hiftreduce parsers LR0) parsing LR0) items Finite automata of items LR0) parsing algorithm LR0) grammar LR1) parsing LR1) parsing algorithm LR1) grammar Parsing conflict Parsing 2 Introduction Parsing is a process that constructs a syntactic structure i.e. parse tree) from the stream of tokens. We already learned how to describe the syntactic structure of a language using contextfree) grammar. o, a parser only needs to do this? tream of tokens Contextfree grammar Parser Parse tree Parsing 3 1
2 Top Down Parsing Bottom Up Parsing A parse tree is created from root to leaves The traversal of parse trees is a preorder traversal Tracing leftmost derivation Two types: Backtracking parser Predictive parser Backtracking: Try different structures and backtrack if it does not matched the input A parse tree is created from leaves to root The traversal of parse trees is a reversal of postorder traversal Tracing rightmost derivation More powerful than topdown parsing Predictive: Guess the structure of the parse tree from the next input Parsing 4 Parse Trees and Derivations E E E + id E * E id id Topdown parsing E E + E id E * E id id Bottomup parsing E E + E id + E id + E * E id + id * E id + id * id E E + E E + E * E E + E * id E + id * id id + id * id Parsing 5 TOP DOWN PARING Parsing 6 2
3 Topdown Parsing What does a parser need to decide? Which production rule is to be used at each point of time? How to guess? What is the guess based on? What is the next token? Reserved word if, open parentheses, etc. What is the structure to be built? If statement, expression, etc. Parsing 7 Topdown Parsing Why is it difficult? Cannot decide until later Next token: if tructure to be built: t t Matchedt Unmatchedt Unmatchedt if E) t if E) Matchedt else Unmatchedt Matchedt if E) Matchedt else Matchedt... Production with empty string Next token: id tructure to be built: par par parlist parlist exp, parlist exp Parsing 8 RecursiveDescent Write one procedure for each set of productions with the same nonterminal in the LH Each procedure recognizes a structure described by a nonterminal. A procedure calls other procedures if it needs to recognize other structures. A procedure calls match procedure if it needs to recognize a terminal. Parsing 9 3
4 RecursiveDescent: Example E E O F F O +  F E ) id E ::= F {O F} O ::= +  F ::= E ) id procedure E { E; O; F; } procedure F { switch token { case : match ); E; match ) ); case id: matchid); default: error; } } For this grammar: We cannot decide which rule to use for E, and If we choose E E O F, it leads to infinitely recursive loops. Rewrite the grammar into EBNF procedure E { F; while token=+ or token=) { O; F; } } Parsing 10 Match procedure procedure matchexptok) { if token==exptok) then gettoken else error } The token is not consumed until gettoken is executed. Parsing 11 Problems in RecursiveDescent Difficult to convert grammars into EBNF Cannot decide which production to use at each point Cannot decide when to use production A Parsing 12 4
5 LL1) LL1) Parsing Read input from L) left to right imulate L) leftmost derivation 1 lookahead symbol Use stack to simulate leftmost derivation Part of sentential form produced in the leftmost derivation is stored in the stack. Top of stack is the leftmost nonterminal symbol in the fragment of sentential form. Parsing 13 Concept of LL1) Parsing imulate leftmost derivation of the input. Keep part of sentential form in the stack. If the symbol on the top of stack is a terminal, try to match it with the next input token and pop it out of stack. If the symbol on the top of stack is a nonterminal X, replace it with Y if we have a production rule X Y. Which production will be chosen, if there are both X Y and X Z? Parsing 14 Example of LL1) Parsing E TX FNX E)NX TX)NX FNX)NX nnx)nx nx)nx natx)nx n+tx)nx n+fnx)nx n+e)nx)nx n+tx)nx)nx n+fnx)nx)nx n+nnx)nx)nx n+nx)nx)nx n+n)nx)nx n+n)x)nx n+n))nx n+n))mfnx n+n))*fnx n+n))*nnx n+n))*nx n+n))*n F n T N n + n ) ) * n $ E X n A + F ) E T X T N X A T X Finished E X M * A +  T F N ) F n N M F N T N M * F E ) n E X $ Parsing 15 5
6 LL1) Parsing Algorithm Push the start symbol into the stack WHILE stack is not empty $ is not on top of stack) and the stream of tokens is not empty the next input token is not $) WITCH Top of stack, next token) CAE terminal a, a): Pop stack; Get next token CAE nonterminal A, terminal a): IF the parsing table entry M[A, a] is not empty THEN Get A X 1 X 2... X n from the parsing table entry M[A, a] Pop stack; Push X n... X 2 X 1 into stack in that order ELE Error CAE $,$): Accept OTHER: Error Parsing 16 LL1) Parsing Table If the nonterminal N is on the top of stack and the next token is t, which production rule to use? Choose a rule N X such that X * ty or X * and * WNtY t N X Y Q t N X t Y Parsing 17 First et Let X be or be in V or T. FirstX ) is the set of the first terminal in any sentential form derived from X. If X is a terminal or, then FirstX ) ={X }. If X is a nonterminal and X X 1 X 2... X n is a rule, then FirstX 1 ) {} is a subset of FirstX) FirstX i ){} is a subset of FirstX) if for all j<i FirstX j ) contains {} is in FirstX) if for all j n FirstX j )contains Parsing 18 6
7 Examples of First et exp exp addop term term st ifst ifst other if exp ) st elsepart addop +  elsepart else st term term mulop factor exp 0 1 factor mulop * Firstexp) = {0,1} factor exp) num Firstelsepart) = {else, } Firstaddop) = {+, } Firstmulop) = {*} Firstfactor) = {, num} Firstterm) = {, num} Firstexp) = {, num} Firstifst) Firstst) = {if} = {if, other} Parsing 19 Algorithm for finding FirstA) For all terminals a, Firsta) = {a} For all nonterminals A, FirstA) := {} While there are changes to any FirstA) For each rule A X 1 X 2... X n For each X i in {X 1, X 2,, X n } If for all j<i FirstX j ) contains, Then add FirstX i ){} to FirstA) If is in FirstX 1 ), FirstX 2 ),..., and FirstX n ) Then add to FirstA) If A is a terminal or, then FirstA) = {A}. If A is a nonterminal, then for each rule A X 1 X 2... X n, FirstA) contains FirstX 1 )  {}. If also for some i<n, FirstX 1 ), FirstX 2 ),..., and FirstX i ) contain, then FirstA) contains FirstX i+1 ){}. If FirstX 1 ), FirstX 2 ),..., and FirstX n ) contain, then FirstA) also contains. Parsing 20 Finding First et: An Example exp term exp exp addop term exp addop +  term factor term term mulop factor term mulop * factor exp ) num exp exp addop term term mulop factor First +  num * num Parsing 21 7
8 Follow et Let $ denote the end of input tokens If A is the start symbol, then $ is in FollowA). If there is a rule B X A Y, then FirstY)  {} is in FollowA). If there is production B X A Y and is in FirstY), then FollowA) contains FollowB). Parsing 22 Algorithm for Finding FollowA) Follow) = {$} FOR each A in V{} FollowA)={} WHILE change is made to some Follow sets FOR each production A X 1 X 2... X n, FOR each nonterminal X i Add FirstX i+1 X i+2...x n ){} into FollowX i ). NOTE: If i=n, X i+1 X i+2...x n = ) IF is in FirstX i+1 X i+2...x n ) THEN Add FollowA) to FollowX i ) If A is the start symbol, then $ is in FollowA). If there is a rule A Y X Z, then FirstZ)  {} is in FollowX). If there is production B X A Y and is in FirstY), then FollowA) contains FollowB). Parsing 23 Finding Follow et: An Example exp term exp exp addop term exp addop +  term factor term term mulop factor term mulop * factor exp ) num exp exp addop term term mulop factor First num num * * num Follow $ ) $ ) +  $ ) Parsing 24 8
9 Constructing LL1) Parsing Tables FOR each nonterminal A and a production A X FOR each token a in FirstX) A X is in MA, a) IF is in FirstX) THEN FOR each element a in FollowA) Add A X to MA, a) Parsing 25 Example: Constructing LL1) Parsing Table First Follow exp {, num} {$,)} exp {+,, } {$,)} addop {+,} {,num} term {,num} {+,,),$} term {*, } {+,,),$} mulop {*} {,num} factor {, num} {*,+,,),$} 1 exp term exp 2 exp addop term exp 3 exp 4 addop + 5 addop  6 term factor term 7 term mulop factor term 8 term 9 mulop * 10 factor exp ) 11 factor num exp exp addop term term mulop factor ) +  * n $ Parsing 26 LL1) Grammar A grammar is an LL1) grammar if its LL1) parsing table has at most one production in each table entry. Parsing 27 9
10 LL1) Parsing Table for nonll1) Grammar 1 exp exp addop term 2 exp term 3 term term mulop factor 4 term factor 5 factor exp ) 6 factor num 7 addop + 8 addop  9 mulop * Firstexp) = {, num } Firstterm) = {, num } Firstfactor) = {, num } Firstaddop) = { +,  } Firstmulop) = { * } ) +  * num $ exp 1,2 1,2 term 3,4 3,4 factor 5 6 addop 7 8 mulop 9 Parsing 28 Causes of NonLL1) Grammar What causes grammar being nonll1)? Leftrecursion Left factor Parsing 29 Immediate left recursion A A X Y Left Recursion A=Y X* A A X 1 A X 2 A X n Y 1 Y 2... Y m A={Y 1, Y 2,, Y m } {X 1, X 2,, X n }* General left recursion A => X =>* A Y Can be removed very easily A Y A, A X A A Y 1 A Y 2 A... Y m A, A X 1 A X 2 A X n A Can be removed when there is no emptystring production and no cycle in the grammar Parsing 30 10
11 Removal of Immediate Left Recursion exp exp + term exp  term term term term * factor factor factor exp ) num Remove left recursion exp term exp exp = term term)* exp + term exp  term exp term factor term term = factor * factor)* term * factor term factor exp ) num Parsing 31 Bad News! General Left Recursion Can only be removed when there is no emptystring production and no cycle in the grammar. Good News!!!! Never seen in grammars of any programming languages Parsing 32 Left Factoring Left factor causes nonll1) Given A X Y X Z. Both A X Y and A X Z can be chosen when A is on top of stack and a token in FirstX) is the next token. A X Y X Z can be leftfactored as A X A and A Y Z Parsing 33 11
12 Example of Left Factor ift if exp ) st else st if exp ) st can be leftfactored as ift if exp ) st elsepart elsepart else st seq st ; seq st can be leftfactored as seq st seq seq ; seq Parsing 34 BOTTOM UP PARING Parsing 35 Bottomup Parsing Use explicit stack to perform a parse imulate rightmost derivation R) from left L) to right, thus called LR parsing More powerful than topdown parsing Left recursion does not cause problem Two actions hift: take next input token into the stack Reduce: replace a string B on top of stack by a nonterminal A, given a production A B Parsing 36 12
13 Example of hiftreduce Parsing Grammar ) Parsing actions tack Input Action $ ) ) $ shift $ ) ) $ shift $ ) ) $ reduce $ ) ) $ shift $ ) ) $ reduce $ ) ) $ reduce ) $ ) $ shift $ ) $ reduce $ ) $ reduce ) $ $ accept Reverse of rightmost derivation from left to right 1 ) ) 2 ) ) 3 ) ) 4 ) ) 5 ) ) 6 ) ) 7 ) 8 ) 9 ) 10 Parsing 37 Example of hiftreduce Parsing Grammar ) Parsing actions tack Input Action $ ) ) $ shift $ ) ) $ shift $ ) ) $ reduce $ ) ) $ shift $ ) ) $ reduce $ ) ) $ reduce ) $ ) $ shift $ ) $ reduce $ ) $ reduce ) $ $ Viable prefix accept 1 ) ) 2 ) ) 3 ) ) 4 ) ) 5 ) ) 6 ) ) 7 ) 8 ) 9 ) 10 handle Parsing 38 Terminology Right sentential form sentential form in a rightmost derivation Viable prefix sequence of symbols on the parsing stack Handle right sentential form + position where reduction can be performed + production used for reduction LR0) item production with distinguished position in its RH Right sentential form ) ) ) Viable prefix ), ),, ), ),,, Handle ). with ). with ). ) with ) LR0) item ). ).. ). ). ) Parsing 39 13
14 hiftreduce parsers There are two possible actions: shift and reduce Parsing is completed when the input stream is empty and the stack contains only the start symbol The grammar must be augmented a new start symbol is added a production is added To make sure that parsing is finished when is on top of stack because never appears on the RH of any production. Parsing 40 LR0) parsing Keep track of what is left to be done in the parsing process by using finite automata of items An item A w. B y means: A w B y might be used for the reduction in the future, at the time, we know we already construct w in the parsing process, if B is constructed next, we get the new item A w B. Y Parsing 41 LR0) items LR0) item production with a distinguished position in the RH Initial Item Item with the distinguished position on the leftmost of the production Complete Item Item with the distinguished position on the rightmost of the production Closure Item of x Item x together with items which can be reached from x via transition Kernel Item Original item, not including closure items Parsing 42 14
15 Finite automata of items Grammar: ) Items:...).).) ). ).....)..) )..) ) ). Parsing 43 DFA of LR0) Items...)..).) ) )...)..).)...) )..). ) ). ). Parsing 44 LR0) parsing algorithm Item in state token Action A> x.by where B is terminal B shift B and push state s containing A > xb.y A> x.by where B is terminal not B error A > x.  reduce with A > x i.e. pop x, backup to the state s on top of stack) and push A with new state ds,a) >. none accept >. any error Parsing 45 15
16 LR0) Parsing Table A.A A A.A) A.a 0 a a A.A) A.A) A A.a 3 A A. 1 A a. 2 A A.) 4 ) A A). 5 tate Action Rule a ) A 0 shift reduce A > A 2 reduce A > a 3 shift shift 5 5 reduce A > A) Parsing 46 Example of LR0) Parsing tate Action Rule a ) A 0 shift reduce A > A 2 reduce A > a 3 shift shift 5 tack Input 5 reduce Action A > A) $0 a ) ) $ shift $03 a ) ) $ shift $033 a ) ) $ shift $033a2 ) ) $ reduce $033A4 ) ) $ shift $033A4)5 ) $ reduce $03A4 ) $ shift $03A4)5 $ reduce $0A1 $ accept Parsing 47 NonLR0)Grammar Conflict hiftreduce conflict A state contains a complete item A x. and a shift item A x.by Reducereduce conflict A state contains more than one complete items. A grammar is a LR0) grammar if there is no conflict in the grammar...). 0.).) ) 3 )..). 4 ). 5 ) Parsing 48 16
17 LR1) parsing imple LR with 1 lookahead symbol Examine the next token before deciding to shift or reduce If the next token is the token expected in an item, then it can be shifted into the stack. If a complete item A x. is constructed and the next token is in FollowA), then reduction can be done using A x. Otherwise, error occurs. Can avoid conflict Parsing 49 LR1) parsing algorithm Item in state token Action A> x.by B is terminal) B shift B and push state s containing A > xb.y A> x.by B is terminal) not B error A > x. in reduce with A > x i.e. pop x, FollowA) backup to the state s on top of stack) and push A with new state ds,a) A > x. not in error FollowA) >. none accept >. any error Parsing 50 Conflict hiftreduce conflict LR1) grammar A state contains a shift item A x.wy such that W is a terminal and a complete item B z. such that W is in FollowB). Reducereduce conflict A state contains more than one complete item with some common Follow set. A grammar is an LR1) grammar if there is no conflict in the grammar. Parsing 51 17
18 LR1) Parsing Table A A) a A.A A A A. 1 A.A) A.a a 0 A.A) A.A) A.a 3 a A A a. 2 A A.) 4 ) A A). 5 tate a ) $ A AC 2 R R1 Parsing 52 LR1) Grammar not LR0)..). 0.).) ) 3 )..). 4 ) ) tate ) $ 0 2 R2 R2 1 1 AC 2 2 R2 R R2 R2 5 5 R1 R1 ). 5 Parsing 53 Disambiguating Rules for Parsing Conflict hiftreduce conflict Prefer shift over reduce In case of nested if statements, preferring shift over reduce implies most closely nested rule for dangling else Reducereduce conflict Error in design Parsing 54 18
19 Dangling Else. 0.I.other I.if I.if else other.other 3 other I if. 5 I if. else I if if other other. 1 I. 2 I else I if. 4 I if. else.i.other I.if I.if else I if I if else. 6.I.other I.if I.if else I.if else 7 state if else other $ I ACC 2 R1 R1 3 R2 R R R4 R4 Parsing 55 LALR1) parsing Goal: reduce number of states in LR1) parser. ome states LR1) automaton have the same core items and differ only in the possible lookahead. tates I 3 and I 3 ', I 5 and I 5 ', I 7 and I 7 ', I 8 and I 8 ' We shrink our parser by merging such states. LR : 10 states, LR1): 14 states, LALR1) : 10 states 56 Conflicts in LALR1) parsing Most conflicts that existed in LR1) parser can be eliminated with LALR1) Can LALR1) parsers introduce conflicts that did not exist in the LR1) parser? Unfortunately YE. BUT, only reduce/reduce conflicts. YACC generates LALR1) parser 57 19
8 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 informationNote that for recursive descent to work, if A ::= B1 B2 is a grammar rule we need First k (B1) disjoint from First k (B2).
LL(k) Grammars We need a bunch of terminology. For any terminal string a we write First k (a) is the prefix of a of length k (or all of a if its length is less than k) For any string g of terminal and
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 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 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 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 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 nonterminals S is the start symbol P is the set of productions
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 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 informationTypes of parsing. CMSC 430 Lecture 4, Page 1
Types of parsing Topdown 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 backtrackfree (predictive)
More informationTabledriven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and nonterminals.
Bottomup Parsing: Tabledriven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and nonterminals. Basic operation is to shift terminals from the input to the
More informationParsing. Lecture 11: Parsing. Recursive Descent Parser. Arithmetic grammar.  drops irrelevant details from parse tree
Parsing Lecture 11: Parsing CSC 131 Fall, 2014 Kim Bruce Build parse tree from an expression Interested in abstract syntax tree  drops irrelevant details from parse tree Arithmetic grammar ::=
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 informationParsing. Roadmap. > Contextfree grammars > Derivations and precedence > Topdown parsing > Leftrecursion > Lookahead > Tabledriven parsing
Roadmap > Contextfree grammars > Derivations and precedence > Topdown parsing > Leftrecursion > Lookahead > Tabledriven parsing The role of the parser > performs contextfree syntax analysis > guides
More informationTopDown Parsing and Intro to BottomUp Parsing. Lecture 7
TopDown Parsing and Intro to BottomUp Parsing Lecture 7 1 Predictive Parsers Like recursivedescent but parser can predict which production to use Predictive parsers are never wrong Always able to guess
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 informationBottomUp Parsing. Parser Generation. LR Parsing. Constructing LR Parser
Parser Generation Main Problem: given a grammar G, how to build a topdown parser or a bottomup parser for it? parser : a program that, given a sentence, reconstructs a derivation for that sentence 
More informationTableDriven Parsing
TableDriven Parsing It is possible to build a nonrecursive predictive parser by maintaining a stack explicitly, rather than implicitly via recursive calls [1] The nonrecursive parser looks up the production
More informationshiftreduce parsing
Parsing #2 Bottomup 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 bottomup
More informationTopDown Parsing and Intro to BottomUp Parsing. Lecture 7
TopDown Parsing and Intro to BottomUp Parsing Lecture 7 1 Predictive Parsers Like recursivedescent but parser can predict which production to use Predictive parsers are never wrong Always able to guess
More informationFormal Languages and Compilers Lecture VII Part 3: Syntactic A
Formal Languages and Compilers Lecture VII Part 3: Syntactic Analysis Free University of BozenBolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/
More informationSyntax Analysis. Martin Sulzmann. Martin Sulzmann Syntax Analysis 1 / 38
Syntax Analysis Martin Sulzmann Martin Sulzmann Syntax Analysis 1 / 38 Syntax Analysis Objective Recognize individual tokens as sentences of a language (beyond regular languages). Example 1 (OK) Program
More informationBottomUp Parsing. Lecture 1112
BottomUp Parsing Lecture 1112 (From slides by G. Necula & R. Bodik) 9/22/06 Prof. Hilfinger CS164 Lecture 11 1 BottomUp Parsing Bottomup parsing is more general than topdown parsing And just as efficient
More informationBottomUp Parsing. Lecture 1112
BottomUp Parsing Lecture 1112 (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 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 informationSection A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.
Section A 1. What do you meant by parser and its types? A parser for grammar G is a program that takes as input a string w and produces as output either a parse tree for w, if w is a sentence of G, or
More informationCSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D1
CSE P 501 Compilers LR Parsing Hal Perkins Spring 2018 UW CSE P 501 Spring 2018 D1 Agenda LR Parsing Tabledriven Parsers Parser States ShiftReduce and ReduceReduce conflicts UW CSE P 501 Spring 2018
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 informationCS 4120 Introduction to Compilers
CS 4120 Introduction to Compilers Andrew Myers Cornell University Lecture 6: BottomUp Parsing 9/9/09 Bottomup parsing A more powerful parsing technology LR grammars  more expressive than LL can handle
More informationCompilers. Predictive Parsing. Alex Aiken
Compilers Like recursivedescent but parser can predict which production to use By looking at the next fewtokens No backtracking Predictive parsers accept LL(k) grammars L means lefttoright scan of input
More informationParsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1)
TD parsing  LL(1) Parsing First and Follow sets Parse table construction BU Parsing Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1) Problems with SLR Aho, Sethi, Ullman, Compilers
More informationParser Generation. BottomUp Parsing. Constructing LR Parser. LR Parsing. Construct parse tree bottomup  from leaves to the root
Parser Generation Main Problem: given a grammar G, how to build a topdown parser or a bottomup parser for it? parser : a program that, given a sentence, reconstructs a derivation for that sentence 
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 informationConcepts Introduced in Chapter 4
Concepts Introduced in Chapter 4 Grammars ContextFree Grammars Derivations and Parse Trees Ambiguity, Precedence, and Associativity Top Down Parsing Recursive Descent, LL Bottom Up Parsing SLR, LR, LALR
More informationOutline CS412/413. Administrivia. Review. Grammars. Left vs. Right Recursion. More tips forll(1) grammars Bottomup parsing LR(0) parser construction
C12/1 Introduction to Compilers and Translators pring 00 Outline More tips forll1) grammars Bottomup parsing LR0) parser construction Lecture 5: Bottomup parsing Lecture 5 C 12/1 pring '00 Andrew Myers
More informationEDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:
EDAN65: Compilers, Lecture 06 A LR parsing Görel Hedin Revised: 20170911 This lecture Regular expressions Contextfree grammar Attribute grammar Lexical analyzer (scanner) Syntactic analyzer (parser)
More informationLR Parsing Techniques
LR Parsing Techniques Introduction BottomUp Parsing LR Parsing as Handle Pruning ShiftReduce Parser LR(k) Parsing Model Parsing Table Construction: SLR, LR, LALR 1 BottomUP Parsing A bottomup parser
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 contextfree grammars n Grammar derivations n More about parse trees n Topdown and
More informationMore BottomUp Parsing
More BottomUp 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 informationContextfree grammars
Contextfree grammars Section 4.2 Formal way of specifying rules about the structure/syntax of a program terminals  tokens nonterminals  represent higherlevel structures of a program start symbol,
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 informationCOP4020 Programming Languages. Syntax Prof. Robert van Engelen
COP4020 Programming Languages Syntax Prof. Robert van Engelen Overview Tokens and regular expressions Syntax and contextfree grammars Grammar derivations More about parse trees Topdown and bottomup
More informationCompiler Construction: Parsing
Compiler Construction: Parsing Mandar Mitra Indian Statistical Institute M. Mitra (ISI) Parsing 1 / 33 Contextfree grammars. Reference: Section 4.2 Formal way of specifying rules about the structure/syntax
More informationIntroduction to parsers
Syntax Analysis Introduction to parsers Contextfree grammars Pushdown automata Topdown parsing LL grammars and parsers Bottomup parsing LR grammars and parsers Bison/Yacc  parser generators Error
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 informationLR Parsing LALR Parser Generators
LR Parsing LALR Parser Generators Outline Review of bottomup parsing Computing the parsing DFA Using parser generators 2 Bottomup Parsing (Review) A bottomup parser rewrites the input string to the
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 informationBottomup parsing. BottomUp Parsing. Recall. Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form
Bottomup parsing Bottomup 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 informationLexical and Syntax Analysis. BottomUp Parsing
Lexical and Syntax Analysis BottomUp Parsing Parsing There are two ways to construct derivation of a grammar. TopDown: begin with start symbol; repeatedly replace an instance of a production s LHS with
More informationIt parses an input string of tokens by tracing out the steps in a leftmost derivation.
It parses an input string of tokens by tracing out CS 4203 Compiler Theory the steps in a leftmost derivation. CHAPTER 4: TOPDOWN PARSING Part1 And the implied traversal of the parse tree is a preorder
More informationChapter 3: CONTEXTFREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees
Chapter 3: CONTEXTFREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees 3.3.1 Parse trees 1. Derivation V.S. Structure Derivations do not uniquely represent the structure of the strings
More informationBottomUp Parsing LR Parsing
BottomUp 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 informationChapter 4. Lexical and Syntax Analysis
Chapter 4 Lexical and Syntax Analysis Chapter 4 Topics Introduction Lexical Analysis The Parsing Problem RecursiveDescent Parsing BottomUp Parsing Copyright 2012 AddisonWesley. All rights reserved.
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 informationSyntax Analysis: Contextfree Grammars, Pushdown Automata and Parsing Part  4. Y.N. Srikant
Syntax Analysis: Contextfree Grammars, Pushdown Automata and Part  4 Department of Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Principles of Compiler
More informationSyntax Analysis. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill
Syntax Analysis Björn B. Brandenburg The University of North Carolina at Chapel Hill Based on slides and notes by S. Olivier, A. Block, N. Fisher, F. HernandezCampos, and D. Stotts. The Big Picture Character
More informationParsing Wrapup. Roadmap (Where are we?) Last lecture Shiftreduce parser LR(1) parsing. This lecture LR(1) parsing
Parsing Wrapup Roadmap (Where are we?) Last lecture Shiftreduce parser LR(1) parsing LR(1) items Computing closure Computing goto LR(1) canonical collection This lecture LR(1) parsing Building ACTION
More informationSyntax Analysis. The Big Picture. The Big Picture. COMP 524: Programming Languages Srinivas Krishnan January 25, 2011
Syntax Analysis COMP 524: Programming Languages Srinivas Krishnan January 25, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block. 1 The Big Picture Character Stream Token
More informationLR Parsing LALR Parser Generators
Outline LR Parsing LALR Parser Generators Review of bottomup parsing Computing the parsing DFA Using parser generators 2 Bottomup Parsing (Review) A bottomup parser rewrites the input string to the
More informationDownloaded from Page 1. LR Parsing
Downloaded from http://himadri.cmsdu.org Page 1 LR Parsing We first understand Context Free Grammars. Consider the input string: x+2*y When scanned by a scanner, it produces the following stream of tokens:
More informationCA Compiler Construction
CA4003  Compiler Construction David Sinclair A topdown 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 informationCompiler Construction Class Notes
Compiler Construction Class Notes Reg Dodds Department of Computer Science University of the Western Cape rdodds@uwc.ac.za c 2006,2017 Reg Dodds March 22, 2017 Introduction What is a Compiler? What is
More informationCompiler Construction 2016/2017 Syntax Analysis
Compiler Construction 2016/2017 Syntax Analysis Peter Thiemann November 2, 2016 Outline 1 Syntax Analysis Recursive topdown parsing Nonrecursive topdown parsing Bottomup parsing Syntax Analysis tokens
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 information4 (c) parsing. Parsing. Top down vs. bo5om up parsing
4 (c) parsing Parsing A grammar describes syntac2cally 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 informationCS 164 Programming Languages and Compilers Handout 9. Midterm I Solution
Midterm I Solution Please read all instructions (including these) carefully. There are 5 questions on the exam, some with multiple parts. You have 1 hour and 20 minutes to work on the exam. The exam is
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 informationVIVA QUESTIONS WITH ANSWERS
VIVA QUESTIONS WITH ANSWERS 1. What is a compiler? A compiler is a program that reads a program written in one language the source language and translates it into an equivalent program in another languagethe
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 informationCSE431 Translation of Computer Languages
CSE431 Translation of Computer Languages Top Down Parsers Doug Shook Top Down Parsers Two forms: Recursive Descent Table Also known as LL(k) parsers: Read tokens from Left to right Produces a Leftmost
More informationParser. Larissa von Witte. 11. Januar Institut für Softwaretechnik und Programmiersprachen. L. v. Witte 11. Januar /23
Parser Larissa von Witte Institut für oftwaretechnik und Programmiersprachen 11. Januar 2016 L. v. Witte 11. Januar 2016 1/23 Contents Introduction Taxonomy Recursive Descent Parser hift Reduce Parser
More informationCSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D1
CSE 401 Compilers LR Parsing Hal Perkins Autumn 2011 10/10/2011 200211 Hal Perkins & UW CSE D1 Agenda LR Parsing Tabledriven Parsers Parser States ShiftReduce and ReduceReduce conflicts 10/10/2011
More informationParsing II Topdown parsing. Comp 412
COMP 412 FALL 2018 Parsing II Topdown parsing Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled
More informationChapter 3. Parsing #1
Chapter 3 Parsing #1 Parser source file get next character scanner get token parser AST token A parser recognizes sequences of tokens according to some grammar and generates Abstract Syntax Trees (ASTs)
More informationLR(0) Parsing Summary. LR(0) Parsing Table. LR(0) Limitations. A NonLR(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 informationCSCI312 Principles of Programming Languages
Copyright 2006 The McGrawHill 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 McGrawHill
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 informationProgramming Language Syntax and Analysis
Programming Language Syntax and Analysis 2017 Kwangman Ko (http://compiler.sangji.ac.kr, kkman@sangji.ac.kr) Dept. of Computer Engineering, Sangji University Introduction Syntax the form or structure of
More informationBuilding a Parser III. CS164 3:305:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1
Building a Parser III CS164 3:305:00 TT 10 Evans 1 Overview Finish recursive descent parser when it breaks down and how to fix it eliminating left recursion reordering productions Predictive parsers (aka
More informationCS2210: Compiler Construction Syntax Analysis Syntax Analysis
Comparison with Lexical Analysis The second phase of compilation Phase Input Output Lexer string of characters string of tokens Parser string of tokens Parse tree/ast What Parse Tree? CS2210: Compiler
More informationDr. D.M. Akbar Hussain
Syntax Analysis Parsing Syntax Or Structure Given By Determines Grammar Rules Context Free Grammar 1 Context Free Grammars (CFG) Provides the syntactic structure: A grammar is quadruple (V T, V N, S, R)
More informationSyntactic Analysis. TopDown Parsing
Syntactic Analysis TopDown 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 informationParsing #1. Leonidas Fegaras. CSE 5317/4305 L3: Parsing #1 1
Parsing #1 Leonidas Fegaras CSE 5317/4305 L3: Parsing #1 1 Parser source file get next character scanner get token parser AST token A parser recognizes sequences of tokens according to some grammar and
More informationRecursive Descent Parsers
Recursive Descent Parsers Lecture 7 Robb T. Koether HampdenSydney College Wed, Jan 28, 2015 Robb T. Koether (HampdenSydney College) Recursive Descent Parsers Wed, Jan 28, 2015 1 / 18 1 Parsing 2 LL Parsers
More informationPart 3. Syntax analysis. Syntax analysis 96
Part 3 Syntax analysis Syntax analysis 96 Outline 1. Introduction 2. Contextfree grammar 3. Topdown parsing 4. Bottomup parsing 5. Conclusion and some practical considerations Syntax analysis 97 Structure
More informationCIT Lecture 5 ContextFree Grammars and Parsing 4/2/2003 1
CIT3136  Lecture 5 ContextFree Grammars and Parsing 4/2/2003 1 Definition of a Contextfree Grammar: An alphabet or set of basic symbols (like regular expressions, only now the symbols are whole tokens,
More informationExample CFG. Lectures 16 & 17 BottomUp 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 BottomUp 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 informationSYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram
CS6660 COMPILER DESIGN Question Bank UNIT IINTRODUCTION TO COMPILERS 1. Define compiler. 2. Differentiate compiler and interpreter. 3. What is a language processing system? 4. List four software tools
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 informationUNITIII BOTTOMUP PARSING
UNITIII BOTTOMUP PARSING Constructing a parse tree for an input string beginning at the leaves and going towards the root is called bottomup parsing. A general type of bottomup parser is a shiftreduce
More informationParsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.
Parsing Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students
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 informationHomework. Lecture 7: Parsers & Lambda Calculus. Rewrite Grammar. Problems
Homework Lecture 7: Parsers & Lambda Calculus CSC 131 Spring, 2019 Kim Bruce First line:  module Hmwk3 where  Next line should be name as comment  Name of program file should be Hmwk3.hs Problems How
More informationA leftsentential form is a sentential form that occurs in the leftmost derivation of some sentence.
Bottomup 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 leftsentential form is a sentential form that occurs
More informationChapter 4. Lexical and Syntax Analysis. Topics. Compilation. Language Implementation. Issues in Lexical and Syntax Analysis.
Topics Chapter 4 Lexical and Syntax Analysis Introduction Lexical Analysis Syntax Analysis Recursive Descent Parsing BottomUp parsing 2 Language Implementation Compilation There are three possible approaches
More informationIntroduction to Parsing. Comp 412
COMP 412 FALL 2010 Introduction to Parsing Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make
More informationCIT 3136 Lecture 7. TopDown Parsing
CIT 3136 Lecture 7 TopDown Parsing Chapter 4: Topdown Parsing A topdown parsing algorithm parses an input string of tokens by tracing out the steps in a leftmost derivation. Such an algorithm is called
More informationCSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis
Chapter 4 Lexical and Syntax Analysis Introduction  Language implementation systems must analyze source code, regardless of the specific implementation approach  Nearly all syntax analysis is based on
More informationLR Parsing  The Items
LR Parsing  The Items Lecture 10 Sections 4.5, 4.7 Robb T. Koether HampdenSydney College Fri, Feb 13, 2015 Robb T. Koether (HampdenSydney College) LR Parsing  The Items Fri, Feb 13, 2015 1 / 31 1 LR
More information