The syntax and semantics of Beginning Student

Similar documents
The syntax and semantics of Beginning Student

Welcome to CS 115 (Winter 2018)

Welcome to CS 115 (Winter 2019)

Welcome to CS 135 (Winter 2018) Themes of the course. Lectures. cs135/ Instructors: Sandy Graham, Paul Nijjar

CS115 - Module 1. Cameron Morland. Fall Cameron Morland CS115 - Module 1

Module 8: Local and functional abstraction

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

Local definitions and lexical scope

Local definitions and lexical scope. Local definitions. Motivating local definitions. s(s a)(s b)(s c), where s = (a + b + c)/2.

Welcome to CS 135 (Winter 2018)

Module 3: New types of data

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

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

Welcome to CS 135 (Fall 2018) Themes of the course. Lectures. cs135/

Programming Languages Third Edition

The design recipe. Readings: HtDP, sections 1-5. (ordering of topics is different in lectures, different examples will be used)

Project 2 Interpreter for Snail. 2 The Snail Programming Language

Working with recursion

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Chapter 3. Describing Syntax and Semantics

CS115 - Module 3 - Booleans, Conditionals, and Symbols

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

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

Functional Programming. Pure Functional Languages

Lecture 3: Recursion; Structural Induction

3. DESCRIBING SYNTAX AND SEMANTICS

Module 5: Lists. Readings: HtDP, Sections 9, 10.

Functions & First Class Function Values

CS 3360 Design and Implementation of Programming Languages. Exam 1

Chapter 3: Syntax and Semantics. Syntax and Semantics. Syntax Definitions. Matt Evett Dept. Computer Science Eastern Michigan University 1999

Lecture 2: SML Basics

Sardar Vallabhbhai Patel Institute of Technology (SVIT), Vasad M.C.A. Department COSMOS LECTURE SERIES ( ) (ODD) Code Optimization

Formal Semantics. Chapter Twenty-Three Modern Programming Languages, 2nd ed. 1

Project 1: Scheme Pretty-Printer

Semantics via Syntax. f (4) = if define f (x) =2 x + 55.

6.001 Notes: Section 15.1

CMSC 330: Organization of Programming Languages

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

Functional Programming. Pure Functional Languages

CSCC24 Functional Programming Scheme Part 2

CS 314 Principles of Programming Languages

Introduction to Scheme

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Syntax and Semantics

Habanero Extreme Scale Software Research Project

Syntax. A. Bellaachia Page: 1

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

CS 360 Programming Languages Interpreters

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.

(Refer Slide Time: 4:00)

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

CS102 Unit 2. Sets and Mathematical Formalism Programming Languages and Simple Program Execution

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

CS2104 Prog. Lang. Concepts

Syntax vs. semantics. Detour: Natural deduction. Syntax vs. semantics (cont d) Syntax = grammatical structure Semantics = underlying meaning

Welcome to CS 135 (Fall 2018)

Intro to semantics; Small-step semantics Lecture 1 Tuesday, January 29, 2013

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CS 314 Principles of Programming Languages. Lecture 16

Scheme: Expressions & Procedures

Programming Language Concepts, cs2104 Lecture 04 ( )

CS422 - Programming Language Design

Syntax and Grammars 1 / 21

(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

Implementing HtDP Teachpacks, Libraries, and Customized Teaching Languages

Case Study: Undefined Variables

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Assignment: 7. Due: Language level: Allowed recursion:

Parsing II Top-down parsing. Comp 412

CS 3360 Design and Implementation of Programming Languages. Exam 1

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

CSCI 3155: Lab Assignment 2

Fundamental Concepts. Chapter 1

CPS 506 Comparative Programming Languages. Syntax Specification

A Simple Syntax-Directed Translator

Visual Basic for Applications

Python Evaluation Rules

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics

CS 115 Lecture Notes Winter 2019

Evaluating Scheme Expressions

ECE570 Lecture 2: Types and Conventions

CS115 - Module 4 - Compound data: structures

CS 314 Principles of Programming Languages

ECE473 Lecture 2: Types and Conventions

Project 5 - The Meta-Circular Evaluator

YOUR NAME PLEASE: *** SOLUTIONS ***

CS1 Lecture 3 Jan. 18, 2019

CSE 3302 Programming Languages Lecture 2: Syntax

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

CS 320: Concepts of Programming Languages

A Brief Introduction to Scheme (I)

Formal Semantics of Programming Languages

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

CS1 Lecture 3 Jan. 22, 2018

Compiler Code Generation COMP360

Project 5 - The Meta-Circular Evaluator

Organization of Programming Languages CS3200/5200N. Lecture 11

Transcription:

The syntax and semantics of Beginning Student Readings: HtDP, Intermezzo 1 (Section 8). We are covering the ideas of section 8, but not the parts of it dealing with section 6/7 material (which will come later), and in a somewhat different fashion. CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 1

A program has a precise meaning and effect. A model of a programming language provides a way of describing the meaning of a program. Typically this is done informally, by examples. With Racket, we can do better. CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 2

Advantages in modelling Racket Few language constructs, so model description is short We don t need anything more than the language itself! No diagrams No vague descriptions of the underlying machine CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 3

Spelling rules for Beginning Student Identifiers are the names of constants, parameters, and user-defined functions. They are made up of letters, numbers, hyphens, underscores, and a few other punctuation marks. They must contain at least one non-number. They can t contain spaces or any of these: ( ), ; { } [ ]. Symbols start with a single quote followed by something obeying the rules for identifiers. CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 4

There are rules for numbers (integers, rationals, decimals) which are fairly intuitive. There are some built-in constants, like true and false. Of more interest to us are the rules describing program structure. For example: a program is a sequence of definitions and expressions. CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 5

Syntax and grammar There are three problems we need to address: 1. Syntax: The way we re allowed to say things.?is This Sentence Syntactically Correct 2. Semantics: the meaning of what we say. Trombones fly hungrily. 3. Ambiguity: valid sentences have exactly one meaning. Sally was given a book by Joyce. English rules on these issues are pretty lax. For Racket, we need rules that always avoid these problems. CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 6

Grammars To enforce syntax and avoid ambiguity, we can use grammars. For example, an English sentence can be made up of a subject, verb, and object, in that order. We might express this as follows: sentence = subject verb object The linguist Noam Chomsky formalized grammars in this fashion in the 1950 s. The idea proved useful for programming languages. CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 7

The textbook describes function definitions like this: def = (define ( var var... var ) exp ) There is a similar rule for defining constants. Additional rules define cond expressions, etc. The Help Desk presents the same idea as definition = (define (id id id... ) expr) CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 8

In CS 135, we will use informal descriptions instead. CS 241, CS 230, CS 360, and CS 444 discuss the mathematical formalization of grammars and their role in the interpretation of computer programs and other structured texts. CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 9

Semantic Model The second of our three problems (syntax, semantics, ambiguity) we will solve rigorously with a semantic model. A semantic model of a programming language provides a method of predicting the result of running any program. Our model will repeatedly simplify the program via substitution. Every substitution step yields a valid Racket program, until all that remains is a sequence of definitions and values. A substitution step finds the leftmost subexpression eligible for rewriting, and rewrites it by the rules we are about to describe. CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 10

Application of built-in functions We reuse the rules for the arithmetic expressions we are familiar with to substitute the appropriate value for expressions like (+ 3 5) and (expt 2 10). (+ 3 5) 8 (expt 2 10) 1024 Formally, the substitution rule is: (f v1... vn) v where f is a built-in function and v is the value of f(v 1,..., v n ). Note the two uses of an ellipsis (...). What does it mean? CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 11

Ellipses For built-in functions f with one parameter, the rule is: (f v1) v where v is the value of f(v 1 ) For built-in functions f with two parameters, the rule is: (f v1 v2) v where v is the value of f(v 1, v 2 ) For built-in functions f with three parameters, the rule is: (f v1 v2 v3) v where v is the value of f(v 1, v 2, v 3 ) We can t just keep writing down rules forever, so we use ellipses to show a pattern: (f v1... vn) v where v is the value of f(v 1,..., v n ). CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 12

Application of user-defined functions As an example, consider (define (term x y) ( x (sqr y))). The function application (term 2 3) can be evaluated by taking the body of the function definition and replacing x by 2 and y by 3. The result is ( 2 (sqr 3)). The rule does not apply if an argument is not a value, as in the case of the second argument in (term 2 (+ 1 2)). Any argument which is not a value must first be simplified to a value using the rules for expressions. CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 13

The general substitution rule is: (f v1... vn) exp where (define (f x1... xn) exp) occurs to the left, and exp is obtained by substituting into the expression exp, with all occurrences of the formal parameter xi replaced by the value vi (for i from 1 to n). Note we are using a pattern ellipsis in the rules for both built-in and user-defined functions to indicate several arguments. CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 14

An example (define (term x y) ( x (sqr y))) (term ( 3 1) (+ 1 2)) (term 2 (+ 1 2)) (term 2 3) ( 2 (sqr 3)) ( 2 9) 18 CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 15

A constant definition binds a name (the constant) to a value (the value of the expression). We add the substitution rule: id val, where (define id val) occurs to the left. CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 16

An example (define x 3) (define y (+ x 1)) y (define x 3) (define y (+ 3 1)) y (define x 3) (define y 4) y (define x 3) (define y 4) 4 CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 17

Substitution in cond expressions There are three rules: when the first expression is false, when it is true, and when it is else. (cond [false exp]... ) (cond... ). (cond [true exp]... ) exp. (cond [else exp]) exp. These suffice to simplify any cond expression. Here the ellipses are serving a different role. They are not showing a pattern, but showing an omission. The rule just says whatever else appeared after the [false exp], you just copy it over. CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 18

An example (define n 5) (cond [(even? n) x][(odd? n) y]) (cond [(even? 5) x] [(odd? n) y]) (cond [false x][(odd? n) y]) (cond [(odd? n) y]) (cond [(odd? 5) y]) (cond [true y]) y Error: y undefined CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 19

Errors A syntax error occurs when a sentence cannot be interpreted using the grammar. A run-time error occurs when an expression cannot be reduced to a value by application of our (still incomplete) evaluation rules. Example: (cond [(> 3 4) x]) CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 20

Simplification Rules for and and or The simplification rules we use for Boolean expressions involving and and or are different from the ones the Stepper in DrRacket uses. The end result is the same, but the intermediate steps are different. The implementers of the Stepper made choices which result in more complicated rules, but whose intermediate steps appear to help students in lab situations. CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 21

(and false... ) false. (and true... ) (and... ). (and) true. (or true... ) true. (or false... ) (or... ). (or) false. As in the rewriting rules for cond, we are using an omission ellipsis. CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 22

Substitution Rules (so far) 1. Apply functions only when all arguments are values. 2. When given a choice, evaluate the leftmost expression first. 3. (f v1...vn) v when f is built-in... 4. (f v1...vn) exp when (define (f x1...xn) exp) occurs to the left... 5. id val when (define id val) occurs to the left. CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 23

6. (cond [false exp]... ) (cond... ). 7. (cond [true exp]... ) exp. 8. (cond [else exp]) exp. 9. (and false... ) false. 10. (and true... ) (and... ). 11. (and) true. 12. (or true... ) true. 13. (or false... ) (or... ). 14. (or) false. CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 24

Importance of the model We will add to the semantic model when we introduce a new feature of Racket. Understanding the semantic model is very important in understanding the meaning of a Racket program. Doing a step-by-step reduction according to these rules is called tracing a program. It is an important skill in any programming language or computational system. We will test this skill on assignments and exams. CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 25

Goals of this module You should understand the substitution-based semantic model of Racket, and be prepared for future extensions. You should be able to trace the series of simplifying transformations of a Racket program. CS 135 Winter 2018 03: The syntax and semantics of Beginning Student 26