Compiler Construction

Slides:



Advertisements
Similar presentations
Compiler Construction
Advertisements

A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
Compiler Designs and Constructions
Bottom-up Parsing A general style of bottom-up syntax analysis, known as shift-reduce parsing. Two types of bottom-up parsing: Operator-Precedence parsing.
LR Parsing – The Items Lecture 10 Mon, Feb 14, 2005.
1 Chapter 5: Bottom-Up Parsing (Shift-Reduce). 2 - attempts to construct a parse tree for an input string beginning at the leaves (the bottom) and working.
Top-Down Parsing.
CS 310 – Fall 2006 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho,
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
1 Chapter 4: Top-Down Parsing. 2 Objectives of Top-Down Parsing an attempt to find a leftmost derivation for an input string. an attempt to construct.
Top-Down Parsing.
– 1 – CSCE 531 Spring 2006 Lecture 7 Predictive Parsing Topics Review Top Down Parsing First Follow LL (1) Table construction Readings: 4.4 Homework: Program.
CPSC 388 – Compiler Design and Construction
Review: –How do we define a grammar (what are the components in a grammar)? –What is a context free grammar? –What is the language defined by a grammar?
Chapter 5 Top-Down Parsing.
1 Compiler Construction Syntax Analysis Top-down parsing.
CMSC 331, Some material © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Chapter 4 Bottom Up Parsing.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
COP4020 Programming Languages Parsing Prof. Xin Yuan.
LL(1) Parser. What does LL signify ? The first L means that the scanning takes place from Left to right. The first L means that the scanning takes place.
1 Compiler Construction Syntax Analysis Top-down parsing.
1 Context free grammars  Terminals  Nonterminals  Start symbol  productions E --> E + T E --> E – T E --> T T --> T * F T --> T / F T --> F F --> (F)
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
Parsing methods: –Top-down parsing –Bottom-up parsing –Universal.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Bernd Fischer RW713: Compiler and Software Language Engineering.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
9/30/2014IT 3271 How to construct an LL(1) parsing table ? 1.S  A S b 2.S  C 3.A  a 4.C  c C 5.C  abc$ S1222 A3 C545 LL(1) Parsing Table What is the.
Parsing #1 Leonidas Fegaras.
Compiler Construction
Chapter 4 - Parsing CSCE 343.
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
LR Parsing – The Items Lecture 10 Fri, Feb 13, 2004.
Compiler design Bottom-up parsing Concepts
The role of parser Lexical Analyzer Parser Rest of Front End Symbol
Context free grammars Terminals Nonterminals Start symbol productions
Unit-3 Bottom-Up-Parsing.
Lecture #12 Parsing Types.
Compilers Welcome to a journey to CS419 Lecture15: Syntax Analysis:
Table-driven parsing Parsing performed by a finite state machine.
Syntactic Analysis and Parsing
Introduction to Top Down Parser
Top-down parsing cannot be performed on left recursive grammars.
SYNTAX ANALYSIS (PARSING).
Parsing with Context Free Grammars
CS 404 Introduction to Compiler Design
Compiler Construction
UNIT 2 - SYNTAX ANALYSIS Role of the parser Writing grammars
Bottom-Up Syntax Analysis
Top-Down Parsing.
3.2 Language and Grammar Left Factoring Unclear productions
Top-Down Parsing CS 671 January 29, 2008.
Lecture 7 Predictive Parsing
Syntax Analysis source program lexical analyzer tokens syntax analyzer
4d Bottom Up Parsing.
Compiler Design 7. Top-Down Table-Driven Parsing
Top-Down Parsing Identify a leftmost derivation for an input string
Top-Down Parsing The parse tree is created top to bottom.
Chapter 4 Top Down Parser.
Bottom Up Parsing.
Predictive Parsing Lecture 9 Wed, Feb 9, 2005.
Syntax Analysis - Parsing
4d Bottom Up Parsing.
Lecture 7 Predictive Parsing
Nonrecursive Predictive Parsing
4d Bottom Up Parsing.
Predictive Parsing Program
Chap. 3 BOTTOM-UP PARSING
4d Bottom Up Parsing.
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Compiler Construction Chapter 4 Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Parsing Determining whether a string is derivable a grammar or not. Two approaches based on the order in which a parse tree is constructed are given below: Top-down parsing ∙ Starts construction at root of parse tree and proceeds to children. ∙ Uses LMD Bottom-up parsing ∙ Starts at leaves and proceeds to root ∙ Uses reverse or rightmost derivation Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Top Down Parsing Example: of top-down parsing using a subset of the types of Pascal (the token dotdot stands for "..", and  "^" means a pointer)       type     --> simple                   |    id                   |    array [ simple ] of type       simple -->  integer                   |      char                   |      num   dotdot   num      The top down construction of a parse tree for the string             array [ num dotdot num ] of integer Compiler Construction Dr K. V. N. Sunitha

Array [Num Dotdot Num] of Integer Grammar type     --> simple                   |    id  | array [ simple ] of type simple -->  integer               |      char | num dotdot   num Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Bottom up parsing: S ->aABe A - > Abc | b B -> d , w= abbcde abbcde aAbcde aAde aABe S Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Types of Parsers Top down parsers Recursive descent (predictive /non-predictive) parsers Non-recursive descent predictive parser, i.e. LL(1) Bottom up parsers LR parsers LR(0) – Least powerful than SLR Simple LR, i.e. SLR(1) – Simple and least powerful. Look ahead LR, i.e. LALR(1) – power and cost is intermediate between the two Canonical LR, i.e. CLR(1)/LR(1) – Most powerful and costly Compiler Construction Dr K. V. N. Sunitha

Compiler Construction To design predictive parsers, grammar has to be free of left recursion and should be left factored. Recursive descent predictive parser for parsing i+i using E→ E+ T | T, T→ i After eliminating left recursion ‘G’ is, E → TE’ , E’ → +TE’ |ε , T → i $ Compiler Construction Dr K. V. N. Sunitha

Compiler Construction { if (L==‘+’) Match(‘+’); T(); E’(); } else return; Match(chat t) { if (t==L) L=getchar(); else Printf(“err\n”); } char L; L=getchar(); main() { E(); E() { T(); E’(); } T() { Match(i); } if (L==“$”) printf(“successful”); } Compiler Construction Dr K. V. N. Sunitha

Non-recursive Predictive Parsers Avoid recursion for efficiency reasons Used widely INPUT BUFFER a + b $ X Y Z $ STACK LL(1) Parser Output Parsing Table Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Parsing Algorithm Let X be the grammar symbol on top of stack, ‘a’ is current input symbol Stack contents and remaining input called parser configuration (initially push $ then start symbol S on to stack and read complete input string in input buffer) If X=a=$ halt and announce successful completion If X=a ≠ $ pop X off stack advance input ptr to next symbol If X is a nonterminal, the parser consults parsing table entry M[X,a]. This parsing table entry will be either a production of the grammar or blank entry. If, for example, X->UVW replace X with WVU (U on top) Output the production used above Compiler Construction Dr K. V. N. Sunitha

Example: input string: id + id *id E → TE’ E’ → +TE’ | ε T → F T’ T’ → *FT’ | ε F → id |(E) Example: input string: id + id *id Parsing table: id + * ( ) $ E E→TE’ E’ E’→+TE’ E’→ε T T→FT’ T’ T’→ε T’→*FT’ F F→id F→(E) Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Construction of Parsing Table Requires Two Functions: First() and Follow(); Evaluated by following 2 rules If α is terminal, First(X)=(X) If X is a non-terminal (a) & has null production (i.e.) X →ε , First(X)=(ε) (b) & has non null production, X →X1X2X3 First(X)=First(X1X2X3) = First(X1) if X1 => ε First(X)=First(X1)-{ε} U First(X2X3 ) if X1 => ε * * Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Expr Grammar Expr Grammar without Left Recursion E → E + T | T T → T * F | F F → id |(E) E → TE’ E’ → +TE’ | ε T → F T’ T’ → *FT’ | ε F → id |(E) First() E {id, ( } E’ {+, ε} T T’ {*, ε} F Compiler Construction Dr K. V. N. Sunitha

Compiler Construction More Examples A → Bb | C d B → a B | ε C → c C | ε First(A) = {a,b, c,d}, First (B) = {a, ε},First (C) = {c, ε} S → ABCDE A → a | ε B → b | ε C → c | ε D → d | ε E → e First(S)= ? Compiler Construction Dr K. V. N. Sunitha

Compiler Construction S → ACB | C bB | Ba A → da | BC B → g | ε C → h | ε First(S) = ? First(A) = ? First(B) = ? First(C) = ? Compiler Construction Dr K. V. N. Sunitha

Follow(A): Set of Terminals That May Follow Immediately to Right of A Evaluted by 3 rules If A is start symbol, Follow (A) = {$} If S → α A β is in G, Follow (A) = First(β) –{ε } If S → α A or S → α A β {β => ε}, Follow (A) = Follow (S); * Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Expr Grammar without left recursion Expr Grammar E → TE’ E’ → +TE’ | ε T → FT’ T’ → *FT’ | ε F → id |(E) E → E + T | T T → T * F | F F → id |(E) First() E {id, ( } E’ {+, ε} T T’ {*, ε} F Follow() { ), $ } { +, ),$} {*,+,),$} Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Examples - Follow( ) 1. S → a AB b A → c | ε B → d | ε Follow (A)={ b, d} Follow (B)={ b} Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Examples - Follow( ) 2. S → a B D h B → c C C → b C | ε D → E F E → g | ε F → f | ε Follow (B) = ? = Follow (C) Follow( D ) = ? Follow (E) = ? Follow (F)= ? Compiler Construction Dr K. V. N. Sunitha

LL(1) Parsing Table Construction For each production A → α , repeat the following steps. For each terminal ‘a’ in first(α), add A → α to M [A ,a] If ε is in First(α), add A → α to M [A,b] for each ‘b’ in Follow (A). Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Construct LL(1) parser to parse string (a,a) G is S → ( L ) | a, L → L , S | S. After eliminating left recursion, S → ( L ) | a L → S L’ L’ → , S L’ | є a , ( ) $ S S → a S → (L) L L → S L’ L’ L’ → , S L’ L’ → є. Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Construct LL(1) parser S → AaBb |AbBa, A → є, B → є Check whether above grammar is LL(1)? Compiler Construction Dr K. V. N. Sunitha