Class Information ANNOUCEMENTS

Similar documents
CS 314 Principles of Programming Languages. Lecture 9

CS 314 Principles of Programming Languages Fall 2017 A Compiler and Optimizer for tinyl Due date: Monday, October 23, 11:59pm

UNIVERSITY OF CALIFORNIA

Question Points Score

Error Detection in LALR Parsers. LALR is More Powerful. { b + c = a; } Eof. Expr Expr + id Expr id we can first match an id:

Class Information ANNOUCEMENTS

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

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

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

Compiler Code Generation COMP360

CS 314 Principles of Programming Languages. Lecture 16

Parsing II Top-down parsing. Comp 412

Alternatives for semantic processing

ECE251 Midterm practice questions, Fall 2010

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412

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

Syntax Analysis, III Comp 412

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

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

Project 2 Interpreter for Snail. 2 The Snail Programming Language

Table-Driven Top-Down Parsers

CS 314 Principles of Programming Languages

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

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

Problem Set 6 (Optional) Due: 5pm on Thursday, December 20

CS415 Compilers. Lexical Analysis

CS241 Final Exam Review

CS5363 Final Review. cs5363 1

COP5621 Exam 3 - Spring 2005

Introduction to Compiler

CS /534 Compiler Construction University of Massachusetts Lowell

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

CS 314 Principles of Programming Languages. Lecture 11

INSTITUTE OF AERONAUTICAL ENGINEERING (AUTONOMOUS)

CSCI312 Principles of Programming Languages

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

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

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

Syntax. In Text: Chapter 3

CSCI 1260: Compilers and Program Analysis Steven Reiss Fall Lecture 4: Syntax Analysis I

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

CSE302: Compiler Design

Recursive Descent Parsers

COP4020 Programming Assignment 2 - Fall 2016

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

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

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

Lexical Analysis. Introduction

ASTs, Objective CAML, and Ocamlyacc

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

Lecture 14 Sections Mon, Mar 2, 2009

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

CS2383 Programming Assignment 3

Syntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask?

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

CS415 Compilers. Intermediate Represeation & Code Generation

Programming Language Syntax and Analysis

CS 536 Midterm Exam Spring 2013

Compilers. Lecture 2 Overview. (original slides by Sam

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

Programming Languages (CS 550) Lecture 4 Summary Scanner and Parser Generators. Jeremy R. Johnson

CS 406/534 Compiler Construction Putting It All Together

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

Project 1: Scheme Pretty-Printer

COP4020 Spring 2011 Midterm Exam

CS164: Midterm I. Fall 2003

A clarification on terminology: Recognizer: accepts or rejects strings in a language. Parser: recognizes and generates parse trees (imminent topic)

Compiler Design (40-414)

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Friday 20th May 2016 Time: 14:00-16:00

LECTURE 7. Lex and Intro to Parsing

Chapter 4. Lexical and Syntax Analysis

1. Explain the input buffer scheme for scanning the source program. How the use of sentinels can improve its performance? Describe in detail.

Today. Assignments. Lecture Notes CPSC 326 (Spring 2019) Quiz 2. Lexer design. Syntax Analysis: Context-Free Grammars. HW2 (out, due Tues)

How do LL(1) Parsers Build Syntax Trees?

CS 132 Compiler Construction, Fall 2012 Instructor: Jens Palsberg Multiple Choice Exam, Nov 6, 2012

G.PULLAIH COLLEGE OF ENGINEERING & TECHNOLOGY

Question Marks 1 /12 2 /6 3 /14 4 /8 5 /5 6 /16 7 /34 8 /25 Total /120

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

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

Computing Inside The Parser Syntax-Directed Translation. Comp 412

10/26/17. Attribute Evaluation Order. Attribute Grammar for CE LL(1) CFG. Attribute Grammar for Constant Expressions based on LL(1) CFG

Syntax Analysis, VI Examples from LR Parsing. Comp 412

ECE573 Introduction to Compilers & Translators

Introduction to Parsing. Lecture 8

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 4

Configuration Sets for CSX- Lite. Parser Action Table

CSCI 565 Compiler Design and Implementation Spring 2014

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

Compiler construction in4303 lecture 3

Compilers. Compiler Construction Tutorial The Front-end

Part I: Multiple Choice Questions (40 points)

Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;}

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

Semantic Analysis. Lecture 9. February 7, 2018

COP4020 Programming Assignment 2 Spring 2011

CS 536. Class Meets. Introduction to Programming Languages and Compilers. Instructor. Key Dates. Teaching Assistant. Charles N. Fischer.

Topic 3: Syntax Analysis I

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

Introduction to Parsing

QUESTION BANK CHAPTER 1 : OVERVIEW OF SYSTEM SOFTWARE. CHAPTER 2: Overview of Language Processors. CHAPTER 3: Assemblers

Transcription:

Class Information ANNOUCEMENTS Third homework due Friday, October 13, 11:59pm. First project will be posted soon. Midterm exam: Friday, October 27, in class. Don t forget to work on your C and Linux skills (ilab). CS 314 fall 17, lecture 10 page 1

Review - Recursive Descent Parsing Now, we can produce a simple recursive descent parser from our favorite LL(1) expression grammar. Recursive descent is one of the simplest parsing techniques used in practical compilers: Each non terminal has an associated parsing procedure that can recognize any sequence of tokens generated by that non terminal. There is a main routine to initialize all globals (e.g.: token) and call the start symbol. On return, check whether token == eof, and whether errors occurred. (Note: left-to-right evaluation of expressions). Within a parsing procedure, both non terminals and terminals can be matched : non terminal A call parsing procedure for A token t compare t with current input token; if match, consume input, otherwise ERROR Parsing procedures may contain code that performs some useful computation (syntax directed translation). CS 314 fall 17, lecture 10 page 2

Syntax Directed Translation Examples: 1. Interpreter (done last lecture) 2. Code generator 3. Type checker 4. Performance estimator Use hand-written recursive descent LL(1) parser CS 314 fall 17, lecture 10 page 3

Syntax-Directed Translation Skeleton <expr> ::= + <expr> <expr> <digit> <digit> :: = 0 1 2 3... 9... expr: case +: token := next token( ); /*1*/ expr( ); /*2*/ expr( ); /*3*/ return; case 0..9: /*4*/ return digit( );... digit: case 1: token := next token( ); /*5*/ return ; case 2: token := next token( ); /*6*/ return;... This skeleton code implements a tree walk over the parse tree. Define return values and put code where you need it. CS 314 fall 17, lecture 10 page 4

Example: Interpreter <expr> ::= + <expr> <expr> <digit> <digit> :: = 0 1 2 3... 9 int expr: // returns value of expression int val1, val2; // values case +: token := next token( ); val1 = expr( ); val2 = expr( ); return val1+val2; case 0..9: return digit( ); int digit: // returns value of constant case 1: token := next token( ); return 1; case 2: token := next token( ); return 2;... CS 314 fall 17, lecture 10 page 5

Example: Interpreter What happens when you parse subprogram + 2+12? The parsing produces: 5 CS 314 fall 17, lecture 10 page 6

Example: Simple Code Generation <expr> ::= + <expr> <expr> <digit> <digit> :: = 0 1 2 3... 9 int expr: // returns target register of operation int target reg; // fresh register int reg1, reg2; // other registers case +: token := next token( ); target reg = next register( ); reg1 = expr( ); reg2 = expr( ); CodeGen(ADD, reg1, reg2, target reg); return target reg; case 0..9: return digit( ); int digit: // returns target register of operation int target reg = next register( ); // fresh register case 1: token := next token( ); CodeGen(LOADI, 1, target reg); return target reg; case 2: token := next token( ); CodeGen(LOADI, 2, target reg); return target reg;... CS 314 fall 17, lecture 10 page 7

Example: Simple Code Generation What happens when you parse subprogram + 2+12? Assumption: first call to next register( ) will return 1 The parsing produces: loadi 2 => r2 loadi 1 => r4 loadi 2 => r5 add r4, r5 => r3 add r2, r3 => r1 CS 314 fall 17, lecture 10 page 8

Example: Simple Type Checker <expr> ::= + <expr> <expr> <digit> <digit> :: = 0 1 2 3... 9 string expr: // returns type expression string type1, type2; // other type expressions case +: token := next token( ); type1 = expr( ); type2 = expr( ); if (type1 == int and type2 == int ) return int else return error ; case 0..9: return digit( ); string digit: // returns type expression case 1: token := next token( ); return int ; case 2: token := next token( ); return int ;... CS 314 fall 17, lecture 10 page 9

Example: Simple Type Checker What happens when you parse subprogram + 2+12? The parsing produces: int CS 314 fall 17, lecture 10 page 10

Example: Basic Performance Predictor <expr> ::= + <expr> <expr> <digit> <digit> :: = 0 1 2 3... 9 int expr: // returns cycles needed to compute expression int cyc1, cyc2; // subexpression cycles case +: token := next token( ); cyc1 = expr( ); cyc2 = expr( ); return cyc1+cyc2+2 // ADD takes 2 cycles; case 0..9: return digit( ); int digit: // returns cycles case 1: token := next token( ); return 1; // LOADI takes 1 cycle case 2: token := next token( ); return 1; // LOADI takes 1 cycle... CS 314 fall 17, lecture 10 page 11

Example: Basic Performance Predictor What happens when you parse subprogram + 2+12? The parsing produces: 7 CS 314 fall 17, lecture 10 page 12

Project1: tinyl Language <program> ::= <stmtlist>. <stmtlist> ::= <stmt> <morestmts> <morestmts> ::= ; <stmtlist> ɛ <stmt> ::= <assign> <print> <assign> ::= <variable> = <expr> <print> ::=! <variable> <expr> ::= + <expr> <expr> - <expr> <expr> * <expr> <expr> / <expr> <expr> <variable> <digit> <variable> :: = a b c d e f g h i j k x y z <digit> :: = 0 1 2 3... 9 CS 314 fall 17, lecture 10 page 13

Project 1: Structure Overview compiler tinyl program example: test1 compile example: compile test1 tinyl.out output always "tinyl.out" optimizer RISC machine code example: tinyl.out optimize example: optimize < tinyl.out RISC machine code output to stdout RISC machine code example: tinyl.out virtual machine sim input and output of execution CS 314 fall 17, lecture 10 page 14

Project 1: Peephole Optimization Goal: To replace a sequence of instructions by another, more efficient sequence. Constant folding example: loadi 5 => r4 loadi 7 => r5 can be replaced by loadi 12 => r6 add r4, r5 => r6 Strength reduction example: loadi 4 => r7 mult r6, r7 => r8 can be replaced by lshifti r6, 2 => r8 Original instructions are deleted. CS 314 fall 17, lecture 10 page 15

Next Lecture Things to do: Start programming in C. Check out the web for tutorials. Next time: Introduction to imperative programming Programming in C, pointers, explicit memory allocation Read Scott 5.1-5.3 (some background - chapter on CD) CS 314 fall 17, lecture 10 page 16