CS152: Programming Languages. Lecture 2 Syntax. Dan Grossman Spring 2011

Similar documents
CS152: Programming Languages. Lecture 5 Pseudo-Denotational Semantics. Dan Grossman Spring 2011

CSE 505: Concepts of Programming Languages

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

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

Consider a description of arithmetic. It includes two equations that define the structural types of digit and operator:

CSE505, Fall 2012, Midterm Examination October 30, 2012

To prove something about all Boolean expressions, we will need the following induction principle: Axiom 7.1 (Induction over Boolean expressions):

Introduction to Parsing Ambiguity and Syntax Errors

Introduction to Parsing Ambiguity and Syntax Errors

Data Abstraction. An Abstraction for Inductive Data Types. Philip W. L. Fong.

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

7. Introduction to Denotational Semantics. Oscar Nierstrasz

([1-9] 1[0-2]):[0-5][0-9](AM PM)? What does the above match? Matches clock time, may or may not be told if it is AM or PM.

CMPT 379 Compilers. Parse trees

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

Programming Languages Fall 2013

To prove something about all Boolean expressions, we will need the following induction principle: Axiom 7.1 (Induction over Boolean expressions):

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

CHAPTER 10 GRAPHS AND TREES. Alessandro Artale UniBZ - artale/

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.

CS525 Winter 2012 \ Class Assignment #2 Preparation

Lecture 2: Big-Step Semantics

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees

Chapter 3. Describing Syntax and Semantics

Programming Languages

Introduction to Parsing. Lecture 5

Typical workflow. CSE341: Programming Languages. Lecture 17 Implementing Languages Including Closures. Reality more complicated

Lecture 4: Syntax Specification

Supplementary Notes on Abstract Syntax

CS 11 Haskell track: lecture 1

THE COMPILATION PROCESS EXAMPLE OF TOKENS AND ATTRIBUTES

Formal Semantics of Programming Languages

Propositional Logic Formal Syntax and Semantics. Computability and Logic

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

Lecture 3: Recursion; Structural Induction

Formal Semantics of Programming Languages

Defining Languages GMU

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Recursion and Structural Induction

CIT Lecture 5 Context-Free Grammars and Parsing 4/2/2003 1

Syntax. In Text: Chapter 3

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

Context Free Languages

Solution Set 8 Date: 1 December, 1992

CS 314 Principles of Programming Languages

CS 403: Scanning and Parsing

EECS 203 Spring 2016 Lecture 8 Page 1 of 6

A simple syntax-directed

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

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, SPRING SEMESTER COMPILERS ANSWERS. Time allowed TWO hours

Formal Systems and their Applications

PROGRAM ANALYSIS & SYNTHESIS

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5

Context-Free Grammars

CSCI312 Principles of Programming Languages!

MIDTERM EXAMINATION - CS130 - Spring 2003

Chapter 4. Syntax - the form or structure of the expressions, statements, and program units

ASTS, GRAMMARS, PARSING, TREE TRAVERSALS. Lecture 14 CS2110 Fall 2018

syntax tree - * * * * * *

Induction and Semantics in Dafny

( ) i 0. Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5.

CS-XXX: Graduate Programming Languages. Lecture 17 Recursive Types. Dan Grossman 2012

Lecture 5: Formation Tree and Parsing Algorithm

Programming Languages: the ultimate user interface. Welcome to CS301. Why programming languages? Some language features

Introduction to Parsing. Lecture 5

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

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

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

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

Today. Elements of Programming Languages. Concrete vs. abstract syntax. L Arith. Lecture 1: Abstract syntax

Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;}

Part VI. Imperative Functional Programming

Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5. Derivations.

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

Programming Languages Third Edition

Chapter 3 (part 3) Describing Syntax and Semantics

CS 415 Midterm Exam Spring 2002

Introduction to Parsing. Lecture 5. Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

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

Syntax Analysis. Chapter 4

Warmup Problem. Translate the following sentence from English into Propositional Logic. I want to eat ice cream even though I am on a diet.

CMSC 330: Organization of Programming Languages

Chapter 11 :: Functional Languages

Context-Free Grammars

Semantics of Programming Languages - Autumn 2004

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

Compiler Design Concepts. Syntax Analysis

15 212: Principles of Programming. Some Notes on Grammars and Parsing

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

This is already grossly inconvenient in present formalisms. Why do we want to make this convenient? GENERAL GOALS

At build time, not application time. Types serve as statically checkable invariants.

Syntax and Grammars 1 / 21

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Syntax. A. Bellaachia Page: 1

EDA180: Compiler Construc6on Context- free grammars. Görel Hedin Revised:

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

Transcription:

CS152: Programming Languages Lecture 2 Syntax Dan Grossman Spring 2011

Finally, some formal PL content For our first formal language, let s leave out functions, objects, records, threads, exceptions,... What s left: integers, mutable variables, control-flow (Abstract) syntax using a common metalanguage: A program is a statement s, which is defined as follows s : skip x e s; s if e s s while e s e : c x e + e e e (c {..., 2, 1, 0, 1, 2,... }) (x {x 1, x 2,..., y 1, y 2,..., z 1, z 2,...,... }) Dan Grossman CS152 Spring 2011, Lecture 2 2

Syntax Definition s : skip x e s; s if e s s while e s e : c x e + e e e (c {..., 2, 1, 0, 1, 2,... }) (x {x 1, x 2,..., y 1, y 2,..., z 1, z 2,...,... }) Blue is metanotation: : for can be a and for or Metavariables represent anything in the syntax class By abstract syntax, we mean that this defines a set of trees Node has some label for which alternative Children are more abstract syntax (subtrees) from the appropriate syntax class Dan Grossman CS152 Spring 2011, Lecture 2 3

Examples s : skip x e s; s if e s s while e s e : c x e + e e e if ; x skip ; y 42 x y if x skip y 42 x y Dan Grossman CS152 Spring 2011, Lecture 2 4

Comparison to ML if ; x skip ; if x skip x y y 42 x y y 42 type exp = Const of int Var of string Add of exp * exp Mult of exp * exp type stmt = Skip Assign of string * exp Seq of stmt * stmt If of exp * stmt * stmt While of exp * stmt If(Var("x"),Skip,Seq(Assign("y",Const 42),Assign("x",Var "y"))) Seq(If(Var("x"),Skip,Assign("y",Const 42)),Assign("x",Var "y")) Very similar to trees built with ML datatypes ML needs extra nodes for, e.g., e can be a c Also pretending ML s int is an integer Dan Grossman CS152 Spring 2011, Lecture 2 5

Comparison to strings if ; x skip ; if x skip x y y 42 x y y 42 We are used to writing programs in concrete syntax, i.e., strings That can be ambiguous: if x skip y 42 ; x y Since writing strings is such a convenient way to represent trees, we allow ourselves parentheses (or defaults) for disambiguation Trees are our truth with strings as a convenient notation if x skip (y 42 ; x y) versus (if x skip y 42) ; x y Dan Grossman CS152 Spring 2011, Lecture 2 6

Last word on concrete syntax Converting a string into a tree is parsing Creating concrete syntax such that parsing is unambiguous is one challenge of grammar design Always trivial if you require enough parentheses or keywords Extreme case: LISP, 1960s; Scheme, 1970s Extreme case: XML, 1990s Very well studied in 1970s and 1980s, now typically the least interesting part of a compilers course For the rest of this course, we start with abstract syntax Using strings only as a convenient shorthand and asking if it s ever unclear what tree we mean Dan Grossman CS152 Spring 2011, Lecture 2 7

Inductive definition s : skip x e s; s if e s s while e s e : c x e + e e e This grammar is a finite description of an infinite set of trees The apparent self-reference is not a problem, provided the definition uses well-founded induction Just like an always-terminating recursive function uses self-reference but is not a circular definition! Can give precise meaning to our metanotation & avoid circularity: Let E 0 =. For i > 0, let E i be E i 1 union expressions of the form c, x, e 1 + e 2, or e 1 e 2 where e 1, e 2 E i 1. Let E = i 0 E i. The set E is what we mean by our compact metanotation Dan Grossman CS152 Spring 2011, Lecture 2 8

Inductive definition s : skip x e s; s if e s s while e s e : c x e + e e e Let E 0 =. For i > 0, let E i be E i 1 union expressions of the form c, x, e 1 + e 2, or e 1 e 2 where e 1, e 2 E i 1. Let E = i 0 E i. The set E is what we mean by our compact metanotation To get it: What set is E 1? E 2? Could explain statements the same way: What is S 1? S 2? S? Dan Grossman CS152 Spring 2011, Lecture 2 9

Proving Obvious Stuff All we have is syntax (sets of abstract-syntax trees), but let s get the idea of proving things carefully... Theorem 1: There exist expressions with three constants. Dan Grossman CS152 Spring 2011, Lecture 2 10

Our First Theorem There exist expressions with three constants. Pedantic Proof: Consider e = 1 + (2 + 3). Showing e E 3 suffices because E 3 E. Showing 2 + 3 E 2 and 1 E 2 suffices... PL-style proof: Consider e = 1 + (2 + 3) and definition of E. Theorem 2: All expressions have at least one constant or variable. Dan Grossman CS152 Spring 2011, Lecture 2 11

Our Second Theorem All expressions have at least one constant or variable. Pedantic proof: By induction on i, for all e E i, e has 1 constant or variable. Base: i = 0 implies E i = Inductive: i > 0. Consider arbitrary e E i by cases: e E i 1... e = c... e = x... e = e1 + e 2 where e 1, e 2 E i 1... e = e1 e 2 where e 1, e 2 E i 1... Dan Grossman CS152 Spring 2011, Lecture 2 12

A Better Proof All expressions have at least one constant or variable. PL-style proof: By structural induction on (rules for forming an expression) e. Cases: c... x... e 1 + e 2... e 1 e 2... Structural induction invokes the induction hypothesis on smaller terms. It is equivalent to the pedantic proof, and more convenient in PL Dan Grossman CS152 Spring 2011, Lecture 2 13