CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz...

30
CS252 Lab 2 Prepared by El Kindi Rezig

Transcript of CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz...

Page 1: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

CS252Lab 2

Prepared by El Kindi Rezig

Page 2: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

Notes

• Check out new version of the “official” fiz interpreter at

• https://www.cs.purdue.edu/homes/ninghui/courses/252_Spring15/code/fizlab/fiz

• New functionality: tracing

Page 3: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

Use “help” in the interpreterfiz> helpYou can use the following commands: import <file_name> tracing on tracing off (define (<func_name> <<arg_list>>) <<expr>>) <<expr>>The grammar for <<expr>> is: <<expr>> ::= (inc <<expr>>) | (dec <<expr>>) | (ifz <<expr>> <<expr>> <<expr>>) | (halt) | (<func_name> <<expr_list>>)

Page 4: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

Result of tracing (for add)fiz> tracing onfiz> (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) = 55

Page 5: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

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) = 11

Page 6: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

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

Page 7: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

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

Page 8: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

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.

Page 9: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

Lab 2: Part 1 (FIZ without user-defined functions)

The language grammar:<expression> : (inc <expression>) | (dec <expression>)| (ifz <expression> <expression> <expression>)| (halt) | NUMBER

All expressions evaluate to a non-negative integer value.

Page 10: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

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 3Example 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”.

Page 11: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

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.

Page 12: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

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 <arg>+) <expression> )2. (ID <expression>+)

- Evaluate the user-defined declarations and calls.

Page 13: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

Lab 2: (support of user-defined functions’ declaration and calls) (50%)

name argNames bodynumArgs

FUNC_DECL structure definition:

foo 1

x

Example: (define (foo x) (inc x))

INC

FUNC_DECL TREE_NODE

NODE TYPE

name numArgs argNames body

xARG_NAME

Could be a size-10 array, with only the first element used

Page 14: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

Lab 2: (support of user-defined functions’ declaration and calls) (50%)

add 2

x IFZ

NODE TYPE

Example: (define (add x y) (ifz y x (add (inc x) (dec y))))

y

FUNC_CALL add 2

argNames

numArgsname

numArgs body

FUNC_DECL

INC DEC

yARG_NAME xARG_NAME

xARG_NAME yARG_NAME

Could be a size-10 array, with only the first two elements used

Page 15: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

Lab 2: (support of user-defined functions’ declaration and calls) (50%)

add 2

argsnumArgs

INC NUMBER_NODE 2

FUNC_CALL

Example: (add (inc 1) 2)

name

1. Resolve expression

NUMBER_NODE 1

Page 16: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

Lab 2: (support of user-defined functions’ declaration and calls) (50%)

2

argsnumArgs

INC NUMBER_NODE 2

FUNC_EVAL

Example: (add (inc 1) 2)

func

1. Resolve expression

add 2

x IFZ yy x

FUNC_CALL add 2

FUNC_DECL

INC DEC

NUMBER_NODE 1

Add reference to the function declaration structure

xARG_NAME yARG_NAME

xARG_NAME yARG_NAME

Page 17: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

Lab 2: (support of user-defined functions’ declaration and calls) (50%)

2

argsnumArgs

INC NUMBER_NODE 2

FUNC_EVAL

Example: (add (inc 1) 2)

func

1. Resolve expression

add 2

x IFZ yy x

FUNC_CALL add 2

FUNC_DECL

INC DEC

NUMBER_NODE 1

Add reference to the function declaration structure

xARG_NAME yARG_NAME

xARG_NAME yARG_NAME

2. Evaluate parameters

Page 18: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

Lab 2: (support of user-defined functions’ declaration and calls) (50%)

2

argsnumArgs

INC NUMBER_NODE 2

FUNC_EVAL

Example: (add (inc 1) 2)

func

1. Resolve expression2. Evaluate parameters

eval()

2

add 2

x IFZ yy x

FUNC_CALL add 2

FUNC_DECL

INC DEC

NUMBER_NODE 1 xARG_NAME yARG_NAME

xARG_NAME yARG_NAME

Page 19: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

Lab 2: (support of user-defined functions’ declaration and calls) (50%)

2

argsnumArgs

NUMBER_NODE 2

FUNC_EVAL

Example: (add (inc 1) 2)

func

1. Resolve expression2. Evaluate parameters

eval()

2eval()

2

INC

add 2

x IFZ yy x

FUNC_CALL add 2

FUNC_DECL

INC DEC

NUMBER_NODE 1 xARG_NAME yARG_NAME

xARG_NAME yARG_NAME

Page 20: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

Lab 2: (support of user-defined functions’ declaration and calls) (50%)

add 2

x IFZ yy x

FUNC_EVAL 2

FUNC_DECL

INC DEC

NUMBER_NODE 1 xARG_NAME yARG_NAME

xARG_NAME yARG_NAME

1. Resolve expression2. Evaluate parameters3. Resolve body of add

Page 21: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

Lab 2: (support of user-defined functions’ declaration and calls) (50%)

add 2

x IFZ yy x

FUNC_EVAL 2

FUNC_DECL

INC DEC

NUMBER_NODE 1 xARG_NAME yARG_NAME

xARG_NAME yARG_NAME

1. Resolve expression2. Evaluate parameters3. Resolve body of add4. Pass computed

params to the function.

Page 22: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

Lab 2: (support of user-defined functions’ declaration and calls) (50%)

TODO for this part1. 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.

Page 23: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

Where to Add Your Code in fiz.y?

%token INC OPENPAR CLOSEPAR // more tokens%type <node_val> expr // more non-terminalsenum 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 };};

Page 24: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

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.

Page 25: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

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 = 0x12345678; 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

Page 26: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

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.

Page 27: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

Where to Add Your Code in fiz.y?

struct FUNC_DECL { char *name; // Other information of the function needs to be added};

// More grammar rulesstatement: OPENPAR DEFINE OPENPAR … // define a function

expr: … // handle function call

// auxiliary rules that may be needed to define the above two

Page 28: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

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;}

Page 29: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

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 } }

Page 30: CS252 Lab 2 Prepared by El Kindi Rezig. Notes Check out new version of the “official” fiz interpreter at .

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