Dara Hazeghi 12/22/11 COMS 4115 Fall 2011

Similar documents
An Overview of Compilation

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

JJTree. The Compilation Task. Automated? JJTree. An easier way to create an Abstract Syntax Tree

LECTURE 3. Compiler Phases

CS453 Compiler Construction

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

Undergraduate Compilers in a Day

Introduction to Programming Using Java (98-388)

CS 314 Principles of Programming Languages. Lecture 9

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

Compilers. Compiler Construction Tutorial The Front-end

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

CA4003 Compiler Construction Assignment Language Definition

Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 2. Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 4

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Program Representations

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

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

CPS 506 Comparative Programming Languages. Syntax Specification

Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications

UNIVERSITY OF COLUMBIA ADL++ Architecture Description Language. Alan Khara 5/14/2014

Lexical Analysis. Introduction

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

Language Reference Manual simplicity

Semantic actions for declarations and expressions

Semantic actions for declarations and expressions. Monday, September 28, 15

1 Lexical Considerations

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Project 3 - Parsing. Misc Details. Parser.java Recursive Descent Parser... Using Lexer.java

CSE302: Compiler Design

Syntax and Grammars 1 / 21

A Tour of Language Implementation

CSE 401 Midterm Exam Sample Solution 11/4/11

Full file at

Compilers Project 3: Semantic Analyzer

Question Points Score

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Optimizing Finite Automata

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

ECE251 Midterm practice questions, Fall 2010

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

DEMO A Language for Practice Implementation Comp 506, Spring 2018

COSE312: Compilers. Lecture 1 Overview of Compilers

Abstract Syntax Trees

Mandatory Exercise 1 INF3110

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Last Time. What do we want? When do we want it? An AST. Now!

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CS 406/534 Compiler Construction Putting It All Together

Language Authors. Doug Bienstock (dmb2168) Chris D Angelo (cd2665) Zhaarn Maheswaran (zsm2103) Tim Paine (tkp2108) Kira Whitehouse (kbw2116)

Decaf Language Reference Manual

Decaf Language Reference

COMPILER CONSTRUCTION Seminar 02 TDDB44

Full file at

The Decaf Language. 1 Lexical considerations

CSE 340 Fall 2014 Project 4

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Lecture 16: Static Semantics Overview 1

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

Test I Solutions MASSACHUSETTS INSTITUTE OF TECHNOLOGY Spring Department of Electrical Engineering and Computer Science

The Design and Implementation of Gnu Compiler Collection

An ANTLR Grammar for Esterel

Attribute Grammars. Attribute Grammars

CS 403: Scanning and Parsing

CS2112 Fall Assignment 4 Parsing and Fault Injection. Due: March 18, 2014 Overview draft due: March 14, 2014

Compiler Techniques MN1 The nano-c Language

TDDD55 - Compilers and Interpreters Lesson 3


UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Project Overview. 1 Introduction. 2 A quick introduction to SOOL

Language Reference Manual

Mid-Term 2 Grades

Building a Parser Part III

cmps104a 2002q4 Assignment 3 LALR(1) Parser page 1

Introduction to Parsing

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Semantic actions for declarations and expressions

COMPILERS AND INTERPRETERS Lesson 4 TDDD16

Intermediate Representations

Code Genera*on for Control Flow Constructs

Compiler principles, PS1

GBIL: Generic Binary Instrumentation Language. Language Reference Manual. By: Andrew Calvano. COMS W4115 Fall 2015 CVN

Chapter 2 A Quick Tour

Properties of Regular Expressions and Finite Automata

CS164: Programming Assignment 2 Dlex Lexer Generator and Decaf Lexer

Computational Expression

Time : 1 Hour Max Marks : 30

ECE220: Computer Systems and Programming Spring 2018 Honors Section due: Saturday 14 April at 11:59:59 p.m. Code Generation for an LC-3 Compiler

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

The SPL Programming Language Reference Manual

Programming Assignment I Due Thursday, October 7, 2010 at 11:59pm

Implementing a VSOP Compiler. March 12, 2018

Announcements. Working in pairs is only allowed for programming assignments and not for homework problems. H3 has been posted

Jianning Yue Wookyun Kho Young Jin Yoon. AGL : Animation applet Generation Language

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Compilers. Lecture 2 Overview. (original slides by Sam

COP4020 Programming Assignment 2 - Fall 2016

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

Transcription:

Dara Hazeghi 12/22/11 COMS 4115 Fall 2011

Simple static imperative language for text processing Sparse, minimalist syntax C-like structure Allow programmer to easily and efficiently manipulate strings Strongly-typed to catch errors at compile-time Produce code that can be optimized and executed quickly

String as a primary data type Full set of operators for building, searching and transforming strings Maps for associating key-value pairs Procedural structure Functions, blocks, loops, conditionals All computation performed in expressions Generates linearized (low-level) C++ code as output Simplified expressions, no blocks, no loops

Variables and types Declaration: type name; String (text) - $ - $ str;! Number (integral) - # - # num;! Map (aggregate) %[k;v] - %[$;#] map;! Expressions Literals Number: 12345 String: str_literal! Assignment name <- expression! Unary and binary operators expr + expr or expr % expr or ^expr or See table Function calls name(expr 1 ; expr 2 ; expr 3 )! Rvalues (variables) Name Example: a <- b <- 3 + 5 / 4 3;!

Functions name, list of parameters, return type, block (containing function s code) name(type 1 name 1 ; type 2 name 2 -> type ret! "{ code block }! No return value, or no parameters (void): ^ Parameters passed by reference Program control starts in (required) main function main(^) -> # { code block }! Blocks List of variable declarations, followed by list of statements { decl 1 decl 2 stmt 1 stmt 2 }! Variables declared in block only valid in that block (scope rules) Statements Expressions see above expression;! Blocks same syntax as above Conditionals test expression must be numeric, second clause optional [ expr ] block if-true![ ] block if-false! [ expr ] block if-true! Loops test expression must be numeric < expr > block! Return expression may be empty -> expr opt ;!

// hello.str - comment! main(^) -> # " "// main take no input, returns a number! {! "$name;" " "// string variable! }! "write("enter your name:\t"); "// write string to the output stream! "name <- read(); " "// read string from the input stream, store in variable! "print_banner("hello " +! " "name + "!"; 10); "// call print_banner function with 2 parameters! "-> 0; " " "// return the value 0 to the calling environment! print_banner($ msg; # max) -> ^ "// print_banner takes string and number, returns nothing! {! "#i; " " "// number variable! }! "i <- 0; " "// assignment: i set to 0! "<i < max> " "// loop: while(i < max)! "{ " " " "write(msg + "\n"); "// begin loop block! "// + concatenates two strings, which are then written out! " " "msg <- " " + msg;! "i <- i + 1;" "// + adds two numbers! "} " " "// end loop block! "<i > 0> " "// another loop! "{! " "write(msg + "\n");! " "msg <- msg - 1;! " "}! "i <- i - 1;! "write(msg + "\n");!

$./strlang c hello.str! #include "strlib.h"! int main(void);! void print_banner(string&, int&);! int main(void)! {! string name_1("");! string reg_str_25_("");! string reg_str_24_("");! int reg_num_23_(0);! string reg_str_22_("");! string reg_str_21_("");! string reg_str_20_("");! string reg_str_19_("");! int reg_num_18_(0);! int reg_num_26_(0);! reg_str_25_ = "Enter your name:\t";! write( reg_str_25_);! reg_str_24_ = read();! name_1 = reg_str_24_;! reg_num_23_ = 10;! reg_str_21_ = "!";! reg_str_19_ = "Hello ";! reg_str_20_ = str_concat( reg_str_19_, name_1);! reg_str_22_ = str_concat( reg_str_20_, reg_str_21_);! print_banner( reg_str_22_, reg_num_23_);! reg_num_18_ = 0;! return reg_num_18_;! return reg_num_26_;! }! void print_banner(string& msg_4, int& max_4)! {! int i_4(0);! int reg_num_17_(0);! string reg_str_16_("");! string reg_str_15_("");! string reg_str_14_("");! string reg_str_13_("");! int reg_num_12_(0);! int reg_num_11_(0);! int reg_num_10_(0);! string reg_str_9_("");! string reg_str_8_("");! string reg_str_7_("");! int reg_num_6_(0);! int reg_num_5_(0);! int reg_num_4_(0);! int reg_num_3_(0);! int reg_num_2_(0);! string reg_str_1_("");! string reg_str_0_("");! reg_num_17_ = 0;! i_4 = reg_num_17_;! goto LABEL_3;! LABEL_2: ;! reg_str_15_ = "\n";! reg_str_16_ = str_concat(msg_4, reg_str_15_);! write( reg_str_16_);! reg_str_13_ = " ";! reg_str_14_ = str_concat( reg_str_13_, msg_4);! msg_4 = reg_str_14_;! reg_num_11_ = 1;! reg_num_12_ = i_4 + reg_num_11_;! i_4 = reg_num_12_;! LABEL_3: ;! reg_num_10_ = i_4 < max_4;! if( reg_num_10_) goto LABEL_2;! goto LABEL_1;! LABEL_0: ;! reg_str_8_ = "\n";! reg_str_9_ = str_concat(msg_4, reg_str_8_);! write( reg_str_9_);! reg_num_6_ = 1;! reg_str_7_ = str_substr(msg_4, reg_num_6_);! msg_4 = reg_str_7_;! reg_num_4_ = 1;! reg_num_5_ = i_4 - reg_num_4_;! i_4 = reg_num_5_;! LABEL_1: ;! reg_num_2_ = 0;! reg_num_3_ = i_4 > reg_num_2_;! if( reg_num_3_) goto LABEL_0;! reg_str_0_ = "\n";! reg_str_1_ = str_concat(msg_4, reg_str_0_);! write( reg_str_1_);! return;! }!

aiguille:strlang dara$./strlang -e hello.str hello! aiguille:strlang dara$./hello! Enter your name: "strlang! aiguille:strlang dara$!

6 step compilation process scanner split source input into stream of tokens parser parse tokens to generate abstract syntax tree symtab build symbol table for all identifiers in the AST check validate AST and annotate it with type information simple simplify AST by converting expressions to SSA-like form, flattening blocks and replacing loops with gotos output dump simple IR as C++ code (pretty-printer) Final step C++ compiler generates executable from code output by strlang compiler

Major goals 0) Gain experience in language design 1) Come up with a coherent design 2) Implement it cleanly and correctly 3) Make the language/compiler useful 4) Complete deliverables by deadline Success? strlang design is reasonably clear, comprehensible Compiler meets the design spec, finished by deadline Code is generally clean Testsuite passes, no major known defects But not quite as useful as hoped for Missing split operator for strings Syntax can be restrictive

Working as 1-person group has pluses and minuses + having control of design allows focus Able to emphasize simplicity and feasability in design No issues with integration, coding could be done rapidly and efficiently - could have used some feedback in coding phase Easy to get tunnel vision, miss important design considerations Not infrequently thinking, there must be a better way to do this Overall, did benefit from earlier group participation Design phase was simplified - had already gone over many of the major issues Planning is key deadlines, well-defined milestones, building the testsuite as you go Writing a compiler is fun everybody should do it at least once!

So long and thanks for all the strings!