CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at https://www.cs.purdue.edu/homes/ninghui/courses/252_Spri.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
C Language.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Intermediate Code Generation
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Programming Languages and Paradigms The C Programming Language.
CS252: Systems Programming Ninghui Li Topic 4: Regular Expressions and Lexical Analysis.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
ECE 103 Engineering Programming Chapter 11 One Minute Synopsis Herbert G. Mayer, PSU CS Status 7/1/2014.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
Environments and Evaluation
Structures, Unions, and Typedefs CS-2303, C-Term Structures, Unions, and Typedefs CS-2303 System Programming Concepts (Slides include materials from.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Review of C++ Programming Part II Sheng-Fang Huang.
Symbol Table (  ) Contents Map identifiers to the symbol with relevant information about the identifier All information is derived from syntax tree -
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
Values, variables and types © Allan C. Milne v
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Stack and Heap Memory Stack resident variables include:
Arrays- Part 2 Spring 2013Programming and Data Structure1.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Pointers OVERVIEW.
Midterm Review CS1220 Spring Disclaimer The following questions are representative of those that will appear on the midterm exam. They do not represent.
© M. Winter COSC 4P41 – Functional Programming Programming with actions Why is I/O an issue? I/O is a kind of side-effect. Example: Suppose there.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
1 LEX & YACC Tutorial February 28, 2008 Tom St. John.
Pointers *, &, array similarities, functions, sizeof.
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
Semantic Values and Symbol Tables © Allan C. Milne Abertay University v
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
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.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
CMSC 330: Organization of Programming Languages Operational Semantics.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Programming Fundamentals Enumerations and Functions.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Lecture 9 Symbol Table and Attributed Grammars
User-Written Functions
Chapter 7: User-Defined Functions II
COMP261 Lecture 18 Parsing 3 of 4.
Constructing Precedence Table
Student Book An Introduction
C Basics.
Programmazione I a.a. 2017/2018.
DATA HANDLING.
Buy book Online -
Mini Language Interpreter Programming Languages (CS 550)
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
C Language B. DHIVYA 17PCA140 II MCA.
Presentation transcript:

CS252 Lab 2 Prepared by El Kindi Rezig

Notes Check out new version of the “official” fiz interpreter at ng15/code/fizlab/fizhttps:// ng15/code/fizlab/fiz New functionality: tracing

Use “help” in the interpreter fiz> help You can use the following commands: import tracing on tracing off (define ( >) >) > The grammar for > is: > ::= (inc >) | (dec >) | (ifz > > >) | (halt) | ( >)

Result of tracing (for add) fiz> tracing on fiz> (add 3 2) Evaluating (add 3 2) Evaluating (ifz 2 3 (add (inc..) (dec..))) Evaluating (add 4 1) Evaluating (ifz 1 4 (add (inc..) (dec..))) Evaluating (add 5 0) Evaluating (ifz 0 5 (add (inc..) (dec..))) (add 5 0) = 5 (add 4 1) = 5 (add 3 2) = 5 5

Result of tracing (for sub) fiz> (sub 3 2) Evaluating (sub 3 2) Evaluating (ifz 2 3 (ifz 3 (halt) (sub (dec..) (dec..)))) Evaluating (ifz 3 (halt) (sub (dec..) (dec..))) Evaluating (sub 2 1) Evaluating (ifz 1 2 (ifz 2 (halt) (sub (dec..) (dec..)))) Evaluating (ifz 2 (halt) (sub (dec..) (dec..))) Evaluating (sub 1 0) Evaluating (ifz 0 1 (ifz 1 (halt) (sub (dec..) (dec..)))) (sub 1 0) = 1 (sub 2 1) = 1 (sub 3 2) = 1 1

Notes Try write some fiz programs using the interpreter E.g., write (max x y z), which outputs the max value among x,y,z. Do it from the ground up Will help you in the exam

Formal and Actual Arguments Arguments are also known as parameters Formal arguments/parameters appear in function definition Actual arguments/parameters appear in function calls E.g., (define (add x y) (ifz y x (add (inc x) (dec y)))) (add 3 2) Formal Actual

Overview -Lab 2 has two parts: 1.FIZ without user-defined functions (50%): Parse and evaluate built-in FIZ functions. 2.Support of user-defined functions’ declarations and calls (50%): Parse and evaluate user-defined functions’ declarations and calls.

Lab 2: Part 1 (FIZ without user-defined functions) The language grammar: : (inc ) | (dec ) | (ifz ) | (halt) | NUMBER All expressions evaluate to a non-negative integer value.

Lab 2: Part 1 (FIZ without user-defined functions) Example statements that are supported by FIZ: (inc (inc 5)) // interpreter prints 6 (dec (inc (dec 3))) // interpreter prints 2 (dec (ifz 0 4 (inc 1))) // interpreter prints 3 Example statements that are NOT supported by FIZ: (inc 4 5) // Error: inc only takes 1 argument. (ifz 0 halt) // Error: halt used without parenthesis, no else statement (inc (dec )) //Error: no argument for “dec”.

Lab 2: Part 1 (FIZ without user-defined functions) TODO -Implement the scanner and parser. -For any newly added files, type: 1.git add {new file name} 2.git commit -am "description of the changes you have made." 3.git push -You need to push all your changes before: 02/09/2015, 11:59pm.

Lab 2: (support of user-defined functions’ declaration and calls) (50%) -Parse and evaluate user-defined functions’ declarations and calls. -Add to the parser to parse the following: 1.(define (ID +) ) 2.(ID +) -Evaluate the user-defined declarations and calls.

Lab 2: (support of user-defined functions’ declaration and calls) (50%) nameargNamesbody numArgs FUNC_DECL structure definition: foo 1 x Example: (define (foo x) (inc x)) INC FUNC_DECL TREE_NODE NODE TYPE name numArgsargNames body x ARG_NAME Could be a size-10 array, with only the first element used

Lab 2: (support of user-defined functions’ declaration and calls) (50%) add 2 xIFZ NODE TYPE Example: (define (add x y) (ifz y x (add (inc x) (dec y)))) y FUNC_CALL add2 argNames numArgs name numArgs body FUNC_DECL INC DEC y ARG_NAME x x y Could be a size-10 array, with only the first two elements used

Lab 2: (support of user-defined functions’ declaration and calls) (50%) add 2 args numArgs INC NUMBER_NODE 2 FUNC_CALL Example: (add (inc 1) 2) name 1.Resolve expression NUMBER_NODE 1

Lab 2: (support of user-defined functions’ declaration and calls) (50%) 2 args numArgs INC NUMBER_NODE 2 FUNC_EVAL Example: (add (inc 1) 2) func 1.Resolve expression add 2 xIFZyyx FUNC_CALLadd 2 FUNC_DECL INC DEC NUMBER_NOD E 1 Add reference to the function declaration structure x ARG_NAME y x y

Lab 2: (support of user-defined functions’ declaration and calls) (50%) 2 args numArgs INC NUMBER_NODE 2 FUNC_EVAL Example: (add (inc 1) 2) func 1.Resolve expression add 2 xIFZyyx FUNC_CALLadd 2 FUNC_DECL INC DEC NUMBER_NOD E 1 Add reference to the function declaration structure x ARG_NAME y x y 2. Evaluate parameters

Lab 2: (support of user-defined functions’ declaration and calls) (50%) 2 args numArgs INC NUMBER_NODE 2 FUNC_EVAL Example: (add (inc 1) 2) func 1.Resolve expression 2.Evaluate parameters eval() 2 add 2 xIFZyyx FUNC_CALLadd 2 FUNC_DECL INC DEC NUMBER_NOD E 1 x ARG_NAME y x y

Lab 2: (support of user-defined functions’ declaration and calls) (50%) 2 args numArgs NUMBER_NODE 2 FUNC_EVAL Example: (add (inc 1) 2) func 1.Resolve expression 2.Evaluate parameters eval() 2 2 INC add 2 xIFZyyx FUNC_CALLadd 2 FUNC_DECL INC DEC NUMBER_NOD E 1 x ARG_NAME y x y

Lab 2: (support of user-defined functions’ declaration and calls) (50%) add 2 xIFZyyx FUNC_EVAL 2 FUNC_DECL INC DEC NUMBER_NOD E 1 x ARG_NAME y x y 1.Resolve expression 2.Evaluate parameters 3.Resolve body of add

Lab 2: (support of user-defined functions’ declaration and calls) (50%) add 2 xIFZyyx FUNC_EVAL 2 FUNC_DECL INC DEC NUMBER_NOD E 1 x ARG_NAME y x y 1.Resolve expression 2.Evaluate parameters 3.Resolve body of add 4.Pass computed params to the function.

Lab 2: (support of user-defined functions’ declaration and calls) (50%) TODO for this part 1.Add the (define) construct into the grammar. 2.Function declaration: Build the AST for each user- defined function and store it. 3.Function call: Lookup the AST of the called function, and evaluate its branches using the provided parameters. 4. You need to push all your changes before: 02/16/2015, 11:59pm.

Where to Add Your Code in fiz.y? %token INC OPENPAR CLOSEPAR// more tokens %type expr // more non-terminals enum NODE_TYPE{ INC_NODE, NUMBER_NODE,// more node types }; struct TREE_NODE{ enum NODE_TYPE type; union { struct TREE_NODE *first_arg; int intValue; // more union members for other node types };

Union in C union { struct TREE_NODE *first_arg; int intValue; // more union members for other node types // such as struct { … } field; }; A union stores different data types in the same memory location In the union above, first_arg and intValue refer to the same memory location, as is any other additional member in the union. The size of a union is the max of the size of all its components. A union member can be a struct as well.

Why Use Union Reason 1: Want to access the same data in different ways, e.g., an IP address, or a hardware register can be defined as union { struct { unsigned char byte1; unsigned char byte2; unsigned char byte3; unsigned char byte4; } bytes; unsigned int word; } HW_Register; reg.dword = 0x ; reg.bytes.byte3 = 4; Similarly, using a union for an IP address allows access individual bytes in the address as well as treating them as integer

Why Use Union Reason 2: Implement pseudo-polymorphism. A structure may include different types of data (e.g., a TREE_NODE in FIZ interpreter may be of several different types). A field, type, indicates which type of node this is. The union includes the actual data needed for this type of node. Typically, assigning value to and retrieving from the node should use the same union member. See code for example.

Where to Add Your Code in fiz.y? struct FUNC_DECL { char *name; // Other information of the function needs to be added }; // More grammar rules statement: OPENPAR DEFINE OPENPAR … // define a function expr: … // handle function call // auxiliary rules that may be needed to define the above two

Where to Add Your Code in fiz.y? void resolve (struct TREE_NODE *node, struct FUNC_DECL *cf){ // cf==NULL when resolving an expression as a statement // cf points to the function when resolving a function // body switch(node->type) { // Add code to resolve appearances of function names, and // usage of arguments if we are resolving the body of a // function } return; }

Where to Add Your Code in fiz.y? int eval(struct TREE_NODE * node, int *env){ // env=NULL when evaluating an expression as a statement // env points to an array of actual arguments when evaluating the // body of a function switch(node->type){ case NUMBER_NODE: return node->intValue; case INC_NODE: return eval(node->first_arg, env) + 1; // Add code to evaluate other kinds of expressions, especially // a function call, for which we need to evaluate the actual // arguments first, and then evaluate the body, passing in the // array of actual argument values }

Avoiding Buffer Overflow and Memory Leak Count as 10% extra credit for Lab 2. Avoid buffer overflow: avoid using fixed-size buffer for identifiers Avoid memory leak: Free allocated memory when no longer needed An expression statement after evaluated Define statement when there is an error in functional definition Possibly other things