A Tour of Language Implementation

Similar documents
CSCE 314 Programming Languages. Type System

CSCE 314 Programming Languages

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

General Concepts. Abstraction Computational Paradigms Implementation Application Domains Influence on Success Influences on Design

Syntax and Grammars 1 / 21

COMPILER DESIGN LECTURE NOTES

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

LECTURE 18. Control Flow

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

The role of semantic analysis in a compiler

Com S 541. Programming Languages I

Compiling Techniques

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

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

Why are there so many programming languages? Why do we have programming languages? What is a language for? What makes a language successful?

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CMSC430 Spring 2014 Midterm 2 Solutions

A Simple Syntax-Directed Translator

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

CSE302: Compiler Design

Syntax. A. Bellaachia Page: 1

Syntax-Directed Translation. Lecture 14

Lecture 1: Course Introduction

Compilation 2012 The What and Why of Compilers

CS 415 Midterm Exam Spring 2002

Introduction to Scientific Computing Languages

CSCI 3136 Principles of Programming Languages

Programmiersprachen (Programming Languages)

Introduction to Scientific Computing Languages

Structure of Programming Languages Lecture 5

Introduction to Compiler Design

CPS 506 Comparative Programming Languages. Syntax Specification

Formal Languages and Grammars. Chapter 2: Sections 2.1 and 2.2

Programming Languages, Summary CSC419; Odelia Schwartz

CMSC 330: Organization of Programming Languages

Why are there so many programming languages?

Concepts of Programming Languages

LECTURE 1. Overview and History

Outline. Programming Languages 1/16/18 PROGRAMMING LANGUAGE FOUNDATIONS AND HISTORY. Current

Code Generation Introduction

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Just In Time Compilation

Topic I. Introduction and motivation References: Chapter 1 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

CS 3304 Comparative Languages. Lecture 1: Introduction

When do We Run a Compiler?

Lecture 1: Course Introduction

Recap: Printing Trees into Bytecodes

The Structure of a Syntax-Directed Compiler

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

CSCE 314 Programming Languages. Functional Parsers

Compiler construction 2009

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

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

CSE 413 Programming Languages & Implementation. Hal Perkins Winter 2019 Grammars, Scanners & Regular Expressions

COP 3402 Systems Software. Lecture 4: Compilers. Interpreters

CSc 453 Interpreters & Interpretation

Earlier edition Dragon book has been revised. Course Outline Contact Room 124, tel , rvvliet(at)liacs(dot)nl

Type Inference. Prof. Clarkson Fall Today s music: Cool, Calm, and Collected by The Rolling Stones

Introduction to Lexical Analysis

1 Lexical Considerations

Principles of Computer Architecture. Chapter 5: Languages and the Machine

Compilation I. Hwansoo Han

Semantic Analysis. Lecture 9. February 7, 2018

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

Problem Score Max Score 1 Syntax directed translation & type

Logic Programming II & Revision

Lecture 16: Static Semantics Overview 1

CSCE 314 Programming Languages. Monadic Parsing

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CSC 326H1F, Fall Programming Languages. What languages do you know? Instructor: Ali Juma. A survey of counted loops: FORTRAN

Undergraduate Compilers in a Day

Simply-Typed Lambda Calculus

The Structure of a Syntax-Directed Compiler

Week 3: Compilers and Interpreters

Early computers (1940s) cost millions of dollars and were programmed in machine language. less error-prone method needed

CSCI312 Principles of Programming Languages!

CS558 Programming Languages

Java: framework overview and in-the-small features

Compiling Techniques

Compiler construction 2009

CSCI 334: Principles of Programming Languages. Lecture 2: Lisp Wrapup & Fundamentals. Higher-Order Functions. Activity

Compiling Regular Expressions COMP360

Lexical and Syntax Analysis

G Programming Languages - Fall 2012

Compiler construction Compiler Construction Course aims. Why learn to write a compiler? Lecture 1

CS 415 Midterm Exam Fall 2003

CSE 413 Programming Languages & Implementation. Hal Perkins Autumn 2012 Grammars, Scanners & Regular Expressions

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

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

PLAGIARISM. Administrivia. Course home page: Introduction to Programming Languages and Compilers

Introduction to Programming Languages and Compilers. CS164 11:00-12:00 MWF 306 Soda

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

Transcription:

1 CSCE 314: Programming Languages Dr. Flemming Andersen A Tour of Language Implementation

Programming is no minor feat. Prometheus Brings Fire by Heinrich Friedrich Füger. Image source: https://en.wikipedia.org/wiki/prometheus 2

3 Programming Language Characteristics Different approaches to describe computations, to instruct computing devices E.g., Imperative, declarative, functional Different approaches to communicate ideas between humans E.g., Procedural, object-oriented, domain-specific languages Programming languages need to have a specification: meaning (semantics) of all sentences (programs) of the language should be unambiguously specified

4 Programming Language Expressiveness Different levels of abstraction CSCE 314 TAMU Fall 2017 More abstract Haskell, Prolog sum[1..100] Scheme, Java mynum.add(5) C Assembly language Machine language 10111001010110 iadd i++;

In the beginning Harvard Mark I Plugboard Tape reader Image source: https://www.cs.auckland.ac.nz 5

6 Evolution of Languages 1940 s: connecting wires to represent 0 s and 1 s 1950 s: assemblers, FORTRAN, COBOL, LISP 1960 s: ALGOL, BCPL ( B C), SIMULA 1970 s: Prolog, FP, ML, Miranda 1980 s: Eiffel, C++ 1990 s: Haskell, Java, Python 2000 s: D, C#, Spec#, F#, X10, Fortress, Scala, Ruby,... 2010 s: Agda, Coq... Evolution has been and is toward higher level of abstraction

A notable exception: Konrad Zuse The Z3 Computer (1941) Sources: http://cornpedia.net/ and http://imgur.com/gallery/ujn7l 7

Zuse s Plankalkül (Plan Calculus) Heinz Rutishauser, one of the founders of ALGOL: The very first attempt to devise an algorithmic language was undertaken in 1948 by K. Zuse. His notation was quite general, but the proposal never attained the consideration it deserved. Source: http://lol54.ru/tech/hitech/110903-kompjutery-konrada-cuze.html 8

9 Defining a Programming Language Syntax: Defines the set of valid programs Usually defined with the help of grammars and other conditions if-statement ::= if cond-expr then stmt else stmt if cond-expr then stmt cond-expr ::=... stmt ::=... Semantics: Defines the meaning of programs Defined, e.g., as the effect of individual language constructs to the values of program variables if cond then true-part else false-part If cond evaluates to true, the meaning is that of true-part; if cond evaluates to false, the meaning is that of false-part.

10 Implementing a Programming Language Task is to undo abstraction. From the source: int i; i = 2; i = i + 7; to assembly (this is actually Java bytecode): iconst_2 // Put integer 2 on stack istore_1 // Store the top stack value at location 1 iload_1 // Put the value at location 1 on stack bipush 7 // Put the value 7 on the stack iadd // Add two top stack values together istore_1 // The sum, on top of stack, stored at location 1 to machine language: 00101001010110 01001010100101... CSCE 314 TAMU Fall 2017

11 CSCE 314 TAMU Fall 2017 Implementing a Programming Language How to Undo the Abstraction Source program Optimizer Lexer Parser Type checker Code generator Machine code I/O Machine Bytecode JIT Interpreter I/O Virtual machine I/O

12 Compiling and Interpreting Typically compiled languages: C, C++, Eiffel, FORTRAN Java, C# (compiled to bytecode) Typically interpreted languages: Python, Perl, Prolog, LISP Both compiled and interpreted: Haskell, ML, Scheme

13 Compiling and Interpreting (cont.) Borderline between interpretation and compilation not clear (not that important either) Same goes with machine code vs. byte code. Examples of modern compiling/interpreting/executing scenarios: C and C++ can be compiled to LLVM bytecode Java compiled to bytecode, bytecode interpreted by JVM, unless it is first JITted to native code, which can then be run on a virtual machine such as VMWare

14 Lexical Analysis From a stream of characters if (a == b) return; to a stream of tokens: keyword[ if ] symbol[ ( ] identifier[ a ] symbol[ == ] identifier[ b ] symbol[ ) ] keyword[ return ] symbol[ ; ]

Syntactic Analysis (Parsing) From a stream of characters if (a == b) return; to a stream of tokens: keyword[ if ] symbol[ ( ] identifier[ a ] symbol[ == ] identifier[ b ] symbol[ ) ] keyword[ return ] symbol[ ; ] to a syntax tree if-statement expression equality operator statement return stmt identifier identifier a b 15

Type Checking if (a == b) return; Annotate the syntax tree with types, check that types are used correctly if-statement ok expression bool equality operator integer equality statement ok return stmt void identifier int a identifier int b 16

Optimization if-statement ok int a = 10; int b = 20 - a; if (a == b) return; if-statement ok CSCE 314 TAMU Fall 2017 Constant propagation can deduce that always a==b, allowing the optimizer to transform the tree: return stmt void expression bool statement ok constant bool statement ok equality operator integer equality return stmt void true return stmt void identifier int a identifier int b 17

18 Code Generation Code generation is essentially undoing abstractions, until code is executable by some target machine: Control structures become jumps and conditional jumps to labels (essentially goto statements) Variables become memory locations Variable names become addresses to memory locations Abstract data types etc. disappear. What is left is data types directly supported by the machine such as integers, bytes, floating point numbers, etc. Expressions become loads of memory locations to registers, register operations, and stores back to memory

19 Phases of Compilation/Execution Characterized by Errors Detected CSCE 314 TAMU Fall 2017 Alan J. Perlis (The first recipient of the Turing Award): For all its power, the computer is a harsh taskmaster. Its programs must be correct, and what we wish to say must be said accurately in every detail.

20 Phases of Compilation/Execution Characterized by Errors Detected CSCE 314 TAMU Fall 2017 Lexical analysis: 5abc a === b Syntactic analysis: if + then; int f(int a]; Type checking: void f(); int a; a + f(); Execution time: int a[100]; a[101] = 5;

21 Homework Read The Beauty of Programming by Linus Torvalds