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

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

Compilers. Predictive Parsing. Alex Aiken

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

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

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

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.

Top down vs. bottom up parsing

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

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

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

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

CS502: Compilers & Programming Systems

CA Compiler Construction

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

CSCI312 Principles of Programming Languages

Part 3. Syntax analysis. Syntax analysis 96

Lexical and Syntax Analysis. Top-Down Parsing

CS2210: Compiler Construction Syntax Analysis Syntax Analysis

Types of parsing. CMSC 430 Lecture 4, Page 1

Abstract Syntax Trees & Top-Down Parsing

Table-Driven Parsing

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

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

Review of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1

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

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

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

CSE431 Translation of Computer Languages

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

Syntax-Directed Translation. Lecture 14

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

LL Parsing: A piece of cake after LR

The procedure attempts to "match" the right hand side of some production for a nonterminal.

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

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

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

Syntactic Analysis. Top-Down Parsing

LL parsing Nullable, FIRST, and FOLLOW

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

Question Points Score

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

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

Chapter 3. Parsing #1

Lexical and Syntax Analysis

Some Basic Definitions. Some Basic Definitions. Some Basic Definitions. Language Processing Systems. Syntax Analysis (Parsing) Prof.

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

Lexical and Syntax Analysis (2)

Wednesday, September 9, 15. Parsers

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

Wednesday, August 31, Parsers

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

CS 314 Principles of Programming Languages

3. Parsing. Oscar Nierstrasz

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

Building Compilers with Phoenix

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

Extra Credit Question

Bottom-Up Parsing. Lecture 11-12

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

Introduction to parsers

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

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

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

Monday, September 13, Parsers

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

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

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

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

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

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

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

Topic 3: Syntax Analysis I

Parser. Larissa von Witte. 11. Januar Institut für Softwaretechnik und Programmiersprachen. L. v. Witte 11. Januar /23

Bottom-Up Parsing II. Lecture 8


Configuration Sets for CSX- Lite. Parser Action Table

Syntactic Analysis. Top-Down Parsing. Parsing Techniques. Top-Down Parsing. Remember the Expression Grammar? Example. Example

EDA180: Compiler Construc6on. Top- down parsing. Görel Hedin Revised: a

Syntax Analysis, III Comp 412

Syntax Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

LECTURE 7. Lex and Intro to Parsing

Recursive Descent Parsers

Error Recovery. Computer Science 320 Prof. David Walker - 1 -

Parsing Techniques. CS152. Chris Pollett. Sep. 24, 2008.

COMP3131/9102: Programming Languages and Compilers

CMSC 330: Organization of Programming Languages

Context-free grammars (CFG s)

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

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

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1

Lecture 8: Deterministic Bottom-Up Parsing

Bottom-Up Parsing LR Parsing

컴파일러구성 제 2 강 Recursive-descent Parser / Predictive Parser

Syntax Analysis, III Comp 412

Transcription:

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 every alternative in a production is unique: A a B D b B B B c b c e D d parsing an input abced has no backtracking. LL(k) L left to right scan L leftmost derivation k k symbols of lookahead in practice, k = 1 It is table-driven and efficient. Left factoring to enable predication: A change to A A A For predicative parsers, must eliminate left recursion LL(k) Parser Structure Sample Parse able int * + ( ) Syntax Stack Parser Driver Output Syntax stack hold right hand side (RHS) of grammar rules M[A,b] an entry containing rule A or error Parser driver next action based on (current token, stack top) Implementation with 2-D parse table: A row for each non-terminal A column for all possible terminals and (the end of input marker) Every table entry contains at most one production Required for a grammar to be LL(1) No backtracking Fixed action for each (non-terminal, input symbol) combination Algorithm symbol at the top of the syntax stack a current input symbol Push RHS in Reverse Order symbol at the top of the syntax stack a current input symbol Parsing based on (, a): If = a =, then parser halts with success If = a, then pop from stack and advance input head If a, then Case (a): if, then parser halts with failed, input rejected Case (b): if N, M[,a] = RHS if M[,a] = B c D : B c D pop and push RHS to stack in reverse order 1

LL(1) Grammars Remove left recursive and perform left factoring Given the grammar: E + E int * int ( E ) int * int he grammar has no left recursion but requires left factoring. E After rewriting grammar, we have: E + E int ( E ) int * + ( ) int * int int * int E int * + ( ) int * + ( ) int * int int * int int int * + ( ) int * + ( ) 2

int * int int * int int * + ( ) int * + ( ) int * int int * int * int * + ( ) int * + ( ) int * int int * int int int * + ( ) int * + ( ) 3

int * int int * int int * + ( ) int * + ( ) int * int int * int Accept! int * + ( ) int * + ( ) Action List Stack Input Action E int * int E int * int int int int * int terminal * int * * int terminal int int int int terminal Halt and accept Constructing the Parse able We need to know what non-terminals to place our productions in the table? We know that we have restricted our grammars so that left recursion is eliminated and they have been left factored. hat means that each production is uniquely recognizable by the first terminal that production would derive. hus, we can construct our table from 2 sets: For each symbol A, the set of terminals that can begin a string derived from A. his set is called the FIRS set of A For each non-terminal A, the set of terminals that can appear after a string derived from A is called the FOLLOW set of A 4

First( ) First( ) = set of terminals that start string of terminals derived from. Apply following rules until no terminal or can be added 1. If t, then First( t ) = { t }. For example First( + ) = { + }. 2. If N and exists (nullable), then add to First( ). For example, First( ) = { *, }. 3. If N and 1 2 3 m, where 1, 2, 3,... m are nonterminals, then: for each i from 1 to m if 1 i-1 are all nullable (or if i = 1) First( ) = First( ) First( i ) Follow( ) Follow( ) = { t S * t } Intuition: if A B, then First( B ) Follow( A ) However, B may be i.e., Apply following rules until no terminal or can be added 1. Follow( S ), where S is the start symbol. e.g., Follow( E ) = {... }. 2. Look at the occurrence of a non-terminal on the right hand side of a production which is followed by something If A B, then First( ) - { } Follow( B ) 3. Look at N on the RHS that is not followed by anything, if (A B) or (A B and First( )), then Follow( A ) Follow( B ) Algorithm to Compute FIRS, FOLLOW, and nullable Initialize FIRS and FOLLOW to all empty sets, and nullable to all false. foreach terminal symbol Z FIRS[Z] {Z} do foreach production 1 2 k if 1 k are all nullable (or if k = 0) then nullable[] true foreach i from 1 to k, each j from i + 1 to k if 1 i 1 are all nullable (or if i = 1) then FIRS[] FIRS[] FIRS[ i ] if i+1 k are all nullable (or if i = k) then FOLLOW[ i ] FOLLOW[ i ] FOLLOW[] if i+1 j 1 are all nullable (or if i + 1 = j ) then FOLLOW[ i ] FOLLOW[ i ] FIRS[ j ] until FIRS, FOLLOW, and nullable did not change in this iteration. Example E + E int ( E ) First Set: E + E int ( E ) Follow Set: E + E int ( E ) int Int *,, ), + +,, ) (, int, ), + E (, int, ) Constructing LL(1) Parse able Constructing LL(1) Parse able o construct the parse table, we check each A For each terminal a First( ), add A to M[A, ]. If First( ), then for each terminal b Follow(A), add A to M[A, ]. If First( ) and Follow(A), then add A to M[A, ]. For each terminal a First( ), add A to M[A, ]. E + E int ( E ) int int *,, ), + +,, ) (, int, ), + E (, int, ) int * + ( ) E E E + E 5

Constructing LL(1) Parse able Constructing LL(1) Parse able If First( ), then for each terminal b Follow(A), add A to M[A, ]. If First( ) and Follow(A), then add A to M[A, ]. E + E int ( E ) int int *,, ), + +,, ) (, int, ), + E (, int, ) E + E int ( E ) int int *,, ), + +,, ) (, int, ), + E (, int, ) int * + ( ) E E E + E int * + ( ) E E E + E Is a Grammar LL(1)? Observation If a grammar is LL(1), then each of its LL(1) table entries contain at most one rule. Otherwise, it is not LL(1) wo methods to determine if a grammar is LL(1) or not: 1. Construct LL(1) table, and check if there is a multi-rule entry or 2. Check each rule as if the table were being constructed: G is LL1(1) iff for a rule A a) First( ) First( ) = b) at most one of and can derive c) If derives, then First( ) Follow( ) = Ambiguous Grammars Some grammars may need more than one token of lookahead (k). However, some grammars are not LL regardless of how the grammar is changed. S if C then S if C then S else S C b change to S if C then S else S C b problem sentence: if b then if b then a else a else First() First()- Follow(S) else... else Follow() Removing Ambiguity o remove ambiguity, it is possible to rewrite the grammar. For the if-then-else example, how to rewrite? May not even need to rewrite in this case, we can just use the else S production over the LL(1) Summary LL(1) parsers operate in linear time and at most linear space relative to the length of input because: ime each input symbol is processed constant number of times Space stack is smaller than the input (in case we remove ) However, by changing the grammar, It might make the other phases of the compiler more difficult It becomes harder to determine semantics and generate code It is less appealing to programmers 6

Summary First and Follow sets are used to construct predictive parsing tables Intuitively, First and Follow sets guide the choice of rules: For non-terminal A and input t, use a production rule A where t First( ) For non-terminal A and input t, if A and t Follow(A), use the production A where First( ) What is LL(0)? Why are LL(2)... LL(k) are not widely used? 7