1 Week 5 Questions / Concerns What’s due: Lab2 part a due on Sunday Lab1 check-off by appointment Test#1 HW#5 next Thursday Coming up: Lab3 Posted. Discuss.

Slides:



Advertisements
Similar presentations
1 Week 9 Questions / Concerns Hand back Test#2 What’s due: Final Project due next Thursday June 5. Final Project check-off on Friday June 6 in class. Next.
Advertisements

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.
Lecture # 11 Grammar Problems.
Top-down Parsing By Georgi Boychev, Rafal Kala, Ildus Mukhametov.
Top-Down Parsing.
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 6: 9/6.
Prof. Bodik CS 164 Lecture 61 Building a Parser II CS164 3:30-5:00 TT 10 Evans.
1 Predictive parsing Recall the main idea of top-down parsing: Start at the root, grow towards leaves Pick a production and try to match input May need.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
1 The Parser Its job: –Check and verify syntax based on specified syntax rules –Report errors –Build IR Good news –the process can be automated.
Professor Yihjia Tsai Tamkang University
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
Top-Down Parsing.
MIT Top-Down Parsing Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology.
CPSC 388 – Compiler Design and Construction
1 Week 4 Questions / Concerns Comments about Lab1 What’s due: Lab1 check off this week (see schedule) Homework #3 due Wednesday (Define grammar for your.
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?
Top-Down Parsing - recursive descent - predictive parsing
1 Week 3 Questions / Concerns What’s due: Lab1b due Friday at midnight Lab1b check-off next week (schedule will be announced on Monday) Homework #2 due.
Chapter 5 Top-Down Parsing.
-Mandakinee Singh (11CS10026).  What is parsing? ◦ Discovering the derivation of a string: If one exists. ◦ Harder than generating strings.  Two major.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Parsing Lecture 5 Fri, Jan 28, Syntax Analysis The syntax of a language is described by a context-free grammar. Each grammar rule has the form A.
4 4 (c) parsing. Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces.
Left Recursion Lecture 7 Fri, Feb 4, 2005.
Top Down Parsing - Part I Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
6/4/2016IT 3271 The most practical Parsers: Predictive parser: 1.input (token string) 2.Stacks, parsing table 3.output (syntax tree, intermediate codes)
Syntax Analysis CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Languages and Grammars. A language is a set of strings. Example: The set of all valid C++ programs is a language. Each program consists of a string of.
Chapter 4 Top-Down Parsing Recursive-Descent Gang S. Liu College of Computer Science & Technology Harbin Engineering University.
Parsing Top-Down.
1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
More Parsing CPSC 388 Ellen Walker Hiram College.
Top-down Parsing Recursive Descent & LL(1) Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
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.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Top-Down Parsing.
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
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.
 Welcome Back!  Introduction  Webpage: ◦
Exercises on Chomsky Normal Form and CYK parsing
Lecture # 10 Grammar Problems. Problems with grammar Ambiguity Left Recursion Left Factoring Removal of Useless Symbols These can create problems for.
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.
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.
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
Context free grammars Terminals Nonterminals Start symbol productions
Lecture #12 Parsing Types.
Table-driven parsing Parsing performed by a finite state machine.
Parsing — Part II (Top-down parsing, left-recursion removal)
Compiler Construction
Parsing with Context Free Grammars
Top-Down Parsing.
Parsing Techniques.
3.2 Language and Grammar Left Factoring Unclear productions
Programming Language Syntax 6
Top-Down Parsing CS 671 January 29, 2008.
Lecture 7: Introduction to Parsing (Syntax Analysis)
Lecture 8: Top-Down Parsing
Programming Language Syntax 5
Compiler Construction
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

1 Week 5 Questions / Concerns What’s due: Lab2 part a due on Sunday Lab1 check-off by appointment Test#1 HW#5 next Thursday Coming up: Lab3 Posted. Discuss grading and homework Homework#4 Recursive Descent Parser Compute First and Follow

2 Revised & Expanded Grammar Example S -> id = E ; E -> E + T | E – T | T T ->T * F | T / F | F F -> ( E ) | id S (i) E ET T F = ; + * F T id (a) F i = a + b * c; id (b) id (c)

3 Sample Language This following language consists of statements in 2 types: Function definitions Function calls You don’t have to return values from a function as functions are evaluated to a result value. Every function can have up to 2 parameters. (defun add (x y) (+ x y) ) (add 10 20) (add 20 20) (set x 10) (negate x) (+ x 10) (print x) (input x) Can you define a grammar for this language?

4 Sample Language Grammar Program -> Statement Statements Statements -> Statement Statements | Statement -> FunctionDef | FunctionCall FunctionCall -> (ID Param Param ) | (OP Param Param ) | (BUILTIN Param Param) Param -> ID | Num | BUILTIN -> setx | negate | print OP -> + | - | * | / FunctionDef -> (defun ID (FParam FParam) Statements) FParam -> ID |

5 HW#4 Unit Production S-> aX | Yb X -> S  remove X-> S, don’t change anything else Y -> …. X-> aX | Yb Left Factoring S-> a… | a… | a… => S -> aX

6 HW#4 Left Recursion & Unit production Remove Left Recursion first If there are any unit productions left, take care of them afterwards S-> A | B | Sb | aSb | c After removal: X -> bX | S -> AX | BX | aSbX | cX A-> Ab | Bc | Sb | After removal: Y -> bY | A -> BcY | SbY | Y A -> BcY | SbY | bY | Unit production

7 Recursive Descent Parser A ->aAbb | bB | Bcd bool A() { if curToken == a //match aAbb if (A()) if (nextToken == b) return true; else if curToken == b //match bB if (B()) return true; else if (B()) //trying to match Bcd

8 Recursive Descent Parser A ->aAbb | bB | Bcd bool A() { if curToken == a //match aAbb if (A()) if (nextToken == b) return true; else if curToken == b //match bB if (B()) return true; else if (B()) what if B starts with a or b?

9 Recursive Descent Parser A ->aAbb | bB | Bcd bool A() { if curToken == a //match aAbb if (A()) if (nextToken == b) return true; else if curToken == b //match bB if (B()) return true; else if (B()) You don’t have to remove B because it’s not a unit production but you may need to backtrack here if B starts with a or b also

10 Recursive Descent Parser If possible, try to remove non-terminals as the first symbol even though they are not unit productions. It reduces some backtracking. Example: E -> TX E -> idYX | (E)YX T -> FY T -> idY | (E)Y F -> id | ( E )

11 Recursive Descent Parser (Rules without lambdas) The rule will return false if None of the first tokens from the rule matches the current token. S -> aSb | bS | cd The current token is not a, b or c The first token matches but rest of the rule fails The input string for above grammar is “cc”.

12 Recursive Descent Parser (Rules with lambdas) What if: None of the first tokens from the rule matches the current token. S -> aSb | bS | cd | The current token is not a, b or c. So the lambda means that S doesn’t really match any token, so just take lambda and return true. It lets the next rule to worry about the current token. What if the current token is wrong for the next rule? The first token matches but rest of the rule fails The input string for above grammar is “cc”. But lambda could still mean that “cc” is intended for the next rule and that S should just be lambda. We would have to see what the next rule needs if “cc” is wrong here.

13 Recursive Descent Parser The parser would be much more accurate and efficient if we know all the first tokens of a rule regardless if they start with a token or a non-terminal. S -> a S b | b B | C d The parser would handle lambdas more accurately if it knows what tokens should come after the rule if lambda is taken. S -> a S b aaac… S -> First set Follow set You only need Follow set if you have lambdas

14 LL(1) Another top-down parser It’s a table-driven parser. LL(1) L – first L, the input is from left to right L – second L, leftmost derivation (top-down) 1 – one token look ahead Grammar pre-req: No left recursion Unit productions are okay, but should minimize MUST left factor to ensure one-token look ahead. Procedure: Compute First and Follow sets from the grammar

15 First set Grammar with no lambda S -> A B S -> c A -> a bA A -> b B -> d B B -> e First set is just the first token. If the first symbol is a non-terminal, then include all the first tokens from that non-terminal.

16 First set S -> A B S -> c A -> a bA A -> b B -> d B B -> e S c, First(A) A a, b B d, e S c, a, b A a, b B d, e

17 First set What about ? S -> A B S -> c A -> a bA A -> b A -> B -> d B B -> e S c, First(A) - A a, b, B d, e S c, a, b A a, b, B d, e Only add to S if S itself also goes to as the result of using that rule

18 First set What about ? S -> A B S -> c A -> a bA A -> b A -> B -> d B B -> e S c, First(A)-, First(B)- A a, b, B d, e S c, a, b, d, e A a, b, B d, e Only add to S if S itself also goes to as the result of using that rule

19 First Set S -> ABc A -> a A -> B -> b B -> S A B

20 Follow set If any one grammar rule in your grammar has lambda productions, then you need to compute the follow set for the entire grammar. Follow set is just a set of tokens that follow a particular non-terminal. There are 4 different cases to consider in computing the Follow set. Not all 4 cases are applicable in every case.

21 Follow Set Case 1: If S is the start symbol, then add $ (end of input) to the Follow set of S. Nothing should follow S after S is done parsing. Case 2: If you have a token following a non-terminal on the right hand side of the rule, then add that token to the follow set of the non-terminal. … ->.. Ab - b follows A. Add b to the Follow set of A. Note: We don’t care what’s on the left hand side of the rule, just that b follows the non-terminal A

22 Follow Set Case 3: If you have a non-terminal following a non- terminal on the right hand side of the rule, then add that First set of the second non-terminal to the follow set of the first non-terminal. … ->.. AB - B follows A. Add First(B) - to the Follow set of A. Note: We don’t care what’s on the left hand side of the rule, just that B follows the non-terminal A. We don’t have in the Follow set.

23 Follow Set Case 4: If you have this following pattern: A -> ….. B This could be an unit production or just any rule that ends with a non-terminal. In this case, anything that follows A also follows B

24 Follow Set Case 4: If you have this following pattern: A -> ….. B A B Whatever follows A, also follows B because there is nothing else after B on the lower level of the parser tree

25 Follow set: Case 1 S -> A B c S -> c A -> a B A -> b B -> d B B -> e S $ A B

26 Follow set: Case 2 S -> A B c S -> c A -> a B A -> b B -> d B B -> e S $ A Bc

27 Follow set: Case 3 S -> A B c S -> c A -> a B A -> b B -> d B B -> e S $ A First(B) Bc S $ Ad, e B c

28 Follow set: Case 4 S -> A B c S -> c A -> a B A -> b B -> d B B -> e S $ Ad, e B c S $ A B c, d, e

29 Follow set: Case 1 S -> ABc A -> a A -> B -> b B -> S $ A B

30 Follow set: Case 2 S -> ABc A -> a A -> B -> b B -> S $ A Bc BUT, B could be a lambda

31 Follow set: Case 2 S -> ABc A -> a A -> B -> b B -> S $ A c Bc

32 Follow set: Case 3 S -> ABc A -> a A -> B -> b B -> S $ A c, First(B) Bc S $ A c, b Bc No in Follow set

33 Follow set: Case 4 S -> ABc A -> a A -> B -> b B -> S $ A c, b Bc No case 4 for this grammar

34 In-Class Exercise #7 Please compute first and follow sets for the following grammar: S -> ABCd A -> e | f | B -> g | h | C -> p | q