Expression Parsing and Evaluation in CS 5300 - SJAllan's Compiler - Prof. Stephen J. Allan, Study notes of Computer Science

The code for parsing and evaluating expressions in cs 5300 - sjallan's compiler. It includes definitions for expression structure, new expression creation, making expressions from identifiers, constant expressions, string expressions, binary operators, unary minus, and boolean operators. The code demonstrates how to create new expressions, check type compatibility, and generate mips code for various operators.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-5uz
koofers-user-5uz 🇺🇸

5

(1)

10 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Expressions
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Expression Parsing and Evaluation in CS 5300 - SJAllan's Compiler - Prof. Stephen J. Allan and more Study notes Computer Science in PDF only on Docsity!

Expressions

CS 5300 - SJAllan 2

Expression Structure

typedef enum expr_kind EX_KIND; enum expr_kind {GlobalV, LocalV, LocalA, Cons, RegisterV, RegisterA, FuncRef};

typedef struct expr_info EXPR;

struct expr_info { TYPE *ex_type; EX_KIND ex_kind; int ex_inv; EXPR ex_next; }; / expr_info */

CS 5300 - SJAllan 4

Code for makeexpr

EXPR *makeexpr (ID *st_ptr) { TYPE type; EX_KIND kind; int inv; type = st_ptr->id_type; inv = st_ptr->id_addr; if (st_ptr->id_level == GLOBALSCOPE) kind = GlobalV; else if (st_ptr->id_level == currscope) if (st_ptr->id_kind == RParameter) kind = LocalA; else kind = LocalV; else kind = LocalV; return(newexpr(type, kind, inv)); } / makeexpr */

CS 5300 - SJAllan 5

Constant Expression

Expression :... | INTCONSTSY {$$ = newexpr(int_type, Cons, $1);} | CHARCONSTSY {$$ = newexpr(char_type, Cons, $1);} | STRINGCONSTSY {$$ = strexpr($1);}

... ;

CS 5300 - SJAllan 7

LValue

LValue :... | IDENTSY {$$ = lvalue($1);} ;

CS 5300 - SJAllan 8

Binary Operators

Expression :... | Expression ADDSY Expression {$$ = binop($1, AddOp, $3);} | Expression SUBSY Expression {$$ = binop($1, SubOp, $3);} | Expression MULSY Expression {$$ = binop($1, MulOp, $3);} | Expression DIVSY Expression {$$ = binop($1, DivOp, $3);} | Expression MODSY Expression {$$ = binop($1, ModOp, $3);}

... ;

CS 5300 - SJAllan 10

Unary Minus

Expression :... | SUBSY Expression %prec UNMINUSSY {$$ = unop(SubOp, $2);}

... ;

CS 5300 - SJAllan 11

MIPS Code for Comparison

s? DEST, S1, S