CSE431 Translation of Computer Languages

Similar documents
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) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

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

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

Types of parsing. CMSC 430 Lecture 4, Page 1

Chapter 4: Syntax Analyzer

Recursive Descent Parsers

CMSC 330, Fall 2009, Practice Problem 3 Solutions

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

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

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

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

Monday, September 13, Parsers

CMSC 330 Practice Problem 4 Solutions

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

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

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

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

Wednesday, September 9, 15. Parsers

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

Syntax Analysis Check syntax and construct abstract syntax tree

Syntax Analysis Top Down Parsing

3. Parsing. Oscar Nierstrasz

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

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.

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

Lecture 8: Deterministic Bottom-Up Parsing

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

Extra Credit Question

Compilers. Predictive Parsing. Alex Aiken

Lecture 7: Deterministic Bottom-Up Parsing

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

CS502: Compilers & Programming Systems

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

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

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

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

Syntax Analysis. Chapter 4

Abstract Syntax Trees & Top-Down Parsing

Parsing II Top-down parsing. Comp 412

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

LL(1) Grammars. Example. Recursive Descent Parsers. S A a {b,d,a} A B D {b, d, a} B b { b } B λ {d, a} D d { d } D λ { a }

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1


Syntax Analysis Part I

Bottom-Up Parsing. Parser Generation. LR Parsing. Constructing LR Parser

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

CSC 4181 Compiler Construction. Parsing. Outline. Introduction

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

Chapter 3. Parsing #1

Building A Recursive Descent Parser. Example: CSX-Lite. match terminals, and calling parsing procedures to match nonterminals.

Chapter 4: LR Parsing

Top down vs. bottom up parsing

Lexical and Syntax Analysis. Top-Down Parsing

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

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

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

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

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

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

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

Bottom-up parsing. Bottom-Up Parsing. Recall. Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form

Grammars and Parsing, second week

Wednesday, August 31, Parsers

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Table-Driven Top-Down Parsers

Parser Generation. Bottom-Up Parsing. Constructing LR Parser. LR Parsing. Construct parse tree bottom-up --- from leaves to the root

CS 230 Programming Languages

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309

LR Parsing. The first L means the input string is processed from left to right.

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

Question Marks 1 /20 2 /16 3 /7 4 /10 5 /16 6 /7 7 /24 Total /100

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

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

CSCI312 Principles of Programming Languages

CSE 3302 Programming Languages Lecture 2: Syntax

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

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.

Configuration Sets for CSX- Lite. Parser Action Table

Action Table for CSX-Lite. LALR Parser Driver. Example of LALR(1) Parsing. GoTo Table for CSX-Lite

Syntactic Analysis. Top-Down Parsing

Lecture Notes on Shift-Reduce Parsing

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

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

Principles of Programming Languages COMP251: Syntax and Grammars

Bottom-Up Parsing. Lecture 11-12

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

Lexical and Syntax Analysis (2)

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

Lexical and Syntax Analysis

COMP3131/9102: Programming Languages and Compilers

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

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

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

A left-sentential form is a sentential form that occurs in the leftmost derivation of some sentence.

LL parsing Nullable, FIRST, and FOLLOW

shift-reduce parsing

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

Transcription:

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 derivation Uses k symbols of lookahead k is typically = 1 2

Recursive Descent Parsers One method for each non-terminal Use lookahead to examine the next symbol and decide which production to take What do we use to make this decision? Use mutual recursion to descend the tree 3

Recursive Descent Parsers function P() { P -> ( P ) if (LookaheadIs( ( )) { a Match( ( ); P(); Match( ) ); } elseif(lookaheadis( a )) { Match( a ); } else { Error(); } } 4

LL(1) Grammars Lookahead is used to predict which production to take next Why would we restrict this to 1? Any grammar that can be successfully parsed using this method with lookahead = 1 is known as an LL(1) grammar Not all grammars are LL(1) 5

LL(1) Grammars Consider the following productions: A -> Aa b What happens if the predict set tells us to take the first production? 6

Left Recursion The solution is to transform the production to use right recursion instead: A -> XY X -> b Y -> ay λ 7

LL(1) Grammars Now consider the following production: A -> bc bd What is the problem? 8

Common Prefix The production can be factored so that the common element is in a single production: A -> bx X -> C D Does this rule apply to common non-terminal prefixes too? 9

Non-LL(1) Grammars Grammars are not LL(1) when they: Have overlapping predict sets for the same nonterminal Can never advance the input There are three cases when the above will occur We just saw two of them What is the third? 10

Non-LL(1) Grammars Consider the Dangling Bracket Language: {[ i ] j i >= j >= 0 } What s wrong with this grammar? S -> [ S C λ C -> ] λ 11

Non-LL(1) Grammars That one was ambiguous, so let s try this one: S -> [ S T T -> [ T ] λ What s wrong with this grammar? Unfortunately there is just no way to make it LL(1) What can we do? 12

Practice Write a pseudocode recursive descent parser for the following grammar: S -> A $ A -> B c B -> d B λ 13

Practice The following grammar is not LL(1). Fix it so that it is LL(1). Make sure to list every operation you perform. S -> A $ A -> a b c D E a b E D -> D d e E -> f 14

Table Driven Parsers What are the possible drawbacks of recursive descent parsers? Table driven parsers rely on a stack Special TOS() function can read the top of the stack without popping Two cases: Terminal match and pop Nonterminal use lookahead and parse table to predict the next production What can we use to construct the table? 15

Table Driven Parsers 16

LL(1) Properties LL(1) grammars: Create leftmost derivations Are unambiguous How efficient are these parsers? Run time of a table driven parser? Recursive descent? 17

Error handling How do we know when an error occurs? Two approaches Error recovery Error repair 18

Error Recovery Error recovery methods attempt to reset the parser to a valid state Common implementation: panic mode What symbols in languages typically denote the end of a statement? Ignore input until one of those symbols is found, then restart Drawbacks? 19

Error Repair Several possibilities here: Insert or delete symbols from the already parsed portion Remove the symbol that caused the error Insert values into the unparsed portion Which of the above seem most reasonable to you? Which seem problematic? 20

More Practice Revisit the pseudocode you wrote for slide 14, and add code to implement panic mode error handling Create a parse table for the following grammar: S -> V $ V -> n ( E ) E -> + V V * W W -> V W λ Use your table to parse (*(+nn))$ 21

More Practice Create a grammar that recognizes valid regular expressions. You only need to handle the primary operators (*, ) and parentheses. For simplicty, use a single terminal as a placeholder for all characters. Examples: x x x xx (x x)x Show the derivation of x xx* using your grammar 22

More Practice Is your regex grammar suitable for LL(1) parsing? If not, modify it. Create the parse table for your grammar. 23