Ben-Gurion University

Slides:



Advertisements
Similar presentations
Parsing 4 Dr William Harrison Fall 2008
Advertisements

Compiler Construction
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.
CPSC Compiler Tutorial 4 Midterm Review. Deterministic Finite Automata (DFA) Q: finite set of states Σ: finite set of “letters” (input alphabet)
Compiler construction in4020 – lecture 4 Koen Langendoen Delft University of Technology The Netherlands.
Compiler Principles Fall Compiler Principles Lecture 4: Parsing part 3 Roman Manevich Ben-Gurion University.
Bottom up Parsing Bottom up parsing trys to transform the input string into the start symbol. Moves through a sequence of sentential forms (sequence of.
Mooly Sagiv and Roman Manevich School of Computer Science
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 — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Compiler Principles Winter Compiler Principles Exercises on scanning & top-down parsing Roman Manevich Ben-Gurion University.
Languages & Strings String Operations Language Definitions.
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.
CS 461 – Oct. 7 Applications of CFLs: Compiling Scanning vs. parsing Expression grammars –Associativity –Precedence Programming language (handout)
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Parsing Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 3.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
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.
Exercise 1 A ::= B EOF B ::=  | B B | (B) Tokens: EOF, (, ) Generate constraints and compute nullable and first for this grammar. Check whether first.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
Parsing Top-Down.
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
Bc. Jozef Lang (xlangj01) Bc. Zoltán Zemko (xzemko01) Increasing power of LL(k) parsers.
Top-Down Parsing.
Exercises on Chomsky Normal Form and CYK parsing
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.
Operator precedence parser Lecturer: Noor Dhia
CS 614: Theory and Construction of Compilers Lecture 4 Fall 2002 Department of Computer Science University of Alabama Joel Jones.
Lecture 7 Syntax Analysis (5) Operator-Precedence Parsing
CSE 3302 Programming Languages
5. Context-Free Grammars and Languages
Parsing — Part II (Top-down parsing, left-recursion removal)
CS 326 Programming Languages, Concepts and Implementation
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
CS510 Compiler Lecture 4.
50/50 rule You need to get 50% from tests, AND
Lecture #12 Parsing Types.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 5, 09/25/2003 Prof. Roy Levow.
Parsing IV Bottom-up Parsing
Parsing — Part II (Top-down parsing, left-recursion removal)
Compiler Construction
Parsing with Context Free Grammars
Fall Compiler Principles Lecture 4: Parsing part 3
Parsing Techniques.
CS 3304 Comparative Languages
Top-Down Parsing.
Programming Language Syntax 6
Programming Language Syntax 7
CPSC 388 – Compiler Design and Construction
CS 540 George Mason University
Programming Language Syntax 2
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
5. Context-Free Grammars and Languages
Compiler Design 7. Top-Down Table-Driven Parsing
R.Rajkumar Asst.Professor CSE
Programming Language Syntax 5
Predictive Parsing Lecture 9 Wed, Feb 9, 2005.
Parsing — Part II (Top-down parsing, left-recursion removal)
5. Bottom-Up Parsing Chih-Hung Wang
Kanat Bolazar February 16, 2010
Chapter Fifteen: Stack Machine Applications
BNF 9-Apr-19.
Nonrecursive Predictive Parsing
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
Presentation transcript:

Ben-Gurion University Winter 2012-2013 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

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)*

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

Answer S ( L ) S L L L S S S a , ( a , a )

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

Parsing question 2 Consider the grammar G L  L , a | a Answer 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)

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

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

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.

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

Answer (with bugs) 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

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

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

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)

Parsing table ; ) c t b w ( a 1 E 2 E’ 4 3 E’’ 5 S 7 6 S’

Running on a ( c; c ) w b Input suffix Stack content Move predict(E, a) = E  a ( S E’ 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 S’ E’ $ match(c, c) ; c ) w b $ S’ E’ $ predict(S’, ;) = S’  ; S ; S E’ $ match( ‘(;, ‘;‘) c ) w b $ ) w b $ predict(S’, ‘)’) = S’  ) ) E’ $ match( ‘)‘, ‘)‘) w b $ E’ $ predict(E’, w) = E’  w E’’ w E’’ $ match(w, w) b $ E’’ $ predict(E’’, b) = E’’  b match(b, b) $

Running on a ( c; c ) w b Input suffix Stack content Move a ( c; c ) w b$ E$ predict(E, a) = E  a ( S E’ 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 S’ E’ $ match(c, c) ; c ) w b $ S’ E’ $ predict(S’, ;) = S’  ; S ; S E’ $ match( ‘(;, ‘;‘) c ) w b $ ) w b $ predict(S’, ‘)’) = S’  ) ) E’ $ match( ‘)‘, ‘)‘) w b $ E’ $ predict(E’, w) = E’  w E’’ w E’’ $ match(w, w) b $ E’’ $ predict(E’’, b) = E’’  b match(b, b) $ Since the input has been read and the stack is empty – the parsing was successful and the input is accepted