Modules, Structs, Hashes, and Operational Semantics

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

The Substitution Model

Semantics of programming languages

Semantics of programming languages

Semantics of programming languages

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Application: Programming Language Semantics

The Substitution Model. Nate Foster Spring 2018

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

Compiler Theory. (Semantic Analysis and Run-Time Environments)

CMSC 330: Organization of Programming Languages

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

7. Introduction to Denotational Semantics. Oscar Nierstrasz

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

CMSC 330: Organization of Programming Languages. Operational Semantics

Programming Languages Fall 2013

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

CSC 467 Lecture 3: Regular Expressions

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

Motivation was to facilitate development of systems software, especially OS development.

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

Syntax-Directed Translation. Lecture 14

CMSC 330: Organization of Programming Languages

Lexical Analysis. Lecture 2-4

CS 11 Ocaml track: lecture 7

CS164: Midterm I. Fall 2003

CS 432 Fall Mike Lam, Professor. Code Generation

1 Lexical Considerations

Motivation was to facilitate development of systems software, especially OS development.

Programming Languages & Translators PARSING. Baishakhi Ray. Fall These slides are motivated from Prof. Alex Aiken: Compilers (Stanford)

CMSC 330: Organization of Programming Languages

Propositional Calculus: Boolean Functions and Expressions. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

CSCI312 Principles of Programming Languages!

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

1. Consider the following program in a PCAT-like language.

CS 360 Programming Languages Interpreters

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

CMSC 330: Organization of Programming Languages

IC Language Specification

Lexical Analysis. Lecture 3-4

A simple syntax-directed

Syntax Intro and Overview. Syntax

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

Last class. CS Principles of Programming Languages. Introduction. Outline

CS 6353 Compiler Construction Project Assignments

CSC 7101: Programming Language Structures 1. Operational Semantics. Winskel, Ch. 2 Slonneger and Kurtz Ch 8.4, 8.5, 8.6. Operational vs.

CSE 12 Abstract Syntax Trees

CPS 506 Comparative Programming Languages. Syntax Specification

Types and Static Type Checking (Introducing Micro-Haskell)

Regular Expressions. Regular Expressions. Regular Languages. Specifying Languages. Regular Expressions. Kleene Star Operation

Principles of Programming Languages COMP251: Syntax and Grammars

CS 6353 Compiler Construction Project Assignments

LECTURE 3. Compiler Phases

CS 11 Ocaml track: lecture 6

Types and Static Type Checking (Introducing Micro-Haskell)

CMSC 330: Organization of Programming Languages. Context Free Grammars

Writing an Interpreter Thoughts on Assignment 6

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

CSCE 314 Programming Languages

In Our Last Exciting Episode

Semester Review CSC 301

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Parsing. CS321 Programming Languages Ozyegin University

Java Bytecode (binary file)

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

Chapter 1 Getting Started

Lexical Analysis. Finite Automata

Programming Languages Third Edition. Chapter 7 Basic Semantics

Formal Systems and their Applications

Administrivia. Lexical Analysis. Lecture 2-4. Outline. The Structure of a Compiler. Informal sketch of lexical analysis. Issues in lexical analysis

CSc 520 Principles of Programming Languages

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

Type Systems Winter Semester 2006

The PCAT Programming Language Reference Manual

CSE 413 Final Exam. June 7, 2011

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

Lexical Analysis. Lecture 3. January 10, 2018

Semantic Analysis. Compiler Architecture

Introduction to Parsing Ambiguity and Syntax Errors

Compilers. Compiler Construction Tutorial The Front-end

The semantics of a programming language is concerned with the meaning of programs, that is, how programs behave when executed on computers.

CMSC 330: Organization of Programming Languages. Context Free Grammars

Type Systems. Today. 1. Organizational Matters. 1. Organizational Matters. Lecture 1 Oct. 20th, 2004 Sebastian Maneth. 1. Organizational Matters

The role of semantic analysis in a compiler

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

CS252 Advanced Programming Language Principles. Prof. Tom Austin San José State University Fall 2013

Programming Languages Third Edition

CS164: Programming Assignment 2 Dlex Lexer Generator and Decaf Lexer

Introduction to Parsing Ambiguity and Syntax Errors

This class is about understanding how programs work

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

2.2 Syntax Definition

Lexical Analysis. Chapter 2

Transcription:

CS 152: Programming Language Paradigms Modules, Structs, Hashes, and Operational Semantics Prof. Tom Austin San José State University

Lab Review (in-class)

Modules

Review Modules from HW 1 (in-class)

How do we organize code in Java? Packages provide units of code Keywords specify method access

In Java, keywords specify access public protected no keyword (package access) private

Java method access levels public: everyone protected: subclasses and code in the same package no keyword (package access): code in the same package private: only code in the current class

Organizing Code in Racket Racket organizes code in terms of imports and exports. The library specifies which code is available to others by the provide keyword. anything not named by provide is hidden. If you want to use the library, you use the keyword require.

Organizing Code in Racket Exports specify public values: (provide big-add) Imports specify code dependencies: (require "big-num.rkt")

Structs and Hashes

Structs Structures allow us to create more sophisticated data structures: (struct name (field1 field2 )) Once we have a structure, we can destructure it with the match keyword to get at the contents. <Example in class>

Hashes Hashes are maps of key/value pairs. Unlike Java, hashes are immutable. <example in class>

Formal Semantics

Why do we need formal semantics?

Everyone knows what an if statement does, right? if true then else x = 1 At the end of this code snippet, the value of x will be 1 x = 0

Everyone knows what an if statement does, right? if false then x = 1 else x = 0 At the end of this code snippet, the value of x will be 0

Everyone knows what an if statement does, right? if 0 then x = 1 else x = 0 Will x be set to 0, like in C/C++? Or will it be an error, like in Java? Will x be set to 1, like in Ruby?

Everyone knows what an if statement does, right? x = if true then 1 Is assignment valid or an error? else 0

Formal semantics define how a language works concisely and with minimal ambiguity.

A Review of Compilers source code Lexer/ Tokenizer tokens Parser We don't care about lexing or parsing. Compiler Machine code Abstract Syntax Tree (AST) We don't care if we have a compiler or interpreter Interpreter Commands

A Review of Compilers source code Lexer/ Tokenizer tokens Parser We don't care about lexing or parsing. Compiler Machine code Abstract Syntax Tree (AST) We don't care if we have a compiler or interpreter Interpreter Commands

Abstract ASTs are the key to understanding a language Syntax Tree (AST)

Bool* Language e ::= true false if e then e else e expressions: constant true constant false conditional Despite appearances, these are really ASTs

Values in Bool* v ::= true false values: constant true constant false

Formal Semantic Styles Operational semantics Big-step (or "natural") Small-step (or "structural") Axiomatic semantics Denotational semantics

Formal Semantic Styles Operational semantics Big-step (or "natural") Small-step (or "structural") Axiomatic semantics Denotational semantics

Operational semantics specify how expressions should be evaluated. There are two different approaches. Small-step semantics evaluate an expression until it is in normal form & cannot be evaluated any further.

In contrast, big-step operational semantics evaluate every expression to a value. Big-step rules tend to have a recursive structure.

Big-Step Evaluation Relation An expression e evaluates to e v a value v.

Big-Step Evaluation Relation Preconditions limits when the rule applies e v

Big-step semantics for Bool* B-IfTrue e1 true e2 v if e1 then e2 else e3 v B-IfFalse e1 false e3 v if e1 then e2 else e3 v B-Value v v

true true if true Bool* big-step example then false else true false false false if (if true then false else true) then true else false false false false

Converting our rules into code (in-class)

Bool* extension: numbers Users demand a new feature numbers! We will add 3 new features: Numbers, represented by n succ, which takes a number and returns the next highest number. pred, which takes a number and returns the next lowest number.

Extended Bool* Language e ::= true false if e then e else e n succ e pred e Let's extend our semantics to handle these new language constructs

Lab: Write a Bool* Interpreter Starter code is available on the course website Extend Bool* with numbers, succ, and pred

Adding State to Semantics

SpartanLang e ::=!x v x:=e e;e e op e if e then e else e end while e do e end dereferencing values assignment sequence binary operations conditionals while loops

SpartanLang (continued) v ::= i b integers booleans op ::= + - \ * < > <= >= binary operators

Bool* vs. SpartanLang evaluation Bool* relation: e v SpartanLang relation: e,σ v,σ' A "store", represented by the Greek letter sigma

The Store A mapping of references to values Roughly analogous to the heap in Java

Key store operations σ(x) get the value for reference x. σ[x:=v] create a copy of store σ, except reference x has value v.

Special syntax for references In languages like ML, references are accessed with special syntax: x = ref 42 creates a new reference with value 42 and stores the reference in variable x. x := 7 changes the value of the reference to 7.!x gets the value of the reference that x refers to.

HW 2: Write an Interpreter for SpartanLang. Details in Canvas