Compilers Project 3: Semantic Analyzer

Similar documents
Introduction to Compilers and Language Design Copyright (C) 2017 Douglas Thain. All rights reserved.

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

Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 2. Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 4

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

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

Programming Project II

Compiler construction 2002 week 5

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

CS 6353 Compiler Construction Project Assignments

flex is not a bad tool to use for doing modest text transformations and for programs that collect statistics on input.

Semantic actions for declarations and expressions

Semantic actions for declarations and expressions. Monday, September 28, 15

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS 6353 Compiler Construction Project Assignments

Semantic actions for declarations and expressions

Compiler construction in4020 lecture 5

CS /534 Compiler Construction University of Massachusetts Lowell

Error Detection in LALR Parsers. LALR is More Powerful. { b + c = a; } Eof. Expr Expr + id Expr id we can first match an id:

CS 426 Fall Machine Problem 1. Machine Problem 1. CS 426 Compiler Construction Fall Semester 2017

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Implementing a VSOP Compiler. March 20, 2018

CS131 Compilers: Programming Assignment 2 Due Tuesday, April 4, 2017 at 11:59pm

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Parser Tools: lex and yacc-style Parsing

1 Lexical Considerations

Motivation was to facilitate development of systems software, especially OS development.

CSE 340 Fall 2014 Project 4

Compiler construction 2005 lecture 5

CS164: Programming Assignment 2 Dlex Lexer Generator and Decaf Lexer

Compilers CS S-05 Semantic Analysis

Semantic Analysis. Lecture 9. February 7, 2018

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Project Compiler. CS031 TA Help Session November 28, 2011

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

A Simple Syntax-Directed Translator

Programming Assignment IV Due Monday, November 8 (with an automatic extension until Friday, November 12, noon)

Syntax Errors; Static Semantics

A simple syntax-directed

Parser Tools: lex and yacc-style Parsing

Lexical Considerations

Engineering project Compiler Construction Assignments

Qualifying Exam in Programming Languages and Compilers

Compiler construction assignments

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

CS 314 Principles of Programming Languages Fall 2017 A Compiler and Optimizer for tinyl Due date: Monday, October 23, 11:59pm

cmps104a 2002q4 Assignment 3 LALR(1) Parser page 1

Lexical Considerations

Programming Assignment IV

CS 321 Homework 4 due 1:30pm, Thursday, March 15, 2012 This homework specification is copyright by Andrew Tolmach. All rights reserved.

Compiler Lab. Introduction to tools Lex and Yacc

COMPILER CONSTRUCTION Seminar 02 TDDB44

CS 2210 Programming Project (Part IV)

COP4020 Programming Assignment 2 - Fall 2016

Syntax Analysis Check syntax and construct abstract syntax tree

Semantic Analysis and Type Checking

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

Motivation was to facilitate development of systems software, especially OS development.

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Starting to Program in C++ (Basics & I/O)

Chapter 4 Defining Classes I

ChAmElEoN Parse Tree

A Short Summary of Javali

A Bison Manual. You build a text file of the production (format in the next section); traditionally this file ends in.y, although bison doesn t care.

Chapter 2 A Quick Tour

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

EECS483 D6: Project 3 Overview

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Introduction to Compilers and Language Design

3.5 Practical Issues PRACTICAL ISSUES Error Recovery

CS3114 (Fall 2013) PROGRAMMING ASSIGNMENT #2 Due Tuesday, October 11:00 PM for 100 points Due Monday, October 11:00 PM for 10 point bonus

Programming Assignment IV Due Thursday, November 18th, 2010 at 11:59 PM

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

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

Programming Languages

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 4

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

Out: April 19, 2017 Due: April 26, 2017 (Wednesday, Reading/Study Day, no late work accepted after Friday)

More On Syntax Directed Translation

Lexical and Syntax Analysis

Please refer to the turn-in procedure document on the website for instructions on the turn-in procedure.

The role of semantic analysis in a compiler

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1

Programming Assignment II

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

The compilation process is driven by the syntactic structure of the program as discovered by the parser

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

Programming Project 4: COOL Code Generation

CSE 230 Intermediate Programming in C and C++ Functions

Abstract Syntax Trees

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

Language Reference Manual simplicity

CSE-304 Programming Assignment #4

Acknowledgement. CS Compiler Design. Semantic Processing. Alternatives for semantic processing. Intro to Semantic Analysis. V.

Intermediate Code Generation

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator

Implementing Actions

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CSCI Compiler Design

Transcription:

Compilers Project 3: Semantic Analyzer CSE 40243 Due April 11, 2006 Updated March 14, 2006 Overview Your compiler is halfway done. It now can both recognize individual elements of the language (scan) and determine the structure of a string of the language (parse). The next step is to determine the meaning of the string based on its parse, which is what we call semantic analysis. The main goal of the semantic analysis is to build an Abstract Syntax Tree (AST), which the next project can use to generate code. In order for the code generator to do its job correctly, you also need to resolve names in the AST to symbols so that operations are consistent in the generated code. Finally, you will need to make sure types and their associated operations match up, otherwise your code generator may not be able to make sense of what. Abstract Syntax Tree You will once again use Bison for this part of the assignment. For each syntax rule of Bison, you will add actions for Bison to perform whenever it encounters such a rule, placing the necessary elements of the parse into the abstract syntax tree. As discussed in class, for each element of the AST (decl, stmt, expr, etc.), you should create a function that generates the element: decl make, stmt make, expr make, and so on. For example, a parser rule matching an if statement should create a statement structure like this: stmt : TOKEN_IF TOKEN_LPAREN expr TOKEN_RPAREN stmt { $$ = stmt_make( STMT_KIND_IF, $3, $5, 0, 0, 0 ); } Remember that yyparse() only returns an integer indicating success or failure. Thus, for the top-level rule, you must save the root of the abstract syntax tree in a global pointer to be examined later. Do something like this: %{ struct decl * ast_root; %} %% program : decl_list { ast_root = $1 }; 1

By default, Bison assumes that the result of each rule production is an integer. Although this makes sense for constructing a calculator interpreter, it is no good for an AST; each rule produces a pointer to a structure. Thus, we must tell Bison that each rule is going to produce a pointer to an arbitrary object like so: #define YYSTYPE void * Before embarking on the remaining steps, it is very important to ensure that you have generated the AST correctly. The easiest way to do this is to traverse the AST and print the program back out: you should see a program that is equivalent to the input, although missing comments and some whitespace. To do this, you should implement for each element of the AST a routine that prints the element on the standard output stream. Of course, these routines should traverse the entire AST: decl display should use stmt display which should use expr display, and so forth. Then to display the entire AST, simply invoke decl display(ast root); Name Resolution Once you have constructed the AST, you must then perform the name resolution algorithm described in class. The name resolution does two things while traversing the AST: it enters symbols into the scope table using scope bind and it matches names in expressions using scope lookup. As each use of a name in an expression is matched, the symbol field of the expression should be changed to point to the symbol. Note that we will provide you with a working scope table to use. Like the other routines, name resolution is also recursive: decl resolve should call stmt resolve and so forth. To help debug this step, your compiler should output a line every time that it resolves a name to a symbol. Typechecking Once the AST is constructed and names are bound to symbols, we are ready to begin checking types. As you might expect, this routine traverses down the AST, starting with decl typecheck, then stmt typecheck, expr typecheck, and so forth. Let s talk about these from the bottom up. As you know, every expression has a return type: (1+(a+b)) has type integer, while (a==b) has type boolean. The function expr typecheck must return the type of an expression in the form of a struct type *. This is a recursive action: expr typecheck(1+(a+b)) must result in calls to expr typecheck(1), expr typecheck(a+b), expr typecheck(a), and expr typecheck(b) in order to check the entire expression down to its leaves. For constants, the result of expr typecheck is obvious: it must return the type corresponding to the constant. For names, expr typecheck should return the type of the symbol corresponding to the name, as determined by the name resolution step. For operators, expr typecheck should evaluate the left and right sides of the expression, and then return the type that results from the operator: arithmetic expressions return type integer, comparison operators return type boolean. Once you have implemented typechecking for expressions, then implement decl typecheck and stmt typecheck which traverse the tree like all of the other operations. 2

Working together, these routines should enforce the following type rules: 1. Arithmetic operations must only accept integer operands. 2. Comparison operations may operate on any type, but both sides of the comparison must have the same type. 3. An assignment must have a name on the left side and an expression with the same type on the right side. 4. The arguments to a function call must match the types of the function parameters. 5. The controlling expression of an if or while statement must be boolean. 6. The type of an expression in a return statement must match the return type of the function it is in. In addition, the following semantic rules must be checked: 1. In a function or a code block, all variable declarations must come before any code statements. 2. A function may only appear in a global declaration. Required Output Your compiler must produce the following output: The input program, printed out from the AST. One line for each time a name is resolved to a symbol. If an undefined name is used, the compiler should stop immediately and print X is undefined. where X is the invalid name. If a type error is detected, the compiler should stop immediately and print a meaningful error message: e.g. type error: cannot assign string to char If there are no type errors, then display No type errors. For example, consider the following program: /* here is a comment */ int b=6; int main() { int x=3; int y; } y = x*b + c ; A compiler processing this program would produce: 3

int b=6; int main() { int x=3; int y; y=x*b+ c ; } y is local 1 x is local 0 b is global type error: cannot add int to char Starting Files As usual, the solution to the previous assignment will be provided as a starter in case that your own code was not entirely correct. We will also provide the header files which define the C-flat abstract syntax tree and symbol table. All these files can be located at the following URLs: file:///afs/nd.edu/coursesp.06/cse/cse40243.01/projects/semantics/code http:///www.nd.edu/courses/cse/cse40243.01/projects/semantics/code These files should be copied into your project s source code directory in order to complete your assignment. Grading The assignment will be graded out of 100 points and is worth 10 percent of the course grade. Grading will be based on functionality, but partial credit will be assigned if, due to clearly formatted and commented code, the grader can determine what is going on. As usual, up to 5 points extra credit will be given for any significant improvement to the compilation process or extension to the language and will be based logarithmically on the difficulty of the addition. Meaningful error handling would be a good example. Deliverable This project is due at 9:30 am on Tuesday, April 11. Project source should be archived into a tar file (compression optional) and placed in the /afs/nd.edu/coursesp.06/cse/cse40243.01/ dropbox/netid/semantics directory, where NETID is your Notre Dame NetID. An extra credit attempt for this project should be documented in a file called EXTRA.semantics at the root of your submitted project package. The documentation should explain what has been done to extend/enhance the project and how to go about testing this claim. The submitted package should also contain any test code if required by the nature of the extension/improvement. 4

Coding Your code should conform to the coding conventions mentioned in class. Failure to do so will result in inoperable code. For more information on naming functions and other coding guidelines, see the C AST handout available on the website and the provided header files. 5