March 5, 2004 1 ICE 1341 – Programming Languages (Lecture #4) In-Young Ko Programming Languages (ICE 1341) Lecture #4 Programming Languages (ICE 1341)

Slides:



Advertisements
Similar presentations
Semantics Static semantics Dynamic semantics attribute grammars
Advertisements

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.
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.
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.
ISBN Chapter 3 Describing Syntax and Semantics.
Describing Syntax and Semantics
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.
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.
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.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
Software II: Principles of Programming Languages
CS 2104 Prog. Lang. Concepts Dr. Abhik Roychoudhury School of Computing Introduction.
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.
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.
Context-Free Grammars
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Context-Free Grammars and Parsing
Grammars CPSC 5135.
Chapter Describing Syntax and Semantics. Chapter 3 Topics 1-2 Introduction The General Problem of Describing Syntax Formal Methods of Describing.
Chapter 3 Part I Describing Syntax and Semantics.
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 Semantics -Attribute Grammars -Dynamic 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.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
D Goforth COSC Translating High Level Languages.
ISBN Chapter 3 Describing Semantics.
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.
April 2, ICE 1341 – Programming Languages (Lecture #12) In-Young Ko Programming Languages (ICE 1341) Lecture #12 Programming Languages (ICE 1341)
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.
1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4.
Chapter 3 © 2002 by Addison Wesley Longman, Inc Introduction - Who must use language definitions? 1. Other language designers 2. Implementors 3.
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.
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.
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03CX - Language semantics Programming Language Design.
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 and Semantics
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Describing Syntax and Semantics
Chapter 3 Context-Free Grammar and Parsing
Chapter 3 – Describing Syntax
Chapter 3 Describing Syntax and Semantics.
Presentation transcript:

March 5, ICE 1341 – Programming Languages (Lecture #4) In-Young Ko Programming Languages (ICE 1341) Lecture #4 Programming Languages (ICE 1341) Lecture #4 March 5, 2004 In-Young Ko iko.AT. icu.ac.kr Information and Communications University (ICU) iko.AT. icu.ac.kr

March 5, ICE 1341 – Programming Languages (Lecture #4) In-Young Ko Announcements Confirming the class registration Confirming the class registration Rescheduling of the class on March 10 th Rescheduling of the class on March 10 th Monday, March 8 th 4:00pm-5:00pm, L401 Monday, March 8 th 4:00pm-5:00pm, L401

March 5, ICE 1341 – Programming Languages (Lecture #4) In-Young Ko Review of the Previous Lecture Compilation Process Compilation Process Syntax and Semantics Syntax and Semantics Formal Ways to Define Languages Formal Ways to Define Languages Language Recognizers Language Recognizers Language Generators Language Generators Context-free Grammars Context-free Grammars Chomsky Hierarchy Chomsky Hierarchy

March 5, ICE 1341 – Programming Languages (Lecture #4) In-Young Ko 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

March 5, ICE 1341 – Programming Languages (Lecture #4) In-Young Ko 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

March 5, ICE 1341 – Programming Languages (Lecture #4) In-Young Ko Operator Precedence Specify operator orderings in a grammar to prevent the grammar ambiguity Specify operator orderings in a grammar to prevent the grammar ambiguity const /– e.g., Grammar:  – | e.g., Grammar:  – |  / const | const  / const | const Sentence to parse: const – const / cont Sentence to parse: const – const / cont Derivation:  –  –  const –  const –  const – / const  const – / const  const – const / const  const – const / const * AW Lecture Notes

March 5, ICE 1341 – Programming Languages (Lecture #4) In-Young Ko 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.1,  const + | const e.g.2,  const ** | const e.g., Grammar:  + | const Sentence to parse: const + const + cont Sentence to parse: const + const + cont  Ambiguous  Ambiguous  Can it be solved by specifying operator precedence?  Can it be solved by specifying operator precedence?

March 5, ICE 1341 – Programming Languages (Lecture #4) In-Young Ko 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

March 5, ICE 1341 – Programming Languages (Lecture #4) In-Young Ko Attribute Grammars – Problem Description Example Problem: Type Compatibility Rule Example Problem: Type Compatibility Rule  =  =  + |  + |  A | B | C  A | B | C The type of an expression when the operand types are not the same is always real. The type of an expression when the operand types are not the same is always real. When they are the same, the expression type is that of the operands. When they are the same, the expression type is that of the operands. The type of the left side of the assignment must match the type of the right side. 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

March 5, ICE 1341 – Programming Languages (Lecture #4) In-Young Ko 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 functions): associated with grammar rules (productions) Attribute computation functions (semantic functions): associated with grammar rules (productions) 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

March 5, ICE 1341 – Programming Languages (Lecture #4) In-Young Ko Attribute Grammars – An Example Attributes: actual_type (synthesized attr.), expected_type (inherited attr.) Attributes: actual_type (synthesized 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)

March 5, ICE 1341 – Programming Languages (Lecture #4) In-Young Ko 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

March 5, ICE 1341 – Programming Languages (Lecture #4) In-Young Ko Computing Attribute Values – Flow of Attributes in a Parse Tree An Inherited Attribute A Synthesized Attribute An Intrinsic Attribute

March 5, ICE 1341 – Programming Languages (Lecture #4) In-Young Ko Computing Attribute Values – Fully Attributed Parse Tree

March 5, ICE 1341 – Programming Languages (Lecture #4) In-Young Ko Homework #2 Chapter 3 Problem Set (pp. 157 – 158) Chapter 3 Problem Set (pp. 157 – 158) #2 – a, b #2 – a, b #5 #5 #6 – a #6 – a #8 #8 #13 #13 #14 #14