#include "parser.h" struct StatementNode* parse_generate_intermediate_representation(){ struct StatementNode* finish = new StatementNode; finish = parse_program(); return finish; } struct StatementNode* parse_program(){ //program -> var_section body struct StatementNode* st = new StatementNode; parse_var_section(); st = parse_body(); return st; } void parse_var_section(){ //var_section -> id_list SEMICOLON parse_id_list(); t = lexer.GetToken();//should be semicolon } void parse_id_list(){ //id_list -> ID COMMA id_list | ID struct ValueNode* temp = new ValueNode; t = lexer.GetToken();//ID if(t.token_type != ID) cout << "Token error in id list\n"; temp->name = t.lexeme; variables[next_var] = temp; next_var++; t = lexer.GetToken(); if(t.token_type == COMMA) parse_id_list(); else lexer.UngetToken(t); } struct StatementNode* parse_body(){ //body -> LBRACE stmt_list RBRACE struct StatementNode* stl = new StatementNode; t = lexer.GetToken();//LBRACE if(t.token_type != LBRACE) cout << "Token error in body pos1\n"; stl = parse_stmt_list(); t = lexer.GetToken();//RBRACE if(t.token_type != RBRACE) cout << "Token error in body pos2\n"; return stl; } struct StatementNode* parse_stmt_list(){ //stmt_list -> stmt stmt_list | stmt struct StatementNode* st = new StatementNode; //statment struct StatementNode* stl = new StatementNode; //statement list struct StatementNode* empty = new StatementNode; empty->type = NOOP_STMT; st = parse_stmt(); t = lexer.GetToken(); lexer.UngetToken(t); if( (t.token_type == ID) | (t.token_type == PRINT) | (t.token_type == IF) | (t.token_type == WHILE) | (t.token_type == FOR) | (t.token_type == SWITCH) ){ stl = parse_stmt_list(); if(st->type == IF_STMT){ // st->next = empty; st->next->next = stl; }else{ if(st->next == NULL) st->next = stl; else if(st->next->next == NULL) st->next->next = stl; else st->next->next->next = stl; } } return st; } struct StatementNode* parse_stmt(){ //stmt -> assign_stmt | print_stmt | while_stmt | if_stmt | swithc_stmt | for_stmt struct StatementNode* st = new StatementNode; t = lexer.GetToken(); lexer.UngetToken(t); if(t.token_type == ID){ st = parse_assign_stmt(); }else if(t.token_type == PRINT){ st = parse_print_stmt(); }else if(t.token_type == WHILE){ st = parse_while_stmt(); }else if(t.token_type == IF){ st = parse_if_stmt(); }else if(t.token_type == SWITCH){ st = parse_switch_stmt(); }else if(t.token_type == FOR){ st = parse_for_stmt(); } return st; } struct StatementNode* parse_assign_stmt(){ //assign_stmt -> ID EQUAL primary SEMICOLON //assign_stmt -> ID EQUAL expr SEMICOLON struct StatementNode* st = new StatementNode; struct ValueNode* lhs = new ValueNode; struct AssignmentStatement* assig = new AssignmentStatement; st->type = ASSIGN_STMT; t = lexer.GetToken();//ID if(t.token_type != ID) cout << "Token error assign stmt loc 1\n"; lhs = find_valueNode(t.lexeme); t = lexer.GetToken();//EQUAL if(t.token_type != EQUAL) cout << "Token error assign stmt loc 2\n"; t = lexer.GetToken(); t2 = lexer.GetToken(); lexer.UngetToken(t2); lexer.UngetToken(t); if( (t2.token_type == PLUS) | (t2.token_type == MINUS) | (t2.token_type == MULT) | (t2.token_type == DIV) ){ assig = parse_expr(); }else{ struct ValueNode* operand1 = new ValueNode; operand1 = parse_primary(); struct ValueNode* operand2 = new ValueNode; operand2 = NULL; ArithmeticOperatorType op = OPERATOR_NONE; assig->operand1 = operand1; assig->operand2 = operand2; assig->op = op; } assig->left_hand_side = lhs; st->assign_stmt = assig; t = lexer.GetToken(); if(t.token_type != SEMICOLON) cout << "Token error in assign stmt\n"; return st; } struct AssignmentStatement* parse_expr(){ //expr -> primary op primary struct AssignmentStatement* ast = new AssignmentStatement; struct ValueNode* operand1 = new ValueNode; struct ValueNode* operand2 = new ValueNode; ArithmeticOperatorType op; operand1 = parse_primary(); op = parse_op(); operand2 = parse_primary(); ast->operand1 = operand1; ast->op = op; ast->operand2 = operand2; return ast; } struct ValueNode* parse_primary(){ //primary -> ID | NUM struct ValueNode* node = new ValueNode; t = lexer.GetToken(); if(t.token_type == ID){ node = find_valueNode(t.lexeme); }else if(t.token_type == NUM){ node->name = ""; node->value = stoi(t.lexeme); }else cout << "Token error in primary\n"; return node; } enum ArithmeticOperatorType parse_op(){ //op -> PLUS | MINUS | MULT | DIV ArithmeticOperatorType op; t = lexer.GetToken(); if(t.token_type == PLUS) op = OPERATOR_PLUS; else if(t.token_type == MINUS) op = OPERATOR_MINUS; else if(t.token_type == MULT) op = OPERATOR_MULT; else if(t.token_type == DIV) op = OPERATOR_DIV; else cout << "Token error in op\n"; return op; } struct StatementNode* parse_print_stmt(){ //print_stmt -> print ID SEMICOLON struct StatementNode* st = new StatementNode; st->type = PRINT_STMT; t = lexer.GetToken();//print if(t.token_type != PRINT) cout << "Token error in print stmt loc1\n"; t = lexer.GetToken();//ID if(t.token_type != ID) cout << "Token error in print stmt loc2\n"; struct ValueNode* id = new ValueNode; id = find_valueNode(t.lexeme); st->print_stmt = new PrintStatement; st->print_stmt->id = id; t = lexer.GetToken();//SEMICOLON if(t.token_type != SEMICOLON) cout << "Token error in print stmt loc3\n"; return st; } struct StatementNode* parse_while_stmt(){ //while_stmt -> WHILE condition body struct StatementNode* st = new StatementNode; st->type = IF_STMT; t = lexer.GetToken();//WHILE if(t.token_type != WHILE) cout << "Token error in while stmt loc 1\n"; struct IfStatement* ifnode = new IfStatement; ifnode = parse_condition(); ifnode->true_branch = parse_body(); struct StatementNode* gt = new StatementNode; gt->type = GOTO_STMT; struct GotoStatement* goto_node = new GotoStatement; goto_node->target = st; gt->goto_stmt = goto_node; struct StatementNode* temp = new StatementNode; temp = ifnode->true_branch; while(temp->next != NULL){ temp = temp->next; } temp->next = gt; struct StatementNode* noop = new StatementNode; noop->type = NOOP_STMT; ifnode->false_branch = noop; st->next = noop; st->if_stmt = ifnode; return st; } struct StatementNode* parse_if_stmt(){ //IF condition body struct StatementNode* st = new StatementNode; struct IfStatement* ifnode = new IfStatement; st->type = IF_STMT; t = lexer.GetToken();//IF if(t.token_type != IF) cout << "Token error in if stmt pos 1\n"; ifnode = parse_condition(); ifnode->true_branch = parse_body(); struct StatementNode* noop = new StatementNode; noop->type = NOOP_STMT; //append no op node to body of if struct StatementNode* temp = new StatementNode; temp = ifnode->true_branch; while(temp->next != NULL){ temp = temp->next; } temp->next = noop; ifnode->false_branch = noop; st->if_stmt = ifnode; st->next = noop; return st; } struct IfStatement* parse_condition(){ //condition -> primary relop primary struct IfStatement* ifStmt = new IfStatement; ifStmt->condition_operand1 = parse_primary(); ifStmt->condition_op = parse_relop(); ifStmt->condition_operand2 = parse_primary(); return ifStmt; } enum ConditionalOperatorType parse_relop(){ //relop -> GREATER | LESS | NOTEQUAL ConditionalOperatorType op; t = lexer.GetToken(); if(t.token_type == GREATER) op = CONDITION_GREATER; else if(t.token_type == LESS) op = CONDITION_LESS; else if(t.token_type == NOTEQUAL) op = CONDITION_NOTEQUAL; return op; } struct StatementNode* parse_for_stmt(){ //for_stmt -> FOR LPAREN assign_stmt condition SEMICOLON assign_stmt RPAREN body struct StatementNode* st = new StatementNode; struct StatementNode* st1 = new StatementNode; struct StatementNode* st2 = new StatementNode; struct StatementNode* gt = new StatementNode; struct StatementNode* noop = new StatementNode; noop->type = NOOP_STMT; struct IfStatement* ifnode = new IfStatement; struct GotoStatement* goto_node = new GotoStatement; t = lexer.GetToken();//FOR if(t.token_type != FOR) cout << "Token error in for stmt pos 1\n"; t = lexer.GetToken();//LPAREN if(t.token_type != LPAREN) cout << "Token error in for stmt pos 2\n"; st = parse_assign_stmt(); st1->type = IF_STMT; ifnode = parse_condition(); t = lexer.GetToken(); if(t.token_type != SEMICOLON) cout << "Token error in for stmt pos 3\n"; st2 = parse_assign_stmt(); t = lexer.GetToken(); if(t.token_type != RPAREN) cout << "Token error in for stmt pos 4\n"; ifnode->true_branch = parse_body(); struct StatementNode* temp = new StatementNode; temp = ifnode->true_branch; while(temp->next != NULL) temp = temp->next; temp->next = st2; temp = temp->next; gt->type = GOTO_STMT; goto_node->target = st1; gt->goto_stmt = goto_node; temp->next = gt; st1->if_stmt = ifnode; st1->next = noop; ifnode->false_branch = noop; st->next = st1; return st; } struct StatementNode* parse_switch_stmt(){ //swithc_stmt -> SWITCH ID LBRACE case_list RBRACE //switch_stmt -> SWITCH ID LBRACE case_list default_case RBRACE struct StatementNode* st = new StatementNode; struct StatementNode* dummy = new StatementNode; if(nextGlobalNoop == 1){ globalNoop->type = NOOP_STMT; nextGlobalNoop++; }else if(nextGlobalNoop == 2){ globalNoop2->type = NOOP_STMT; nextGlobalNoop++; }else if(nextGlobalNoop == 3){ globalNoop3->type = NOOP_STMT; nextGlobalNoop++; }else if(nextGlobalNoop == 4){ globalNoop4->type = NOOP_STMT; nextGlobalNoop++; }else if(nextGlobalNoop == 5){ globalNoop5->type = NOOP_STMT; nextGlobalNoop++; }else if(nextGlobalNoop == 6){ globalNoop6->type = NOOP_STMT; nextGlobalNoop++; }else if(nextGlobalNoop == 7){ globalNoop7->type = NOOP_STMT; nextGlobalNoop++; }else if(nextGlobalNoop == 8){ globalNoop8->type = NOOP_STMT; nextGlobalNoop++; }else if(nextGlobalNoop == 9){ globalNoop9->type = NOOP_STMT; nextGlobalNoop++; }else if(nextGlobalNoop == 10){ globalNoop10->type = NOOP_STMT; nextGlobalNoop++; } t = lexer.GetToken(); if(t.token_type != SWITCH) cout << "Token error in switch stmt pos 1\n"; t = lexer.GetToken(); if(t.token_type != ID) cout << "Token error in switch stmt pos 2\n"; //t.lexeme = variable in condition struct ValueNode* cond = new ValueNode; cond = find_valueNode(t.lexeme); t = lexer.GetToken(); if(t.token_type != LBRACE) cout << "Token error in switch stmt pos 3\n"; st = parse_case_list(cond); t = lexer.GetToken(); lexer.UngetToken(t); //check if there is a default case or not //if(t.token_type == DEFAULT) //dummy = parse_default_case(); t = lexer.GetToken(); if(t.token_type != RBRACE) cout << "Token error in switch stmt pos 4\n"; return st; } struct StatementNode* parse_case_list(struct ValueNode* var){ //case_list -> case case_list | case struct StatementNode* st = new StatementNode; struct StatementNode* stl = new StatementNode; bool entered = false; st = parse_case(var); t = lexer.GetToken(); lexer.UngetToken(t); if(t.token_type == CASE){ stl = parse_case_list(var); st->if_stmt->true_branch = stl; entered = true; } if(!entered){ t = lexer.GetToken(); lexer.UngetToken(t); if(t.token_type == DEFAULT){ stl = parse_default_case(); st->if_stmt->true_branch = stl; } } if(st->if_stmt->true_branch == NULL){ // st->if_stmt->true_branch = globalNoop;/*here*/ if(nextGlobalNoop-1 == 1){ st->if_stmt->true_branch = globalNoop; }else if(nextGlobalNoop-1 == 2){ st->if_stmt->true_branch = globalNoop2; }else if(nextGlobalNoop-1 == 3){ st->if_stmt->true_branch = globalNoop3; }else if(nextGlobalNoop-1 == 4){ st->if_stmt->true_branch = globalNoop4; }else if(nextGlobalNoop-1 == 5){ st->if_stmt->true_branch = globalNoop5; }else if(nextGlobalNoop-1 == 6){ st->if_stmt->true_branch = globalNoop6; }else if(nextGlobalNoop-1 == 7){ st->if_stmt->true_branch = globalNoop7; }else if(nextGlobalNoop-1 == 8){ st->if_stmt->true_branch = globalNoop8; }else if(nextGlobalNoop-1 == 9){ st->if_stmt->true_branch = globalNoop9; }else if(nextGlobalNoop-1 == 10){ st->if_stmt->true_branch = globalNoop10; } } return st; } struct StatementNode* parse_case(struct ValueNode* var){ //case -> CASE NUM COLON body struct StatementNode* st = new StatementNode; struct IfStatement* ifnode = new IfStatement; struct ValueNode* numnode = new ValueNode; struct StatementNode* body = new StatementNode; struct StatementNode* gt = new StatementNode; struct GotoStatement* gotoNode = new GotoStatement; gt->type = GOTO_STMT; //gotoNode->target = globalNoop;/*here*/ if(nextGlobalNoop-1 == 1){ gotoNode->target = globalNoop; }else if(nextGlobalNoop-1 == 2){ gotoNode->target = globalNoop2; }else if(nextGlobalNoop-1 == 3){ gotoNode->target = globalNoop3; }else if(nextGlobalNoop-1 == 4){ gotoNode->target = globalNoop4; }else if(nextGlobalNoop-1 == 5){ gotoNode->target = globalNoop5; }else if(nextGlobalNoop-1 == 6){ gotoNode->target = globalNoop6; }else if(nextGlobalNoop-1 == 7){ gotoNode->target = globalNoop7; }else if(nextGlobalNoop-1 == 8){ gotoNode->target = globalNoop8; }else if(nextGlobalNoop-1 == 9){ gotoNode->target = globalNoop9; }else if(nextGlobalNoop-1 == 10){ gotoNode->target = globalNoop10; } gt->goto_stmt = gotoNode; t = lexer.GetToken();//CASE if(t.token_type != CASE) cout << "Token error in case pos 1\n"; t = lexer.GetToken(); if(t.token_type != NUM) cout << "Token error in case pos 2\n"; int temps = stoi(t.lexeme); numnode->value = temps;//setting case num numnode->name = ""; t = lexer.GetToken(); if(t.token_type != COLON) cout << "Token error in case pos 3\n"; body = parse_body(); ifnode->condition_operand1 = var; ifnode->condition_operand2 = numnode; ifnode->condition_op = CONDITION_NOTEQUAL; ifnode->false_branch = body; struct StatementNode* temp = new StatementNode; temp = ifnode->false_branch; while(temp->next != NULL){ temp = temp->next; } temp->next = gt; st->type = IF_STMT; st->if_stmt = ifnode; //st->next = globalNoop;/*here*/ if(nextGlobalNoop-1 == 1){ st->next = globalNoop; }else if(nextGlobalNoop-1 == 2){ st->next = globalNoop2; }else if(nextGlobalNoop-1 == 3){ st->next = globalNoop3; }else if(nextGlobalNoop-1 == 4){ st->next = globalNoop4; }else if(nextGlobalNoop-1 == 5){ st->next = globalNoop5; }else if(nextGlobalNoop-1 == 6){ st->next = globalNoop6; }else if(nextGlobalNoop-1 == 7){ st->next = globalNoop7; }else if(nextGlobalNoop-1 == 8){ st->next = globalNoop8; }else if(nextGlobalNoop-1 == 9){ st->next = globalNoop9; }else if(nextGlobalNoop-1 == 10){ st->next = globalNoop10; } return st; } struct StatementNode* parse_default_case(){ //default_case -> DEFAULT COLON body struct StatementNode* st = new StatementNode; struct StatementNode* gt = new StatementNode; struct GotoStatement* gotoNode = new GotoStatement; gt->type = GOTO_STMT; //gotoNode->target = globalNoop;/*here*/ if(nextGlobalNoop-1 == 1){ gotoNode->target = globalNoop; }else if(nextGlobalNoop-1 == 2){ gotoNode->target = globalNoop2; }else if(nextGlobalNoop-1 == 3){ gotoNode->target = globalNoop3; }else if(nextGlobalNoop-1 == 4){ gotoNode->target = globalNoop4; }else if(nextGlobalNoop-1 == 5){ gotoNode->target = globalNoop5; }else if(nextGlobalNoop-1 == 6){ gotoNode->target = globalNoop6; }else if(nextGlobalNoop-1 == 7){ gotoNode->target = globalNoop7; }else if(nextGlobalNoop-1 == 8){ gotoNode->target = globalNoop8; }else if(nextGlobalNoop-1 == 9){ gotoNode->target = globalNoop9; }else if(nextGlobalNoop-1 == 10){ gotoNode->target = globalNoop10; } gt->goto_stmt = gotoNode; t = lexer.GetToken(); t2 = lexer.GetToken(); if( (t.token_type != DEFAULT) | (t2.token_type != COLON) ) cout << "Token error in default case\n"; st = parse_body(); struct StatementNode* temp = new StatementNode; temp = st; while(temp->next != NULL) temp = temp->next; temp->next = gt; //temp->next->next = globalNoop; return st; } struct ValueNode* find_valueNode(string name){ struct ValueNode* node = new ValueNode; int location = next_var - 1; for(int i = 0; i < next_var; i++){ if(name == variables[i]->name){ location = i; i = next_var; } } node = variables[location]; return node; }