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

Similar documents
Modules, Structs, Hashes, and Operational Semantics

The Substitution Model


Principles of Programming Languages COMP251: Syntax and Grammars

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

Syntax and Grammars 1 / 21

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

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

22c:111 Programming Language Concepts. Fall Syntax III

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

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

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

Semantic Analysis. Lecture 9. February 7, 2018

Crafting a Compiler with C (II) Compiler V. S. Interpreter

CMSC 330: Organization of Programming Languages. Context Free Grammars

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

CPS 506 Comparative Programming Languages. Syntax Specification

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

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

Syntax-Directed Translation. Lecture 14

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

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


A simple syntax-directed

Formal Semantics of Programming Languages

Syntax. A. Bellaachia Page: 1

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

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Formal Semantics of Programming Languages

CSCE 314 Programming Languages. Type System

Syntax Errors; Static Semantics

CMSC 330: Organization of Programming Languages

Introduction to Compiler Design

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

CMSC 330: Organization of Programming Languages

CSE 3302 Programming Languages Lecture 2: Syntax

LECTURE 3. Compiler Phases

CSCE 314 Programming Languages

The Substitution Model. Nate Foster Spring 2018

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

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

COP 3402 Systems Software Syntax Analysis (Parser)

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

Working of the Compilers

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

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

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

Building Compilers with Phoenix

CST-402(T): Language Processors

EECS 6083 Intro to Parsing Context Free Grammars

UNIVERSITY OF CALIFORNIA

The role of semantic analysis in a compiler

SOFTWARE ARCHITECTURE 5. COMPILER

CS558 Programming Languages

Principles of Programming Languages COMP251: Syntax and Grammars

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.

EI326 ENGINEERING PRACTICE & TECHNICAL INNOVATION (III-G) Kenny Q. Zhu Dept. of Computer Science Shanghai Jiao Tong University

CS 3360 Design and Implementation of Programming Languages. Exam 1

CS164: Midterm I. Fall 2003

COMPILER DESIGN LECTURE NOTES

A Tour of Language Implementation

Defining syntax using CFGs

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

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

Lecture 14 Sections Mon, Mar 2, 2009

Lecture 13: Expression Evaluation

CMSC 330: Organization of Programming Languages

CS 415 Midterm Exam Spring 2002

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

Introduction. Compiler Design CSE Overview. 2 Syntax-Directed Translation. 3 Phases of Translation

7. Introduction to Denotational Semantics. Oscar Nierstrasz

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

Parsing II Top-down parsing. Comp 412

INF5110 Compiler Construction

More Assigned Reading and Exercises on Syntax (for Exam 2)

ECE251 Midterm practice questions, Fall 2010

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

Lecture content. Course goals. Course Introduction. TDDA69 Data and Program Structure Introduction Cyrille Berger

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

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

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

CMSC 330: Organization of Programming Languages

Language Translation, History. CS152. Chris Pollett. Sep. 3, 2008.

Context-Free Grammars

Programming Languages Third Edition

INSTITUTE OF AERONAUTICAL ENGINEERING

CSE302: Compiler Design

CSE 341 Section 7. Eric Mullen Spring Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang


1 Lexical Considerations

MIDTERM EXAMINATION - CS130 - Spring 2005

CS 230 Programming Languages

CS558 Programming Languages

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

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

PSOA-to-TPTP Converter Documentation

An Overview of Compilation

CSE 12 Abstract Syntax Trees

Transcription:

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

Lab 1 solution (in class)

Formally defining a language When we define a language, we need to keep two concepts in mind: What is the structure (syntax) of a given program in the language. What is the meaning (semantics) of a given program.

Defining Syntax There are 2 parts to the syntax of a language: Lexemes/tokens the "words" of the language Grammar the way that words can be ordered We'll review how a compiler (or interpreter) handles these parts.

How a compiler works source code Lexer/ Tokenizer tokens Tokens are the "words" of the language.

How a compiler works if (x < 42) { y++; } else { y = 42; } source code Lexer/ Tokenizer tokens "if" "(" "x" "<" "42" ")" "{" "y" "++" ";" "}" "else" "{" "y" "=" "42" ";" "}" Tokens are the "words" of the language.

How a compiler works if (x < 42) { y++; } else { y = 42; } source code Lexer/ Tokenizer There are several types of tokens: Identifiers Numbers Reserved words Special characters tokens "if" "(" "x" "<" "42" ")" "{" "y" "++" ";" "}" "else" "{" "y" "=" "42" ";" "}"

How a compiler works source code Lexer/ Tokenizer tokens Parser The parser's job is to combine the tokens together into an abstract syntax tree. Abstract Syntax Tree (AST)

Parsing Example if "if" "(" "x" "<" "42" ")" "{" "y" "++" ";" "}" "else" "{" "y" "=" "42" ";" "}" Parser < = = X 42 y + y 42 Note that y++ has disappeared in the AST. The ++ operator is an example of syntactic sugar. y 1

Formally defining language syntax Context-free grammars define the structure of a language. Backas-Naur Form (BNF) is a common notation.

Context-free grammar for math expressions (in BNF notation) <expr> -> <expr> + <term> <expr> - <term> <term> <term> -> <term> * <factor> <term> / <factor> <factor>

How a compiler works source code Lexer/ Tokenizer tokens Parser Compiler Abstract Syntax Tree (AST) Interpreter Machine code Commands

Compilers and interpreters must derive meaning from the ASTs in order to turn programs into actions. Approaches for formally defining the meaning of a language include: Operational semantics Denotational semantics Axiomatic semantics

Judging a language

Louden & Lambert's Design Criteria 1. Efficiency 2. Regularity 3. Security 4. Extensibility

Efficiency Can mean different things, such as Machine efficiency Does the language give tips to the compiler for more efficient compilation? Programmer efficiency How easy is it to write in the language? How expressive is the language? (conciseness helps) Reliability How difficult is code maintenance?

Efficiency Java: int i = 10; String s = "hi"; Python: i = 10; s = "hi"; Machine efficiency: Java offers tips to the compiler Programmer efficiency: Python reduces the amount of typing required

Regularity Not always well-defined, but can be divided into Generality: avoids special cases in favor of more general constructs Orthogonal design: different constructs can be combined with no unexpected restrictions Uniformity: similar things look similar, different things look different

Bad uniformity example (PHP): Same things look different Inconsistent function naming: isset() is_null() strip_tags() stringslashes()

Bad uniformity example (Pascal): Different things look the same function f : boolean; begin... f := true; end; Return value is true

Security Stop programmer from making errors (or handle them gracefully when they arise). Closely related to reliability. Semantically-safe languages prevent executing code that violates the definition of the language. Contrast array handling by Java and by C/C++ Strong typing prevents some run-time errors.

Safety (Java vs. Scheme) Java: int x = 4; boolean b = true; if (b) { x++; } else { x = x / "2"; } Scheme: (let ([x 4] [b #t]) (if b (+ 1 x) (/ x "2")))

Extensibility With an extensible allows the programmer to add new language constructs easily. Macros in Lisp and Scheme specify the syntax of code that expands to other code.

Lab 2: More Scheme practice Download lab2.rkt from the course website Implement reverse function Implement add-two-lists Implement positive-nums-only Implement js-if Using Louden & Lambert's criteria, compare Java & Scheme (or two languages of your choice)