C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model From kernel to practical language (CTM 2.6) Exceptions (CTM.
ISBN Chapter 3 Describing Syntax and Semantics.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
CS 355 – Programming Languages
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005.
ISBN Chapter 3 Describing Syntax and Semantics.
Concepts of Programming Languages 1 Describing Syntax and Semantics Brahim Hnich Högskola I Gävle
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Single assignment store Kernel language syntax Carlos Varela.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
PZ02A - Language translation
Context-Free Grammars Lecture 7
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Kernel language semantics Carlos Varela RPI Adapted with permission.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Compiler Design Lexical Analysis Syntactical Analysis Semantic Analysis Optimization Code Generation.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Programming Techniques Non-declarative needs (VRH 3.8) Program design in the.
Describing Syntax and Semantics
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
EECS 6083 Intro to Parsing Context Free Grammars
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Syntactic Analysis Dr. Hyunyoung Lee.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
Chpater 3. Outline The definition of Syntax The Definition of Semantic Most Common Methods of Describing Syntax.
Compiler1 Chapter V: Compiler Overview: r To study the design and operation of compiler for high-level programming languages. r Contents m Basic compiler.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
CS 331, Principles of Programming Languages Chapter 2.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Grammars CPSC 5135.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
1 Programming Languages Fundamentals Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM.
Muhammad Idrees Lecturer University of Lahore 1. Outline Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
CPS 506 Comparative Programming Languages Syntax Specification.
C. Varela; Adapted w. permission from S. Haridi and P. Van Roy1 Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections ,
D Goforth COSC Translating High Level Languages.
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
Chapter 3 Part II Describing Syntax and Semantics.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
Programming Languages
ISBN Chapter 3 Describing Syntax and Semantics.
1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4.
CS 331, Principles of Programming Languages Chapter 2.
Syntax and Grammars.
The Interpreter Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Syntax and Semantics Form and Meaning of Programming Languages Copyright © by Curt Hill.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
1 CS Programming Languages Class 04 September 5, 2000.
Parser: CFG, BNF Backus-Naur Form is notational variant of Context Free Grammar. Invented to specify syntax of ALGOL in late 1950’s Uses ::= to indicate.
Advanced programming language theory. week 2. Attribute grammars and semantics.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Higher-Order Programming: Iterative computation (CTM Section 3
Chapter 3 – Describing Syntax
Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections , 3.2, , 4.7.2) Carlos Varela RPI September 12,
Chapter 3 – Describing Syntax
Higher-Order Programming: Iterative computation (CTM Section 3
Declarative Computation Model Kernel language semantics (Non-)Suspendable statements (VRH ) Carlos Varela RPI October 11, 2007 Adapted with.
R.Rajkumar Asst.Professor CSE
CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics
Declarative Computation Model Single assignment store (VRH 2
Chapter 3 Describing Syntax and Semantics.
Chapter 10: Compilers and Language Translation
Presentation transcript:

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy2 Programming A computation model: describes a language and how the sentences (expressions, statements) of the language are executed by an abstract machine A set of programming techniques: to express solutions to the problems you want to solve A set of reasoning techniques: to reason about programs to increase the confidence that they behave correctly and calculate their efficiency

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy3 Declarative Programming Model Guarantees that the computations are evaluating functions on (partial) data structures The core of functional programming (LISP, Scheme, ML, Haskell) The core of logic programming (Prolog, Mercury) Stateless programming vs. stateful (imperative) programming We will see how declarative programming underlies concurrent and object-oriented programming (Erlang, Java, SALSA)

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy4 Defining a programming language Syntax (grammar) Semantics (meaning)

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy5 Language syntax Defines what are the legal programs, i.e. programs that can be executed by a machine (interpreter) Syntax is defined by grammar rules A grammar defines how to make ‘sentences’ out of ‘words’ For programming languages: sentences are called statements (commands, expressions) For programming languages: words are called tokens Grammar rules are used to describe both tokens and statements

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy6 Language syntax (2) A statement is a sequence of tokens A token is a sequence of characters A program that recognizes a sequence of characters and produces a sequence of tokens is called a lexical analyzer A program that recognizes a sequence of tokens and produces a sequence of statement representation is called a parser Normally statements are represented as (parse) trees Lexical analyzer Parser characters tokens sentences

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy7 Extended Backus-Naur Form EBNF (Extended Backus-Naur Form) is a common notation to define grammars for programming languages Terminal symbols and non-terminal symbols Terminal symbol is a token Nonterminal symbol is a sequence of tokens, and is represented by a grammar rule  nonterminal  ::=  rule body 

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy8 Grammar rules  digit  ::= 0 | 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9 |  digit  is defined to represent one of the ten tokens 0, 1, …, 9 The symbol ‘ | ’ is read as ‘or’ Another reading is that  digit  describes the set of tokens {0,1,…, 9} Grammar rules may refer to other nonterminals  integer  ::=  digit  {  digit  }  integer  is defined as the sequence of a  digit  followed by a zero or more  digit  ’s

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy9 How to read grammar rules  x  : is a nonterminal x  x  ::= Body :  x  is defined by Body  x  |  y  : either  x  or  y  (choice)  x   y  : the sequence  x  followed by  y  {  x  } : a sequence of zero or more occurrences of  x  {  x  } + : a sequence of one or more occurrences of  x  [  x  ] : zero or one occurrences of  x  Read the grammar rule from left to right to give the following sequence: –Each terminal symbol is added to the sequence –Each nonterminal is replaced by its definition –For each  x  |  y  pick any of the alternatives –For each  x   y  is the sequence  x  followed by the sequence  y 

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy10 Context-free and context-sensitive grammars Grammar rules can be used to either –verify that a statement is legal, or –to generate all possible statements The set of all possible statements generated from a grammar and one nonterminal symbol is called a (formal) language EBNF notation defines a class of grammars called context-free grammars Expansion of a nonterminal is always the same regardless of where it is used For practical languages context-free grammar is not enough, usually a condition on the context is sometimes added

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy11 Context-free and context-sensitive grammars It is easy to read and understand Defines a superset of the language Expresses restrictions imposed by the language (e.g. variable must be declared before use) Makes the grammar rules context sensitive Context-free grammar (e.g. with EBNF) + Set of extra conditions

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy12 Examples  statement  ::= skip |  expression  ‘=‘  expression  | …  expression  ::=  variable  |  integer  | …  statement  ::= if  expression  then  statement  { elseif  expression  then  statement  } [ else  statement  ] end | …

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy13 Example: (Parse Trees) if  expression  then  statement  1 else  statement  2 end conditional expressionstatement 1 statement 2 ifthenelse

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy14 Language Semantics Semantics defines what a program does when it executes Semantics should be simple and yet allows a programmer to reason about programs (correctness, execution time, and memory use) How can this be achieved for a practical language that is used to build complex systems (millions lines of code) ? The kernel language approach

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy15 Kernel Language Approach Define a very simple language (kernel language) Define the computation model of the kernel language By defining how the constructs (statements) of the language manipulate (create and transform) the data structures (the entities) of the language Define a mapping scheme (translation) of full programming language into the kernel language Two kinds of translations: linguistic abstractions and syntactic sugar

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy16 Kernel Language Approach Practical language kernel language Translation fun {Sqr X} X*X end B = {Sqr {Sqr A}} proc {Sqr X Y} { * X X Y} end local T in {Sqr A T} {Sqr T B} end Provides useful abstractions for the programmer Can be extended with linguistic abstractions Is easy to understand and reason with Has a precise (formal) semantics

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy17 Linguistic abstractions vs. syntactic sugar Linguistic abstractions, provide higher level concepts that the programmer can use to model, and reasons about programs (systems) Examples: functions ( fun ), iterations ( for ), classes and objects ( class ), mailboxes ( receive ) The functions (calls) are translated to procedures (calls) The translation answers questions about the functions: {F1 {F2 X} {F3 X}}

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy18 Linguistic abstractions vs. syntactic sugar Linguistic abstractions, provide higher level concepts that the programmer can use to model, and reasons about programs (systems) Syntactic sugar are short cuts and conveniences to improve readability if N==1 then [1] else local L in … end end if N==1 then [1] else L in … end

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy19 Approaches to semantics Programming Language Kernel Language Operational model Formal calculusAbstract machine Aid the programmer in reasoning and understanding Mathematical study of programming (languages) -calculus, predicate calculus,  -calculus Aid to the implementer Efficient execution on a real machine

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy20 Exercises 1.Write a valid EBNF grammar for lists of non-negative integers in Oz. 2.Write a valid EBNF grammar for the λ-calculus. Which are terminal and which are non-terminal symbols? Draw the parse tree for the expression: ((λx.x λy.y) λz.z) 3.The grammar ::= | ::=+ | * is ambiguous (e.g., it can produce two parse trees for the expression 2*3+4). Rewrite the grammar so that it accepts the same language unambiguously. 4.Read VRH Sections 2.2 and 2.3