Skip to content

Commit

Permalink
hw3 part2 debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
lydia7635 committed Nov 18, 2020
1 parent a2adbd0 commit ee2cf49
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 14 deletions.
11 changes: 9 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
*.o
*~

# hw2
hw2/src/lex.yy.*
hw2/src/scanner
hw2/src/symboltable.o
*~

# hw3
hw3/src/parser.output
hw3/src/parser.tab.h
hw3/src/AST_Graph.gv
hw3/src/lex.yy.c
hw3/src/*.jpg
hw3/src/parser
hw3/src/parser.tab.c
hw3/src/test
91 changes: 79 additions & 12 deletions hw3/src/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ static inline AST_NODE* makeExprNode(EXPR_KIND exprKind, int operationEnumValue)
%token RETURN

%type <node> program global_decl_list global_decl function_decl block stmt_list decl_list decl var_decl type init_id_list init_id stmt relop_expr relop_term relop_factor expr term factor var_ref
%type <node> param_list param dim_fn expr_null id_list dim_decl cexpr mcexpr cfactor assign_expr_list test assign_expr rel_op relop_expr_list nonempty_relop_expr_list
%type <node> param_list param dim_fn expr_null id_list dim_decl cexpr mcexpr cfactor assign_expr_list assign_expr rel_op relop_expr_list nonempty_relop_expr_list
%type <node> add_op mul_op dim_list type_decl nonempty_assign_expr_list


Expand Down Expand Up @@ -384,7 +384,7 @@ cexpr : cexpr OP_PLUS mcexpr
| cexpr OP_MINUS mcexpr
{
/*TODO*/
$$ = makeExprNode(BINARY_OPERATION, BINARY_OP_MINUS);
$$ = makeExprNode(BINARY_OPERATION, BINARY_OP_SUB);
makeFamily($$, 2, $1, $3);
}
| mcexpr
Expand Down Expand Up @@ -420,7 +420,7 @@ cfactor: CONST
| MK_LPAREN cexpr MK_RPAREN
{
/*TODO*/
$$ = $1;
$$ = $2;
}
;

Expand Down Expand Up @@ -448,7 +448,7 @@ init_id : ID
| ID OP_ASSIGN relop_expr
{
/*TODO*/
$$ = makeChild(makeIDNode($1, NORMAL_ID), $2);
$$ = makeChild(makeIDNode($1, WITH_INIT_ID), $3);
}
;

Expand All @@ -469,7 +469,7 @@ stmt_list : stmt_list stmt
stmt : MK_LBRACE block MK_RBRACE
{
/*TODO*/
$$ = $1;
$$ = $2;
}
/*TODO: | While Statement */
| WHILE MK_LPAREN relop_expr MK_RPAREN stmt
Expand All @@ -487,39 +487,52 @@ stmt : MK_LBRACE block MK_RBRACE
| var_ref OP_ASSIGN relop_expr MK_SEMICOLON
{
/*TODO*/
$$ = makeStmtNode(ASSIGN_STMT);
makeFamily($$, 2, $1, $3);
}
/*TODO: | If Statement */
| IF MK_LPAREN relop_expr MK_RPAREN stmt
{
/*TODO*/
$$ = makeStmtNode(IF_STMT);
makeFamily($$, 2, $3, $5);
}
/*TODO: | If then else */
| IF MK_LPAREN relop_expr MK_RPAREN stmt ELSE stmt
{
/*TODO*/
$$ = makeStmtNode(IF_STMT);
makeFamily($$, 3, $3, $5, $7);
}
/*TODO: | function call */
| ID MK_LPAREN relop_expr_list MK_RPAREN MK_SEMICOLON
{
/*TODO*/
$$ = makeStmtNode(FUNCTION_CALL_STMT);
makeFamily($$, 2, $1, $3);
}
| MK_SEMICOLON
{
/*TODO*/
$$ = Allocate(NUL_NODE);
}
| RETURN MK_SEMICOLON
{
/*TODO*/
$$ = makeStmtNode(RETURN_STMT);
}
| RETURN relop_expr MK_SEMICOLON
{
/*TODO*/
$$ = makeStmtNode(RETURN_STMT);
makeChild($$, $2);
}
;

assign_expr_list : nonempty_assign_expr_list
{
/*TODO*/
$$ = $1;
}
|
{
Expand All @@ -530,26 +543,27 @@ assign_expr_list : nonempty_assign_expr_list
nonempty_assign_expr_list : nonempty_assign_expr_list MK_COMMA assign_expr
{
/*TODO*/
$$ = Allocate(NONEMPTY_ASSIGN_EXPR_LIST_NODE);
makeFamily($$, 2, $1, $3);
}
| assign_expr
{
/*TODO*/
$$ = Allocate(NONEMPTY_ASSIGN_EXPR_LIST_NODE);
makeChild($$, $1);
}
;

test : assign_expr
{
$$ = $1;
}
;

assign_expr : ID OP_ASSIGN relop_expr
{
/*TODO*/
$$ = makeStmtNode(ASSIGN_STMT);
makeFamily($$, 2, makeIDNode($1, NORMAL_ID), $3);
}
| relop_expr
{
/*TODO*/
$$ = $1;
}
;

Expand All @@ -567,53 +581,65 @@ relop_expr : relop_term
relop_term : relop_factor
{
/*TODO*/
$$ = $1;
}
| relop_term OP_AND relop_factor
{
/*TODO*/
$$ = makeExprNode(BINARY_OPERATION, BINARY_OP_AND);
makeFamily($$, 2, $1, $3);
}
;

relop_factor : expr
{
/*TODO*/
$$ = $1;
}
| expr rel_op expr
{
/*TODO*/
$$ = makeFamily($2, 2, $1, $3);
}
;

rel_op : OP_EQ
{
/*TODO*/
$$ = makeExprNode(BINARY_OPERATION, BINARY_OP_EQ);
}
| OP_GE
{
/*TODO*/
$$ = makeExprNode(BINARY_OPERATION, BINARY_OP_GE);
}
| OP_LE
{
/*TODO*/
$$ = makeExprNode(BINARY_OPERATION, BINARY_OP_LE);
}
| OP_NE
{
/*TODO*/
$$ = makeExprNode(BINARY_OPERATION, BINARY_OP_NE);
}
| OP_GT
{
/*TODO*/
$$ = makeExprNode(BINARY_OPERATION, BINARY_OP_GT);
}
| OP_LT
{
/*TODO*/
$$ = makeExprNode(BINARY_OPERATION, BINARY_OP_LT);
}
;


relop_expr_list : nonempty_relop_expr_list
{
/*TODO*/
$$ = $1;
}
|
{
Expand All @@ -624,20 +650,26 @@ relop_expr_list : nonempty_relop_expr_list
nonempty_relop_expr_list : nonempty_relop_expr_list MK_COMMA relop_expr
{
/*TODO*/
$$ = Allocate(NONEMPTY_RELOP_EXPR_LIST_NODE);
makeFamily($$, 2, $1, $3);
}
| relop_expr
{
/*TODO*/
$$ = Allocate(NONEMPTY_RELOP_EXPR_LIST_NODE);
makeChild($$, $1);
}
;

expr : expr add_op term
{
/*TODO*/
$$ = makeFamily($2, 2, $1, $3);
}
| term
{
/*TODO*/
$$ = $1;
}
;

Expand All @@ -654,35 +686,44 @@ add_op : OP_PLUS
term : term mul_op factor
{
/*TODO*/
$$ = makeFamily($2, 2, $1, $3);
}
| factor
{
/*TODO*/
$$ = $1;
}
;

mul_op : OP_TIMES
{
/*TODO*/
$$ = makeExprNode(BINARY_OPERATION, BINARY_OP_MUL);
}
| OP_DIVIDE
{
/*TODO*/
$$ = makeExprNode(BINARY_OPERATION, BINARY_OP_DIV);
}
;

factor : MK_LPAREN relop_expr MK_RPAREN
{
/*TODO*/
$$ = $2;
}
/*TODO: | -(<relop_expr>) e.g. -(4) */
| OP_MINUS MK_LPAREN relop_expr MK_RPAREN
{
/*TODO*/
$$ = makeExprNode(UNARY_OPERATION, UNARY_OP_NEGATIVE);
makeChild($$, $3);
}
| OP_NOT MK_LPAREN relop_expr MK_RPAREN
{
/*TODO*/
$$ = makeExprNode(UNARY_OPERATION, UNARY_OP_LOGICAL_NEGATION);
makeChild($$, $3);
}
| CONST
{
Expand All @@ -693,65 +734,91 @@ factor : MK_LPAREN relop_expr MK_RPAREN
| OP_MINUS CONST
{
/*TODO*/
$$ = makeExprNode(UNARY_OPERATION, UNARY_OP_NEGATIVE);
AST_NODE *temp = Allocate(CONST_VALUE_NODE);
temp->semantic_value.const1 = $2;
makeChild($$, temp);
}
| OP_NOT CONST
{
/*TODO*/
$$ = makeExprNode(UNARY_OPERATION, UNARY_OP_LOGICAL_NEGATION);
AST_NODE *temp = Allocate(CONST_VALUE_NODE);
temp->semantic_value.const1 = $2;
makeChild($$, temp);
}
| ID MK_LPAREN relop_expr_list MK_RPAREN
{
/*TODO*/
$$ = makeStmtNode(FUNCTION_CALL_STMT);
makeFamily($$, 2, makeIDNode($1, NORMAL_ID), $3);
}
/*TODO: | -<function call> e.g. -f(4) */
| OP_MINUS ID MK_LPAREN relop_expr_list MK_RPAREN
{
/*TODO*/
$$ = makeExprNode(UNARY_OPERATION, UNARY_OP_NEGATIVE);
AST_NODE *temp = makeStmtNode(FUNCTION_CALL_STMT);
makeChild($$, makeFamily(temp, 2, makeIDNode($2, NORMAL_ID), $4));
}
| OP_NOT ID MK_LPAREN relop_expr_list MK_RPAREN
{
/*TODO*/
$$ = makeExprNode(UNARY_OPERATION, UNARY_OP_LOGICAL_NEGATION);
AST_NODE *temp = makeStmtNode(FUNCTION_CALL_STMT);
makeChild($$, makeFamily(temp, 2, makeIDNode($2, NORMAL_ID), $4));
}
| var_ref
{
/*TODO*/
$$ = $1;
}
/*TODO: | -<var_ref> e.g. -var */
| OP_MINUS var_ref
{
/*TODO*/
$$ = makeExprNode(UNARY_OPERATION, UNARY_OP_NEGATIVE);
makeChild($$, $2);
}
| OP_NOT var_ref
{
/*TODO*/
$$ = makeExprNode(UNARY_OPERATION, UNARY_OP_LOGICAL_NEGATION);
makeChild($$, $2);
}
;

var_ref : ID
{
/*TODO*/
$$ = makeIDNode($1, NORMAL_ID);
}
| ID dim_list
{
/*TODO*/
$$ = makeIDNode($1, ARRAY_ID);
makeChild($$, $2);
}
;


dim_list : dim_list MK_LB expr MK_RB
{
/*TODO*/
$$ = makeSibling($1, $3);
}
| MK_LB expr MK_RB
{
/*TODO*/
$$ = $2;
}
;


%%

#include "lex.yy.c"
main (argc, argv)
int main (argc, argv)
int argc;
char *argv[];
{
Expand Down

0 comments on commit ee2cf49

Please sign in to comment.