1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
| %{ #include "lex.yy.c" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h>
#include "myhead.h"
void yyerror();
Node* root=NULL; int flag=0;
void connect_to_next(int next_num, ...) { if (next_num <= 0) { return; } va_list valist; va_start(valist, next_num);
Node* temp = va_arg(valist, Node*); Node* prev = temp; for (int i = 1; i < next_num; i++) { temp = va_arg(valist, Node*); if (prev) { prev->next = temp; } prev = temp; }
if (temp) { temp->next = NULL; } va_end(valist); }
bool is_in_function=false; char *current_function_name; bool index_error=false; void find_compst_return(Node *Compst, char *expected_type); void check_nested_statements(Node *stmt, char *expected_type); void insert_vardec(Node *node) { if (!node->child->next) { add_dec(node->child->id, last_type, node->lineno); } else { if (add_dec(node->child->child->id, "array", node->lineno)) { add_array(node->child->child->id, last_type, node->lineno); } } }
void insert_ExtDecList(Node* node) { insert_vardec(node->child); if (node->child->next) { insert_ExtDecList(node->child->next->next); } }
void add_struct_member_deflist(Node* node, char* struct_name) { if(!node||!node->child) { return; } Node* temp=node->child; Node* temp2=temp->child; char *type; if (strcmp(temp2->child->type,"TYPE")==0) { type=temp2->child->id; } else { type=temp2->child->child->next->id; } Node *temp3=temp->child->next; while(temp3) { Node* temp4=temp3->child; Node* temp5=temp4->child; if(temp5->child->next==NULL) { add_struct_member(struct_name,temp5->child->id,type,temp5->lineno); } else { add_struct_member(struct_name,temp5->child->child->id,"array",temp5->lineno); add_array(temp5->child->child->id,type,temp5->lineno); } if(temp3->child->next) { temp3=temp3->child->next->next; } else { break; } } if(node->child) { add_struct_member_deflist(node->child->next,struct_name); } else { return; } return; }
void insert_function_args(char *func_name, Node *node) { if (node == NULL) { return; }
Node *param = node->child; Node *specifier = param->child; Node *vardec = specifier->next; char *type = specifier->child->id; char *name = vardec->child->id; add_func_args(func_name, name, type, param->lineno);
Node *nextParam = node->child->next; if (nextParam != NULL && strcmp(nextParam->type, "COMMA") == 0) { insert_function_args(func_name, nextParam->next); } } void insert_func_variables(Node *Dec) { if (!is_in_function || Dec == NULL) { return; }
Node *VarDec = Dec->child; char *var_name; char *var_type; if (VarDec->child->next) { var_name = VarDec->child->child->id; var_type = "array"; add_func_variables(current_function_name, var_name, var_type, VarDec->lineno); add_array(var_name, last_type, VarDec->lineno); } else { var_name = VarDec->child->id; var_type = last_type; add_func_variables(current_function_name, var_name, var_type, VarDec->lineno); }
Node *Assign = VarDec->next; if (Assign && strcmp(Assign->type, "ASSIGN") == 0) { Node *Exp = Assign->next; if (strcmp(Exp->value_type, "error") != 0 && strcmp(Exp->value_type, last_type) != 0) { printf("Error type 5 at line %d: Type mismatched for assignment\n", Exp->lineno); } } }
void check_type(Node *exp, const char *expected_type, int error_type, int lineno) { if (strcmp(exp->value_type, "error") == 0) { return; } if (expected_type != NULL && strcmp(exp->value_type, expected_type) != 0) { printf("Error type %d at line %d: Type mismatched for operands\n", error_type, lineno); } }
void check_single_arithmetic_exp(Node *exp) { check_type(exp, NULL, 7, exp->lineno); if (strcmp(exp->value_type, "int") != 0 && strcmp(exp->value_type, "float") != 0) { printf("Error type 7 at line %d: Type mismatched for operands, expected int or float\n", exp->lineno); } }
void check_single_logical_exp(Node *exp) { check_type(exp, "int", 7, exp->lineno); }
void check_exp_arithmetic_symbols(Node *exp1, Node *exp2) { check_type(exp1, NULL, 7, exp1->lineno); check_type(exp2, NULL, 7, exp2->lineno); if ((strcmp(exp1->value_type, "int") != 0 && strcmp(exp1->value_type, "float") != 0) || (strcmp(exp2->value_type, "int") != 0 && strcmp(exp2->value_type, "float") != 0)) { printf("Error type 7 at line %d: Type mismatched for operands, expected int or float\n", exp1->lineno); } if (strcmp(exp1->value_type, exp2->value_type) != 0) { printf("Error type 7 at line %d: Type mismatched for operands, expected same type\n", exp1->lineno); } }
void check_exp_logical_symbols(Node *exp1, Node *exp2) { check_type(exp1, "int", 7, exp1->lineno); check_type(exp2, "int", 7, exp2->lineno); }
void check_exp_assign(Node *exp1, Node *exp2) { check_type(exp1, NULL, 6, exp1->lineno); check_type(exp2, NULL, 5, exp2->lineno); if (exp1->rvalue) { printf("Error type 6 at line %d: rvalue on the left side of assignment operator\n", exp1->lineno); return; } if (strcmp(exp1->value_type, exp2->value_type) != 0) { printf("Error type 5 at line %d: Type mismatched for assignment\n", exp1->lineno); } }
void check_Exp_with_specific_type(Node *Exp, char *expected_type) { check_type(Exp, expected_type, 7, Exp->lineno); }
void check_Def(Node *Def) { char *expected_type = last_type; Node *DecList = Def->child->next; while (DecList) { Node *Dec = DecList->child; Node *VarDec = Dec->child;
Node *Assign = VarDec->next; if (Assign && strcmp(Assign->type, "ASSIGN") == 0) { Node *Exp = Assign->next; if (strcmp(Exp->value_type, "error") != 0 && strcmp(Exp->value_type, expected_type) != 0) { printf("Error type 5 at line %d: Type mismatched for assignment\n", Exp->lineno); } }
DecList = (Dec->next && strcmp(Dec->next->type, "COMMA") == 0) ? Dec->next->next : NULL; } }
void set_rvalue(Node *node) { if (node) { node->rvalue = 1; } }
void set_EXP_value_type(Node *node, char *type) { if (node && type) { strcpy(node->value_type, type); } }
void check_func_return_type(Node* Compst, char *expected_type) { if (!is_in_function || Compst == NULL) { return; } find_compst_return(Compst, expected_type); }
void find_compst_return(Node *Compst, char *expected_type) { Node *stmtlist = Compst->child->next->next; while (stmtlist && stmtlist->child) { Node *stmt = stmtlist->child; if (strcmp(stmt->child->type, "RETURN") == 0) { Node *exp = stmt->child->next; if (exp && strcmp(exp->value_type, expected_type) != 0 && strcmp(exp->value_type, "error") != 0) { printf("Error type 8 at line %d: The return type mismatched\n", exp->lineno); } } else if (strcmp(stmt->child->type, "CompSt") == 0) { find_compst_return(stmt->child, expected_type); } else { check_nested_statements(stmt, expected_type); } stmtlist = stmtlist->next ? stmtlist->next->child : NULL; } }
void check_nested_statements(Node *stmt, char *expected_type) { Node *head = stmt->child; while (head) { if (strcmp(head->type, "Stmt") == 0) { if (strcmp(head->child->type, "CompSt") == 0) { find_compst_return(head->child, expected_type); } else if (strcmp(head->child->type, "RETURN") == 0) { Node *exp = head->child->next; if (exp && strcmp(exp->value_type, expected_type) != 0 && strcmp(exp->value_type, "error") != 0) { printf("Error type 8 at line %d: The return type mismatched\n", exp->lineno); } } } head = head->next; } }
void check_func_args(char *func_name, Node *Args, int arg_index) { int func_index; for(func_index = 0; func_index < def_num; func_index++) { if(strcmp(def[func_index].name, func_name) == 0) { break; } } if(func_index == def_num) return;
Node *current_arg = Args->child; if(arg_index < def[func_index].arg_num) { if(strcmp(current_arg->value_type, def[func_index].args[arg_index].type) != 0) { printf("Error type 9 at line %d: Unmatched argument type for Function \"%s\"\n", current_arg->lineno, func_name); return; } } else { printf("Error type 9 at line %d: Too many arguments for Function \"%s\"\n", current_arg->lineno, func_name); return; } if(current_arg->next) { check_func_args(func_name, current_arg->next->next, arg_index + 1); } else if(arg_index + 1 < def[func_index].arg_num) { printf("Error type 9 at line %d: Too few arguments for Function \"%s\"\n", current_arg->lineno, func_name); } }
void check_func_without_args(char *func_name, int lineno) { int func_index; for(func_index = 0; func_index < def_num; func_index++) { if(strcmp(def[func_index].name, func_name) == 0) { break; } } if(func_index == def_num) return;
if(def[func_index].arg_num != 0) { printf("Error type 9 at line %d: Function \"%s\" expects no arguments\n", lineno, func_name); } }
bool check_exp_array(Node *array_exp, Node *index_exp) { if (strcmp(index_exp->value_type, "error") == 0 || strcmp(array_exp->value_type, "error") == 0) { return false; } if (strcmp(array_exp->value_type, "array") != 0) { printf("Error type 10 at line %d: Indexing operator ([...]) applied to non-array variable \"%s\".\n", array_exp->lineno, array_exp->id); return false; } if (strcmp(index_exp->value_type, "int") != 0) { printf("Error type 12 at line %d: Array index is not an integer.\n", index_exp->lineno); index_error = true; return true; } return true; }
char* find_array_elem_type(Node *exp) { Node *id = NULL; bool found = false; while (exp->child && !found) { exp = exp->child; Node *temp = exp; while (temp) { if (strcmp(temp->type, "ID") == 0) { id = temp; found = true; break; } temp = temp->next; } } for (int i = 0; i < def_num; i++) { if (strcmp(def[i].name, id->id) == 0) { return def[i].elem_type; } } return NULL; }
bool check_exp_structure(Node *structure_exp, Node *member_id) { if (strcmp(structure_exp->value_type, "error") == 0 || strcmp(member_id->value_type, "error") == 0) { return false; }
bool is_structure_found = false; for (int i = 0; i < dec_num && !is_structure_found; i++) { if (strcmp(dec[i].name, structure_exp->value_type) == 0) { if (strcmp(dec[i].type, "structure") != 0) { printf("Error type 13 at line %d: Illegal member access on non-structure variable \"%s\"\n", structure_exp->lineno, structure_exp->id); return false; } is_structure_found = true; } }
if (!is_structure_found) { printf("Error type 13 at line %d: Illegal member access on non-structure variable \"%s\"\n", structure_exp->lineno, structure_exp->id); return false; }
char *type = find_structure_member_type(structure_exp->value_type, member_id->id); if (!type) { printf("Error type 14 at line %d: Structure '%s' has no member named '%s'\n", structure_exp->lineno, structure_exp->value_type, member_id->id); return false; }
return true; }
bool check_if_func_exist(char *name, int lineno) { for (int i = 0; i < dec_num; ++i) { if (strcmp(dec[i].name, name) == 0) { if (strcmp(dec[i].type, "function") != 0) { printf("Error type 11 at Line %d: '%s' is not a function name.\n", lineno, name); return false; } return true; } }
for (int i = 0; i < def_num; ++i) { if (strcmp(def[i].name, current_function_name) == 0) { for (int j = 0; j < def[i].arg_num; ++j) { if (strcmp(def[i].args[j].name, name) == 0) { printf("Error type 11 at Line %d: '%s' is a parameter, not a function.\n", lineno, name); return false; } } for (int j = 0; j < def[i].dec_num; ++j) { if (strcmp(def[i].dec[j].name, name) == 0) { if (strcmp(def[i].dec[j].type, "function") != 0) { printf("Error type 11 at Line %d: '%s' is not a function name.\n", lineno, name); return false; } return true; } } } }
printf("Error type 2 at Line %d: Function \"%s\" is invoked without definition.\n", lineno, name); return false; }
char* check_exp_id(Node *id) { char *type = "error";
for (int i = 0; i < dec_num; ++i) { if (strcmp(dec[i].name, id->id) == 0) { return dec[i].type; } }
for (int i = 0; i < def_num; ++i) { for (int j = 0; j < def[i].dec_num; ++j) { if (strcmp(def[i].dec[j].name, id->id) == 0) { return def[i].dec[j].type; } } }
for (int i = 0; i < def_num; ++i) { if (strcmp(def[i].name, current_function_name) == 0) { for (int j = 0; j < def[i].arg_num; ++j) { if (strcmp(def[i].args[j].name, id->id) == 0) { return def[i].args[j].type; } } for (int j = 0; j < def[i].dec_num; ++j) { if (strcmp(def[i].dec[j].name, id->id) == 0) { return def[i].dec[j].type; } } } }
if (strcmp(type, "error") == 0 && is_in_function) { printf("Error type 1 at line %d: Undefined variable \"%s\".\n", id->lineno, id->id); } return type; } %}
%union { int num; char* str; struct Head* node; }
%right ASSIGN %left OR %left AND %left LT LE GT GE NE EQ %left PLUS MINUS %left MUL DIV %right NOT NEGATIVE %nonassoc LC RC LB RB LP RP DOT
%token <node> ID INT FLOAT CHAR STRUCT RETURN IF ELSE WHILE PLUS MINUS MUL DIV AND OR LT LE GT GE NE EQ NOT ASSIGN TYPE LP RP LB RB LC RC SEMI COMMA DOT ILLEGAL ILLEGAL_ID ILLEGAL_HEX_INT ILLEGAL_CHAR
%type <node> Program ExtDefList ExtDef ExtDecList Specifier StructSpecifier VarDec FunDec VarList ParamDec CompSt StmtList Stmt DefList Def DecList Dec Exp Args %%
Program: ExtDefList {$$=create_Node(0,"Program","",$1,NULL,0);root=$$;}
ExtDefList: ExtDef ExtDefList {connect_to_next(2,$1,$2);$$=create_Node(0,"ExtDefList","",$1,NULL,0);} | {$$=create_Node(0,"empty","",NULL,NULL,100);}
ExtDef: Specifier ExtDecList SEMI {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"ExtDef","",$1,NULL,0);insert_ExtDecList($2);} | Specifier ExtDecList error {printf("Error type B at line %d: missing semicolon ';'\n", $1->lineno);} | Specifier SEMI {connect_to_next(2,$1,$2);$$=create_Node(0,"ExtDef","",$1,NULL,0);} | Specifier FunDec CompSt {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"ExtDef","",$1,NULL,0);if(is_in_function){set_func_return_type(current_function_name,$1->child->id);check_func_return_type($3,$1->child->id);}is_in_function=false;}
ExtDecList: VarDec {$$=create_Node(0,"ExtDecList","",$1,NULL,0);} | VarDec COMMA ExtDecList {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"ExtDecList","",$1,NULL,0);}
Specifier: TYPE {$$=create_Node(0,"Specifier","",$1,NULL,0);set_last_type($1->id);} | StructSpecifier {$$=create_Node(0,"Specifier","",$1,NULL,0);}
StructSpecifier: STRUCT ID LC DefList RC {connect_to_next(5,$1,$2,$3,$4,$5);$$=create_Node(0,"StructSpecifier","",$1,NULL,0);if(add_dec($2->id,"structure",$2->lineno)&&add_func_or_struct($2->id,$2->lineno,"structure")){add_struct_member_deflist($4,$2->id);}} | STRUCT ID {connect_to_next(2,$1,$2);$$=create_Node(0,"StructSpecifier","",$1,NULL,0);set_last_type($2->id);}
VarDec: ID {$$=create_Node(0,"VarDec","",$1,NULL,0);} | ILLEGAL_ID error {printf("Error type A at line %d: illegal identifier '%s'\n", $1->lineno,$1->id);} | VarDec LB INT RB {connect_to_next(4,$1,$2,$3,$4);$$=create_Node(0,"VarDec","",$1,NULL,0);}
FunDec: ID LP VarList RP {connect_to_next(4,$1,$2,$3,$4);$$=create_Node(0,"FunDec","",$1,NULL,0);if(add_dec($1->id,"function",$1->lineno)&&add_func_or_struct($1->id,$1->lineno,"function")){insert_function_args($1->id,$3);current_function_name=$1->id;is_in_function=true;} else{is_in_function=false;}} | ID LP RP {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"FunDec","",$1,NULL,0);if(add_dec($1->id,"function",$1->lineno)&&add_func_or_struct($1->id,$1->lineno,"function")){current_function_name=$1->id;is_in_function=true;} else{is_in_function=false;}} | ID LP VarList error {printf("Error type B at line %d: missing closing symbols ')'\n", $1->lineno);} | ID LP error {printf("Error type B at line %d: missing closing symbols ')'\n", $1->lineno);}
VarList: ParamDec COMMA VarList {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"VarList","",$1,NULL,0);} | ParamDec {$$=create_Node(0,"VarList","",$1,NULL,0);}
ParamDec: Specifier VarDec {connect_to_next(2,$1,$2);$$=create_Node(0,"ParamDec","",$1,NULL,0);}
CompSt: LC DefList StmtList RC {connect_to_next(4,$1,$2,$3,$4);$$=create_Node(0,"CompSt","",$1,NULL,0);}
StmtList: Stmt StmtList {connect_to_next(2,$1,$2);$$=create_Node(0,"StmtList","",$1,NULL,0);} |Stmt Def StmtList error {printf("Error type B at line %d: definition after statement\n", $2->lineno);} | {$$=create_Node(0,"empty","",NULL,NULL,100);}
Stmt: Exp SEMI {connect_to_next(2,$1,$2);$$=create_Node(0,"Stmt","",$1,NULL,0);} | CompSt {$$=create_Node(0,"Stmt","",$1,NULL,0);} | RETURN Exp SEMI {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"Stmt","",$1,NULL,0);} | RETURN Exp error {printf("Error type B at line %d: missing semicolon ';'\n", $1->lineno);} | IF LP Exp RP Stmt {connect_to_next(5,$1,$2,$3,$4,$5);$$=create_Node(0,"Stmt","",$1,NULL,0);} | IF LP Exp RP Stmt ELSE Stmt {connect_to_next(7,$1,$2,$3,$4,$5,$6,$7);$$=create_Node(0,"Stmt","",$1,NULL,0);} | WHILE LP Exp RP Stmt {connect_to_next(5,$1,$2,$3,$4,$5);$$=create_Node(0,"Stmt","",$1,NULL,0);}
DefList: Def DefList {connect_to_next(2,$1,$2);$$=create_Node(0,"DefList","",$1,NULL,0);} | {$$=create_Node(0,"empty","",NULL,NULL,100);}
Def: Specifier DecList SEMI {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"Def","",$1,NULL,0);check_Def($$);} | Specifier DecList error {printf("Error type B at line %d: missing semicolon ';'\n", $1->lineno);}
DecList: Dec {$$=create_Node(0,"DecList","",$1,NULL,0);} | Dec COMMA DecList {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"DecList","",$1,NULL,0);}
Dec: VarDec {$$=create_Node(0,"Dec","",$1,NULL,0);insert_func_variables($$);} | VarDec ASSIGN Exp {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"Dec","",$1,NULL,0);insert_func_variables($$);}
Exp: Exp ASSIGN Exp {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"Exp","",$1,NULL,0);check_exp_assign($1,$3);set_rvalue($$);set_EXP_value_type($$,"int");} | Exp AND Exp {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"Exp","",$1,NULL,0);check_exp_logical_symbols($1,$3);set_rvalue($$);set_EXP_value_type($$,"int");} | Exp OR Exp {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"Exp","",$1,NULL,0);check_exp_logical_symbols($1,$3);set_rvalue($$);set_EXP_value_type($$,"int");} | Exp LT Exp {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"Exp","",$1,NULL,0);check_exp_logical_symbols($1,$3);set_rvalue($$);set_EXP_value_type($$,"int");} | Exp LE Exp {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"Exp","",$1,NULL,0);check_exp_logical_symbols($1,$3);set_rvalue($$);set_EXP_value_type($$,"int");} | Exp GT Exp {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"Exp","",$1,NULL,0);check_exp_logical_symbols($1,$3);set_rvalue($$);set_EXP_value_type($$,"int");} | Exp GE Exp {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"Exp","",$1,NULL,0);check_exp_logical_symbols($1,$3);set_rvalue($$);set_EXP_value_type($$,"int");} | Exp NE Exp {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"Exp","",$1,NULL,0);check_exp_logical_symbols($1,$3);set_rvalue($$);set_EXP_value_type($$,"int");} | Exp EQ Exp {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"Exp","",$1,NULL,0);check_exp_logical_symbols($1,$3);set_rvalue($$);set_EXP_value_type($$,"int");} | Exp PLUS Exp {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"Exp","",$1,NULL,0);check_exp_arithmetic_symbols($1,$3);set_rvalue($$);set_EXP_value_type($$,$1->value_type);} | Exp MINUS Exp {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"Exp","",$1,NULL,0);check_exp_arithmetic_symbols($1,$3);set_rvalue($$);set_EXP_value_type($$,$1->value_type);} | Exp MUL Exp {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"Exp","",$1,NULL,0);check_exp_arithmetic_symbols($1,$3);set_rvalue($$);set_EXP_value_type($$,$1->value_type);} | Exp DIV Exp {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"Exp","",$1,NULL,0);check_exp_arithmetic_symbols($1,$3);set_rvalue($$);set_EXP_value_type($$,$1->value_type);} | LP Exp RP {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"Exp","",$1,NULL,0);strcpy($$->value_type,$2->value_type);$$->rvalue=$2->rvalue;} | LP Exp error {printf("Error type B at line %d: missing closing symbols ')'\n", $1->lineno);} | MINUS Exp {connect_to_next(2,$1,$2);$$=create_Node(0,"Exp","",$1,NULL,0);check_single_arithmetic_exp($2);$$->rvalue=$2->rvalue;set_EXP_value_type($$,$2->value_type);} | NOT Exp {connect_to_next(2,$1,$2);$$=create_Node(0,"Exp","",$1,NULL,0);check_single_logical_exp($2);$$->rvalue=$2->rvalue;set_EXP_value_type($$,$2->value_type);} | ID LP Args RP {connect_to_next(4,$1,$2,$3,$4);$$=create_Node(0,"Exp","",$1,NULL,0);if(check_if_func_exist($1->id,$1->lineno)){check_func_args($1->id,$3,0);set_EXP_value_type($$,find_func_return_type($1->id));} else{set_EXP_value_type($$,"error");}} | ID LP Args error {printf("Error type B at line %d: missing closing symbols ')'\n", $1->lineno);} | ID LP RP {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"Exp","",$1,NULL,0);if(check_if_func_exist($1->id,$1->lineno)){check_func_without_args($1->id,$1->lineno);set_EXP_value_type($$,find_func_return_type($1->id));} else{set_EXP_value_type($$,"error");}} | Exp LB Exp RB {connect_to_next(4,$1,$2,$3,$4);$$=create_Node(0,"Exp","",$1,NULL,0);if(check_exp_array($1,$3)){if(index_error){set_EXP_value_type($$,"None");index_error=false;}else{set_EXP_value_type($$,find_array_elem_type($1));}}else{set_EXP_value_type($$,"error");}} | Exp DOT ID {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"Exp","",$1,NULL,0);if(check_exp_structure($1,$3)){set_EXP_value_type($$,find_structure_member_type($1->value_type,$3->id));}else{strcpy($$->value_type,"None");}} | ID {$$=create_Node(0,"Exp","",$1,NULL,0);set_EXP_value_type($$,check_exp_id($1));} | INT {$$=create_Node(0,"Exp","",$1,NULL,0);set_EXP_value_type($$,"int");$$->rvalue=1;} | FLOAT {$$=create_Node(0,"Exp","",$1,NULL,0);set_EXP_value_type($$,"float");$$->rvalue=1;} | CHAR {$$=create_Node(0,"Exp","",$1,NULL,0);set_EXP_value_type($$,"char");$$->rvalue=1;} | ILLEGAL error {printf("Error type A at line %d: unknown lexeme '%s'\n", $1->lineno,$1->id);} | ILLEGAL_HEX_INT error {printf("Error type A at line %d: illegal hexadecimal integer '%s'\n", $1->lineno,$1->id);} | ILLEGAL_CHAR error {printf("Error type A at line %d: illegal hex_character '%s'\n", $1->lineno,$1->id);} | Exp ILLEGAL Exp error {printf("Error type A at line %d: illegal operator '%s'\n", $2->lineno,$2->id);}
Args: Exp COMMA Args {connect_to_next(3,$1,$2,$3);$$=create_Node(0,"Args","",$1,NULL,0);} | Exp {$$=create_Node(0,"Args","",$1,NULL,0);}
%%
void yyerror() { flag=1; return ; }
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: %s <file_path>\n", argv[0]); exit(-1); } else if (!(yyin = fopen(argv[1], "r"))) { perror(argv[1]); exit(-1); } yyparse(); if(flag==0) { }
return 0; }
|