1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix.

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.
Semantic Analysis Chapter 4. Role of Semantic Analysis Following parsing, the next two phases of the "typical" compiler are – semantic analysis – (intermediate)
CSE 425: Semantic Analysis Semantic Analysis Allows rigorous specification of a program’s meaning –Lets (parts of) programming languages be proven correct.
ISBN Chapter 3 Describing Syntax and Semantics.
Copyright © 2006 Addison-Wesley. All rights reserved. 3.5 Dynamic Semantics Meanings of expressions, statements, and program units Static semantics – type.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
CS 330 Programming Languages 09 / 19 / 2006 Instructor: Michael Eckmann.
CS 355 – Programming Languages
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 3: Lexical Analysis COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
Concepts of Programming Languages 1 Describing Syntax and Semantics Brahim Hnich Högskola I Gävle
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 5: Syntax Analysis COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Slide 1 Chapter 3 Attribute Grammars. Slide 2 Attribute Grammars Certain language structures cannot be described using EBNF. Attribute grammars are extensions.
ISBN Chapter 3 Describing Syntax and Semantics.
Topic 5 -Semantic Analysis Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 4: Syntax Specification COMP 144 Programming Language Concepts Spring 2002 Felix.
Describing Syntax and Semantics
ISBN Chapter 3 Describing Syntax and Semantics.
1 Lecture 11: Semantic Analysis (Section ) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
Copyright © 2005 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
Semantic Analysis Legality checks –Check that program obey all rules of the language that are not described by a context-free grammar Disambiguation –Name.
ISBN Chapter 3 Describing Syntax and Semantics.
Describing Syntax and Semantics
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 3 Syntax - the form or structure of the expressions, statements, and program units Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic 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.
CS 363 Comparative Programming Languages Semantics.
1 Lecture 5: Syntax Analysis (Section 2.2) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC.
Muhammad Idrees Lecturer University of Lahore 1. Outline Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute.
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
ISBN Chapter 3 Describing Semantics.
Chapter 3 Part II Describing Syntax and Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Semantics In Text: Chapter 3.
Chapter 3 Describing Syntax and Semantics. Chapter 3: Describing Syntax and Semantics - Introduction - The General Problem of Describing Syntax - Formal.
Syntax and Semantics CIS 331 Syntax: the form or structure of the expressions, statements, and program units. Semantics: the meaning of the expressions,
Theory of Programming Languages Introduction. What is a Programming Language? John von Neumann (1940’s) –Stored program concept –CPU actions determined.
Languages and Compilers
ISBN Chapter 3 Describing Syntax and Semantics.
1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4.
Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases.
Chapter 3 © 2002 by Addison Wesley Longman, Inc Introduction - Who must use language definitions? 1. Other language designers 2. Implementors 3.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Copyright © 2009 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
CSC 4181 Compiler Construction
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
C HAPTER 3 Describing Syntax and Semantics. D YNAMIC S EMANTICS Describing syntax is relatively simple There is no single widely acceptable notation or.
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03CX - Language semantics Programming Language Design.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Describing Syntax and Semantics
Syntax Analysis Chapter 4.
Compiler Lecture 1 CS510.
CS 3304 Comparative Languages
Syntax Questions 6. Define a left recursive grammar rule.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 3 Describing Syntax and Semantics.
CH 4 - Language semantics
Presentation transcript:

1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos Feb 6 The University of North Carolina at Chapel Hill

2 COMP 144 Programming Language Concepts Felix Hernandez-Campos Semantic Analysis From Code Form To Program Meaning Compiler or Interpreter Translation Execution Source Code Target Code Interpre-tation

3 COMP 144 Programming Language Concepts Felix Hernandez-Campos Phases of Compilation

4 COMP 144 Programming Language Concepts Felix Hernandez-Campos Specification of Programming Languages PLs require precise definitions (i.e. no ambiguity)PLs require precise definitions (i.e. no ambiguity) –Language form (Syntax) –Language meaning (Semantics) Consequently, PLs are specified using formal notation:Consequently, PLs are specified using formal notation: –Formal syntax »Tokens »Grammar –Formal semantics »Attribute Grammars »Dynamic Semantics

5 COMP 144 Programming Language Concepts Felix Hernandez-Campos Attribute Grammars Context-Free Grammars (CFGs) are used to specify the syntax of programming languagesContext-Free Grammars (CFGs) are used to specify the syntax of programming languages –E.g. arithmetic expressions How do we tie these rules to mathematical concepts?How do we tie these rules to mathematical concepts? Attribute grammars are annotated CFGs in which annotations are used to establish meaning relationships among symbolsAttribute grammars are annotated CFGs in which annotations are used to establish meaning relationships among symbols –Annotations are also known as decorations

6 COMP 144 Programming Language Concepts Felix Hernandez-Campos Attribute Grammars Example Each grammar symbols has a set of attributesEach grammar symbols has a set of attributes –E.g. the value of E 1 is the attribute E 1.val Each grammar rule has a set of rules over the symbol attributesEach grammar rule has a set of rules over the symbol attributes –Copy rules –Semantic Function rules »E.g. sum, quotient

7 COMP 144 Programming Language Concepts Felix Hernandez-Campos Attribute Flow Context-free grammars are not tied to an specific parsing orderContext-free grammars are not tied to an specific parsing order –E.g. Recursive descent, LR parsing Attribute grammars are not tied to an specific evaluation orderAttribute grammars are not tied to an specific evaluation order –This evaluation is known as the annotation or decoration of the parse tree

8 COMP 144 Programming Language Concepts Felix Hernandez-Campos Attribute Flow Example The figure shows the result of annotating the parse tree for (1+3)*2The figure shows the result of annotating the parse tree for (1+3)*2 Each symbols has at most one attribute shown in the corresponding boxEach symbols has at most one attribute shown in the corresponding box –Numerical value in this example –Operator symbols have no value Arrows represent attribute flowArrows represent attribute flow

9 COMP 144 Programming Language Concepts Felix Hernandez-Campos Attribute Flow Example

10 COMP 144 Programming Language Concepts Felix Hernandez-Campos Attribute Flow Synthetic and Inherited Attributes In the previous example, semantic information is pass up the parse treeIn the previous example, semantic information is pass up the parse tree –We call this type of attributes are called synthetic attributes –Attribute grammar with synthetic attributes only are said to be S-attributed Semantic information can also be passed down the parse treeSemantic information can also be passed down the parse tree –Using inherited attributes –Attribute grammar with inherited attributes only are said to be non-S-attributed

11 COMP 144 Programming Language Concepts Felix Hernandez-Campos Attribute Flow Inherited Attributes Semantic information can also be passed down the parse treeSemantic information can also be passed down the parse tree –Using inherited attributes –Attribute grammar with inherited attributes only are said to be non-S-attributed Top-down grammars generally require non-S- attributed flowsTop-down grammars generally require non-S- attributed flows –The previous annotated grammar was an S-attributed LR(1)

12 COMP 144 Programming Language Concepts Felix Hernandez-Campos Non-S-Attributed Grammars Example

13 COMP 144 Programming Language Concepts Felix Hernandez-Campos Action Routines Automatic tools can construct a parser for a given context-free grammarAutomatic tools can construct a parser for a given context-free grammar –E.g. yacc Automatic tools can construct a semantic analyzer for an attribute grammarAutomatic tools can construct a semantic analyzer for an attribute grammar –An ad hoc techniques is to annotate the grammar with executable rules –These rules are known as action routines

14 COMP 144 Programming Language Concepts Felix Hernandez-Campos Action Routines Example

15 COMP 144 Programming Language Concepts Felix Hernandez-Campos Static and Dynamic Semantics Attribute grammars add basic semantic rules to the specification of a languageAttribute grammars add basic semantic rules to the specification of a language –They specify static semantics But they are limited to the semantic form that can be checked at compile timeBut they are limited to the semantic form that can be checked at compile time Other semantic properties cannot be checked at compile timeOther semantic properties cannot be checked at compile time –They are described using dynamic semantics

16 COMP 144 Programming Language Concepts Felix Hernandez-Campos Dynamic Semantics Use to formally specify the behavior of a programming languageUse to formally specify the behavior of a programming language –Semantic-based error detection –Correctness proofs There is not a universally accepted notationThere is not a universally accepted notation –Operational semantics »Executing statements that represent changes in the state of a real or simulated machine –Axiomatic semantics »Using predicate calculus (pre and post-conditions) –Denotational semantics »Using recursive function theory

17 COMP 144 Programming Language Concepts Felix Hernandez-Campos Semantic Specification The most common way of specifying the semantics of a language is plain englishThe most common way of specifying the semantics of a language is plain english – – – c.html# c.html#24588http://java.sun.com/docs/books/jls/first_edition/html/14.do c.html#24588 There is a lack of formal rigor in the semantic specification of programming languagesThere is a lack of formal rigor in the semantic specification of programming languages –Guess why

18 COMP 144 Programming Language Concepts Felix Hernandez-Campos Reading Assignment Scott’s chapter 4Scott’s chapter 4 –Section 4.1 –Section 4.2 –Section 4.3 –Section 4.4