Compiler construction lecture 3

Similar documents
Compiler construction in4303 lecture 3

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

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

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

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

Compiler construction in4020 lecture 5

Wednesday, September 9, 15. Parsers

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

Compiler construction 2002 week 5

Compilers. Predictive Parsing. Alex Aiken

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

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

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

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

Compiler construction 2005 lecture 5

Monday, September 13, Parsers

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Syntax-Directed Translation. Lecture 14

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

Syntax Analysis Part I

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

IN4305 Engineering project Compiler construction

Configuration Sets for CSX- Lite. Parser Action Table

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

Wednesday, August 31, Parsers

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

A programming language requires two major definitions A simple one pass compiler

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

Compila(on (Semester A, 2013/14)

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

CS502: Compilers & Programming Systems

How do LL(1) Parsers Build Syntax Trees?

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

Building Compilers with Phoenix

Table-Driven Top-Down Parsers

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

CSE 401 Midterm Exam Sample Solution 2/11/15

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

Lexical and Syntax Analysis. Top-Down Parsing

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

Lexical and Syntax Analysis


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

4. Lexical and Syntax Analysis

CS 4120 Introduction to Compilers

CSCI312 Principles of Programming Languages

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

4. Lexical and Syntax Analysis

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

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

CA Compiler Construction

LR Parsing - The Items

Context-free grammars

Bottom-Up Parsing. Lecture 11-12

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

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

Types of parsing. CMSC 430 Lecture 4, Page 1

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Lexical and Syntax Analysis. Bottom-Up Parsing

LL Parsing: A piece of cake after LR

Compiler Construction: 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.

Chapter 4: LR Parsing

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

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

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing

Bottom-Up Parsing. Lecture 11-12

CPS 506 Comparative Programming Languages. Syntax Specification

Chapter 3. Parsing #1

CS 314 Principles of Programming Languages

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

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

LL parsing Nullable, FIRST, and FOLLOW

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

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

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

Chapter 4. Lexical and Syntax Analysis

Building Compilers with Phoenix

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

CS2210: Compiler Construction Syntax Analysis Syntax Analysis

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Peace cannot be kept by force; it can only be achieved by understanding. Albert Einstein

Lecture 8: Deterministic Bottom-Up Parsing

Syntactic Analysis. Top-Down Parsing

Syntax Analysis. Chapter 4

LECTURE 3. Compiler Phases

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

Semantic Analysis. Lecture 9. February 7, 2018

CS 314 Principles of Programming Languages. Lecture 9

Lecture 7: Deterministic Bottom-Up Parsing

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

More Bottom-Up Parsing

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

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

CSE302: Compiler Design

Question Bank. 10CS63:Compiler Design

Compiler construction in4303 answers

Transcription:

Compiler construction in4303 lecture 3 Top-down parsing Chapter 2.2-2.2.4 Overview syntax analysis: tokens AST language grammar parser generator program text lexical analysis tokens syntax analysis AST context handling AST construction annotated AST by hand: recursive descent automatic: top-down LLgen, bottom-up yacc Syntax analysis parsing: given a context-free grammar and a stream of tokens, find the derivation that ties them together. result: parse tree - * factor factor * factor identifier factor * factor identifier identifier c identifier b constant a b 4 Context free grammar G = V N, V T, S, P V N : set of Non-inal symbols V T : set of Terminal symbols S : Start symbol S V N P : set of Production rules {N α V N V T = P = {N α N V N α V N V T * Top-down parsing Bottom-up parsing process tokens left to right grammar rest_ IFIER rest_ rest_ process tokens left to right grammar rest_ IFIER rest_ rest_ rest_expr IFIER example aap noot mies aap noot mies 1

Comparison Recursive descent parsing node creation alternative selection top-down pre-order 1 2 3 first token 4 5 bottom-up post-order 5 1 4 last token 2 3 each rule N translates to a boolean function return true if a inal production of N was matched return false otherwise without consuming any token try alternatives of N in turn a inal symbol must match the current token a non-inal is matched by calling its routine grammar type implementation restricted LL1 manual automatic relaxed LR1 automatic int void { return && requiretoken; Recursive descent parsing Recursive descent parsing rest_ int void { return && requirerest_; rest_ int rest_void { return token'' && require 1; IFIER int void { return tokenifier token'' && require && requiretoken''; auxiliary functions consume matched tokens report syntax errors int tokenint tk { if Token.class!= tk return 0; get_next_token; return 1; int requireint found { if!found error; return 1; Automatic top-down parsing Exercise 4 min. follow recursive descent scheme, but avoid interpretation overhead for each rule and alternative deine the tokens it can start with: FIRST set parsing scheme for rule N A 1 A 2 if token FIRSTA 1 then parse A 1 else if token FIRSTA 2 then parse A 2 else ERROR deine the FIRST set of the noninals in our grammar rest_ IFIER rest_ and, for this grammar S A B A A a B B b b 2

Answers Computing the FIRST set N w w V T N A 1 A 2 N closure algorithm: initial values inference rules iterative solution N A β Predictive parsing similar to recursive descent, but no back-tracking functions know what they are doing FIRST = {, void void { switch Token.class { case : case '': ; token; break; default: error; void tokenint tk { if Token.class!= tk error; get_next_token; Predictive parsing rest_ FIRST = {, void void { switch Token.class { case : case '': ; rest_; break; default: error; IFIER void void { switch Token.class { case : token; break; case '': token''; ; token''; break; default: error; Predictive parsing rest_ FIRSTrest_expr = {, void rest_void { switch Token.class { case '': token''; ; break; case : case '': break; default: break; error; FOLLOWrest_expr = {, FIRST = { check nothing? NO: token FOLLOWrest_expr Limitations of LL1 parsers FIRST/FIRST conflict IFIER IFIER [ ] FIRST/FOLLOW conflict S A a b A a left recursion - FIRSTA = { a, { a = FOLLOWA 3

Making grammars LL1 Left factoring manual labour rewrite grammar adjust semantic actions three rewrite methods left factoring substitution left-recursion removal IFIER IFIER [ ] factor out common prefix IFIER after_identifier after_identifier [ ] [ FOLLOWafter_identifier Substitution Left-recursion removal S A a b A a replace non-inal by its alternative S a a b a b N N α β replace by N βm M αm example β βα βαα βααα... N α β - tail tail - tail Exercise 7 min. Answers make the following grammar LL1 - * factor / factor factor factor func-call identifier constant func-call identifier expr-list? expr-list, * and what about S if E then S else S? 4

automatic generation LL1 push-down automaton language grammar parser generator program text lexical analysis tokens syntax analysis AST context handling top of stack transition table LL1 push-down automaton annotated AST stack right-hand side of production LL1 push-down automaton LL1 push-down automaton aap noot mies aap noot mies replace non-inal by transition entry replace non-inal by transition entry top of stack top of stack LL1 push-down automaton LL1 push-down automaton pop matching token aap noot mies aap noot mies replace non-inal by transition entry top of stack top of stack 5

LL1 push-down automaton LL1 push-down automaton pop matching token noot mies noot mies replace non-inal by transition entry top of stack top of stack 6