Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.

Slides:



Advertisements
Similar presentations
CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi –
Advertisements

Chapter 2-2 A Simple One-Pass Compiler
Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.
Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
Lecture # 20 Type Systems. 2 A type system defines a set of types and rules to assign types to programming language constructs Informal type system rules,
Type Checking Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic.
Chapter 6 Type Checking Section 0 Overview 1.Static Checking Check that the source program follows both the syntactic and semantic conventions of the source.
CH4.1 CSE244 Type Checking Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs,
Compiler Construction
Lesson 12 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 Beyond syntax analysis An identifier named x has been recognized. Is x a scalar, array or function? How big is x? If x is a function, how many and what.
Intermediate Code Generation Professor Yihjia Tsai Tamkang University.
Chapter 6 Type Checking Section 0 Overview
Abstract Syntax Tree (AST)
Syntax Directed Translation
Syntax-Directed Translation Context-free grammar with synthesized and/or inherited attributes. The showing of values at nodes of a parse tree is called.
CH4.1 CSE244 Syntax Directed Translation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
Syntax-Directed Translation
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
Static checking and symbol table Chapter 6, Chapter 7.6 and Chapter 8.2 Static checking: check whether the program follows both the syntactic and semantic.
Copyright © 2005 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
Topic #5: Translations EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Syntax-Directed Translation
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Topic: Syntax Directed Translations
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Topic #2: Infix to Postfix EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
1 Semantic Analysis. 2 Semantic Analyzer Attribute Grammars Syntax Tree Construction Top-Down Translators Bottom-Up Translators Recursive Evaluators Type.
Chapter 5: Syntax directed translation –Use the grammar to direct the translation The grammar defines the syntax of the input language. Attributes are.
Scribe Sumbission Date: 28 th October, 2013 By M. Sudeep Kumar.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Chapter 5. Syntax-Directed Translation. 2 Fig Syntax-directed definition of a simple desk calculator ProductionSemantic Rules L  E n print ( E.val.
Review: Syntax directed translation. –Translation is done according to the parse tree. Each production (when used in the parsing) is a sub- structure of.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Syntax Directed Definition and Syntax directed Translation
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Copyright © 2009 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
Lecture 8 Semantic Analysis.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Chapter4 Syntax-Directed Translation Introduction : 1.In the lexical analysis step, each token has its attribute , e.g., the attribute of an id is a pointer.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 9 Ahmed Ezzat Semantic Analysis.
Lecture 9 Symbol Table and Attributed Grammars
Semantic analysis Jakub Yaghob
Semantics Analysis.
Syntax-Directed Translation
Context-Sensitive Analysis
Compiler Construction
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Syntax-Directed Translation Part I
Semantic Analysis Chapter 6.
Chapter 5. Syntax-Directed Translation
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Syntax-Directed Definition
פרק 5 תרגום מונחה תחביר תורת הקומפילציה איתן אביאור.
Chapter 4 Action Routines.
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Compiler Construction
Syntax-Directed Translation Part I
Compiler Construction
Presentation transcript:

Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr. Mohsen Chitsaz

Chapter 8: Semantic Analyzer2 Semantic Analyzer A CFG can be used to help guide the translation of programs. This technique is known as Syntax-Directed Translation

Chapter 8: Semantic Analyzer3 Syntax-Directed Translation: (Each symbol has a set of attributes) Example: E --> E+E E --> E*E E --> id E.value := id E.value := E 1.value + E 2.value E.value := E 1.value * E 2.value E E+Eid Introduction:

Chapter 8: Semantic Analyzer4 Example: id * id + id E E E* EE+id E.val := E 1.val * E 2.val E.val := E 1.val + E 2.val E.val := id When Parsing is Done Code is Generated E.val := id Introduction:

Chapter 8: Semantic Analyzer5 Syntax Directed Translation: Definition Syntax Tree: S --> if A then S 1 else S 2 ifthenelse A S 1 S 2 Key words do not appear as leaves

Chapter 8: Semantic Analyzer6 Construction Syntax Tree: Implement each node as a class Example: a + b * 3 * +3 ab Constant 3 Entry to symbol table for bEntry to symbol table for a Introduction:

Chapter 8: Semantic Analyzer7 Syntax Directed Definition With each Grammar Symbol, it associates a set of Attributes, and with each Production, a set of Semantic Rules, for computing values of the attributes associated with the symbol appearing in that production. Example 1: Exp --> Exp + Term {Print ‘+’} Exp --> Term Term --> 0|1|2|3|…|9 {Print ‘0’|’1’|…}

Chapter 8: Semantic Analyzer8 Parse: Exp.t := Exp.t := 2+3Term.t := 4 Exp.t := 2 Term.t := Term.t := Example 1:

Chapter 8: Semantic Analyzer9 Grammar for a Robot --> --> Begin --> Up | Down | Left | Right Example 2:

Chapter 8: Semantic Analyzer10 Assume: Up x=1 Down x= -1 Left x= 0 Right x = 0 y= 0 y= 0 y= -1 y= 1 Start (0, 0)(-1, 0) (-1, -1) (0, -1)(1, -1)(2, -1) Input Begin, L, D, R, R, R Example 2:

Chapter 8: Semantic Analyzer11 Start y=2 Start x=-1 Start y=1 Start x=-1 ???? Start y=0 Start x=-1 Dir y=1 Dir x= 0 Start y=-1 Start x=-1 Dir y=1 Dir x=0 Start y=-1 Start x=0 Dir y=0 Dir x=-1 Start y=0 Start x=0 Start y=-1 Start x=0 Begin(0,0) Right Down Left Start.x=0 Start.y=0 Example 2:

Chapter 8: Semantic Analyzer12 --> Start1.x = Dir.x + Start2.x Start1.y = Dir.y + Start2.y Print(Start1.x, Start1.y) --> Begin Start.x = 0 Start.y = 0 Example 2:

Chapter 8: Semantic Analyzer13 --> Up (Down, Left, Right) Dir.x = 0 Dir.y = 1 What do I need to produce the Code? --> (0.0) (-1,0) (-1,-1) (0,-1) (1,-1) (2,-1) --> Begin, Left, Down, Right, Right, Right Example 2:

Chapter 8: Semantic Analyzer14 1. MakeNode (Op, Left, Right) 2. MakeLeafId(id, entry); 3. MakeLeafNum(Num, Val) + Op is a label Left and Right are pointers id Pointer to Symbol Table We need the Following Functions: Num2 Val

Chapter 8: Semantic Analyzer15 Create Syntax Tree For: a+b*3 P 1 – MakeLeafId (a, Entry a) P 2 – MakeLeafId (b, Entry b) P 3 – MakeNode (+, P 1, P 2 ) (Print P 1, + P 2 ) P 4 – MakeLeafNum (Num, 3) P 5 – MakeNode (*, P 3, P 4 ) (Print P 4 = P 3 * P 5 ) Example:

Chapter 8: Semantic Analyzer16 Tree constructed Bottom-Up Every time a production is used, some code is generated When parsing is done, we have the intermediate code Facts:

Chapter 8: Semantic Analyzer17 E--> E1 + T E.Ptr := MakeNode (‘+’, E 1.Ptr, T.Ptr) E --> E1 – T E.Ptr := MakeNode (‘-’, E 1.Ptr, T.Ptr) E --> T E.Ptr := T.Ptr T --> (E) T.Ptr := E.Ptr T --> id T.Ptr := MakeLeafId (id, id.entry) T --> Num T.Ptr := MakeLeafNum (Num, Num.Val) Semantic Rules

Chapter 8: Semantic Analyzer18 Draw the Syntax Directed Tree: E T.Ptr E.Ptr E1E1 + T Num T id T.Ptr + a Num2 a + 2 Semantic Rules

Chapter 8: Semantic Analyzer19 We use Attributed Grammar (AG) notation to describe Syntax Tree Structure Attributed Grammar is a CFG where attributes are associated with each grammar symbol in a production Summary

Chapter 8: Semantic Analyzer20 Attribute: 1. Synthesized Attributes: Information Flows-up from leaves to the root of the tree 2. Inherited Attributes: Information Flows-down to the leaves Summary

Chapter 8: Semantic Analyzer21 L --> E${Print (E.val)} E --> E + T {E.val := E 1.val + T.val} E --> T{E.val := T.val} T --> T*F {T.val := T 1.val * F.val} T --> F {T.val := F.val} F --> (E){F.val := E.val} F --> digit {F.val := Scanner Value} Example of Desk Calculator:

Chapter 8: Semantic Analyzer22 Synthesized Attributes: Parse: 2 * L {Print (10)} E.val :=10 E.val :=6 T.val :=4 T.val :=2 F.val :=3 F.val :=4 F.val :=2 Digit (Lex 3) Digit(Lex 4) Digit (Lex 2) * + T.val :=6

Chapter 8: Semantic Analyzer23 Inherited Attributes: (Example) D --> T L {L.Insert := T.Type} T --> integer{T.Type := integer} T --> Real {T.Type := Real} L --> L 1, id {L 1.Insert := L.Insert} {Addtype(id.Entry,L.Insert)} L --> id {Addtype(id.Entry,L.Insert)}

Chapter 8: Semantic Analyzer24 Parse: integer a, b, c D T.Type := integer L.Insert := integer id integer,, c b a Inherited Attributes: (Example)

Chapter 8: Semantic Analyzer25 Type Checking: 1. Variable Type Checking A: integer; B: boolean; C: Real; A:= C; A[I] := C:= A; 2. Flow of Control Checking Boolean Expression While Exp Do

Chapter 8: Semantic Analyzer26 3. Uniqueness Checking An object must be defined exactly once (Name of the Program) 4. Name Related Checking (Ada, Modula) Procedure Lookup …. End Lookup; Type Checking:

Chapter 8: Semantic Analyzer27 Type Checking (Top-down or Bottom-up) --> ; --> id : {AddSymbolTable (id.entry, T.type)} --> char {T.type := char} --> integer {T.type := integer} --> {T.type := pointer(T 1.type)} --> Array[Num] of {T.type := Array(Num, T 1.type)} 1…Num 0…Num

Chapter 8: Semantic Analyzer28 Type Checking for expression: --> Literal{E.type := char} --> Num{E.type := integer} --> id {E.type := LookupSymbolTable(id.entry)} --> MOD {E.type :=IF E1.type = integer AND E2.type = integer THEN integer ELSE Type_Error} -->  {E.type := IF E 1.type = Pointer(t) THEN t ELSE Type_Error Add other Types to this …

Chapter 8: Semantic Analyzer29 Type Checking of Statements: --> id := {S.type := IF id.type = E.type THEN void ELSE Type_Error --> IF THEN {S.type := IF E.type = boolean THEN void ELSE Type_Error} --> WHILE DO {S.type := IF E.type = boolean THEN void ELSE Type_Error} -->, {S.type := IF S 1.type = void AND S 2.type = void THEN void ELSE Type_Error}

Chapter 8: Semantic Analyzer30 Type Checking: 1. Static 2. Dynamic Data Types: 1. Simple Data Type Int Long char 2. Structured Data Type Record Array StructClass Summary: