Procedures – Building the Syntax Tree

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Symbol Table.
Arrays.
Spring Semester 2013 Lecture 5
Template. 2 Using templates, it is possible to create generic functions and classes. In a generic function or class, the type of data upon which the function.
Abstract Syntax Mooly Sagiv html:// 1.
Chapter 8 Intermediate Code Generation. Intermediate languages: Syntax trees, three-address code, quadruples. Types of Three – Address Statements: x :=
The Symbol Table Lecture 13 Wed, Feb 23, The Symbol Table When identifiers are found, they will be entered into a symbol table, which will hold.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
5-1 5 Compilation  Overview  Compilation phases –syntactic analysis –contextual analysis –code generation  Abstract syntax trees  Case study: Fun language.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Procedures – Generating the Code Lecture 21 Mon, Apr 4, 2005.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
Lambda Expressions Version 1.0
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
Pascal Programming Pointers and Dynamic Variables.
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
More yacc. What is yacc – Tool to produce a parser given a grammar – YACC (Yet Another Compiler Compiler) is a program designed to compile a LALR(1) grammar.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
JavaScript Variables. Definition A variable is a "container" for information you want to store. A variable's value can change during the script.
Chapter 3 – Describing Syntax
User-Written Functions
Pointers.
A Simple Syntax-Directed Translator
Semantic Analysis with Emphasis on Name Analysis
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Ch. 4 – Semantic Analysis Errors can arise in syntax, static semantics, dynamic semantics Some PL features are impossible or infeasible to specify in grammar.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Procedures: Building the Syntax Tree
PL/SQL Scripting in Oracle:
FIGURE 4-10 Function Return Statements
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
CSE 3302 Programming Languages
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Return_DT F_name ( list of formal parameters)
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Chapter 6 Intermediate-Code Generation
Array and Method.
Compiler design.
Compiler Construction
FIGURE 4-10 Function Return Statements
CS 432: Compiler Construction Lecture 11
Deletion in AVL Tree There are 5 cases to consider.
Backpatching Lecture 23 Wed, Apr 13, 2005.
FIGURE 4-10 Function Return Statements
Variable Storage Memory Locations (Logical) Variable Classes Stack
Scope Rules.
Presentation transcript:

Procedures – Building the Syntax Tree Lecture 20 Wed, Mar 30, 2005

Building the Function Definition Syntax Tree To write the assembly code for the beginning of a function definition, we must know The name of the function. The size of the local variable block. The relevant production is fbeg  fname fargs LBRACE dcls Therefore, the information is available. This information will be stored in the left and right subtrees of a FUNC tree.

Building the Function Definition Syntax Tree From the production fbeg  fname fargs LBRACE dcls we see that the function name, parameters, and declarations have already been processed. Also, the function name should be global, while the parameters and declarations should be local.

Building the Function Definition Syntax Tree Therefore, we must call on enterBlock() after installing the function name, but before installing the parameters or local variables.

A Function Definition Syntax Tree f.mode ID f.id.name NUM n.num

A Function Definition Syntax Tree Name of the function FUNC f.mode ID f.id.name NUM n.num

A Function Definition Syntax Tree Return type FUNC f.mode ID f.id.name NUM n.num

A Function Definition Syntax Tree Size of the local variable block FUNC f.mode NUM n.num ID f.id.name

Building the Function Return Syntax Tree To write the assembly code to return from a function, we must know The return type. The object to be returned. The return type will be stored in the SymbolTable class variable retType.

Function Return Syntax Trees retType RET retType e e.mode RET retType CAST e e.mode

Building the Function End Syntax Tree To write the assembly code for the end of a function definition, we do not need any special information, although we will know the name of the function. This marks the physical end of the function. A return statement marks a logical end. To be safe, at the physical end of the function, we will insert a return with no returned value.

Building the Function End Syntax Tree Since this marks the physical end of the function, the block level should be decreased. Be sure to call on leaveBlock() to do this, so that it can delete the hash table of local variables.

Return at End of Function At the end of the function, there needs to be an automatic return, even if the programmer didn’t write a return statement. But then, what about the return value? Similarly, what if the programmer writes return; without specifying a return value when there should be one?

A Function End Syntax Tree FEND f.mode ID

Building a Function Call Syntax Tree To write the assembly code for a function call, we need to know The name of the function. Each actual parameter. Each actual parameter is an expression. The parameters are arranged in a LIST tree.

LIST Trees A LIST tree is a recursive structure where the right subtree is an expression tree and the left subtree is an expression tree, or a LIST tree. In the LIST tree of actual parameters, each LIST node, except the last, has an parameter in its right subtree and a LIST node in its left subtree. The last LIST node has two parameters in its subtrees.

A LIST Tree of 4 Parameters

Example The syntax tree for the function call a = sum(10, 20, 30); would be ASSIGN INT ID PTR|INT value = “a" CALL INT ID PTR|PROC|INT value = "sum" LIST NUM INT value = 10 NUM INT value = 20 NUM INT value = 30

A LIST Tree of Two Parameters

A Tree of One Parameter If there is only one parameter, then we do not need a LIST tree. param1

Building a Function Call Syntax Tree The productions involved in function calls are expr  ID LPAREN exprs RPAREN | ID LPAREN RPAREN exprs  exprs COMMA expr | expr The production exprs  exprs COMMA expr tells us that the parameters are parsed from left to right.

A Function Call Syntax Tree f.mode ID LIST param3 param2 param1

A Function Call with Only One Parameter f.mode ID param1