Exercises. Exercise: Grammar Rewriting

Similar documents
Creating a Recursive Descent Parse Table

Syntax Analysis Part I

Syntax Analysis Part III

Introduction to Bottom-Up Parsing

Introduction to Bottom-Up Parsing

Introduction to Bottom-Up Parsing

Introduction to Bottom-Up Parsing

CSE302: Compiler Design

Syntax Analysis Part I

Top-Down Parsing and Intro to Bottom-Up Parsing

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

Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 3. Y.N. Srikant

CS415 Compilers Syntax Analysis Top-down Parsing

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

CS 314 Principles of Programming Languages

Computer Science 160 Translation of Programming Languages

Syntactic Analysis. Top-Down Parsing

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

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

Compiler Principles, PS4

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

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

Compiling Techniques

Top-Down Parsing, Part II

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.

Bottom-Up Parsing. Ÿ rm E + F *idÿ rm E +id*idÿ rm T +id*id. Ÿ rm F +id*id Ÿ rm id + id * id

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs.

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

Parsing -3. A View During TD Parsing

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

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

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

LL(1) Grammar and parser

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

CONTEXT FREE GRAMMAR AND

Syntactical analysis. Syntactical analysis. Syntactical analysis. Syntactical analysis

Follow sets. LL(1) Parsing Table

Compiling Techniques

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

Syntax Analysis (Part 2)

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

Bottom-up syntactic parsing. LR(k) grammars. LR(0) grammars. Bottom-up parser. Definition Properties

Context free languages

CA Compiler Construction

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

Compila(on* **0368/3133*(Semester*A,*2013/14)*

Compiler Principles, PS7

Syntax Analysis - Part 1. Syntax Analysis

THEORY OF COMPILATION

EXAM. Please read all instructions, including these, carefully NAME : Problem Max points Points 1 10 TOTAL 100

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

Announcement: Midterm Prep

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 }

Bottom-up syntactic parsing. LR(k) grammars. LR(0) grammars. Bottom-up parser. Definition Properties

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

Bottom-up syntactic parsing. LR(k) grammars. LR(0) grammars. Bottom-up parser. Definition Properties

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs(dot)nl. college 6, woensdag 28 oktober 2015

Syntax Directed Transla1on

INF5110 Compiler Construction

CS153: Compilers Lecture 5: LL Parsing

Computing if a token can follow

Bottom-Up Syntax Analysis

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)

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

Parsing with Context-Free Grammars

Compiler construction

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

Context-Free Grammars (and Languages) Lecture 7

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

CS 406: Bottom-Up Parsing

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

Compiler Construction Lectures 13 16

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

2.4 Parsing. Computer Science 332. Compiler Construction. Chapter 2: A Simple One-Pass Compiler : Parsing. Top-Down Parsing

Recursive descent for grammars with contexts

Parsing VI LR(1) Parsers

Computer Science 160 Translation of Programming Languages

INF5110 Compiler Construction

Compiler Construction Lent Term 2015 Lectures (of 16)

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)

Compiler Construction Lent Term 2015 Lectures (of 16)

Parsing. Unger s Parser. Laura Kallmeyer. Winter 2016/17. Heinrich-Heine-Universität Düsseldorf 1 / 21

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

From EBNF to PEG. Roman R. Redziejowski

Compiler Design Spring 2017

Course Script INF 5110: Compiler construction

Context-free Grammars and Languages

Introduction to Computational Linguistics

MA/CSSE 474 Theory of Computation

A* Search. 1 Dijkstra Shortest Path

Parsing. Context-Free Grammars (CFG) Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 26

From EBNF to PEG. Roman R. Redziejowski. Concurrency, Specification and Programming Berlin 2012

SYLLABUS. Introduction to Finite Automata, Central Concepts of Automata Theory. CHAPTER - 3 : REGULAR EXPRESSIONS AND LANGUAGES

Lecture Notes on Inductive Definitions

CS20a: summary (Oct 24, 2002)

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 7a Syntax Analysis Elias Athanasopoulos

d(ν) = max{n N : ν dmn p n } N. p d(ν) (ν) = ρ.

Lecture 11 Context-Free Languages

Lecture Notes on Inductive Definitions

UNIT II REGULAR LANGUAGES

Transcription:

Exercises Text adapted from : Alessandro Artale, Free University of Bolzano Exercise: Grammar Rewriting Consider the following grammar for Boolean expressions: Bexp Bexp or Bterm Bterm Bterm Bterm and not ( Bexp ) id 1

Exercise: Grammar Rewriting Show the parse tree and the right-most derivation for the sentence not id or id and id Exercise: Grammar Rewriting Bexp Bexp or Bterm Bterm Bterm and id not id id 2

Exercise: Grammar Rewriting Bexp rm Bexp or Bterm rm Bexp or Bterm and rm Bexp or Bterm and id rm Bexp or and id rm Bexp or id and id rm Bterm or id and id rm or id and id rm not or id and id rm not id or id and id Exercise: Grammar Rewriting What is the precedence between the Boolean operators? Are they left- or rightassociative? 3

Exercise: Grammar Rewriting Precedence : not > and > or Left-associative : and, or Right-associative : not Exercise: Grammar Rewriting Rewrite the grammar in such a way that the operators and, or have the same precedence while the operator not has the highest precedence 4

Exercise: Grammar Rewriting Eliminate : Bexp Bexp or Bexp and not ( Bexp ) id Exercise: Grammar Rewriting Show the parse tree in the modified grammar for the sentence not id or id and id 5

Exercise: Grammar Rewriting Bexp Bexp and or id not id id Exercise: Grammar Rewriting What information is transmitted by the lexical analyzer to the parser when it recognizes an identifier and when it recognizes a boolean operator? 6

Exercise: Grammar Rewriting Identifier : id, pointer to symbol table Boolean symbol : relop, and, relop, or, etc. Exercise: Top-Down Parsing Define the notion of left-recursive grammar and explain why top down parsers cannot handle left-recursive grammars 7

Exercise: Top-Down Parsing Eliminate immediate left-recursion from the grammar Bexp Bexp or Bterm Bterm Bterm Bterm and not ( Bexp ) id Recall: Immediate Left- Recursion Elimination Method Rewrite every left-recursive production A A α A δ β γ into a right-recursive production: A β A R γ A R A R α A R δ A R ε 8

Exercise: Top-Down Parsing Unfold left recursion : not ( Bexp ) id Bterm Bterm R Bterm R and Bterm R ε Bexp Bterm Bexp R Bexp R or Bterm Bexp R ε Exercise: Top-Down Parsing Provide the parsing table for the LL(1) top down parser recognizing the grammar obtained from the previous point 9

Recall: FIRST() FIRST(α) = the set of terminals that begin all strings derived from α FIRST(a) = {a} if a T FIRST(ε) = {ε} FIRST(A) = A α FIRST(α) for A α P FIRST(X 1 X 2 X k ) : if for all j = 1,, i-1 : ε FIRST(X j ) then add FIRST(X i )\{ε} to FIRST(X 1 X 2 X k ) if for all j = 1,, k : ε FIRST(X j ) then add ε to FIRST(X 1 X 2 X k ) Exercise: Top-Down Parsing FIRST(a) = {a}, if a T = { and, or, not, (, } FIRST() = { not, (, id } FIRST(Bterm) = FIRST() = { not, (, id }, since cannot derive ε FIRST(Bterm R ) = { and, ε } FIRST(Bexp) = FIRST(Bterm) = { not, (, id }, since Bterm cannot derive ε FIRST(Bexp R ) = { or, ε } 10

Exercise: Top-Down Parsing A α FIRST(α) not not ( Bexp ) ( id id Bterm Bterm R not ( id Bterm R and Bterm R and Bterm R ε ε Bexp Bterm Bexp R not ( id Bexp R or Bterm Bexp R or Bexp R ε ε Recall: FOLLOW() FOLLOW(A) = the set of terminals that can immediately follow nonterminal A FOLLOW(A) = for all (B α A β) P do add FIRST(β)\{ε} to FOLLOW(A) for all (B α A β) P and ε FIRST(β) 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 S then add $ to FOLLOW(A) 11

Exercise: Top-Down Parsing FOLLOW(Bexp) = { $, ) } FOLLOW(Bexp R ) = FOLLOW(Bexp) = { $, ) } FOLLOW(Bterm) = FIRST(Bexp R )\{ε} FOLLOW(Bexp) = { or } { $, ) } = { or, $, ) } FOLLOW(Bterm R ) = FOLLOW(Bterm) = { or, $, ) } Exercise: Top-Down Parsing FOLLOW() = FIRST(Bterm R )\{ε} FOLLOW(Bterm) FOLLOW(Bterm) = { and } { or, $, ) } { or, $, ) } = { and, or, $, ) } 12

Exercise: Top-Down Parsing A FOLLOW(A) and or $ ) Bterm or $ ) Bterm R or $ ) Bexp $ ) Bexp R $ ) Recall: Constructing an LL(1) Predictive Parsing Table for each production A α do for each a FIRST(α) do add A α to M[A, a] enddo if ε FIRST(α) then for each b FOLLOW(A) do add A α to M[A, b] enddo endif enddo Mark each undefined entry in M error 13

Bterm Bterm R Bexp Bexp R not or and ( ) id $ not Bterm Bterm R Bexp Bterm Bexp R Bterm R ε Bexp R or Bterm Bexp R Bterm R and Bterm R ( Bexp ) Bterm Bterm R Bexp Bterm Bexp R Bterm R ε Bexp R ε id Bterm Bterm R Bexp Bterm Bexp R Bterm R ε Bexp R ε Exercise: Top-Down Parsing Provide the stack and the moves of the LL(1) parser on input sentence not id or id 14

Example Table-Driven Parsing Stack $ Bx $ Bx R Bt $ Bx R Bt R Bf $ Bx R Bt R Bf not $ Bx R Bt R Bf $ Bx R Bt R id $ Bx R Bt R $ Bx R Input not id or id $ not id or id $ not id or id $ not id or id $ id or id $ id or id $ or id $ or id $ Production applied Bx Bt Bx R Bt Bf Bt R Bf not Bf Bf id Bterm R ε Bx R or Bt Bx R Exercise: Bottom-Up Parsing [ ] 15

Exercise: Semantic Analysis Consider the SDD in the next slide, where newlabel() generates a fresh symbolic label newtemp() generates a fresh variable name gen() generates strings code is the attribute with three-address code id.place is the name of the variable associated to the token id relop.op is a comparison operator (<, <=, =, ) Exercise: Semantic Analysis Productions Prog S S S 1 ; S 2 S while Test do { S 1 } Semantic rules S.next = newlabel(); Prog.code = S.code gen(s.next : ) S 1.next = newlabel(); S 2.next = S.next; S.code = S 1.code gen(s 1.next : ) S 2.code Test.begin = newlabel(); Test.true = newlabel(); Test.false = S.next; S 1.next = Test.begin; S.code = gen(test.begin : ) Test.code gen(test.true : ) S 1.code gen( goto Test.begin) 16

Exercise: Semantic Analysis Productions S id = E Test id 1 relop id 2 E E 1 + id E id Semantic rules S.code = E.code gen(id.place = E.place) Test.code = gen( if id 1.place relop.op id 2.place goto Test.true) gen( goto Test.false) E.place = newtemp(); E.code = E 1.code gen(e.place = E 1.place + id.place) E.Place = id.place; E.code = Exercise: Semantic Analysis Consider the input while x < y do { y = y 1; x = x + 1 } 17

Exercise: Semantic Analysis Provide The annotated parse tree (without the code attribute) for the input together with the values of the attributes The three-address code produced by the semantic actions for the given input The semantic rules for the production S if Test then S 1 18