CS 2104 Prog. Lang. Concepts Dr. Abhik Roychoudhury School of Computing Introduction.

Slides:



Advertisements
Similar presentations
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.
Advertisements

ISBN Chapter 3 Describing Syntax and Semantics.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
PZ02A - Language translation
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Slide 1 Chapter 2-b Syntax, Semantics. Slide 2 Syntax, Semantics - Definition The syntax of a programming language is the form of its expressions, statements.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Compilers and Syntax.
Chapter 3: Formal Translation Models
Specifying Languages CS 480/680 – Comparative Languages.
COP4020 Programming Languages
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.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax.
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
Context-Free Grammars
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
C H A P T E R TWO Syntax and Semantic.
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.
March 5, ICE 1341 – Programming Languages (Lecture #4) In-Young Ko Programming Languages (ICE 1341) Lecture #4 Programming Languages (ICE 1341)
Context Free Grammars. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
CMSC 330: Organization of Programming Languages Context-Free Grammars.
CPS 506 Comparative Programming Languages Syntax Specification.
Chapter 3 Part II Describing Syntax and Semantics.
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
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 Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03BX –Recursive descent parsing Programming Language.
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.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
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
Describing Syntax and Semantics
CS 326 Programming Languages, Concepts and Implementation
Programming Languages Translator
CS510 Compiler Lecture 4.
CSC 8310 Programming Languages
Chapter 3 – Describing Syntax
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
CS 363 Comparative Programming Languages
Context-Free Grammars
CSC 4181Compiler Construction Context-Free Grammars
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CSC 4181 Compiler Construction Context-Free Grammars
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
Context-Free Grammars
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Programming Languages 2nd edition Tucker and Noonan
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:

CS 2104 Prog. Lang. Concepts Dr. Abhik Roychoudhury School of Computing Introduction

Learning Objectives Familiarity with the key concepts underlying modern programming languages. Highlight the similarities and differences between various programming paradigms. Ability to choose a programming paradigm or program construct given a problem scenario.

Course Focus More on the concepts of programming. Less on individual prog. Languages. More on clean programming styles. Less on specific programming tricks.

Topics Basics of program syntax and semantics. Elementary and structured types Subprograms Abstract Data types, Inheritence, OO Functional and Logic Programming Type Checking/Polymorphism

Assessment 10 Homeworks : 20% Midterm : 25% Tutorial participation : 5% Final examination : 50%

Textbook Programming Languages Allen Tucker and Robert Noonan McGraw Hill Publishers Available in Bookstore Textbook changed from last year.

Course Workload Weekly homeworks : 2-3 hrs. Weekly reading : 4-5 hrs. Lecture : 2 hrs. Tutorial : 1 hr. TOTAL : 10 hrs. (approx) Workload reduced from last year

The people You TA : Soo Yuen Jien Instructor : Dr. Abhik Roychoudhury Look up the course web-page

Keeping in touch Post a message to the IVLE discussion forum Course code CS2104 Send to Meet lecturer/TA during consultation hours. Announcements posted in the course web-page: Coming to class….. Might want to consider it

CS 2104 Prog. Lang. Concepts Dr. Abhik Roychoudhury School of Computing Language Syntax Reading: Textbook chapter

Program structure Syntax What a program looks like BNF (context free grammars) - a useful notation for describing syntax. Semantics : Meaning of a program Static semantics - Semantics determined at compile time: var A: integer; Type and storage for A Dynamic semantics - Semantics determined during execution: X = ``ABC'' X a string; value of X

Formal study of syntax Programming languages typically have common building blocks: Identifiers Expressions Statements Subprograms Need to formally specify how a “syntactically correct” program is constructed out of these building blocks. This need is satisfied by BNF grammars. It is simply a notation which allows us to write how “synt. Correct” programs are constructed.

An Example A grammar for arithmetic expressions (common in programming languages) ::= + ::= * ::= ( ) ::= Assuming a,b,c are identifiers (a + b) is an expression (a + b) * c is an expression All arith. Expressions with addition and multiplication can be generated using the above rules.

Study of Grammars Grammars simply give us rules to generate the syntactic building blocks of a program e.g. expressions, statements. We saw an example of a grammar for expressions. The rules in the grammar can be applied repeatedly to generate all possible expressions. These expressions are called the language of the grammar. Furthermore, given an expression, the grammar could be used to check whether it can be generated using its rules. This is called parsing. Let us now study BNF grammars more carefully.

BNF grammars Nonterminal: A finite set of symbols: Terminal: A finite set of symbols: the, boy, girl, ran, ate, cake Start symbol: One of the nonterminals:

BNF grammars Rules (productions): A finite set of replacement rules: ::= ::= ran | ate ::= the ::= boy | girl | cake Replacement Operator: Replace any nonterminal by a right hand side value using any rule (written  )

Empty strings How to characterize strings of length 0? – In BNF,  -productions: S  SS | (S) | () |  Can always delete them in grammar. For example: X  abYc Y   Delete  -production and add production without  : X  abYc X  abc

Example BNF sentences  First rule  Second rule  the Fifth rule...  the boy ate the cake Also from you can derive  the cake ate the boy Syntax does not imply correct semantics Note: Rule ::= This BNF rule also written with equivalent syntax: A  BC

Language of a Grammar Any string derived from the start symbol is a sentential form. Sentence: String of terminals derived from start symbol by repeated application of replacement operator A language generated by grammar G (written L(G)) is the set of all strings over the terminal alphabet (i.e., sentences) derived from start symbol. That is, a language is the set of sentential forms containing only terminal symbols.

Derivations A derivation is a sequence of sentential forms starting from start symbol. Grammar: B  0B | 1B | 0 | 1 Derivation: B  0B  01B  010 Each step in the derivation is the application of a production rule.

Parse tree A parse tree is a hierarchical synt. structure Internal node denote non-terminals Leaf nodes denote terminals. Grammar: B  0B | 1B | 0 | 1 Derivation: B  0B  01B  010 From derivation get parse tree as shown in the right.

Derivations Derivations may not be unique S  SS | (S) | () S  SS  (S)S  (())S  (())() S  SS  S()  (S)()  (())() Different derivations but get the same parse tree

Ambiguity Each corresponds to a unique derivation: S  SS  SSS  ()SS  ()()S  ()()() But from some grammars you can get 2 different parse trees for the same string: ()()() A grammar is ambiguous if some sentence has 2 distinct parse trees.

Why Ambiguity is a problem BNF grammar is used to represent language constructs. If the grammar of a language is non-ambiguous, then we can assign a unique meaning to every program written in that language. If the grammar is ambiguous, then a program can have two or more different interpretations. The two different interpretations of a given program will be shown by the two different parse trees constructed from the grammar.

Exercise 1 Is the grammar of arithmetic expressions shown earlier an ambiguous grammar ? Try to construct a derivation with two different parse trees. ::= + ::= * ::= ( ) ::=

Exercise 1 - Answer ::= + ::= * * 4 ::= ( ) ::= E E E + Id * 2 34 EE E +*

Extended BNF This is a shorthand notation for BNF rules. It adds no power to the syntax,only a shorthand way to write productions: [ ] – Grouping from which one must be chosen Binary_E -> T [+|-] T {}* - Repetition - 0 or more E -> T {[+|-] T}*

Extended BNF {} + - Repetition - 1 or more Usage similar to {}* {} opt - Optional I -> if E then S | if E then S else S Can be written in EBNF as I -> if E then S { else S} opt

Extended BNF Example: Identifier - a letter followed by 0 or more letters or digits: Extended BNF Regular BNF I  L { L | D }* I  L | L M L  a | b |... M  CM | C D  0 | 1 |... C  L | D L  a | b |... D  0 | 1 |...

Exercise 2: BNF and EBNF are convenient notations for writing syntax of programs. Try to write both the BNF and the EBNF descriptions for the switch statement in Java. Remember that your description must generate All syntactically correct switch statements No other statements.

Parsing BNF and extended BNF are notations for formally describing program syntax. Given the BNF grammar for the syntax of a programming language (say Java), how do we determine that a given Java program obeys all the grammar rules. This is achieved by parsing. We now discuss a very simple parsing algorithm to give an idea about the process.

Recursive descent parsing overview A simple parsing algorithm Shows the relationship between the formal description of a programming language and the ability to generate executable code for programs in the language. Use extended BNF for a grammar, e.g., expressions: ::= {[+|-] }*

Recursive descent parsing ::= {[+|-] }* ( Each non-terminal of grammar becomes a procedure ) procedure Expression; begin Term; /* Call Term to find first term */ while ((nextchar=`+') or (nextchar=`-')) do begin nextchar:=getchar; /* Skip operator */ Term end

Partially Completed Recursive Descent Parse for Assignments

Summary We need a “description language” for describing the set of all allowed programs in a Prog. Lang. BNF and EBNF grammars are such descriptions. Given a program P in a programming language L and the BNF grammar for L, we can find out whether P is a syntactically correct program in language L. This activity is called parsing. The Recursive Descent Parsing technique is one such parsing technique.