# Extra Credit Question

Size: px
Start display at page:

Transcription

1 Top-Down Parsing #1

2 Extra Credit Question Given this grammar G: E E+T E T T T * int T int T (E) Is the string int * (int + int) in L(G)? Give a derivation or prove that it is not. #2

3 Revenge of Theory How do we tell if DFA P is equal to DFA Q? We can do: is DFA P empty? How? We can do: P := not Q How? We can do: P := Q intersect R How? So do: is P intersect not Q empty? Does this work for CFG X and CFG Y? Can we tell if s is in CFG X? #3

4 Outline Recursive Descent Parsing Left Recursion LL(1) Parsing LL(1) Parsing Tables LP(1) Parsing Algorithm Constructing LL(1) Parsing Tables First, Follow #4

5 In One Slide An LL(1) parser reads tokens from left to right and constructs a top-down leftmost derivation. LL(1) parsing is a special case of recursive descent parsing in which you can predict which single production to use from one token of lookahead. LL(1) parsing is fast and easy, but it does not work if the grammar is ambiguous, left-recursive, or not left-factored (i.e., it does not work for most programming languages). #5

6 Intro to Top-Down Parsing Terminals are seen in order of appearance in the token stream: t1 t2 t3 t4 t5 The parse tree is constructed From the top From left to right A t1 B C t2 t4 D t3 t4 #6

7 Recursive Descent Parsing We ll try recursive descent parsing first Try all productions exhaustively, backtrack Consider the grammar E T+E T T ( E ) int int * T Token stream is: int * int Start with top-level non-terminal E Try the rules for E in order #7

8 Recursive Descent Example Try E0 T1 + E2 E T+E T T ( E ) int int * T Input = int * int Then try a rule for T1 ( E3 ) But ( does not match input token int Try T1 int. Token matches. But + after T1 does not match input token * Try T1 int * T2 This will match but + after T1 will be unmatched Have exhausted the choices for T1 Backtrack to choice for E0 #8

9 Recursive Descent Example (2) E T+E T T ( E ) int int * T Input = int * int Try E0 T1 Follow same steps as before for T1 And succeed with T1 int * T2 and T2 int With the following parse tree E0 T1 int * T2 int #9

10 Recursive Descent Parsing Parsing: given a string of tokens t1 t2... tn, find its parse tree Recursive descent parsing: Try all the productions exhaustively At a given moment the fringe of the parse tree is: t1 t2 tk A Try all the productions for A: if A! BC is a production, the new fringe is t1 t2 tk B C Backtrack when the fringe doesn t match the string Stop when there are no more non-terminals #10

11 When Recursive Descent Does Not Work Consider a production S S a: In the process of parsing S we try the above rule What goes wrong? A left-recursive grammar has S + S for some Recursive descent does not work in such cases It goes into an 1 loop #11

12 What's Wrong With That Picture? #12

13 Elimination of Left Recursion Consider the left-recursive grammar S S S generates all strings starting with a and followed by a number of Can rewrite using right-recursion S T T T #13

14 Example of Eliminating Left Recursion Consider the grammar S!1 S0 ( = 1 and = 0 ) It can be rewritten as S!1T T!0T #14

15 More Left Recursion Elimination In general S S 1 S n 1 m All strings derived from S start with one of 1,, m and continue with several instances of 1,, n Rewrite as S 1 T m T T 1 T n T #15

16 General Left Recursion The grammar S A A S is also left-recursive because S + S This left-recursion can also be eliminated See book, Section 2.3 Detecting and eliminating left recursion are popular test questions #16

17 Summary of Recursive Descent Simple and general parsing strategy Left-recursion must be eliminated first but that can be done automatically Unpopular because of backtracking Thought to be too inefficient (repetition) We can avoid backtracking Sometimes... #17

18 Predictive Parsers Like recursive descent but parser can predict which production to use By looking at the next few tokens No backtracking Predictive parsers accept LL(k) grammars First L means left-to-right scan of input Second L means leftmost derivation The k means predict based on k tokens of lookahead In practice, LL(1) is used #18

19 Sometimes Things Are Perfect The.ml-lex format you emit in PA2 Will be the input for PA3 actually the reference.ml-lex will be used It can be parsed with no lookahead You always know just what to do next Ditto with the.ml-ast output of PA3 Just write a few mutually-recursive functions They read in the input, one line at a time #19

20 LL(1) In recursive descent, for each non-terminal and input token there may be a choice of which production to use LL(1) means that for each non-terminal and token there is only one production that could lead to success Can be specified as a 2D table One dimension for current non-terminal to expand One dimension for next token Each table entry contains one production #20

21 Predictive Parsing and Left Factoring Recall the grammar E T+E T T int int * T ( E ) Impossible to predict because For T two productions start with int For E it is not clear how to predict A grammar must be left-factored before use for predictive parsing #21

22 Left-Factoring Example Recall the grammar E T+E T T int int * T ( E ) Factor out common prefixes of productions E T X X + E T ( E ) Y * T #22

23 Introducing: Parse Tables #23

24 LL(1) Parsing Table Example Left-factored grammar E T ( E ) X +E Y *T The LL(1) parsing table (\$ is a special end marker): T E X Y int * + +E *T ( (E) ) \$ #24

25 LL(1) Parsing Table Example Analysis Consider the [E, int] entry When current non-terminal is E and next input is int, use production E T X This production can generate an int in the first position T E X Y int * + +E *T ( (E) ) \$ #25

26 LL(1) Parsing Table Example Analysis Consider the [Y,+] entry When current non-terminal is Y and current token is +, get rid of Y We ll see later why this is so T E X Y int * + +E *T ( (E) ) \$ #26

27 LL(1) Parsing Tables: Errors Blank entries indicate error situations Consider the [E,*] entry There is no way to derive a string starting with * from non-terminal E T E X Y int * + +E *T ( (E) ) \$ #27

28 Using Parsing Tables Method similar to recursive descent, except For each non-terminal S We look at the next token a And choose the production shown at [S,a] We use a stack to keep track of pending nonterminals We reject when we encounter an error state We accept when we encounter end-of-input #28

29 LL(1) Parsing Algorithm initialize stack = <S \$> next = (pointer to tokens) repeat match stack with <X, rest>: if T[X,*next] = Y1 Yn then stack <Y1 Yn rest> else error () <t, rest>: if t == *next ++ then stack <rest> else error () until stack == < > #29

30 Stack Input int * + Action ( ) \$ +E T (E) E X Y *T #30

31 Stack E\$ Input int * int \$ int * + Action ( ) \$ +E T (E) E X Y *T #31

32 Stack E\$ \$ Input int * int \$ int * int \$ int * + Action ( ) \$ +E T (E) E X Y *T #32

33 Stack E\$ \$ X \$ int Input int * int \$ int * int \$ int * int \$ * + Action terminal ( ) \$ +E T (E) E X Y *T #33

34 Stack E\$ \$ X \$ YX\$ int Input int * int \$ int * int \$ int * int \$ * int \$ * + Action terminal *T ( ) \$ +E T (E) E X Y *T #34

35 Stack E\$ \$ X \$ YX\$ *\$ int Input int * int \$ int * int \$ int * int \$ * int \$ * int \$ * + Action terminal *T terminal ( ) \$ +E T (E) E X Y *T #35

36 Stack E\$ \$ X \$ YX\$ *\$ \$ int Input int * int \$ int * int \$ int * int \$ * int \$ * int \$ int \$ * + Action terminal *T terminal ( ) \$ +E T (E) E X Y *T #36

37 Stack E\$ \$ X \$ YX\$ *\$ \$ X \$ int Input int * int \$ int * int \$ int * int \$ * int \$ * int \$ int \$ int \$ * + Action terminal *T terminal terminal ( ) \$ +E T (E) E X Y *T #37

38 Stack E\$ \$ X \$ YX\$ *\$ \$ X \$ YX\$ int Input int * int \$ int * int \$ int * int \$ * int \$ * int \$ int \$ int \$ \$ * + Action terminal *T terminal terminal ( ) \$ +E T (E) E X Y *T #38

39 Stack E\$ \$ X \$ YX\$ *\$ \$ X \$ YX\$ X\$ int Input int * int \$ int * int \$ int * int \$ * int \$ * int \$ int \$ int \$ \$ \$ * + Action terminal *T terminal terminal ( ) \$ +E T (E) E X Y *T #39

40 Stack E\$ \$ X \$ YX\$ *\$ \$ X \$ YX\$ X\$ \$ int Input int * int \$ int * int \$ int * int \$ * int \$ * int \$ int \$ int \$ \$ \$ \$ * + Action terminal *T terminal terminal ACCEPT ( ) \$ +E T (E) E X Y *T #40

41 LL(1) Languages LL(1) languages can be LL(1) parsed A language Q is LL(1) if there exists an LL(1) table such the LL(1) parsing algorithm using that table accepts exactly the strings in Q No table entry can be multiply defined Once we have the table The parsing algorithm is simple and fast No backtracking is necessary Want to generate parsing tables from CFG! #41

42 Q: Movies (263 / 842) This 1982 Star Trek film features Spock nerve-pinching McCoy, Kirstie Alley "losing" the Kobayashi Maru, and Chekov being mind-controlled by a slug-like alien. Ricardo Montalban is "is intelligent, but not experienced. His pattern indicates two-dimensional thinking."

43 Q: Music (238 / 842) For two of the following four lines from the 1976 Eagles song Hotel California, give enough words to complete the rhyme. So I called up the captain / "please bring me my wine" Mirrors on the ceiling / pink champagne on ice And in the master's chambers / they gathered for the feast We are programmed to receive / you can checkout any time you like,

44 Q: Books (727 / 842) Name 5 of the 9 major characters in A. A. Milne's 1926 books about a "bear of very little brain" who composes poetry and eats honey.

45 Top-Down Parsing. Review Top-down parsing expands a parse tree from the start symbol to the leaves Always expand the leftmost non-terminal E T int * + E int + int #45

46 Top-Down Parsing. Review Top-down parsing expands a parse tree from the start symbol to the leaves Always expand the leftmost non-terminal E T int * + E T The leaves at any point form a string A contains only terminals The input string is b The prefix matches int * int + int The next token is b #46

47 Top-Down Parsing. Review Top-down parsing expands a parse tree from the start symbol to the leaves Always expand the leftmost non-terminal E T int * + T E T int int * int + int The leaves at any point form a string A contains only terminals The input string is b The prefix matches The next token is b #47

48 Top-Down Parsing. Review Top-down parsing expands a parse tree from the start symbol to the leaves Always expand the leftmost non-terminal E T int * int * + E T T int int int + int The leaves at any point form a string A contains only terminals The input string is b The prefix matches The next token is b #48

49 Constructing Predictive Parsing Tables Consider the state S!* A With b the next token Trying to match b There are two possibilities: b belongs to an expansion of A Any A! can be used if b can start a string derived from In this case we say that b 2 First( ) Or #49

50 Constructing Predictive Parsing Tables b does not belong to an expansion of A The expansion of A is empty and b belongs to an expansion of (e.g., b ) Means that b can appear after A in a derivation of the form S!* Ab We say that b 2 Follow(A) in this case What productions can we use in this case? Any A! can be used if can expand to We say that 2 First(A) in this case #50

51 Computing First Sets Definition First(X) = { b X * b } { X * } First(b) = { b } For all productions X! A1 An Add First(A1) {} to First(X). Stop if First(A1) Add First(A2) {} to First(X). Stop if First(A2) Add First(An) {} to First(X). Stop if First(An) Add to First(X) (ignore Ai if it is X) #51

52 Example First Set Computation Recall the grammar E T ( E ) X +E Y *T First sets First( ( ) = { ( } First( First( ) ) = { ) } First( First( int) = { int } First( First( + ) = { + } First( First( * ) = { * } T ) = {int, ( } E ) = {int, ( } X ) = {+, } Y ) = {*, } #52

53 Computing Follow Sets Definition Follow(X) = { b S * X b } Compute the First sets for all non-terminals first Add \$ to Follow(S) (if S is the start non-terminal) For all productions Y! X A1 An Add First(A1) {} to Follow(X). Stop if First(A1) Add First(A2) {} to Follow(X). Stop if First(A2) Add First(An) {} to Follow(X). Stop if First(An) Add Follow(Y) to Follow(X) #53

54 Example Follow Set Computation Recall the grammar E T ( E ) X +E Y *T Follow sets Follow( + ) = { int, ( } Follow( Follow( ( ) = { int, ( } Follow( Follow( X ) = {\$, ) } Follow( Follow( ) ) = {+, ), \$} Follow( Follow( int) = {*, +, ), \$} * ) = { int, ( } E ) = {), \$} T ) = {+, ), \$} Y ) = {+, ), \$} #54

55 Constructing LL(1) Parsing Tables Here is how to construct a parsing table T for context-free grammar G For each production A in G do: For each terminal b First( ) do T[A, b] = If! *, for each b Follow(A) do T[A, b] = #55

56 LL(1) Table Construction Example Recall the grammar E T ( E ) X +E Y *T Where in the row of Y do we put Y! * T? In the columns of First( *T ) = { * } T E X Y int * + +E *T ( (E) ) \$ #56

57 LL(1) Table Construction Example Recall the grammar E T ( E ) X +E Y *T Where in the row of Y we put Y!? In the columns of Follow(Y) = { \$, +, ) } T E X Y int * + +E *T ( (E) ) \$ #57

58 Avoid Multiple Definitions! #58

59 Notes on LL(1) Parsing Tables If any entry is multiply defined then G is not LL(1) If G is ambiguous If G is left recursive If G is not left-factored And in other cases as well Most programming language grammars are not LL(1) (e.g., Java, Ruby, C++, OCaml, Cool, Perl,...) There are tools that build LL(1) tables #59

60 Simple Parsing Strategies Recursive Descent Parsing But backtracking is too annoying, etc. Predictive Parsing, aka. LL(k) Predict production from k tokens of lookahead Build LL(1) table Parsing using the table is fast and easy But many grammars are not LL(1) (or even LL(k)) Next: a more powerful parsing strategy for grammars that are not LL(1) #60

61 Homework WA1 (written homework) due Turn in to drop-box. PA2 (Lexer) due You may work in pairs. Keep up with the reading... #61

### Building a Parser III. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

Building a Parser III CS164 3:30-5:00 TT 10 Evans 1 Overview Finish recursive descent parser when it breaks down and how to fix it eliminating left recursion reordering productions Predictive parsers (aka

### Administrativia. WA1 due on Thu PA2 in a week. Building a Parser III. Slides on the web site. CS164 3:30-5:00 TT 10 Evans.

Administrativia Building a Parser III CS164 3:30-5:00 10 vans WA1 due on hu PA2 in a week Slides on the web site I do my best to have slides ready and posted by the end of the preceding logical day yesterday,

### Abstract Syntax Trees & Top-Down Parsing

Review of Parsing Abstract Syntax Trees & Top-Down Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree

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

Ambiguity, Precedence, Associativity & Top-Down Parsing Lecture 9-10 (From slides by G. Necula & R. Bodik) 9/18/06 Prof. Hilfinger CS164 Lecture 9 1 Administrivia Please let me know if there are continued

### Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing Review of Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree

### Abstract Syntax Trees & Top-Down Parsing

Review of Parsing Abstract Syntax Trees & Top-Down Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree

### CS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)

CS1622 Lecture 9 Parsing (4) CS 1622 Lecture 9 1 Today Example of a recursive descent parser Predictive & LL(1) parsers Building parse tables CS 1622 Lecture 9 2 A Recursive Descent Parser. Preliminaries

### Compilers. Predictive Parsing. Alex Aiken

Compilers Like recursive-descent but parser can predict which production to use By looking at the next fewtokens No backtracking Predictive parsers accept LL(k) grammars L means left-to-right scan of input

### 8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing

8 Parsing Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces strings A parser constructs a parse tree for a string

### Ambiguity. Grammar E E + E E * E ( E ) int. The string int * int + int has two parse trees. * int

Administrivia Ambiguity, Precedence, Associativity & op-down Parsing eam assignments this evening for all those not listed as having one. HW#3 is now available, due next uesday morning (Monday is a holiday).

### Top down vs. bottom up parsing

Parsing A grammar describes the strings that are syntactically legal A recogniser simply accepts or rejects strings A generator produces sentences in the language described by the grammar A parser constructs

### Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

Top-Down Parsing and Intro to Bottom-Up Parsing Lecture 7 1 Predictive Parsers Like recursive-descent but parser can predict which production to use Predictive parsers are never wrong Always able to guess

### Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

Top-Down Parsing and Intro to Bottom-Up Parsing Lecture 7 1 Predictive Parsers Like recursive-descent but parser can predict which production to use Predictive parsers are never wrong Always able to guess

### LL Parsing: A piece of cake after LR

LL Parsing: A piece of cake after LR Lecture 11 Dr. Sean Peisert ECS 142 Spring 2009 1 LL Parsing Still specified using a CFG Still reads left-to-right (Lx) Now is leftmost derivation (xl) rather than

### Note that for recursive descent to work, if A ::= B1 B2 is a grammar rule we need First k (B1) disjoint from First k (B2).

LL(k) Grammars We need a bunch of terminology. For any terminal string a we write First k (a) is the prefix of a of length k (or all of a if its length is less than k) For any string g of terminal and

### 4 (c) parsing. Parsing. Top down vs. bo5om up parsing

4 (c) parsing Parsing A grammar describes syntac2cally legal strings in a language A recogniser simply accepts or rejects strings A generator produces strings A parser constructs a parse tree for a string

### LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

Predictive Parsers LL(k) Parsing Can we avoid backtracking? es, if for a given input symbol and given nonterminal, we can choose the alternative appropriately. his is possible if the first terminal of

### Building a Parser II. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

Building a Parser II CS164 3:30-5:00 TT 10 Evans 1 Grammars Programming language constructs have recursive structure. which is why our hand-written parser had this structure, too An expression is either:

### Types of parsing. CMSC 430 Lecture 4, Page 1

Types of parsing Top-down parsers start at the root of derivation tree and fill in picks a production and tries to match the input may require backtracking some grammars are backtrack-free (predictive)

### Administrativia. PA2 assigned today. WA1 assigned today. Building a Parser II. CS164 3:30-5:00 TT 10 Evans. First midterm. Grammars.

Administrativia Building a Parser II CS164 3:30-5:00 TT 10 Evans PA2 assigned today due in 12 days WA1 assigned today due in a week it s a practice for the exam First midterm Oct 5 will contain some project-inspired

### More Bottom-Up Parsing

More Bottom-Up Parsing Lecture 7 Dr. Sean Peisert ECS 142 Spring 2009 1 Status Project 1 Back By Wednesday (ish) savior lexer in ~cs142/s09/bin Project 2 Due Friday, Apr. 24, 11:55pm My office hours 3pm

### Table-Driven Parsing

Table-Driven Parsing It is possible to build a non-recursive predictive parser by maintaining a stack explicitly, rather than implicitly via recursive calls [1] The non-recursive parser looks up the production

### Chapter 3. Parsing #1

Chapter 3 Parsing #1 Parser source file get next character scanner get token parser AST token A parser recognizes sequences of tokens according to some grammar and generates Abstract Syntax Trees (ASTs)

### 1 Introduction. 2 Recursive descent parsing. Predicative parsing. Computer Language Implementation Lecture Note 3 February 4, 2004

CMSC 51086 Winter 2004 Computer Language Implementation Lecture Note 3 February 4, 2004 Predicative parsing 1 Introduction This note continues the discussion of parsing based on context free languages.

### Parsing #1. Leonidas Fegaras. CSE 5317/4305 L3: Parsing #1 1

Parsing #1 Leonidas Fegaras CSE 5317/4305 L3: Parsing #1 1 Parser source file get next character scanner get token parser AST token A parser recognizes sequences of tokens according to some grammar and

### CS Parsing 1

CS414-20034-03 Parsing 1 03-0: Parsing Once we have broken an input file into a sequence of tokens, the next step is to determine if that sequence of tokens forms a syntactically correct program parsing

### CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages Context Free Grammars and Parsing 1 Recall: Architecture of Compilers, Interpreters Source Parser Static Analyzer Intermediate Representation Front End Back

### CSE431 Translation of Computer Languages

CSE431 Translation of Computer Languages Top Down Parsers Doug Shook Top Down Parsers Two forms: Recursive Descent Table Also known as LL(k) parsers: Read tokens from Left to right Produces a Leftmost

### Monday, September 13, Parsers

Parsers Agenda Terminology LL(1) Parsers Overview of LR Parsing Terminology Grammar G = (Vt, Vn, S, P) Vt is the set of terminals Vn is the set of non-terminals S is the start symbol P is the set of productions

### Syntactic Analysis. Top-Down Parsing

Syntactic Analysis Top-Down Parsing Copyright 2017, Pedro C. Diniz, all rights reserved. Students enrolled in Compilers class at University of Southern California (USC) have explicit permission to make

### CA Compiler Construction

CA4003 - Compiler Construction David Sinclair A top-down parser starts with the root of the parse tree, labelled with the goal symbol of the grammar, and repeats the following steps until the fringe of

### CS2210: Compiler Construction Syntax Analysis Syntax Analysis

Comparison with Lexical Analysis The second phase of compilation Phase Input Output Lexer string of characters string of tokens Parser string of tokens Parse tree/ast What Parse Tree? CS2210: Compiler

### Wednesday, September 9, 15. Parsers

Parsers What is a parser A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda

### Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

What is a parser Parsers A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda

### CSCI312 Principles of Programming Languages

Copyright 2006 The McGraw-Hill Companies, Inc. CSCI312 Principles of Programming Languages! LL Parsing!! Xu Liu Derived from Keith Cooper s COMP 412 at Rice University Recap Copyright 2006 The McGraw-Hill

### Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Parsing Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students

### Syntax Analysis. Martin Sulzmann. Martin Sulzmann Syntax Analysis 1 / 38

Syntax Analysis Martin Sulzmann Martin Sulzmann Syntax Analysis 1 / 38 Syntax Analysis Objective Recognize individual tokens as sentences of a language (beyond regular languages). Example 1 (OK) Program

### Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

Roadmap > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing The role of the parser > performs context-free syntax analysis > guides

### Part 3. Syntax analysis. Syntax analysis 96

Part 3 Syntax analysis Syntax analysis 96 Outline 1. Introduction 2. Context-free grammar 3. Top-down parsing 4. Bottom-up parsing 5. Conclusion and some practical considerations Syntax analysis 97 Structure

### Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

Parsers Xiaokang Qiu Purdue University ECE 468 August 31, 2018 What is a parser A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure

### Lexical and Syntax Analysis. Top-Down Parsing

Lexical and Syntax Analysis Top-Down Parsing Easy for humans to write and understand String of characters Lexemes identified String of tokens Easy for programs to transform Data structure Syntax A syntax

### Wednesday, August 31, Parsers

Parsers How do we combine tokens? Combine tokens ( words in a language) to form programs ( sentences in a language) Not all combinations of tokens are correct programs (not all sentences are grammatically

### Compiler Design 1. Top-Down Parsing. Goutam Biswas. Lect 5

Compiler Design 1 Top-Down Parsing Compiler Design 2 Non-terminal as a Function In a top-down parser a non-terminal may be viewed as a generator of a substring of the input. We may view a non-terminal

### JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

JavaCC Parser The Compilation Task Input character stream Lexer stream Parser Abstract Syntax Tree Analyser Annotated AST Code Generator Code CC&P 2003 1 CC&P 2003 2 Automated? JavaCC Parser The initial

### Fall Compiler Principles Lecture 2: LL parsing. Roman Manevich Ben-Gurion University of the Negev

Fall 2017-2018 Compiler Principles Lecture 2: LL parsing Roman Manevich Ben-Gurion University of the Negev 1 Books Compilers Principles, Techniques, and Tools Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman

### 3. Parsing. Oscar Nierstrasz

3. Parsing Oscar Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes. http://www.cs.ucla.edu/~palsberg/ http://www.cs.purdue.edu/homes/hosking/

### Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 1. Top-Down Parsing. Lect 5. Goutam Biswas

Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 1 Top-Down Parsing Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 2 Non-terminal as a Function In a top-down parser a non-terminal may be viewed

### Parsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1)

TD parsing - LL(1) Parsing First and Follow sets Parse table construction BU Parsing Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1) Problems with SLR Aho, Sethi, Ullman, Compilers

### Parsing III. (Top-down parsing: recursive descent & LL(1) )

Parsing III (Top-down parsing: recursive descent & LL(1) ) Roadmap (Where are we?) Previously We set out to study parsing Specifying syntax Context-free grammars Ambiguity Top-down parsers Algorithm &

### Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

Derivations vs Parses Grammar is used to derive string or construct parser Context ree Grammars A derivation is a sequence of applications of rules Starting from the start symbol S......... (sentence)

### Fall Compiler Principles Lecture 3: Parsing part 2. Roman Manevich Ben-Gurion University

Fall 2014-2015 Compiler Principles Lecture 3: Parsing part 2 Roman Manevich Ben-Gurion University Tentative syllabus Front End Intermediate Representation Optimizations Code Generation Scanning Lowering

### Outline. Top Down Parsing. SLL(1) Parsing. Where We Are 1/24/2013

Outline Top Down Parsing Top-down parsing SLL(1) grammars Transforming a grammar into SLL(1) form Recursive-descent parsing 1 CS 412/413 Spring 2008 Introduction to Compilers 2 Where We Are SLL(1) Parsing

### Lexical and Syntax Analysis

Lexical and Syntax Analysis (of Programming Languages) Top-Down Parsing Lexical and Syntax Analysis (of Programming Languages) Top-Down Parsing Easy for humans to write and understand String of characters

### Parsing Part II (Top-down parsing, left-recursion removal)

Parsing Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit

### Compila(on (Semester A, 2013/14)

Compila(on 0368-3133 (Semester A, 2013/14) Lecture 4: Syntax Analysis (Top- Down Parsing) Modern Compiler Design: Chapter 2.2 Noam Rinetzky Slides credit: Roman Manevich, Mooly Sagiv, Jeff Ullman, Eran

### 1. Consider the following program in a PCAT-like language.

CS4XX INTRODUCTION TO COMPILER THEORY MIDTERM EXAM QUESTIONS (Each question carries 20 Points) Total points: 100 1. Consider the following program in a PCAT-like language. PROCEDURE main; TYPE t = FLOAT;

### CS502: Compilers & Programming Systems

CS502: Compilers & Programming Systems Top-down Parsing Zhiyuan Li Department of Computer Science Purdue University, USA There exist two well-known schemes to construct deterministic top-down parsers:

### CSE 401 Midterm Exam Sample Solution 2/11/15

Question 1. (10 points) Regular expression warmup. For regular expression questions, you must restrict yourself to the basic regular expression operations covered in class and on homework assignments:

### CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

CS 2210 Sample Midterm 1. Determine if each of the following claims is true (T) or false (F). F A language consists of a set of strings, its grammar structure, and a set of operations. (Note: a language

### Syntax Analysis. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Syntax Analysis Björn B. Brandenburg The University of North Carolina at Chapel Hill Based on slides and notes by S. Olivier, A. Block, N. Fisher, F. Hernandez-Campos, and D. Stotts. The Big Picture Character

### Syntax Analysis. The Big Picture. The Big Picture. COMP 524: Programming Languages Srinivas Krishnan January 25, 2011

Syntax Analysis COMP 524: Programming Languages Srinivas Krishnan January 25, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block. 1 The Big Picture Character Stream Token

### ECE251 Midterm practice questions, Fall 2010

ECE251 Midterm practice questions, Fall 2010 Patrick Lam October 20, 2010 Bootstrapping In particular, say you have a compiler from C to Pascal which runs on x86, and you want to write a self-hosting Java

### Syntax Analysis, III Comp 412

COMP 412 FALL 2017 Syntax Analysis, III Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2017, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp

### PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science and Engineering

TEST 1 Date : 24 02 2015 Marks : 50 Subject & Code : Compiler Design ( 10CS63) Class : VI CSE A & B Name of faculty : Mrs. Shanthala P.T/ Mrs. Swati Gambhire Time : 8:30 10:00 AM SOLUTION MANUAL 1. a.

### Part III : Parsing. From Regular to Context-Free Grammars. Deriving a Parser from a Context-Free Grammar. Scanners and Parsers.

Part III : Parsing From Regular to Context-Free Grammars Deriving a Parser from a Context-Free Grammar Scanners and Parsers A Parser for EBNF Left-Parsable Grammars Martin Odersky, LAMP/DI 1 From Regular

### Compilation Lecture 3: Syntax Analysis: Top-Down parsing. Noam Rinetzky

Compilation 0368-3133 Lecture 3: Syntax Analysis: Top-Down parsing Noam Rinetzky 1 Recursive descent parsing Define a function for every nonterminal Every function work as follows Find applicable production

### CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages Parsing CMSC 330 - Spring 2017 1 Recall: Front End Scanner and Parser Front End Token Source Scanner Parser Stream AST Scanner / lexer / tokenizer converts

### Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

CMSC 330: Organization of Programming Languages Context Free Grammars Where We Are Programming languages Ruby OCaml Implementing programming languages Scanner Uses regular expressions Finite automata Parser

### Syntax Analysis, III Comp 412

Updated algorithm for removal of indirect left recursion to match EaC3e (3/2018) COMP 412 FALL 2018 Midterm Exam: Thursday October 18, 7PM Herzstein Amphitheater Syntax Analysis, III Comp 412 source code

### CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler

### Lecture Notes on Top-Down Predictive LL Parsing

Lecture Notes on Top-Down Predictive LL Parsing 15-411: Compiler Design Frank Pfenning Lecture 8 1 Introduction In this lecture we discuss a parsing algorithm that traverses the input string from l eft

### Prelude COMP 181 Tufts University Computer Science Last time Grammar issues Key structure meaning Tufts University Computer Science

Prelude COMP Lecture Topdown Parsing September, 00 What is the Tufts mascot? Jumbo the elephant Why? P. T. Barnum was an original trustee of Tufts : donated \$0,000 for a natural museum on campus Barnum

### Fall Compiler Principles Lecture 2: LL parsing. Roman Manevich Ben-Gurion University of the Negev

Fall 2016-2017 Compiler Principles Lecture 2: LL parsing Roman Manevich Ben-Gurion University of the Negev 1 Books Compilers Principles, Techniques, and Tools Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman

### Compilers. Yannis Smaragdakis, U. Athens (original slides by Sam

Compilers Parsing Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Next step text chars Lexical analyzer tokens Parser IR Errors Parsing: Organize tokens into sentences Do tokens conform

### Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Parsing III (Top-down parsing: recursive descent & LL(1) ) (Bottom-up parsing) CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones Copyright 2003, Keith D. Cooper,

### LL parsing Nullable, FIRST, and FOLLOW

EDAN65: Compilers LL parsing Nullable, FIRST, and FOLLOW Görel Hedin Revised: 2014-09- 22 Regular expressions Context- free grammar ATribute grammar Lexical analyzer (scanner) SyntacKc analyzer (parser)

### CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler

### Topic 3: Syntax Analysis I

Topic 3: Syntax Analysis I Compiler Design Prof. Hanjun Kim CoreLab (Compiler Research Lab) POSTECH 1 Back-End Front-End The Front End Source Program Lexical Analysis Syntax Analysis Semantic Analysis

### Intro To Parsing. Step By Step

#1 Intro To Parsing Step By Step #2 Self-Test from Last Time Are practical parsers and scanners based on deterministic or non-deterministic automata? How can regular expressions be used to specify nested

### Parsing Algorithms. Parsing: continued. Top Down Parsing. Predictive Parser. David Notkin Autumn 2008

Parsing: continued David Notkin Autumn 2008 Parsing Algorithms Earley s algorithm (1970) works for all CFGs O(N 3 ) worst case performance O(N 2 ) for unambiguous grammars Based on dynamic programming,

### In this simple example, it is quite clear that there are exactly two strings that match the above grammar, namely: abc and abcc

JavaCC: LOOKAHEAD MiniTutorial 1. WHAT IS LOOKAHEAD The job of a parser is to read an input stream and determine whether or not the input stream conforms to the grammar. This determination in its most

### Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

Architecture of Compilers, Interpreters : Organization of Programming Languages ource Analyzer Optimizer Code Generator Context Free Grammars Intermediate Representation Front End Back End Compiler / Interpreter

### CS 321 Programming Languages and Compilers. VI. Parsing

CS 321 Programming Languages and Compilers VI. Parsing Parsing Calculate grammatical structure of program, like diagramming sentences, where: Tokens = words Programs = sentences For further information,

### LL(1) predictive parsing

LL(1) predictive parsing Informatics 2A: Lecture 11 Mary Cryan School of Informatics University of Edinburgh mcryan@staffmail.ed.ac.uk 10 October 2018 1 / 15 Recap of Lecture 10 A pushdown automaton (PDA)

### CS 536 Midterm Exam Spring 2013

CS 536 Midterm Exam Spring 2013 ID: Exam Instructions: Write your student ID (not your name) in the space provided at the top of each page of the exam. Write all your answers on the exam itself. Feel free

### EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing Görel Hedin Revised: 2017-09-04 This lecture Regular expressions Context-free grammar Attribute grammar

### CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Scanner Parser Static Analyzer Intermediate Representation Front End Back End Compiler / Interpreter

### Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity.

Eliminating Ambiguity Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity. Example: consider the following grammar stat if expr then stat if expr then stat else stat other One can

### CSE 3302 Programming Languages Lecture 2: Syntax

CSE 3302 Programming Languages Lecture 2: Syntax (based on slides by Chengkai Li) Leonidas Fegaras University of Texas at Arlington CSE 3302 L2 Spring 2011 1 How do we define a PL? Specifying a PL: Syntax:

### Computing PREDICT and FOLLOW sets

Computing PREDICT and FOLLOW sets SI 413 - Programming Languages and Implementation Dr. Daniel S. Roche United States Naval Academy Fall 2011 Roche (USNA) SI413 PREDICT & FOLLOW Fall 2011 1 / 9 Set Definitions

### Language Processing note 12 CS

CS2 Language Processing note 12 Automatic generation of parsers In this note we describe how one may automatically generate a parse table from a given grammar (assuming the grammar is LL(1)). This and

### Compilers - Chapter 2: An introduction to syntax analysis (and a complete toy compiler)

Compilers - Chapter 2: An introduction to syntax analysis (and a complete toy compiler) Lecturers: Paul Kelly (phjk@doc.ic.ac.uk) Office: room 304, William Penney Building Naranker Dulay (nd@doc.ic.ac.uk)

### LL(1) predictive parsing

LL(1) predictive parsing Informatics 2A: Lecture 11 John Longley School of Informatics University of Edinburgh jrl@staffmail.ed.ac.uk 13 October, 2011 1 / 12 1 LL(1) grammars and parse tables 2 3 2 / 12

### CMSC 330: Organization of Programming Languages. Context Free Grammars

CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler

### Chapter 3: Lexing and Parsing

Chapter 3: Lexing and Parsing Aarne Ranta Slides for the book Implementing Programming Languages. An Introduction to Compilers and Interpreters, College Publications, 2012. Lexing and Parsing* Deeper understanding

### Question Points Score

CS 453 Introduction to Compilers Midterm Examination Spring 2009 March 12, 2009 75 minutes (maximum) Closed Book You may use one side of one sheet (8.5x11) of paper with any notes you like. This exam has

### CMSC 330, Fall 2009, Practice Problem 3 Solutions

CMC 330, Fall 2009, Practice Problem 3 olutions 1. Context Free Grammars a. List the 4 components of a context free grammar. Terminals, non-terminals, productions, start symbol b. Describe the relationship