principles of programming languages course introduction vasudeva varma venkatesh choppella monsoon...
TRANSCRIPT
![Page 1: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/1.jpg)
Principles of Programming Languages
Course Introduction
Vasudeva Varma
Venkatesh Choppella
Monsoon Semester 2009
IIIT Hyderabad
![Page 2: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/2.jpg)
Course Administration
• Time: 10:00 to 11:30 AM on Tuesdays and Fridays
• TAs: – PDSR Sandeep – P Thulasi Ram Naidu – …
• Instructors:– Vasudeva Varma - vv@ iiit.ac.in– Venkatesh Choppella – choppell@ gmail.com
![Page 3: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/3.jpg)
Course Administration
• Course website
• Course mailing list• One Mid Semester Exam (30%) During first mid term
• End Semester Exam (50%) During second mid term
• Quizzes/Assignments (20%) – Four in total• Rules
– Attendance– Strict Plagiarism Policy– Punishment: F Grade– Be on-time for the class
![Page 4: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/4.jpg)
Text and Reference Books• Text books
– Several – all are equal. – Robert Sebesta (6th Edition) is more equal (VV)– Essentials of Programming Languages by Friedman,
Wand and Haynes (VC)• Reference Books:
– Programming Languages – Design and Implementation, 4th Edition• Pratt and Zelkowitz, Prentice Hall
– Programming Language Design Concepts• David Watt, Wiley
– Programming Languages – Principles and Paradigms• Tucker and Noonan, McGraw Hill
– Concepts, Techniques and Models of Computer Programming• Roy and Haridi, Prentice Hall
![Page 5: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/5.jpg)
Outline of the Course
• Part I (3 lectures) VV– Motivation– Evolution of PLs– Basic Concepts
• Values and Types• Variables and Storage• Bindings and scope• Procedural Abstraction
– Advanced Concepts• Data Abstraction• Generic Abstraction• Type systems• Control flow
• Part II (7 lectures) VC– Paradigms of PLs
• Structural (1 lecture)• OOP (2 lectures)• Functional (3 lectures)• Logical ? (1 lecture)
• Part III (3-4 lectures) VV/VC– Language Selection – Language Design
![Page 6: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/6.jpg)
Object of Study
• Programming Linguistics (PL)– Concepts and paradigms of PL– Syntax, Semantics and Pragmatics
• What are they in general?• What are they for PL?
– Language Processors
• Comparison with natural languages (differences and similarities)
![Page 7: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/7.jpg)
Motivation to Study PL?
• To write effective algorithms
• To improve use of your existing PL
• To increase your vocabulary of useful programming constructs
• To allow better choice of PL
• To make it easy to learn a new PL
• To make it easy to design a new PL
![Page 8: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/8.jpg)
The main themes of programming language design and use
• Model of computation• Expressiveness
– types and their operations– control structures– abstraction mechanisms– tools for programming in the large
• Ease of use: Writeability/Readability/Maintainability
![Page 9: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/9.jpg)
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– 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.
• Wide-spectrum languages try to do both, more or less well:– Ada, C++, Java
![Page 10: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/10.jpg)
Language as a tool for thought (Iverson)
• Drawing a histogram in APL:
– “* “ [ V ° V]– Is it natural ? ( only if you happen to think that way)– APL is an extreme case (write-only language)– 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”.
– Role of language as a communication vehicle among programmers is more important than ease of writing
![Page 11: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/11.jpg)
The programming environment may be larger than the language
• The predefined libraries are indispensable to the proper use of the language.
• 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.
![Page 12: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/12.jpg)
Language Definition
• Different users have different needs:– programmers
• tutorials, reference manuals, programming guides (idioms)
– implementers• 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!
![Page 13: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/13.jpg)
Language Functionality
• Different stake holders have different requirements– Architects and Designers– Developers
• Those who write programs• Other developers from same team• Other developers from different team
– Testers and QA team– Maintenance team– Customers, …
![Page 14: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/14.jpg)
What makes a language better than the other?
• Expressiveness– Which language/feature allows you to write clear,
concise, and maintainable code for your application?– C++ vs. C to implement classes
• Simplicity– Small talk vs. C++
• Ease of implementation– Multiple (C++) vs. Single (Java) inheritance
• Taste
![Page 15: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/15.jpg)
Exercise
• For two of your favorite languages – how do you compare them in terms of:– Expressiveness– Simplicity– Ease of implementation– Taste
![Page 16: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/16.jpg)
Reading Exercise(recap from ToC)
• Syntax and Semantics
• Grammars
• The Chomsky Hierarchy
• Lexical Analyzer (Lex)
• BNF Notation
• Parsing and Parse Trees
• Resolving ambiguity while parsing
![Page 17: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/17.jpg)
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, but resulting grammar is hard to use.
• In practice, Grammar is used to describe context-free aspects – the rest with prose or a different formal notation (equations,
pre/post conditions, assertions).
![Page 18: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/18.jpg)
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
![Page 19: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/19.jpg)
The Chomsky hierarchy
• Regular grammars:– all productions have the form: N ::= TN– (one non-terminal on each side)
• Context-free grammars:– all productions have the form: N ::= XYZ– (one non-terminal on the left-hand side)
• Context-sensitive languages:– 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
![Page 20: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/20.jpg)
Lexical Issues
• Lexical structure of programming languages is simple.• 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
• Grammars can’t count: previous rule does not specify size limit
![Page 21: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/21.jpg)
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,– need convention for metasymbols: what if ‘|’ is in the language?
![Page 22: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/22.jpg)
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
![Page 23: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/23.jpg)
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)
![Page 24: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/24.jpg)
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• 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
![Page 25: Principles of Programming Languages Course Introduction Vasudeva Varma Venkatesh Choppella Monsoon Semester 2009 IIIT Hyderabad](https://reader035.vdocuments.us/reader035/viewer/2022062422/56649e995503460f94b9c6c9/html5/thumbnails/25.jpg)
Exercise
• Write the case statement in the C language in BNF and as a Parse tree