LL Parsing: A piece of cake after LR

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

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

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

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

Abstract Syntax Trees & Top-Down Parsing

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

Extra Credit Question

Compilers. Predictive Parsing. Alex Aiken

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

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

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

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

More Bottom-Up Parsing

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

Conflicts in LR Parsing and More LR Parsing Types

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

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

Introduction to Bottom-Up Parsing

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

Lexical and Syntax Analysis. Top-Down Parsing

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

Table-Driven Parsing

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

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

Chapter 3. Parsing #1

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

Wednesday, September 9, 15. Parsers

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

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

CS2210: Compiler Construction Syntax Analysis Syntax Analysis

Lexical and Syntax Analysis

CS502: Compilers & Programming Systems

Types of parsing. CMSC 430 Lecture 4, Page 1

Syntax-Directed Translation. Lecture 14

Outline. The strategy: shift-reduce parsing. Introduction to Bottom-Up Parsing. A key concept: handles

Context-free grammars

Compiler Construction: Parsing

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

CSCI312 Principles of Programming Languages

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

Talen en Compilers. Johan Jeuring , period 2. January 17, Department of Information and Computing Sciences Utrecht University

CS453 : Shift Reduce Parsing Unambiguous Grammars LR(0) and SLR Parse Tables by Wim Bohm and Michelle Strout. CS453 Shift-reduce Parsing 1

Syntactic Analysis. Top-Down Parsing

CMSC 330: Organization of Programming Languages

Syntax Analysis. Prof. James L. Frankel Harvard University. Version of 6:43 PM 6-Feb-2018 Copyright 2018, 2015 James L. Frankel. All rights reserved.

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

CIT 3136 Lecture 7. Top-Down Parsing

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

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

3. Parsing. Oscar Nierstrasz

CS 4120 Introduction to Compilers

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

Monday, September 13, Parsers

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

Chapter 4. Lexical and Syntax Analysis. Topics. Compilation. Language Implementation. Issues in Lexical and Syntax Analysis.

Compiler construction lecture 3

Bottom-Up Parsing. Lecture 11-12

Compiler construction in4303 lecture 3

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

Example CFG. Lectures 16 & 17 Bottom-Up Parsing. LL(1) Predictor Table Review. Stacks in LR Parsing 1. Sʹ " S. 2. S " AyB. 3. A " ab. 4.

Compiler Design Concepts. Syntax Analysis

Recursive Descent Parsers

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

CSC 4181 Compiler Construction. Parsing. Outline. Introduction

Wednesday, August 31, Parsers

Syntax Analysis, III Comp 412

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

Bottom-Up Parsing. Lecture 11-12

CMSC 330: Organization of Programming Languages

Compilerconstructie. najaar Rudy van Vliet kamer 140 Snellius, tel rvvliet(at)liacs(dot)nl. college 3, vrijdag 22 september 2017

Syntax Analysis, III Comp 412

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

LECTURE 7. Lex and Intro to Parsing

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

UNIT III & IV. Bottom up parsing

CA Compiler Construction

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

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

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

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

CSE431 Translation of Computer Languages

CS 406/534 Compiler Construction Parsing Part I

BSCS Fall Mid Term Examination December 2012

It parses an input string of tokens by tracing out the steps in a leftmost derivation.


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

Parsing Part II. (Ambiguity, Top-down parsing, Left-recursion Removal)

Downloaded from Page 1. LR Parsing

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

CS 314 Principles of Programming Languages

Lexical and Syntax Analysis (2)

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

CIT Lecture 5 Context-Free Grammars and Parsing 4/2/2003 1

Syntax Analysis, V Bottom-up Parsing & The Magic of Handles Comp 412

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing

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

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

Bottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as Shift-Reduce parsing

Transcription:

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 rightmost (xr) Constructed from the top in the order that tokens appear in the token stream 2

Parsing Algorithms LL E E + E 2 + E 2 + E * E 2 + 3 * E 2 + 3 * 8 LR E 2 E 3 E E + E E 8 E E * E E E 3

LR vs. LL LR (bison/cup): harder to implement, but allows more grammars LL: easier to write, but less flexible in the grammars 4

Recursive Descent Parsing Consider the grammar: E T + E T T int int * T (E) Token stream is int5 * int2 Start with top-level non-terminal E Try the rules E in order 5

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

Recursive Descent Parsing E0 T1 Follow same steps as before for T 1 And succeed with T1 int * T2 and T2 int E0 T1 int5 * T2 7 int2

Recursive Descent Parsing Parsing: given a string of tokens t1, t2, t3, t4, t5, find its parse tree Recursive descent parsing. Try all of the productions exhaustively: At a given moment, the finge of the parse tree is: t1, t2,... tk, A Try all the productions for A: if A BC is a production, the new finge is t1, t2,... tk, BC Backtrack where the finge doesn t match the string. Stop when there are no more non-terminals. 8

Construction of Decision Table for each rule X α for each a in First(α) table (X, a) = X α so if we see the very first symbol, match the rule if ε is in First(alpha) for each b in Follows(X) table(x, b) = X α That s it! 9

Non-LL Grammars There are a lot of non-ll grammars. The patterns to look for are: Left-Recursive rules (goes into an infinite loop) which must be rewritten as right-recursive Common prefix rules which must be rewritten to move the common prefix to one rule 10

Left Recursion A Aa A b For LL, First(A Aa) = b For LL, First(A b) = b this gives a conflict 11

Rewrite the Grammar A ba A aa A ε 12

Common Prefix X abc X abd This works for LR. For LL, the parser only looks at the First set of rules, not the entire rule. So the rules cause a conflict. 13

Rewritten Grammar X aby Y c Y d 14

Recursive Descent Simple and general, but unpopular (and slow) because of backtracking Often we can avoid backtracking (somewhat) 15

Predictive Parsing 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 16

LL(1) Languages In recursive-descent, for each non-terminal and input token, there may be a choice of production 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 1 dim for curent non-terminal to expand 1 dim for next token A table entry contains one production 17

Example Grammar #1 Grammar: E T + E T T int int * T (E) Impossible to predict because T has two productions with int. For E it is not clear how to predict. Grammar must be left-factored 18

Left Factoring Grammar E T + E T T int int * T (E) Factor out common prefixes of productions: E T X X + E ε T (E) int Y Y * T ε 19

LL(1) Parse Table Example Left-Factored Grammar E T X X + E ε T (E) int Y Y * T ε The LL(1) parsing table ($ is a special end marker): int * + ( ) $ T int Y (E) E T X T X X + E ε ε Y * T ε ε ε 20

LL(1) Parse Table Consider the [E, int] entry: When current non-terminal is E and next input is int, use production E TX This production can generate an int in the first place 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 21

LL(1) Parse Table Blank entries indicate error situtations E.g., consider the [E, *] entry There is no way to derive a string starting with * from non-terminal E 22

Using Parse Tables 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] Use a stack to keep track of pending non-terminals Reject when we encounter an error state Accept when we encounter an end-to-input 23

LL(1) Parsing Algorithm initialize stack = <S, $> and next (pointer to tokens) repeat case stack of <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 == < > 24

LL(1) Parsing Example Stack Input Action E$ int * int $ TX TX$ int * int $ int Y int YX$ int * int * terminal YX$ *int$ *T *TX$ *int$ terminal TX$ int$ int Y int YX$ int$ terminal YX$ $ ε X$ $ ε $ $ ACCEPT 25

Example Grammar #2 D v L c T L L m i L i T r v = Var, L = IdList c = colon, T = type m = comma, i = id, r = REAL 26

After fixing L (which was right-recursive) D v L c T L i X X m i X X ε T r 27

Algorithm start with the start symbol on the stack repeat until done if top of stack == first char of input pop 1 of stack advance input else if top of stack == non-terminal T consult (T, input) if (T,input) == T α pop T push input in reverse order print T α reduce else if (T,input) == empty error - reject else if stack is empty and input is empty accept else if stack is empty and input is not empty reject 28

LL Decision Table v i m c r $ D D->vLcT L L->iX X X->miX X->e T T->r 29

stack input action D vimicr D, v=match D vlct, pop LHS(D), push RHS (vlct) in reverse order TcLv vimicr v, v = match so pop from stack, advance input TcL imicr L, i = match L ix pop L, push ix in reverse order TcXi imicr i, i = match, pop stack, advance TcX micr X, m = match X mix pop X, push mix in reverse order TcXim micr m, m = match pop stack, advance TcXi icr i, i = match pop stack, advance TcX cr X, c = match X ε, pop X, push in reverse order Tc cr c, c = match pop stack, advance T r T, r = match T r pop T, push r in reverse order r r r, r = match pop stack, advance 30

Done! Accept! 31

Status Project 2 Due Friday, Apr. 24, 11:55pm 32