Compiler Principles Winter 2012-2013 Compiler Principles Exercises on scanning & top-down parsing Roman Manevich Ben-Gurion University.

Slides:



Advertisements
Similar presentations
Compiler Principles Fall Compiler Principles Lecture 4: Parsing part 3 Roman Manevich Ben-Gurion University.
Advertisements

Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.
Chap. 5, Top-Down Parsing J. H. Wang Mar. 29, 2011.
YANGYANG 1 Chap 5 LL(1) Parsing LL(1) left-to-right scanning leftmost derivation 1-token lookahead parser generator: Parsing becomes the easiest! Modifying.
Mooly Sagiv and Roman Manevich School of Computer Science
LESSON 18.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
Top-Down Parsing.
1 Contents Introduction A Simple Compiler Scanning – Theory and Practice Grammars and Parsing LL(1) Parsing LR Parsing Lex and yacc Semantic Processing.
Parsing III (Eliminating left recursion, recursive descent parsing)
COS 320 Compilers David Walker. last time context free grammars (Appel 3.1) –terminals, non-terminals, rules –derivations & parse trees –ambiguous grammars.
CS 310 – Fall 2006 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho,
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
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.
Professor Yihjia Tsai Tamkang University
COS 320 Compilers David Walker. last time context free grammars (Appel 3.1) –terminals, non-terminals, rules –derivations & parse trees –ambiguous grammars.
Compiler construction in4020 – lecture 3 Koen Langendoen Delft University of Technology The Netherlands.
CPSC 388 – Compiler Design and Construction
Languages & Strings String Operations Language Definitions.
Top-Down Parsing - recursive descent - predictive parsing
1 Chapter 5 LL (1) Grammars and Parsers. 2 Naming of parsing techniques The way to parse token sequence L: Leftmost R: Righmost Top-down  LL Bottom-up.
Chapter 5 Top-Down Parsing.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
LANGUAGE TRANSLATORS: WEEK 3 LECTURE: Grammar Theory Introduction to Parsing Parser - Generators TUTORIAL: Questions on grammar theory WEEKLY WORK: Read.
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
# 1 CMPS 450 Parsing CMPS 450 J. Moloney. # 2 CMPS 450 Check that input is well-formed Build a parse tree or similar representation of input Recursive.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Parsing Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 3.
CS 153 A little bit about LR Parsing. Background We’ve seen three ways to write parsers:  By hand, typically recursive descent  Using parsing combinators.
TDDD55- Compilers and Interpreters Lesson 1 Zeinab Ganjei Department of Computer and Information Science Linköping University.
Left Recursion Lecture 7 Fri, Feb 4, 2005.
6/4/2016IT 3271 The most practical Parsers: Predictive parser: 1.input (token string) 2.Stacks, parsing table 3.output (syntax tree, intermediate codes)
Exercise 1 A ::= B EOF B ::=  | B B | (B) Tokens: EOF, (, ) Generate constraints and compute nullable and first for this grammar. Check whether first.
Compiler Principles Fall Compiler Principles Lecture 3: Parsing part 2 Roman Manevich Ben-Gurion University.
1 Section 12.3 Context-Free Parsing We know (via a theorem) that the context-free languages are exactly those languages that are accepted by PDAs. When.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
4. Formal Grammars and Parsing and Top-down Parsing Chih-Hung Wang Compilers References 1. C. N. Fischer, R. K. Cytron and R. J. LeBlanc. Crafting a Compiler.
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
Parsing Top-Down.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
TOP-DOWN PARSING Recursive-Descent, Predictive Parsing.
Bc. Jozef Lang (xlangj01) Bc. Zoltán Zemko (xzemko01) Increasing power of LL(k) parsers.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Top-Down Parsing.
Syntax Analyzer (Parser)
Chapter 3 Chang Chi-Chung
Top-Down Predictive Parsing We will look at two different ways to implement a non- backtracking top-down parser called a predictive parser. A predictive.
Parsing methods: –Top-down parsing –Bottom-up parsing –Universal.
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Exercises on Chomsky Normal Form and CYK parsing
Compiler Syntactic Analysis r Two general classes of parsing techniques m Bottom-up (Operator-Precedence parsing) Begin with the terminal nodes.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Top-down parsing 1. Last Time CYK – Step 1: get a grammar in Chomsky Normal Form – Step 2: Build all possible parse trees bottom-up Start with runs of.
Spring 16 CSCI 4430, A Milanova 1 Announcements HW1 due on Monday February 8 th Name and date your submission Submit electronically in Homework Server.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
Eliminating Left-Recursion Where some of a nonterminal’s productions are left-recursive, top-down parsing is not possible “Immediate” left-recursion can.
Lecture 7 Syntax Analysis (5) Operator-Precedence Parsing
Programming Languages Translator
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 5, 09/25/2003 Prof. Roy Levow.
Parsing with Context Free Grammars
Fall Compiler Principles Lecture 4: Parsing part 3
Top-Down Parsing.
Programming Language Syntax 6
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
Compiler Design 7. Top-Down Table-Driven Parsing
Predictive Parsing Lecture 9 Wed, Feb 9, 2005.
Chapter Fifteen: Stack Machine Applications
Ben-Gurion University
Presentation transcript:

Compiler Principles Winter Compiler Principles Exercises on scanning & top-down parsing Roman Manevich Ben-Gurion University

Scanning question 1 Write a regular expression for positive integers where every three consecutive digits, starting from the right, are separated by a _ – Examples: 23 1_000 2_784_153 – Negative examples: 1_0 1_5422 2

Scanning question 1 Write a regular expression for positive integers where every three consecutive digits, starting from the right, are separated by a _ – Examples: 23 1_000 2_784_153 – Negative examples: 1_0 1_5422 Answer: D = 0 | 1 | 2 … | 9 Small = D | DD | DDD Three = DDD R = Small (_Three)* 3

Parsing question 1 Consider the grammar below S  ( L ) | a L  L, S | S 1.show a parse tree for each of the following 1.(a, a) 2.(a, (a, a)) 4

Answer 5 a,a() SS L L S a, ( S L S )L

Parsing question 2 Consider the grammar G L  L, a | a – What is the language generated by G? – Eliminate the left-recursion in G – Write a non-left-recursive version without ε productions – Construct an LL(1) parser and run it on a, a, a 6

Parsing question 2 Consider the grammar G L  L, a | a – What is the language generated by G? – Eliminate the left-recursion in G – Write a non-left-recursive version with ε productions – Is the resulting grammar LL(1)? Answer – The language given by the regular expression (a,)* a – L  a M M , a M | ε – L  a | a M M , a M |, a – The grammar contains FIRST-FIRST conflicts for both L and M and therefore it is not LL(1) 7

Parsing question 3 Consider the grammar G1 below X  P P P  a P  b Q: Is it ambiguous? 8

Parsing question 3 Consider the grammar G1 below X  P P P  a P  b Q: Is it ambiguous? A: since the only non-terminal with alternatives is P and the first of its two alternatives ({a} and {b}) the grammar is LL(1). All LL(1) grammars are unambiguous 9

Parsing question 4 Consider the following grammar E  a ( S ) w t E  a ( S ) w b S  S ; c | c – Construct an LL(1) parser, applying any transformations necessary if the grammar is not in LL(1) – Hint: move the ) down to help transformations – Apply the parser to the input a (c; c) w b Note: the following solution is over-complicated since there is an implicit constraint – no epsilon productions are allowed. Otherwise the solution is pretty simple. 10

Answer The grammar E  a ( S ) w t E  a ( S ) w b S  S ; c | c will have a FIRST-FIRST conflict for E, since the first two rules start with ‘a ( S ) w’. We therefore apply left-factoring and obtain E  a ( S ) E’ E’  w b | w t S  S ; c | c 11

Answer The grammar E  a ( S ) E’ E’  w b | w t S  S ; c | c Is left-recursive on S so we eliminate the left- recursion and obtain the grammar E  a ( S ) E’ E’  w b | w t S  c | c ; S 12

Answer Let’s compute FIRST sets for E  a ( S ) E’ E’  w b | w t S  c | c ; S FIRST( S  c) = FIRST(c ; S) = {c} FIRST(w b)=FIRST(w t) = {w} so there are FIRST-FIRST conflict. We apply left-factoring to both E’ and S and obtain E  a ( S ) E ’ E’  w E ’’ E ’’  b | t S  c S’ S’  ε | ; S Now if we apply substitution for S’ we will just get back to a left- recursive grammar. Time to use the hint 13

Answer Starting from E  a ( S ) E ’ E’  w E ’’ E ’’  b | t S  c | c ; S Let’s move ) from E to S E  a ( S E’ E’  w E ’’ E ’’  b | t S  c ) | c ; S Now let’s apply left-factoring to S and obtain: E  a ( S E’ E’  w E ’’ E ’’  b | t S  c S’ S’  ) | ; S 14

Answer Let’s compute FIRST sets for (1) E  a ( S E ’ (2) E’  w E ’’ (3) E ’’  b (4) E ’’  t (5) S  c S ’ (6) S ’  ) (7) S ’  ; S FIRST(E) = {a} FIRST(E’) = {w} FIRST(E’’) = {b, t} FIRST( S ) = {c} FIRST( S ’) = {), ;} There are no conflicts so the grammar is in LL(1) 15

Parsing table ;)ctbw(a 1E 2E’ 43E’’ 5S 76S’ 16

Running on a ( c; c ) w b 17 Input suffixStack contentMove a ( c; c ) w b $E$ predict(E, a) = E  a ( S E ’ a ( c; c ) w b $ a ( S E ’ $ match(a, a) ( c; c ) w b $ ( S E ’ $ match( ‘(‘, ‘(‘) c; c ) w b $ S E ’ $ predict(S, c) = S  c S ’ c; c ) w b $ c S ’ E ’ $ match(c, c) ; c ) w b $ S ’ E ’ $ predict(S’, ;) = S ’  ; S ; c ) w b $ ; S E ’ $ match( ‘(;, ‘;‘) c ) w b $ S E ’ $ predict(S, c) = S  c S ’ c ) w b $ c S’ E ’ $ match(c, c) ) w b $ S’ E ’ $ predict(S’, ‘)’) = S ’  ) ) w b $ ) E’ $match( ‘)‘, ‘)‘) w b $E’ $ predict(E’, w) = E’  w E ’’ w b $ w E ’’ $ match(w, w) b $E’’ $ predict(E’’, b) = E ’’  b b $ match(b, b) $$

Running on a ( c; c ) w b 18 Input suffixStack contentMove a ( c; c ) w b $E$ predict(E, a) = E  a ( S E ’ a ( c; c ) w b $ a ( S E ’ $ match(a, a) ( c; c ) w b $ ( S E ’ $ match( ‘(‘, ‘(‘) c; c ) w b $ S E ’ $ predict(S, c) = S  c S ’ c; c ) w b $ c S ’ E ’ $ match(c, c) ; c ) w b $ S ’ E ’ $ predict(S’, ;) = S ’  ; S ; c ) w b $ ; S E ’ $ match( ‘(;, ‘;‘) c ) w b $ S E ’ $ predict(S, c) = S  c S ’ c ) w b $ c S’ E ’ $ match(c, c) ) w b $ S’ E ’ $ predict(S’, ‘)’) = S ’  ) ) w b $ ) E’ $match( ‘)‘, ‘)‘) w b $E’ $ predict(E’, w) = E’  w E ’’ w b $ w E ’’ $ match(w, w) b $E’’ $ predict(E’’, b) = E ’’  b b $ match(b, b) $$ Since the input has been read and the stack is empty – the parsing was successful and the input is accepted