G22.2110 Programming Languages T he main themes of programming language design and use: –Model of computation –Expressiveness Abstraction mechanisms control.

Slides:



Advertisements
Similar presentations
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Advertisements

ICE1341 Programming Languages Spring 2005 Lecture #5 Lecture #5 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
ISBN Chapter 3 Describing Syntax and Semantics.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax Lecture 2 - Syntax, Spring CSE3302 Programming Languages, UT-Arlington ©Chengkai.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
G Programming Languages G Walter Williams.
PZ02A - Language translation
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Compiler Design Lexical Analysis Syntactical Analysis Semantic Analysis Optimization Code Generation.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Programming Languages An Introduction to Grammars Oct 18th 2002.
Chapter 3: Formal Translation Models
COP4020 Programming Languages
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Context-Free Grammars and Parsing 1.
G Programming Languages T he main themes of programming language design and use: –Model of computation –Expressiveness types and their operations.
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
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.
Compiler Principle and Technology Prof. Dongming LU Mar. 7th, 2014.
G Programming Languages T he main themes of programming language design and use: –Model of computation –Expressiveness types and their operations.
Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
Syntax Specification and BNF © Allan C. Milne Abertay University v
Syntax and Backus Naur Form
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
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.
PART I: overview material
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing.
C H A P T E R TWO Syntax and Semantic.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
TextBook Concepts of Programming Languages, Robert W. Sebesta, (10th edition), Addison-Wesley Publishing Company CSCI18 - Concepts of Programming languages.
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.
COP4020 Programming Languages Syntax Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Parsing Introduction Syntactic Analysis I. Parsing Introduction 2 The Role of the Parser The Syntactic Analyzer, or Parser, is the heart of the front.
Bernd Fischer RW713: Compiler and Software Language Engineering.
CPS 506 Comparative Programming Languages Syntax Specification.
Language Translation A programming language processor is any system that manipulates programs expressed in a PL A source program in some source language.
Syntax The Structure of a Language. Lexical Structure The structure of the tokens of a programming language The scanner takes a sequence of characters.
1 Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
ISBN Chapter 3 Describing Syntax and Semantics.
Syntax and Grammars.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
C H A P T E R T W O Syntax and Semantic. 2 Introduction Who must use language definitions? Other language designers Implementors Programmers (the users.
1 CS Programming Languages Class 04 September 5, 2000.
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:
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Programming Languages Translator
CS510 Compiler Lecture 4.
Syntax Specification and Analysis
Chapter 3 Describing Syntax and Semantics.
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
COMPILER CONSTRUCTION
Presentation transcript:

G Programming Languages T he main themes of programming language design and use: –Model of computation –Expressiveness Abstraction mechanisms control structures tools for programming in the large –Ease of use: Write-Debug / Readability / Maintainability

Models of Computation Imperative: programs have mutable storage (state) modified by assignments –by far the most common and familiar Functional (applicative): programs are pure functions –much use in AI, formal semantics, language research Declarative: programs are unordered sets of assertions and rules –Prolog, rule-based systems, relational databases

Models of Computation Scalar: basic operations are on a single element; use loops for arrays and other bulk objects. –by far the most common and familiar Array: lists or arrays are first class. –Same operation adds two scalars as two vectors or a scalar and a vector. Think linear algebra.

Linguist vs. Polyglot Polyglot: someone who can speak/write several languages (English, Chinese, Russian, etc.) Linguist: someone who understands the structure of language (e.g. use of case, formation of plural etc.) Goal: Make you a programming language linguist

Goals of Course Intellectual: help you understand benefit/pitfalls of different approaches/features to language design. Practical: you will design languages many times in your career, e.g. finance, science. Academic : good start on core exam

Language Embodies Thought The majesty and grandeur of the English language; it’s the greatest possession we have. The noblest thoughts that ever flowed through the hearts of men are contained in its extraordinary, imaginative and musical mixtures of sounds. And that’s what you’ve set yourself out to conquer, X. Who is X?

Genealogy FORTRAN (1957) => Fortran90, HPF COBOL (1956) => COBOL2000 –still a large chunk of installed software Algol60 => Algol68 => Pascal => Ada APL => K (array language I use) Simula => Smalltalk, C++ LISP => Scheme, ML, Haskell –with plenty of cross-influences: Java inherits from C++, Smalltalk, LISP, Ada, etc.

Common Ideas Modern scalar imperative languages (Ada, C++, Java, but not K) have similar characteristics: –large number of features (grammar with several hundred productions, 500 page reference manuals…) –a rich type system –procedural mechanisms –object-oriented facilities –abstraction mechanisms, with information hiding –several storage-allocation mechanisms –facilities for concurrent programming (not C++) –facilities for generic programming (not Java)

Predictable performance vs. ease of writing Low-level languages mirror the physical machine: –Assembly, C, Fortran High-level languages model an abstract machine with useful capabilities: –ML, Setl, Prolog, Python, SQL Wide-spectrum languages try to do both, more or less well: –Ada, C++, Java, K High-level languages are often interpreted, have garbage collector,are dynamically typed,and cannot be used for real- time programming. Cost of operations is not directly visible. –Java is a hybrid

Language as a tool for thought Computing a sum in K: +/ yields 12 –Prefix sum: +\3 4 5 yields –Is it natural ? ( only if you happen to think that way) –Role of language as a communication vehicle among programmers is more important than ease of writing –All languages have the same expressive power; arguments of the form “you can’t do this in X” are meaningless. –But.. Idioms in language A may be useful inspiration when writing in language B. Try multiplying in Roman numerals. –Ease of debugging very important – joys of interpretation.

The programming environment may be larger than the language The predefined libraries are indispensable to the proper use of many languages. The libraries are defined in the language itself, but they have to be internalized by a good programmer. the C++ standard template library The Java Swing classes The Ada I/O packages There is a law of conservation of junk: whether it goes into the language or in the library, the total amount of miscellaneous information is roughly the same from language to language. (Does not apply to Prolog or to K.)

Language Definition Different users have different needs: –programmers tutorials, reference manuals, programming guides (idioms) –implementors precise operational semantics –verifiers rigorous axiomatic or natural semantics –language designers and language lawyers all of the above Different levels of detail and precision –But none of them can be sloppy!

Syntax and Semantics Syntax refers to external representation Semantics denotes meaning Distinction is convenient but arbitrary: can describe fully a programming language by syntactic means (Algol68 and W-grammars), but resulting grammar is hard to use. In practice, describe context-free aspects with a grammar, the rest with prose or a different formal notation (equations, pre/post conditions, assertions).

Syntax vs. Semantics Good syntax, unclear semantics: “Colorless green ideas sleep furiously” Good semantics, poor syntax: “Me go swimming now, sorry bye” In programming languages: syntax tells you what a well-formed program looks like. Semantic tells you relationship of output to input.

Grammars A set of non-terminal symbols A distinguished non-terminal: the root symbol A set of terminal symbols A series of rewrite rules (productions) of the form: ABC.. ::= XYZ.. where A, B, C, D, X, Y, Z terminals and non terminals. The language is the set of sentences containing only terminal symbols, that can be generated by applying the rewriting rules starting from the root symbol

BNF: standard notation for context-free grammars A series of conventional abbreviations: –alternation: symb ::= Letter | Digit –option: if_stat ::= IF condition THEN statement [else_part] END IF –repetition: else_part ::= {elseif_part} [ELSE statement] elsif_part ::= ELSIF condition THEN statement –also noted with Kleene star: id ::= Letter symb* –Does not add to expressive power of grammar, could be done with recursion (tastes on readability differ) –need convention for metasymbols: what if ‘|’ is in the language? Compare Barnes appendix 3, Stroustrup Appendix A.

The Chomsky hierarchy (“Three theories of language” 1957 – beautiful paper) Regular grammars (noun phrases): –all productions have the form: N ::= TN –(one non-terminal on left side; at most one on right) Context-free grammars (if-then) –all productions have the form: N ::= XYZ –(one non-terminal on the left-hand side; mixture on right) Context-sensitive languages (passive transformation) –The number of symbols on the left is no greater than on the right –(no production shrinks the size of the sentential form) Type-0 languages –no restrictions, full rewriting.

Less powerful languages can still generate infinite numbers of strings Regular grammars noun phrase: NP ::= ‘the’ X | ‘a’ X X ::= A N | A X A ::= ‘big’ | ‘small’ | ‘beautiful’ | ‘splendid’ … N ::= ‘house’ | ‘car’ | ‘friend’ | …. Examples: the big beautiful big small big car Show finite state representation.

However, not all languages Consider sentences of the form “if it is raining then I’ll take an umbrella else I’ll go in shorts” This is of the form “if S then S else S” Chomsky showed there was no way to represent this as a regular grammar. Needed a context-free grammar. Went on to show that context free grammars weren’t enough for all expressability. Need general rewriting rule of the form String of terminals and non-terminals ::= String

Where do Programming Languages Fit In? They are mostly context-free but sometimes more. Professional compiler writers create grammars that are overly permissive and then constrain them later.

Reverse Polish Notation Language yields yields 17because becomes 14 and – yields 17 Line ::= newline | Exp newline Exp ::= NUM | exp exp op Op ::= ‘+’ | ‘-’ | ‘/’ | ‘*’ Try some examples.

How Would you Implement If line is empty return nothing else expr(line) Expr(line) Loop until end of line if next symbol is a number then push to stack else /* you have found an operation */ pop last two entries from stack, apply op put result back on stack end if end loop

Lexical Grammars Lexical = formation of words/tokens. Described mostly by regular grammar Terminals are characters: –need to specify character set: ASCII, Latin-1, ISO646, Unicode… –need to specify if case is significant –need to specify external source representation for portability Identifiers: –Id => letter –Id => letter Id Context-free grammars can’t count: previous rule does not specify size limit

Parse trees A parse tree describes the grammatical structure of a sentence –leaf nodes are terminal symbols –root of tree is root symbol of grammar –internal nodes are non-terminal symbols –an internal node and its descendants correspond to some production for that non terminal – top-down tree traversal represents the process of generating the given sentence from the grammar –Reconstruction of tree from sentence is parsing

Ambiguity If the parse tree for a sentence is not unique, the grammar is ambiguous: – E ::= E + E | E * E | Id – parse tree for A + B * C ? – Either (A + B) * C) or (A + (B * C)) –Solution: non-terminals with different precedences – E ::= E + T | T – T ::= T * Id | Id Harder to resolve syntactically: –function_call ::= name (expression_list) –indexed_component ::= name (index_list) –type_conversion ::= name (expression)

The dangling else problem S ::= if E then S S ::= if E then S else S if E1 then if E2 then S1 else S2 is ambiguous (if E1 holds then can S2 ever execute?) Solutions: –Pascal rule: else matches most recent if –grammatical solution: different productions for balanced and unbalanced if-statements –grammatical solution: introduce explicit end-marker general ambiguity problem is unsolvable