Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Syntax-Directed Translation.

Slides:



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

CPSC 388 – Compiler Design and Construction
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
Semantic Analysis Chapter 4. Role of Semantic Analysis Following parsing, the next two phases of the "typical" compiler are – semantic analysis – (intermediate)
1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
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.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Table-driven syntax-directed.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Syntax-Directed Translation Context-free grammar with synthesized and/or inherited attributes. The showing of values at nodes of a parse tree is called.
Building An Interpreter After having done all of the analysis, it’s possible to run the program directly rather than compile it … and it may be worth it.
Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source.
CS784 (Prasad)L167AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
C Chuen-Liang Chen, NTUCS&IE / 1 COMPILER Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University Taipei,
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.
Invitation to Computer Science 5th Edition
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
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.
Syntax-Directed Translation
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Introduction to code generation.
Semantic Analysis1 Checking what parsers cannot.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Review Joey Paquet,
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
The TINY sample language and it’s compiler
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Templates.
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Code generation Joey Paquet,
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Joey Paquet, 2000, Lecture 5 Error Recovery Techniques in Top-Down Predictive Syntactic Analysis.
Joey Paquet, 2000, Lecture 10 Introduction to Code Generation and Intermediate Representations.
Introduction to Code Generation and Intermediate Representations
Semantic Analysis CPSC 388 Ellen Walker Hiram College.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Introduction to Compiling
Chapter 1 Introduction Major Data Structures in Compiler
Week 6(10.7): The TINY sample language and it ’ s compiler The TINY + extension of TINY Week 7 and 8(10.14 and 10.21): The lexical of TINY + Implement.
COMPILER CONSTRUCTION Lesson 1 – TDDD16 TDDB44 Compiler Construction 2010 Kristian Stavåker (Erik Hansson.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
Joey Paquet, 2000, 2002, 2008, Lecture 8 Syntax-Directed Translation.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Joey Paquet, 2000, Lecture 8 Syntax-Directed Translation.
Copyright © 2009 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
Joey Paquet, 2000, 2002, 2008, 2012, Lecture 5 Error Recovery Techniques in Top-Down Predictive Syntactic 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.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Error recovery in top-down.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
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.
Lecture 9 Symbol Table and Attributed Grammars
Compiler design Syntax-Directed Translation
Constructing Precedence Table
Compiler design Introduction to code generation
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Semantic Analysis Chapter 6.
Compilers B V Sai Aravind (11CS10008).
COP4020 Programming Languages
Compiler design Table-driven syntax-directed translation
COMP 442/6421 – Compiler Design
Presentation transcript:

Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Syntax-Directed Translation Joey Paquet, COMP 442/6421 – Compiler Design

Concordia University Department of Computer Science and Software Engineering Translation process driven by the syntactic structure of the program, as given by the parser Semantic actions are integrated in the parsing process Semantic actions are functions whose goal is to accumulate semantic information about the program as the parsing is done. Different pieces of information are gathered during the parsing of different syntactical rules. These pieces of information are gathered in semantic values, or semantic records, or semantic attributes. Some of the information that must be gathered spans over the application of several syntactical rules. This raises the need to migrate this information across the parse tree, a process known as attribute migration. In syntax-directed translation, compilation is divided in two parts: analysis (syntactic, semantic) synthesis (code generation and optimization) The semantic analysis becomes the link between analysis and synthesis: code generation (synthesis) is conditional to positive analysis Syntax-directed translation Joey Paquet, COMP 442/6421 – Compiler Design

Concordia University Department of Computer Science and Software Engineering Semantic actions (implemented as semantic routines) finish the analysis phase by performing semantic checking in productions that need such checks, e.g. type checking Semantic actions assemble information in order to check and generate a meaning for the program elements generated by the productions They are the starting point for code generation (synthesis) Thus, the semantic routines are the heart of the compiler Syntax-directed translation Joey Paquet, COMP 442/6421 – Compiler Design

Concordia University Department of Computer Science and Software Engineering Semantic routines can be formalized using attribute grammars Attribute grammars augment ordinary context-free grammars with attributes that represent semantic properties such as type, value or correctness used in semantic analysis (checking) and code generation (translation) It is useful to keep checking and translation facilities distinct in the semantic routines’ implementation Semantic checking is machine-independent and code generation is not, so separating them gives more flexibility to the compiler (front/back end) Syntax-directed translation and attribute grammars Joey Paquet, COMP 442/6421 – Compiler Design

Concordia University Department of Computer Science and Software Engineering An attribute is a property of a programming language construct, including data type, value, memory location/size, translated code, etc. Implementation-wise, they are also called semantic records. The process of computing the value of an attribute is called binding. Static binding concerns binding that can be done at compile-time, and dynamic binding happens at run-time, e.g. for polymorphism. In our project, we are concerned solely on static compile-time binding. Attributes Joey Paquet, COMP 442/6421 – Compiler Design

Concordia University Department of Computer Science and Software Engineering Static attribute binding is done by gathering, propagating, and aggregating attributes while traversing the parse tree. Attributes are gathered at tree leaves, propagated across tree nodes, and aggregated at some parent nodes when all necessary information is available. This can be done as the program is being parsed using syntax-directed translation. Synthetized attributes : attributes gathered from a child in the syntax tree Inherited attributes : attributes gathered from a sibling in the syntax tree Attributes migration Joey Paquet, COMP 442/6421 – Compiler Design

Concordia University Department of Computer Science and Software Engineering Example: Semantic rules and attribute migration Joey Paquet, COMP 442/6421 – Compiler Design E 1  id = E 2 E = : [E 2 : v:] [(id:)  defVar] [id.valv] [E 1.val: v] E 1  E 2  E 3 E * : [E 2 : v 2 :] [E 3 : v 3 : ] [E 1.val: v 2 v 3 ] E 1  E 2 + E 3 E + : [E 2 : v 2 :] [E 3 : v 3 : ] [E 1.val: v 2 +v 3 ] E  idE id : [(id:)  defVar] [(E.val: )id.val] E  constE const :[const : v:] [(E.val: )v] Y=3*X+Z E(v 3*vx+vz :) E(v (3*vx)+vz :)id(v Y :) = E(v 3*vx :) id(v Z :) + E(v Z :) const(3:)id(v X :) * E(v X :)E(3:)

Concordia University Department of Computer Science and Software Engineering Problems arise when rules are factorized: Example 2: attribute migration Joey Paquet, COMP 442/6421 – Compiler Design E  TE’ E’  +TE’| T  FT’ T’  FT’| F  id|const E T1T1 T2T2 T’ 2 * F1F1 id(v a :) T’ 1  F2F2 id(v b :) E’ 2  T’ 3  F3F3 id(v c :) E’ 1 + a+b*c

Concordia University Department of Computer Science and Software Engineering Solution: migrate attributes sideways, i.e. inherited attributes Example 2: attribute migration Joey Paquet, COMP 442/6421 – Compiler Design E T1T1 T2T2 T’ 2 * F1F1 id(v a :) T’ 1  F2F2 id(v b :) E’ 2  T’ 3  F3F3 id(v c :) E’ 1 + a+b*c E  TE’ E’  +TE’| T  FT’ T’  FT’| F  id|const

Concordia University Department of Computer Science and Software Engineering Attribute migration: implementation in recursive-descent predictive parser Joey Paquet, COMP 442/6421 – Compiler Design Parse(){ type Es; //semantic record created //before the call lookahead = NextToken() if (E(Es);Match('$')) //passed as a reference //to parsing functions //that will compute its value return(true); else return(false); }

Concordia University Department of Computer Science and Software Engineering Attribute migration: implementation in recursive-descent predictive parser Joey Paquet, COMP 442/6421 – Compiler Design E(type &Es){ type Ts,E's if (lookahead is in [0,1,(]) if (T(Ts);E'(Ts,E's);) // E' inherits Ts from T write(E->TE') Es = E's // Synthetised attribute sent up return(true) else return(false) else return(false) }

Concordia University Department of Computer Science and Software Engineering Attribute migration: implementation in recursive-descent predictive parser Joey Paquet, COMP 442/6421 – Compiler Design E'(type &Ti, type &E's){ type Ts,E'2s if (lookahead is in [+]) if (Match('+');T(Ts);E'(Ts,E'2s))// E' inherits from T write(E'->TE') E's = semcheckop(Ti,E'2s) // Semantic check & synthetized // attribute sent up return(true) else return(false) else if (lookahead is in [$,)] write(E'->epsilon) E's = Ti // Synth. attr. is inhertied // from T (sibling, not child) // and sent up return(true) else return(false) }

Concordia University Department of Computer Science and Software Engineering Attribute migration: implementation in recursive-descent predictive parser Joey Paquet, COMP 442/6421 – Compiler Design T(type &Ts){ type Fs, T's if (lookahead is in [0,1,(]) if (F(Fs);T'(Fs,T's);) // T' inherits Fs from F write(T->FT') Ts = T's// Synthetized attribute sent up return(true) else return(false) else return(false) }

Concordia University Department of Computer Science and Software Engineering Attribute migration: implementation in recursive-descent predictive parser Joey Paquet, COMP 442/6421 – Compiler Design T'(type Fi, type &T's){ type Fs, T'2s if (lookahead is in [*]) if (Match('*');F(Fs);T'(Fs,T'2s)) // T' inherits from F write(T'->*FT') T's = semcheckop(Fi,T'2s) // Semantic check and // synthetized attribute sent up return(true) else return(false) else if (lookahead is in [+,$,)] write(T'->epsilon) T's = Fi // Synthetized attribute is // inhertied from F sibling // and sent up the tree return(true) else return(false) }

Concordia University Department of Computer Science and Software Engineering Attribute migration: implementation in recursive-descent predictive parser Joey Paquet, COMP 442/6421 – Compiler Design F(type &Fs){ type Es if (lookahead is in [0]) if (Match('id')) write(F->id) Fs = gettype(id.name,table) // Gets the attribute ``type'' // from the symbol table and // sends it up the tree as Fs return(true) else return(false) else if (lookahead is in [(]) if (Match('(');E(Es);Match(')')) write(F->(E)) Fs = Es// Synthetized attribute from E // sent up the tree as attribute // of F return(true) else return(false) }

Concordia University Department of Computer Science and Software Engineering Attribute migration: implementation in recursive-descent predictive parser Joey Paquet, COMP 442/6421 – Compiler Design type semcheckop(type ti,type ts){ if (ti == ts) return(ti) else return(typerror) } type gettype(name, table){ if (name is in table) return (type) else return(typerror) }

Concordia University Department of Computer Science and Software Engineering Attribute migration: example Joey Paquet, COMP 442/6421 – Compiler Design E T1T1 T2T2 T’ 2 * F1F1 id(v a :) T’ 1  F2F2 id(v b :) E’ 2  T’ 3  F3F3 id(v c :) E’ 1 + Es E’sTs T’s Ti Fi Fs E’s T’s Fs Ti T’s Fi Ts Fi T’s a+b*c

Concordia University Department of Computer Science and Software Engineering Click to edit Master title style Symbol table generation Joey Paquet, COMP 442/6421 – Compiler Design

Concordia University Department of Computer Science and Software Engineering Symbol tables: example and hints Joey Paquet, COMP 442/6421 – Compiler Design class MyClass1 { int mc1v1[2][4]; float mc1v2; MyClass2 mc1v3[3]; int mc1f1(int p1, MyClass2 p2[3]) { MyClass2 fv1[3]; … } int f2(MyClass1 f2p1[3]) { int mc1v1; … } class MyClass2 { int mc1v1[2][4]; float fp1; MyClass m2[3]; … } program { int m1; float[3][2] m2; MyClass[2] m3;... } float f1(int fp1[2][2], float fp2) { MyClass1[3] fv1; int fv2; … } int f2() {... }

Concordia University Department of Computer Science and Software Engineering Symbol tables: example and hints Joey Paquet, COMP 442/6421 – Compiler Design ::=#createGlobalTable# * ::=class id #createClassEntryAndTable# { * *}; ::=program #createProgramTable# ; * ::= id( ) #createFunctionEntryAndTable# ::= ; ::={ * *} ::= id *; #createVariableEntry# ::= ; |if( )then else ; |for( id ; ; ) ; |get( ); |put( ); |return( ); ::= ::= { *} | |  ::= | ::= ::= | ::=+ | - ::= | ::= | *id( ) |num |( ) |not | ::= *id * ::=id *. ::=[ ] ::=[ int ] ::=int | float | id ::= id * #createParameterEntry# * |  ::= * |  ::=, id * #createParameterEntry# ::=, Insert semantic actions (function calls) that create tables/entries when an identifier is declared. Attribute migration must be used when the information necessary to create an entry is distributed over different rules. The same process will be used to do further semantic processing: Expression type inference Type checking Code generation