Eng. Maha Talaat Page 1

Similar documents
CSC 4181Compiler Construction. Context-Free Grammars Using grammars in parsers. Parsing Process. Context-Free Grammar

[4203] Compiler Theory Sheet # 3-Answers

INF5110 Compiler Construction

Parsing Algorithms. CS 4447/CS Stephen Watt University of Western Ontario

Predictive parsing as a specific subclass of recursive descent parsing complexity comparisons with general parsing

n Top-down parsing vs. bottom-up parsing n Top-down parsing n Introduction n A top-down depth-first parser (with backtracking)

Ambiguity, Precedence, Associativity & Top-Down Parsing. Lecture 9-10

Syntax Analysis Part III

Follow sets. LL(1) Parsing Table

CS153: Compilers Lecture 5: LL Parsing

CS 406: Bottom-Up Parsing

Computing if a token can follow

Compiler Principles, PS4

Lecture VII Part 2: Syntactic Analysis Bottom-up Parsing: LR Parsing. Prof. Bodik CS Berkley University 1

h>p://lara.epfl.ch Compiler Construc/on 2011 CYK Algorithm and Chomsky Normal Form

Context-Free Grammars (and Languages) Lecture 7

CSE302: Compiler Design

Administrivia. Test I during class on 10 March. Bottom-Up Parsing. Lecture An Introductory Example

CYK Algorithm for Parsing General Context-Free Grammars

Top-Down Parsing and Intro to Bottom-Up Parsing

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

CS453 Intro and PA1 1

Lecture 11 Context-Free Languages

Syntax Analysis Part I

1. For the following sub-problems, consider the following context-free grammar: S AA$ (1) A xa (2) A B (3) B yb (4)

Syntax Analysis Part I. Position of a Parser in the Compiler Model. The Parser. Chapter 4

CFLs and Regular Languages. CFLs and Regular Languages. CFLs and Regular Languages. Will show that all Regular Languages are CFLs. Union.

Compiling Techniques

INF5110 Compiler Construction

Syntax Analysis Part I

Compiling Techniques

CS 314 Principles of Programming Languages

Syntax Analysis - Part 1. Syntax Analysis

CA Compiler Construction

Compiler Design Spring 2017

Course Script INF 5110: Compiler construction

Context-free Grammars and Languages

Announcement: Midterm Prep

Introduction to Bottom-Up Parsing

Compiler Design. Spring Syntactic Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

LR2: LR(0) Parsing. LR Parsing. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

CS415 Compilers Syntax Analysis Top-down Parsing

Recursive descent for grammars with contexts

CS20a: summary (Oct 24, 2002)

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

Bottom-Up Syntax Analysis

Introduction to Bottom-Up Parsing

Talen en Compilers. Johan Jeuring , period 2. October 29, Department of Information and Computing Sciences Utrecht University

1. For the following sub-problems, consider the following context-free grammar: S A$ (1) A xbc (2) A CB (3) B yb (4) C x (6)

This lecture covers Chapter 5 of HMU: Context-free Grammars

Compiler Principles, PS7

Compiler construction

Syntactical analysis. Syntactical analysis. Syntactical analysis. Syntactical analysis

Compiler Construction Lent Term 2015 Lectures (of 16)

MA/CSSE 474 Theory of Computation

Creating a Recursive Descent Parse Table

Compiler Construction Lent Term 2015 Lectures (of 16)

Compiler Construction Lectures 13 16

Shift-Reduce parser E + (E + (E) E [a-z] In each stage, we shift a symbol from the input to the stack, or reduce according to one of the rules.

Lecture 11 Sections 4.5, 4.7. Wed, Feb 18, 2009

Announcements. H6 posted 2 days ago (due on Tue) Midterm went well: Very nice curve. Average 65% High score 74/75

Introduction to Bottom-Up Parsing

Compiling Techniques

Definition: A grammar G = (V, T, P,S) is a context free grammar (cfg) if all productions in P have the form A x where

Introduction to Bottom-Up Parsing

Introduction and Motivation. Introduction and Motivation. Introduction to Computability. Introduction and Motivation. Theory. Lecture5: Context Free

INF5110 Compiler Construction

Syntactic Analysis. Top-Down Parsing

Computer Science 160 Translation of Programming Languages

Parsing -3. A View During TD Parsing

LR(1) Parsers Part III Last Parsing Lecture. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.

1. For the following sub-problems, consider the following context-free grammar: S AB$ (1) A xax (2) A B (3) B yby (5) B A (6)

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

THEORY OF COMPILATION

Computer Science 160 Translation of Programming Languages

60-354, Theory of Computation Fall Asish Mukhopadhyay School of Computer Science University of Windsor

Introduction to ANTLR (ANother Tool for Language Recognition)

CPS 220 Theory of Computation

Plan for 2 nd half. Just when you thought it was safe. Just when you thought it was safe. Theory Hall of Fame. Chomsky Normal Form

CONTEXT FREE GRAMMAR AND

Exercises. Exercise: Grammar Rewriting

CS415 Compilers Syntax Analysis Bottom-up Parsing

CS5371 Theory of Computation. Lecture 7: Automata Theory V (CFG, CFL, CNF)

I 1 : {S S } I 2 : {S X ay, Y X } I 3 : {S Y } I 4 : {X b Y, Y X, X by, X c} I 5 : {X c } I 6 : {S Xa Y, Y X, X by, X c} I 7 : {X by } I 8 : {Y X }

LR(1) Parsers Part II. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.

Languages. Languages. An Example Grammar. Grammars. Suppose we have an alphabet V. Then we can write:

Bottom up parsing. General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing);

LL(1) Grammar and parser

Context Free Grammars: Introduction. Context Free Grammars: Simplifying CFGs

Pushdown Automata (2015/11/23)

Compiler Design 1. LR Parsing. Goutam Biswas. Lect 7

MTH401A Theory of Computation. Lecture 17

Parsing VI LR(1) Parsers

LR(1) Parsers Part II. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.

Context Free Grammars

Syntax Analysis (Part 2)

The Parser. CISC 5920: Compiler Construction Chapter 3 Syntactic Analysis (I) Grammars (cont d) Grammars

Homework 4 Solutions. 2. Find context-free grammars for the language L = {a n b m c k : k n + m}. (with n 0,

Bottom-Up Syntax Analysis

Transcription:

El-Shorouk Academy Acad. Year : 2013 / 2014 Higher Institute for Computer & Term : 2nd Information Technology Year : 4th Computer Science Department Supervisor: Prof. Dr. Ahmed Abbassy Section contents: First set Follow set Action table LL(1) parsing table For factoring Compilers Section (10) Consider the following grammar Statement if-stmt other If-stmt if ( exp ) statement if ( exp ) statement else statement Exp 0 1 Left factor this grammar Statement if-stmt other If-stmt if (exp) statement else-part Else-part else statement ε Exp 0 1 b) Construct First and Follow sets for the non-terminal of the resulting grammar We write out the grammar rule choice separately and number them: (1) Statement if-stmt (2) Statement other (3) If-stmt if (exp) statement else-part (4) Else-part else statement (5) (6) Exp 0 (7) Exp 1 Note: This grammar does have an ε-production, but the only nullable non-terminal else-part will not in the beginning of left side of any rule choice and will not complicate the computation process. The First Sets: Eng. Maha Talaat Page 1

The computation process for above First Sets Grammar Rule Pass 1 Pass 2 Statement if-stmt Statement other First(statement)={other} If-stmt if (exp) statement elsepart Else-part else statement First(else-part)={else} Exp 0 First(exp)={1} Exp 1 The Follow Sets: Follow(statement)={$,else} Follow(if-statement)={$,else} Follow(else-part)={$,else} Follow(exp)={)} The first and follow set First Sets Follow Sets Follow(statement)={$,else} Follow(if-statement)={$,else} Follow(else-part)={$,else} Follow(exp)={)} the LL(1) parsing table M[N,T] If Other Else 0 1 $ Statement Statement if-stmt If-stmt Else-part If-stmt if (exp) statement else-part Stateme nt other Else-part else statement Else-part ε exp Exp 0 Exp 1 Eng. Maha Talaat Page 2

Notice for Example: If-Statement A grammar is an LL(1) grammar if the associated LL(1) parsing table has at most on production in each table entry The entry M[else-part, else] contains two entries, i.e. the dangling else ambiguity. Disambiguating rule: always prefer the rule that generates the current look-ahead token over any other, and thus the production Else-part else statement or With this modification, the above table will become unambiguous The grammar can be parsed as if it were an LL(1) grammar Show the action of corresponding LL(1) parser given the input string If (0) if (1) other else other The parsing actions for the string: If (0) if (1) other else other ( for conciseness, statement= S, if-stmt=i, else-part=l, exp=e, if=i, else=e, other=o) Steps Parsing Stack Input Action 1 $S S I 2 $I I i(e)sl 3 $LS)E(i 4 $ LS)E( (0)i(1)oeo $ 5 $ LS)E 0)i(1)oeo $ E o S I I i(e)sl E 1 S o L es S o L ε 22 $ $ accept Eng. Maha Talaat Page 3

Consider the following grammar Stmt-sequence stmt; stmt-sequence stmt Stmt s Left factor this grammar Stmt-sequence stmt stmt-seq Stmt-seq ; stmt-sequence ε Construct First and Follow sets for the non terminal of the resulting grammar The first and follow set First Sets Follow Sets First(stmt-sequence)={s} Follow(stmt-sequence)={$} First(stmt)={s} Follow(stmt)={;} First(stmt-seq )={;, ε} Follow(stmt-seq )={$} the LL(1) parsing table M[N,T] S ; $ Stmtsequence Stmt-sequence stmt stmt-seq Stmt stmt s Stmt-seq Stmt-seq ; stmt-sequence Stmt-seq ε Show the action of corresponding LL(1) parser given the input string s; s Consider the following grammar S (S) S ε Construct First and Follow sets for the non terminals of the grammar M[N,T] ( ) $ S S (S) S S ε S ε Eng. Maha Talaat Page 4

Show the action of corresponding LL(1) parser given the input string ( ) Steps Parsing Stack Input Action 1 $S ( ) $ S (S) S 2 $S)S( ( ) $ 3 $S)S )$ S ε 4 $S) )$ 5 $S $ S ε 6 $ $ accept Left factor the following grammar: exp term+exp term Answer: exp term exp exp + exp ε Eng. Maha Talaat Page 5