intermediate code generator syntax tree token stream intermediate representation parser checker tree

Similar documents
Syntax-Directed Translation. CS Compiler Design. SDD and SDT scheme. Example: SDD vs SDT scheme infix to postfix trans

There is a level of correctness that is deeper than grammar. There is a level of correctness that is deeper than grammar

Context-sensitive Analysis. Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Formal Languages and Compilers Lecture IX Semantic Analysis: Type Chec. Type Checking & Symbol Table

Type Checking. Error Checking

Context-sensitive Analysis

Syntax Directed Translation

Context-sensitive Analysis

CS415 Compilers Context-Sensitive Analysis Type checking Symbol tables

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Syntax-Directed Translation. Concepts Introduced in Chapter 5. Syntax-Directed Definitions

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Syntax-Directed Translation

5. Semantic Analysis!

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Syntax-Directed Translation. Introduction

Compiler Principle and Technology. Prof. Dongming LU April 15th, 2019

Grammars. CS434 Lecture 15 Spring 2005 Department of Computer Science University of Alabama Joel Jones

1 Lexical Considerations

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Syntax Directed Translation

Syntax-Directed Translation

Context-sensitive analysis. Semantic Processing. Alternatives for semantic processing. Context-sensitive analysis

COP5621 Exam 3 - Spring 2005

More On Syntax Directed Translation

Compiler Lab. Introduction to tools Lex and Yacc

Lexical Considerations

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Context-sensitive Analysis Part II Chapter 4 (up to Section 4.3)

Semantic Analysis. Lecture 9. February 7, 2018

5. Syntax-Directed Definitions & Type Analysis

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

Lexical Considerations

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.

Syntax-Directed Translation Part I

Semantic analysis. Syntax tree is decorated with typing- and other context dependent

Intermediate Code Generation

A programming language requires two major definitions A simple one pass compiler

COP4020 Spring 2011 Midterm Exam

Formal Languages and Compilers Lecture X Intermediate Code Generation

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Type systems. Static typing

LECTURE 17. Expressions and Assignment

Syntactic Directed Translation

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

CSC 467 Lecture 13-14: Semantic Analysis

Computational Expression

Time : 1 Hour Max Marks : 30

Syntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask?

Compilers. Compiler Construction Tutorial The Front-end

LECTURE 3. Compiler Phases

Semantic Analysis. Compiler Architecture

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

Introduction to Programming Using Java (98-388)

COMP 181. Prelude. Prelude. Summary of parsing. A Hierarchy of Grammar Classes. More power? Syntax-directed translation. Analysis

CS 406: Syntax Directed Translation

Context-sensitive Analysis Part II

Syntax-Directed Translation Part II

Static and Dynamic Semantics

Semantic actions for expressions

Introduction to Computer Science Midterm 3 Fall, Points

Lecture 14 Sections Mon, Mar 2, 2009

We now allow any grammar symbol X to have attributes. The attribute a of symbol X is denoted X.a

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

Ambiguity and Errors Syntax-Directed Translation

Typescript on LLVM Language Reference Manual

Chapter 4 - Semantic Analysis. June 2, 2015

Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

The role of semantic analysis in a compiler

Compilers. 5. Attributed Grammars. Laszlo Böszörmenyi Compilers Attributed Grammars - 1

PROGRAMMING FUNDAMENTALS

Lecture Overview Code generation in milestone 2 o Code generation for array indexing o Some rational implementation Over Express Over o Creating

Semantic Analysis with Attribute Grammars Part 1

Operational Semantics of Cool

COSE312: Compilers. Lecture 1 Overview of Compilers

CS 415 Midterm Exam Spring 2002

Single-pass Static Semantic Check for Efficient Translation in YAPL

PART 4 - SYNTAX DIRECTED TRANSLATION. F. Wotawa TU Graz) Compiler Construction Summer term / 309

Syntactic Directed Translation

Java is an objet-oriented programming language providing features that support

UNIT IV INTERMEDIATE CODE GENERATION

Intermediate Code Generation Part II

[Syntax Directed Translation] Bikash Balami

Concepts Introduced in Chapter 6

Parsing Techniques. AST Review. AST Data Structures. LL AST Construction. AST Construction CS412/CS413. Introduction to Compilers Tim Teitelbaum

The PCAT Programming Language Reference Manual

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Transcription:

Context-sensitive analysis context-sensitive questions might the compiler What ask? 1. Is x a scalar, anarray, or a function? 2. Is x declared before it is used? 3. Are any names declared but not used? 4. Which declaration of x does this reference? 5. Is an expression type-consistent? Does the dimension of a reference match the 6. declaration? 7. Where can xbe stored? èheap, stack, :::è 8. Does *p reference the result of a mallocèè? 9. Is x dened before it is used? 10. Is an array reference in bounds? 11. Does function foo produce a constant value? hese cannot be answered with a context-free grammar CMC 430 Lecture 9, Page 1 Context-sensitive analysis Why is context-sensitive analysis hard? need non-local information answers depend on values, not on syntax answers may involve computation How can we answer these questions? 1. use context-sensitive grammars general problem is P-space complete 2. use attribute grammars augment context-free grammar with rules calculate attributes for grammar symbols 3. use ad hoc techniques augment grammar with arbitrary code execute code at corresponding reduction store information in attributes, symbol tables CMC 430 Lecture 9, Page 2

Attribute grammars Attribute grammar generalization of context-free grammar each grammar symbol has an associated set of attributes augment grammar with rules that dene values high-level specication, independent of scheme evaluation Dependences between attributes values are computed from constants & other attributes synthesized attribute í value computed from children inherited attribute í value computed from & parent siblings CMC 430 Lecture 9, Page 3 xample attribute grammar A grammar to evaluate signed binary numbers due to cott K. Warren, Rice Ph.D. Production valuation Rules 1 NUM ::= IGN LI LI.pos è 0 è if IGN.neg NUM.val -LI.val then else LI.val IGN ::= + IGN.neg è false 2 IGN ::= - IGN.neg è true 3 LI ::= BI BI.pos è LI.pos 4 è BI.val LI.val LI 0 ::= LI 1 BI LI 1.pos è LI 0.pos + 1 5 è LI 0.pos BI.pos LI 0.val è LI 1.val + BI.val BI ::= 0 BI.val è 0 6 BI ::= 1 BI.val è 2 BI.pos 7 CMC 430 Lecture 9, Page 4

Attribute grammars xample IGN neg: - NUM val: -5 LI BI pos: 2 val: 4 pos: 2 val: 4 1 LI pos: 1 val: 4 LI BI pos: 0 val: 5 pos: 1 val: 0 0 BI val and neg are synthesized attributes pos is an inherited attribute pos: 0 val: 1 CMC 430 Lecture 9, Page 5 1 yntax-directed translation Disadvantages of attribute grammars handling non-local information storage management locating answers avoiding circular evaluation yntax-directed translation allow arbitrary actions provide central repository can place actions amid production xamples YACC A ::= BCf$$ = concatè$1,$2è; g CUP A:n ::= B:m C:p f: n = concatèm,pè; :g ypical uses build symbol table perform errorètype checking CMC 430 Lecture 9, Page 6

If operands of addition are of type integer, then is of type integer result he result of the unary & operator is a pointer the object referred to by the operand to CMC 430 Lecture 9, Page 7 CMC 430 Lecture 9, Page 8 ype checking ype systems ypes token stream parser syntax tree type checker syntax tree intermediate code generator intermediate representation values that share a set of common properties dened by language andèor programmer ype checker ype system enforces rules of type system may be strongèweak, staticèdynamic 1. set of types in a programming language, and 2. rules that use types to specify program behavior tatic type checking xample type rules performed at compile time early detection, no run-time overhead not always possible èe.g., Aëiëè Dynamic type checking Advantages of typed languages performed at run time ensure run-time safety more exible, rapid prototyping expressiveness èoverloading, polymorphismè overhead to check run-time type tags provide information for code generation

ype expressions ype expressions used to represent the type of a language construct describes both language and programmer types xamples basic types: integer, real, character,... constructed types: arrays, records, pointers, functions,... Constructing new types arrays arrayè1:::10, è records 1 2 ::: pointers pointerèè functions 1 2 :::! n CMC 430 Lecture 9, Page 9 A simple type checker a synthesized attribute grammar, we will Using a type checker for arrays, pointers, describe statements, and functions. Grammar for source language: ::= D; P ::= D;jid: D ::= char j integer j array ënumë of j " ::= literal j num j id j mod j ëë j " Basic types char, integer, typerror assume all arrays start at1,e.g., ë256ë of char array in the type expression results ::256,charè arrayè1: builds a pointer type, so " integer " in the type expression pointerèintegerè results CMC 430 Lecture 9, Page 10

ype checking example Partial attribute grammar for the type system ::= id: f addtypeèid.entry,.typeè g D ::= char f.type è char g ::= integer f.type è integer g ::= " 1 f.type è pointerè 1.typeè g ::= array ënumë of 1 f.type è 1.typeè g arrayè1:::num.val, CMC 430 Lecture 9, Page 11 ype checking expressions expression is assigned a type using rules ach with the grammar. associated ::= literal f.type è char g ::= num f.type è integer g ::= id f.type è lookupèid.entryè g ::= 1 mod 2 f.type è if 1.type = integer and = integer then integer 2.type ::= 1 ë 2 ë f.type è if 2.type = integer and = arrayès,tè then t 1.type ::= 1 " f.type è if 1.type =pointer t then CMC 430 Lecture 9, Page 12

ype checking statements do not typically have values, therefore tatements assign them the type void. If an error is detected we within the statement, it gets type typerror. ::= id è f.type è if id.type =.type void then ::= if then 1 f.type è if.type =boolean 1.type then ::= while do 1 f.type è if.type =boolean 1.type then ::= 1 ; 2 f.type è if 1.type =void void then CMC 430 Lecture 9, Page 13 ype checking functions add two new productions to the grammar to We function declarations and applications represent ::=! declaration ::= èè application o capture the argument and return type, we use ::= 1! 2 f.type è è 1.type! 2.typeè g ::= 1 è 2 è f.type è if 1.type =s!t 2.type =s then t and CMC 430 Lecture 9, Page 14