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.

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

ICE1341 Programming Languages Spring 2005 Lecture #4 Lecture #4 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
ISBN Chapter 3 Describing Syntax and Semantics.
Describing Syntax and Semantics
Concepts of Programming Languages 1 Describing Syntax and Semantics Brahim Hnich Högskola I Gävle
ISBN Chapter 3 Describing Syntax and Semantics.
ISBN Chapter 3 More Syntax –BNF –Derivations –Practice.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
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.
1 CSE305 Programming Languages Syntax What is it? How is it specified? Who uses it? Why is it needed?
ISBN Chapter 3 Describing Syntax and Semantics.
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.
ISBN Chapter 3 Describing Syntax and Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
Describing Syntax and Semantics
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
ISBN Chapter 3 Describing Syntax and Semantics.
Software II: Principles of Programming Languages
ISBN Chapter 3 Describing Syntax and Semantics.
Describing Syntax and Semantics
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.
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 3 Syntax - the form or structure of the expressions, statements, and program units Semantics.
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
ISBN Chapter 3 Describing Syntax and Semantics.
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.
Chapter Describing Syntax and Semantics. Chapter 3 Topics 1-2 Introduction The General Problem of Describing Syntax Formal Methods of Describing.
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 Syntax and Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Course: ICS313 Fundamentals of Programming Languages. Instructor: Abdul Wahid Wali Lecturer, College of Computer Science and Engineering.
TextBook Concepts of Programming Languages, Robert W. Sebesta, (10th edition), Addison-Wesley Publishing Company CSCI18 - Concepts of Programming languages.
Describing Syntax and Semantics
March 5, ICE 1341 – Programming Languages (Lecture #4) In-Young Ko Programming Languages (ICE 1341) Lecture #4 Programming Languages (ICE 1341)
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
Muhammad Idrees Lecturer University of Lahore 1. Outline Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute.
CPS 506 Comparative Programming Languages Syntax Specification.
Chapter 3 Part II Describing Syntax and Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Chapter 3 Describing Syntax and Semantics. Copyright © 2012 Addison-Wesley. All rights reserved. 1-2 Chapter 3 Topics Introduction The General Problem.
Chapter 3 Describing Syntax and Semantics. Chapter 3: Describing Syntax and Semantics - Introduction - The General Problem of Describing Syntax - Formal.
C HAPTER 3 Describing Syntax and Semantics. T OPICS Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute.
Copyright © 2006 Addison-Wesley. All rights reserved. Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
Chapter 3 Describing Syntax and Semantics
CCSB 314 Programming Language Department of Software Engineering College of IT Universiti Tenaga Nasional Chapter 3 Describing Syntax and Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Chapter 3 © 2002 by Addison Wesley Longman, Inc Introduction - Who must use language definitions? 1. Other language designers 2. Implementors 3.
Describing Syntax and Semantics Session 2 Course : T Programming Language Concept Year : February 2011.
Chapter 3 Describing Syntax and Semantics. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 3 Topics Introduction The General Problem.
ISBN Chapter 3 Describing Syntax and Semantics.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
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.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Describing Syntax and Semantics Chapter 3: Describing Syntax and Semantics Lectures # 6.
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Describing Syntax and Semantics
Chapter 3 – Describing Syntax
COMPILER CONSTRUCTION
Presentation transcript:

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 (ICU)

Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Announcements Send your project team information to TA Send your project team information to TA Team name Team name Student names and IDs Student names and IDs

Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Language Syntax and Semantics – Definitions Language Syntax and Semantics – Definitions Regular Expression Regular Expression Formal Ways to Define Languages Formal Ways to Define Languages Chomsky Hierarchy Chomsky Hierarchy Last Lecture

Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University This Lecture Backus-Naur Form (BNF) Backus-Naur Form (BNF) Derivations Derivations Parse Trees Parse Trees Ambiguity in Grammars Ambiguity in Grammars Operator Precedence Operator Precedence Associativity of Operators Associativity of Operators Extended BNF (EBNF) Extended BNF (EBNF)

Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Chomsky Hierarchy Regular Grammars (Type 3) Regular Grammars (Type 3) A  wB (or A  Bw) or A  w, where A and B are variables, and w is a string of terminals (or empty) Context-free Grammars (Type 2) Context-free Grammars (Type 2) A  , where A is a variable and  is a string of variables and terminals  Theoretical basis for the syntax of most programming languages Context-sensitive Grammars (Type 1) Context-sensitive Grammars (Type 1)  A   , where A is a variable, and ,  and  are strings of variables and terminals (  and  may be empty,  ≠ )  A   , where A is a variable, and ,  and  are strings of variables and terminals (  and  may be empty,  ≠ є ) Unrestricted Grammars (Type 0) Unrestricted Grammars (Type 0)   , where  and  are strings of variables and terminals (  ≠   , where  and  are strings of variables and terminals (  ≠ є) 01 e.g., 0(10)* Finite Control Input Tape finite automata 01 e.g., S  0S0 | 1S1 | c 1c110 Finite Control Input Tape Stack push-down automata Non-deterministic Turing machines Turing machines

Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Turing Machine An abstract machine introduced in 1936 by Alan Turing to provide a mathematical definition of algorithms Alan Turing ( ) Finite Control Input Tape Turing Machine A simple mathematical model of a computer A simple mathematical model of a computer Input tape is infinite to the right Input tape is infinite to the right n leftmost cells hold the input n leftmost cells hold the input The remaining infinity of cells each the blank The remaining infinity of cells each the blank In one move, In one move, 1.Change state 2.Print the symbol on the tape cell, and replace it 3.Move the head left or right one cell * Hopcroft & Ullman Chap 4

Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Backus-Naur Form (BNF) Invented by John Backus to describe Algol 58 (1959) Invented by John Backus to describe Algol 58 (1959) The most widely used method for programming language syntax The most widely used method for programming language syntax Equivalent to context-free grammars Equivalent to context-free grammars A meta-language to describe other languages A meta-language to describe other languages e.g., A small program (Example 3.1)  begin end  begin end   | ; | ;  =  =  A | B | C  A | B | C  +  + | - | - | | LHS (Left-hand side): Abstraction or Non-terminal or Variable RHS (Right-hand side): Lexemes (terminals), and reference to other abstractions Production (rule)

Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Derivations Repeated application of productions, starting with the start symbol and ending with a sentence (all terminal symbols)  begin end  begin end  begin ; end  begin ; end  begin = ; end  begin = ; end  begin A = ; end  begin A = ; end  begin A = + ; end  begin A = + ; end  begin A = B + ; end  begin A = B + ; end  begin A = B + C; end  begin A = B + C; end  begin A = B + C; = end  begin A = B + C; = end  begin A = B + C; B = end  begin A = B + C; B = end  begin A = B + C; B = C end  begin A = B + C; B = C end Leftmost Derivations Sentential Form Sentence Start Symbol

Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Parse Trees A Parse Tree: a hierarchical representation of a derivation A Parse Tree: a hierarchical representation of a derivation Internal nodes of a parse tree: non-terminal symbols Internal nodes of a parse tree: non-terminal symbols Leaf nodes of a parse tree: terminal symbols Leaf nodes of a parse tree: terminal symbols Each sub-trees of a parse tree: an instance of an abstraction Each sub-trees of a parse tree: an instance of an abstraction C A = B + beginend …

Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Ambiguity in a Grammar A grammar is ambiguous iff it generates a sentential form that has two or more distinct parse trees A grammar is ambiguous iff it generates a sentential form that has two or more distinct parse trees The meaning (semantics) of an ambiguous grammar cannot be determined uniquely The meaning (semantics) of an ambiguous grammar cannot be determined uniquely e.g., Grammar:  | const  / | –  / | – Sentence to parse: const – const / cont Sentence to parse: const – const / cont const –/ const – / * AW Lecture Notes

Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University How to Remove Ambiguity? Make the grammar grow only on one direction (similar to a regular grammar) ? Make the grammar grow only on one direction (similar to a regular grammar) ? e.g., Grammar:  const | const  / | –  / | – const -/ Then, how about the case of parsing the following sentence? const / const - const * AW Lecture Notes ① ②

Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Operator Precedence Specify operator orderings in a grammar to prevent the grammar ambiguity Specify operator orderings in a grammar to prevent the grammar ambiguity e.g., Grammar:  – | e.g., Grammar:  – |  / const | const  / const | const Sentence to parse: const / const - const Sentence to parse: const / const - const Derivation:  –  –  / const -  / const -  const / const -  const / const -  const / const - const  const / const - const * AW Lecture Notes const const / –

Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Associativity of Operators A left recursive grammar (e.g., S  Sw) specifies left associativity A left recursive grammar (e.g., S  Sw) specifies left associativity e.g.,  + const | const A right recursive grammar (e.g., S  wS) specifies right associativity A right recursive grammar (e.g., S  wS) specifies right associativity e.g.,  const ** | const e.g., Grammar:  + | const Sentence to parse: const + const + const Sentence to parse: const + const + const  Ambiguous  Ambiguous  Can it be solved by specifying operator precedence?  Can it be solved by specifying operator precedence?

Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Extended BNF (EBNF) Optional parts Optional parts e.g.,  if ( ) [ else ] Repetition Repetition e.g.,  {, } Multiple choices Multiple choices e.g.,  ( * | / | % ) e.g.,  ( * | / | % ) BNF BNF  +  + | – | – | |  *  * | / | / | | EBNF EBNF  {(+ | -) }  {(+ | -) }  {(* | /) }  {(* | /) } * AW Lecture Notes

Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Attribute Grammars – Problem Description Example Problem: Type Compatibility Rule Example Problem: Type Compatibility Rule  =  =  + |  + |  A | B | C  A | B | C 1.The type of an expression when the operand types are not the same is always real (e.g., C = ) 2.When they are the same, the expression type is that of the operands (e.g., C = ) 3.The type of the left side of the assignment must match the type of the right side This rule represents the static semantics that needs to be checked at compile time (c.f., dynamic semantics) This rule represents the static semantics that needs to be checked at compile time (c.f., dynamic semantics) This rule cannot be represented in BNF This rule cannot be represented in BNF e.g., C =

Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Attribute Grammars – Definition Designed by Donald E. Knuth, 1968 Designed by Donald E. Knuth, 1968 A formal approach to both describing and checking the correctness of the static semantics of a program A formal approach to both describing and checking the correctness of the static semantics of a program Main elements Main elements Attributes: associated with grammar symbols Attributes: associated with grammar symbols Synthesized Attributes: computed based on the attributes in a parse tree Synthesized Attributes: computed based on the attributes in a parse tree Inherited Attributes: inherited from parents in a parse tree Inherited Attributes: inherited from parents in a parse tree Intrinsic Attributes: synthesized attributes of leaf nodes Intrinsic Attributes: synthesized attributes of leaf nodes Attribute computation functions (semantic rules): specify how attribute values are computed Attribute computation functions (semantic rules): specify how attribute values are computed Predicate functions: syntactic and semantic rules of a language, associated with grammar rules Predicate functions: syntactic and semantic rules of a language, associated with grammar rules

Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Attribute Grammars – An Example Attributes: actual_type (synthesized attr.), Attributes: actual_type (synthesized attr.), expected_type (inherited attr.) expected_type (inherited attr.) 1.Syntax rule:  = 1.Syntax rule:  = Semantic rule:.expected_type .actual_type 2.Syntax rule:  [2] + [3] Semantic rule:.actual_type  if ( [2].actual_type = int) and ( [3].actual_type = int) then int ( [3].actual_type = int) then int else real endif Predicate:.actual_type =.expected_type 3.Syntax rule:  3.Syntax rule:  Semantic rule:.actual_type .actual_type Predicate:.actual_type =.expected_type 4.Syntax rule:  A | B | C Semantic rule:.actual_type  look-up(.string)  =  =  + |  + |  A | B | C  A | B | C

Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Computing Attribute Values – Evaluating Attributes Sentence: A = A + B 1..actual_type  look-up(A) (Rule 4) 2..expected_type .actual_type (Rule 1) 3. [2].actual_type  look-up(A) (Rule 4) [3].actual_type  look-up(B) (Rule 4) [3].actual_type  look-up(B) (Rule 4) 4..actual_type  either int or real (Rule 2) either int or real (Rule 2) 5..expected_type =.actual_type is.actual_type is either TRUE or FALSE (Rule 2) either TRUE or FALSE (Rule 2) Grammar:  =  =  + |  + |  A | B | C  A | B | C * Sebesta Figure 3.6

Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Computing Attribute Values – Flow of Attributes in a Parse Tree An Inherited Attribute A Synthesized Attribute An Intrinsic Attribute * Sebesta Figure 3.7

Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Computing Attribute Values – Fully Attributed Parse Tree * Sebesta Figure 3.8

Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Homework #2 Chapter 3 Problem Set (pp. 157 – 158) Chapter 3 Problem Set (pp. 157 – 158) #2 – a #2 – a #5 #5 #7 #7 #8 #8 #13 #13