1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Compilers and Language Translation
1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
Chapter3: Language Translation issues
PZ02A - Language translation
Context-Free Grammars Lecture 7
Environments and Evaluation
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Chapter 2 A Simple Compiler
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
COP4020 Programming Languages
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Semantic Analysis1 Checking what parsers cannot.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
Syntax Directed Translation. Tokens Parser Semantic checking TAC Peephole, pipeline, …… TAC  assembly code/mc Cmm subexpression,……
COMPILER CONSTRUCTION
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Basic Semantics Associating meaning with language entities.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
CPS 506 Comparative Programming Languages Syntax Specification.
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
Semantic Analysis CPSC 388 Ellen Walker Hiram College.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
Chapter 1 Introduction Major Data Structures in Compiler
1 Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
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.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
6. Semantic Analysis. Semantic Analysis Phase – Purpose: compute additional information needed for compilation that is beyond the capabilities of Context-
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Compiler Principle and Technology Prof. Dongming LU Apr. 15th, 2015.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
Lecture 9 Symbol Table and Attributed Grammars
Compiler Construction (CS-636)
Compiler Lecture 1 CS510.
Mini Language Interpreter Programming Languages (CS 550)
CSE 3302 Programming Languages
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSE401 Introduction to Compiler Construction
Compilers B V Sai Aravind (11CS10008).
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Presentation transcript:

1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi

Outline 1. Semantic Analysis 2. Attributes 3. Attribute Grammars 4. The Symbol Table 5. Summary 2

Semantic Analysis Lecture:

Practical Work Write pseudo code for Recursive Descent parsing algorithm A  A ( A ) | a 4

The Compiler So Far Scanner - Lexical analysis  Detects inputs with illegal tokens e.g.: Parser - Syntactic analysis  Detects inputs with ill-formed parse trees e.g.: missing semicolons Semantic analysis  Last “front end” analysis phase  Catches all remaining errors 5

Semantic Analysis 6 Source code Lexical Analysis Syntactic Analysis Semantic Analysis Intermediate Code Gen lexical errors syntax errors semantic errors tokens AST AST’

Beyond Syntax 7 foo(int a, char * s){ … } int bar() { int f[3]; int i, j, k; char *p; float k; foo(f[6], 10, j); break; i->val = 5; j = i + k; printf(“%s,%s.\n”,p,q); goto label23; } What’s wrong with this code? (Note: it parses perfectly) Semantic Analysis

Semantic analysis computes additional information about the program which is beyond the capabilities of CFG and parsing algorithms  The computed information is closely related to eventual meaning or semantics of the program being translated Since this analysis by compiler is by definition static, so it is also called static semantic analysis Static semantic analysis involves both description of the analyses to perform and the implementation of the analyses using appropriate algorithm 8

Semantic Analysis (Continue…) One method of describing semantic analysis is to identify attributes of language entities After identifying attributes, write attribute equation, or semantic rules that express how the computation of such attributes is related to the grammar rules of the language Such a set of attributes and equations is called an attribute grammar Attribute grammars are most useful for languages that obey the principle of syntax-directed semantics 9

Attributes An attribute is any property of the programming language construct  The data type of a variable  The value of an expression  The location of a variable in the memory  The object code of a procedure  The number of significant digits in a number Attributes may be fixed prior to the compilation process and they may be only determinable during program execution 10

Attributes (Continue…) The process of computing an attribute and associating the computed value with the language construct in question is called binding of attribute The time during the compilation/execution process when the binding of an attribute occurs is called its binding time Attributes that can be bound prior execution are static and attributes that can only be bound during execution are dynamic 11

Attribute Grammars In syntax-directed semantics, the attributes are associated directly with grammar symbols of the language If X is a grammar symbol, and a is an attribute associated to X, the we write X a for the value of a associated to X Given a collection of attributes a 1,…,a k, the principle of syntax-directed semantics implies that for each grammar rule X 0 → X 1 X 2 … X n, values of attributes X i a j of each grammar symbol X i are related to the values of attributes of other symbols in the rule 12

Attribute Grammars (Continue…) Should the same symbol X i appear more than once in the grammar rule, then each occurrence must be distinguished from the other occurrences by suitable subscription Each relationship is specified by an attribute equation or semantic rule of the form: X i a j = f ij (X 0 a 1,…,X 0 a k.X 1 a 1,…,X 1 a k,…,X n a 1,…,X n a k ) An attribute grammar for the attributes a 1,…,a k is the collection of all such equations for all the grammar rules of the language 13

Attribute Grammars (Continue…) In the attribute grammar generality, attribute grammars may appear to be extremely complex but it is not the case because usually the functions f ij are quire simple It is rare of attributes to depend on large number of other attributes, and so attributes can be separated into small independent sets of independent attributes and grammar can be written for them Typically, attribute grammars are written in tabular form, with each grammar rule listed with the set of attribute equations, associated to the rule 14

Attribute Grammars (Continue…) Grammar RuleSemantic Rules Rule 1. Rule n Associated attribute equations 15 Below is a general form of attribute grammar in tabular form

Example – Attribute Grammars Consider the following grammar for unsigned numbers number  number digit | digit digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 The most significant attribute of the digit is its value, which we give the name val Each digit has a value which we directly compute from the actual digit it represents  Thus grammar rule digit  0 implies that digit has value 0 16

Example – Attribute Grammars (Continue…) For grammar rule digit  0, we can write its attribute equation as digit.val = 0 and associate it with that rule Furthermore each number has a value based on the digit it contains e.g. number  digit that shows number contains just one digit and in this case the attribute equation will be number.val = digit.val In case number contains more than just one digit then it is derived from number  number digit grammar rule 17

Example – Attribute Grammars (Continue…) Here the number on LHS is different from number on RHS because both has different value so we distinguish between them with the help of subscript number 1  number 2 digit Now to represent 2 digit number e.g. 34 we need to multiply number 2 with 10 hence the attribute equation of grammar number  number digit rule will look like as follows; number 1.val  number 2.val * 10 + digit.val 18

Example – Attribute Grammars (Continue…) Grammar RuleSemantic Rule number  number digit number  digit digit  0 digit  1 digit  2 digit  3 digit  4 digit  5 digit  6 digit  7 digit  8 digit  9 umber 1.val = number 2.val * 10 + digit.val number.val = digit.val digit.val = 0 digit.val = 1 digit.val = 2 digit.val = 3 digit.val = 4 digit.val = 5 digit.val = 6 digit.val = 7 digit.val = 8 digit.val = 9 19

Exercise Construct a parse tree with along with attribute equations to derive and generate value “289” by using following grammar: number  number digit | digit digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 20

Example 2 – Attribute Grammars Consider the following grammar for simple integer arithmetic expressions: exp  exp + term | exp - term| term term  term * factor | factor factor  ( exp ) | number 21

The Symbol Table The symbol table is a major data structure in a compile after the syntax tree In some languages symbol table is involved during the process of parsing and even lexical analysis where they need to add some information in it or may need to look for something from it But in a careful designed language like Pascal or Ada, it is possible and reasonable to put off symbol table operations until after a complete parse, when the program being translated is known to be syntactically correct 22

The Symbol Table (Continue…) The principal symbol table operations include  Insert is used to store the information provided by name declarations  Lookup is needed to retrieve the information associated to a name  Delete is needed to remove the information provided by declaration when that declaration no longer applies Typically symbol table stores data type information, information or region of applicability (scope), and information on eventual location in memory 23

24 Summary Any Questions?