Compiler construction in4303 lecture 3

Similar documents
Compiler construction lecture 3

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

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

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

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

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

Wednesday, September 9, 15. Parsers

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

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

Compiler construction in4020 lecture 5

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

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

Lexical and Syntax Analysis. Top-Down Parsing

Compiler construction 2002 week 5

CS502: Compilers & Programming Systems

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Compiler construction in4303 answers

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

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

Syntax Analysis Part I

CSCI312 Principles of Programming Languages

Monday, September 13, Parsers

2 SECTION 1.1 Why study compiler construction?

Compilers. Predictive Parsing. Alex Aiken

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

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

LL parsing Nullable, FIRST, and FOLLOW

Syntax-Directed Translation. Lecture 14

Abstract Syntax Trees & Top-Down Parsing

CS 4120 Introduction to Compilers

Configuration Sets for CSX- Lite. Parser Action Table

Top down vs. bottom up parsing

Wednesday, August 31, Parsers

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

Compiler construction 2005 lecture 5

Lexical and Syntax Analysis

Table-Driven Top-Down Parsers

4. Lexical and Syntax Analysis

IN4305 Engineering project Compiler construction

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

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

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

4. Lexical and Syntax Analysis

Building Compilers with Phoenix

Compila(on (Semester A, 2013/14)

CA Compiler Construction

Chapter 4. Lexical and Syntax Analysis

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

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

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


CSE 401 Midterm Exam Sample Solution 2/11/15

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

CS 314 Principles of Programming Languages

How do LL(1) Parsers Build Syntax Trees?

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

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

Chapter 3. Parsing #1

Types of parsing. CMSC 430 Lecture 4, Page 1

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

Context-free grammars

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

Parsing. Lecture 11: Parsing. Recursive Descent Parser. Arithmetic grammar. - drops irrelevant details from parse tree

Outline CS412/413. Administrivia. Review. Grammars. Left vs. Right Recursion. More tips forll(1) grammars Bottom-up parsing LR(0) parser construction

Bottom-Up Parsing. Lecture 11-12

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

Table-driven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and non-terminals.

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

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

Compiler Construction: Parsing

Recursive Descent Parsers

Building Compilers with Phoenix

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

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

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

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

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

Syntactic Analysis. Top-Down Parsing

Software II: Principles of Programming Languages

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

LECTURE 3. Compiler Phases

Bottom-Up Parsing. Lecture 11-12

LR Parsing - The Items

CSE302: Compiler Design

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

COP4020 Programming Assignment 2 Spring 2011

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

Lexical and Syntax Analysis (2)

CSX-lite Example. LL(1) Parse Tables. LL(1) Parser Driver. Example of LL(1) Parsing. An LL(1) parse table, T, is a twodimensional

Lexical and Syntax Analysis. Bottom-Up Parsing

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

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

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

COP4020 Programming Assignment 2 - Fall 2016

LL Parsing: A piece of cake after LR

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

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

MIT Parse Table Construction. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Table-Driven Parsing

Transcription:

Compiler construction in4303 lecture 3 Top-down parsing Chapter 2.2-2.2.4

Overview syntax analysis: tokens AST program text lexical analysis language grammar parser generator tokens syntax analysis AST AST construction by hand: recursive descent context handling annotated AST 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 expression expression - term term term * factor term factor * factor identifier term 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-terminal 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 process tokens left to right expression grammar input expression EOF expression term rest_expression term IFIER ( expression ) rest_expression + expression ε input expression EOF term rest_expression IFIER example expression aap + ( noot + mies )

Bottom-up parsing process tokens left to right expression grammar input expression EOF expression term rest_expression term IFIER ( expression ) rest_expression + expression ε term term rest_expression expression term rest_expr aap + ( noot + mies ε )

Comparison node creation alternative selection grammar type implementation top-down pre-order 1 2 3 first token restricted LL(1) manual + automatic 4 5 bottom-up post-order 5 1 4 2 3 last token relaxed LR(1) automatic

Recursive descent parsing each rule N translates to a boolean function return true if a terminal production of N was matched return false otherwise (without consuming any token) try alternatives of N in turn a terminal symbol must match the current token a non-terminal is matched by calling its routine input expression EOF int input(void) { } return expression() && require(token(eof));

Recursive descent parsing expression term rest_expression int expression(void) { return term() && require(rest_expression()); } term IFIER ( expression ) int term(void) { return token(ifier) token('(') && require(expression()) && require(token(')')); }

Recursive descent parsing rest_expression + expression ε int rest_expression(void) { return token('+') && require(expression()) 1; } auxiliary functions consume matched tokens report syntax errors int token(int tk) { if (Token.class!= tk) return 0; get_next_token(); return 1; } int require(int found) { if (!found) error(); return 1; }

Automatic top-down parsing follow recursive descent scheme, but avoid interpretation overhead for each rule and alternative determine the tokens it can start with: FIRST set parsing scheme for rule N A 1 A 2 if token FIRST(A 1 ) then parse A 1 else if token FIRST(A 2 ) then parse A 2 else ERROR

Exercise (4 min.) determine the FIRST set of the nonterminals in our expression grammar input expression EOF expression term rest_expression term IFIER ( expression ) rest_expression + expression ε and, for this grammar S A B A A a ε B B b b

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 input expression EOF FIRST(expression) = {, ( } void input(void) { switch (Token.class) { case : case '(': expression(); token(eof); break; default: error(); } } void token(int tk) { } if (Token.class!= tk) error(); get_next_token();

Predictive parsing expression term rest_expression FIRST(term) = {, ( } void expression(void) { switch (Token.class) { case : case '(': term(); rest_expression(); break; default: error(); } } term IFIER ( expression ) void term(void) { switch (Token.class) { case : token(); break; case '(': token('('); expression(); token(')'); break; default: error(); } }

Predictive parsing rest_expression + expression ε FIRST(rest_expr) = { +, ε} void rest_expression(void) { switch (Token.class) { case '+': token('+'); expression(); break; case EOF: case ')': break; default: break; error(); } } FIRST(ε) = {ε} FOLLOW(rest_expr) = {EOF, ) } check nothing? NO: token FOLLOW(rest_expr)

Limitations of LL(1) parsers FIRST/FIRST conflict term IFIER IFIER [ expression ] ( expression ) FIRST/FOLLOW conflict S A a b A a ε left recursion expression expression - term term FIRST(A) = { a, ε } { a } = FOLLOW(A)

Making grammars LL(1) manual labour rewrite grammar adjust semantic actions three rewrite methods left factoring substitution left-recursion removal

Left factoring term IFIER IFIER [ expression ] factor out common prefix term IFIER after_identifier after_identifier ε [ expression ] [ FOLLOW(after_identifier)

Substitution S A a b A a ε replace non-terminal by its alternative S a a b a b

Left-recursion removal N N α β replace by N βm M αm ε example β βα βαα βααα... N α β expression expression - term term expression term tail tail - term tail ε

Exercise (7 min.) make the following grammar LL(1) expression expression + term expression - term term term term * factor term / factor factor factor ( expression ) func-call identifier constant func-call identifier ( expr-list? ) expr-list expression (, expression)* and what about S if E then S (else S)?

Answers

automatic generation program text language grammar parser generator lexical analysis tokens syntax analysis AST context handling LL(1) push-down automaton annotated AST

LL(1) push-down automaton transition table state look-ahead token (top of stack) + ( ) EOF input expression EOF expression EOF expression term rest-expr term rest-expr term ( expression ) rest-expr + expression ε ε stack right-hand side of production

LL(1) push-down automaton prediction stack input input aap + ( noot + mies ) EOF replace non-terminal by transition entry state look-ahead token (top of stack) + ( ) EOF input expression EOF expression EOF expression term rest-expr term rest-expr term ( expression ) rest-expr + expression ε ε

LL(1) push-down automaton prediction stack expression EOF input aap + ( noot + mies ) EOF replace non-terminal by transition entry state look-ahead token (top of stack) + ( ) EOF input expression EOF expression EOF expression term rest-expr term rest-expr term ( expression ) rest-expr + expression ε ε

LL(1) push-down automaton prediction stack term rest-expr EOF input aap + ( noot + mies ) EOF replace non-terminal by transition entry state look-ahead token (top of stack) + ( ) EOF input expression EOF expression EOF expression term rest-expr term rest-expr term ( expression ) rest-expr + expression ε ε

LL(1) push-down automaton prediction stack input rest-expr EOF aap + ( noot + mies ) EOF pop matching token state look-ahead token (top of stack) + ( ) EOF input expression EOF expression EOF expression term rest-expr term rest-expr term ( expression ) rest-expr + expression ε ε

LL(1) push-down automaton prediction stack rest-expr EOF input + ( noot + mies ) EOF replace non-terminal by transition entry state look-ahead token (top of stack) + ( ) EOF input expression EOF expression EOF expression term rest-expr term rest-expr term ( expression ) rest-expr + expression ε ε

LL(1) push-down automaton prediction stack input + expression EOF + ( noot + mies ) EOF pop matching token state look-ahead token (top of stack) + ( ) EOF input expression EOF expression EOF expression term rest-expr term rest-expr term ( expression ) rest-expr + expression ε ε