identifier_list eval_boolean(abstractSyntaxTree temp, symbol_table_ptr st,int scope) { char temp_name[10]; char name[10]; identifier_list id1; identifier_list id2; identifier_list id3; if(temp->symbol == TK_NUM || temp->symbol == TK_RNUM ) { //printf("Current expr is: %s\n",temp->value); sprintf(name , "%d", num_no); strcpy(temp_name,"num"); strcat(temp_name,name); id3 = create_identifier(3,temp->value,temp_name,scope); num_no ++; return (id3); } else if(temp->symbol == TK_ID) { //printf("HI\n"); return (get_symtable_ptr(" ",temp->value, st, scope)); } else if(temp->symbol == TK_NOT) { id1 = eval_boolean(temp->child[0],st,scope); sprintf(name , "%d", temp_no); strcpy(temp_name,"temp"); strcat(temp_name,name); id3 = create_identifier(id1->id_type," ",temp_name,scope); create_quadruple(temp->symbol,id1,NULL,id3); temp_no++; return (id3); } else { id1 = eval_boolean(temp->child[0],st,scope); //printf("Why\n"); id2 = eval_boolean(temp->child[1],st,scope); sprintf(name , "%d", temp_no); strcpy(temp_name,"temp"); strcat(temp_name,name); id3 = create_identifier(id1->id_type," ",temp_name,scope); create_quadruple(temp->symbol,id1,id2,id3); temp_no++; return(id3); } }
/* ** Perform a reduce action and the shift that must immediately ** follow the reduce. */ static void yy_reduce( yyParser *yypParser, /* The parser */ int yyruleno /* Number of the rule by which to reduce */ ){ int yygoto; /* The next state */ int yyact; /* The next action */ YYMINORTYPE yygotominor; /* The LHS of the rule reduced */ yyStackEntry *yymsp; /* The top of the parser's stack */ int yysize; /* Amount to pop the stack */ ParseARG_FETCH; yymsp = &yypParser->yystack[yypParser->yyidx]; #ifndef NDEBUG if( yyTraceFILE && yyruleno>=0 && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){ yysize = yyRuleInfo[yyruleno].nrhs; fprintf(yyTraceFILE, "%sReduce [%s] -> state %d.\n", yyTracePrompt, yyRuleName[yyruleno], yymsp[-yysize].stateno); } #endif /* NDEBUG */ /* Silence complaints from purify about yygotominor being uninitialized ** in some cases when it is copied into the stack after the following ** switch. yygotominor is uninitialized when a rule reduces that does ** not set the value of its left-hand side nonterminal. Leaving the ** value of the nonterminal uninitialized is utterly harmless as long ** as the value is never used. So really the only thing this code ** accomplishes is to quieten purify. ** ** 2007-01-16: The wireshark project (www.wireshark.org) reports that ** without this code, their parser segfaults. I'm not sure what there ** parser is doing to make this happen. This is the second bug report ** from wireshark this week. Clearly they are stressing Lemon in ways ** that it has not been previously stressed... (SQLite ticket #2172) */ /*memset(&yygotominor, 0, sizeof(yygotominor));*/ yygotominor = yyzerominor; switch( yyruleno ){ /* Beginning here are the reduction cases. A typical example ** follows: ** case 0: ** #line <lineno> <grammarfile> ** { ... } // User supplied code ** #line <lineno> <thisfile> ** break; */ case 0: /* expression ::= expr */ #line 35 "/home/veei/git/calculator/expressions.grammar.y" { state->root = yymsp[0].minor.yy0; } #line 809 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 1: /* identifier ::= IDENTIFIER */ #line 38 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = create_identifier(yymsp[0].minor.yy0, state->vpool); add_to_temp_set(yygotominor.yy0, state->temp_set); } #line 817 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 2: /* identifier ::= identifier DOT IDENTIFIER */ #line 44 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = create_access_operator(yymsp[-2].minor.yy0, yymsp[0].minor.yy0); remove_from_temp_set(yymsp[-2].minor.yy0, state->temp_set); add_to_temp_set(yygotominor.yy0, state->temp_set); yy_destructor(yypParser,18,&yymsp[-1].minor); } #line 827 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 3: /* term ::= identifier */ case 8: /* expr ::= term */ yytestcase(yyruleno==8); #line 50 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = yymsp[0].minor.yy0; } #line 833 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 4: /* term ::= INTEGER */ #line 53 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = create_integer(yymsp[0].minor.yy0); add_to_temp_set(yygotominor.yy0, state->temp_set); } #line 841 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 5: /* term ::= FLOAT */ #line 59 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = create_float(yymsp[0].minor.yy0); add_to_temp_set(yygotominor.yy0, state->temp_set); } #line 849 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 6: /* term ::= BOOLEAN */ #line 65 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = create_boolean(yymsp[0].minor.yy0); add_to_temp_set(yygotominor.yy0, state->temp_set); } #line 857 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 7: /* term ::= STRING */ #line 71 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = create_string(yymsp[0].minor.yy0); add_to_temp_set(yygotominor.yy0, state->temp_set); } #line 865 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 9: /* expr ::= expr PLUS expr */ #line 79 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = create_add_operator(yymsp[-2].minor.yy0, yymsp[0].minor.yy0); add_to_temp_set(yygotominor.yy0, state->temp_set); remove_from_temp_set(yymsp[-2].minor.yy0, state->temp_set); remove_from_temp_set(yymsp[0].minor.yy0, state->temp_set); yy_destructor(yypParser,11,&yymsp[-1].minor); } #line 876 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 10: /* expr ::= expr MINUS expr */ #line 87 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = create_sub_operator(yymsp[-2].minor.yy0, yymsp[0].minor.yy0); add_to_temp_set(yygotominor.yy0, state->temp_set); remove_from_temp_set(yymsp[-2].minor.yy0, state->temp_set); remove_from_temp_set(yymsp[0].minor.yy0, state->temp_set); yy_destructor(yypParser,12,&yymsp[-1].minor); } #line 887 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 11: /* expr ::= expr DIVIDE expr */ #line 95 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = create_div_operator(yymsp[-2].minor.yy0, yymsp[0].minor.yy0); add_to_temp_set(yygotominor.yy0, state->temp_set); remove_from_temp_set(yymsp[-2].minor.yy0, state->temp_set); remove_from_temp_set(yymsp[0].minor.yy0, state->temp_set); yy_destructor(yypParser,15,&yymsp[-1].minor); } #line 898 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 12: /* expr ::= expr MOD expr */ #line 103 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = create_mod_operator(yymsp[-2].minor.yy0, yymsp[0].minor.yy0); add_to_temp_set(yygotominor.yy0, state->temp_set); remove_from_temp_set(yymsp[-2].minor.yy0, state->temp_set); remove_from_temp_set(yymsp[0].minor.yy0, state->temp_set); yy_destructor(yypParser,16,&yymsp[-1].minor); } #line 909 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 13: /* expr ::= expr TIMES expr */ #line 111 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = create_times_operator(yymsp[-2].minor.yy0, yymsp[0].minor.yy0); add_to_temp_set(yygotominor.yy0, state->temp_set); remove_from_temp_set(yymsp[-2].minor.yy0, state->temp_set); remove_from_temp_set(yymsp[0].minor.yy0, state->temp_set); yy_destructor(yypParser,14,&yymsp[-1].minor); } #line 920 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 14: /* expr ::= LEFT_P expr RIGHT_P */ #line 119 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = yymsp[-1].minor.yy0; yy_destructor(yypParser,23,&yymsp[-2].minor); yy_destructor(yypParser,24,&yymsp[0].minor); } #line 929 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 15: /* expr ::= MINUS expr */ #line 124 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = create_minus_operator(yymsp[0].minor.yy0); add_to_temp_set(yygotominor.yy0, state->temp_set); remove_from_temp_set(yymsp[0].minor.yy0, state->temp_set); yy_destructor(yypParser,12,&yymsp[-1].minor); } #line 939 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 16: /* expr ::= NOT expr */ #line 131 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = create_not_operator(yymsp[0].minor.yy0); add_to_temp_set(yygotominor.yy0, state->temp_set); remove_from_temp_set(yymsp[0].minor.yy0, state->temp_set); yy_destructor(yypParser,4,&yymsp[-1].minor); } #line 949 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 17: /* expr ::= expr AND expr */ #line 138 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = create_and_operator(yymsp[-2].minor.yy0, yymsp[0].minor.yy0); add_to_temp_set(yygotominor.yy0, state->temp_set); remove_from_temp_set(yymsp[-2].minor.yy0, state->temp_set); remove_from_temp_set(yymsp[0].minor.yy0, state->temp_set); yy_destructor(yypParser,2,&yymsp[-1].minor); } #line 960 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 18: /* expr ::= expr OR expr */ #line 146 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = create_or_operator(yymsp[-2].minor.yy0, yymsp[0].minor.yy0); add_to_temp_set(yygotominor.yy0, state->temp_set); remove_from_temp_set(yymsp[-2].minor.yy0, state->temp_set); remove_from_temp_set(yymsp[0].minor.yy0, state->temp_set); yy_destructor(yypParser,3,&yymsp[-1].minor); } #line 971 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 19: /* expr ::= expr LESS expr */ #line 154 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = create_less_operator(yymsp[-2].minor.yy0, yymsp[0].minor.yy0); add_to_temp_set(yygotominor.yy0, state->temp_set); remove_from_temp_set(yymsp[-2].minor.yy0, state->temp_set); remove_from_temp_set(yymsp[0].minor.yy0, state->temp_set); yy_destructor(yypParser,5,&yymsp[-1].minor); } #line 982 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 20: /* expr ::= expr LESS_OR_EQUAL expr */ #line 162 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = create_less_or_equal_operator(yymsp[-2].minor.yy0, yymsp[0].minor.yy0); add_to_temp_set(yygotominor.yy0, state->temp_set); remove_from_temp_set(yymsp[-2].minor.yy0, state->temp_set); remove_from_temp_set(yymsp[0].minor.yy0, state->temp_set); yy_destructor(yypParser,6,&yymsp[-1].minor); } #line 993 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 21: /* expr ::= expr GREATER expr */ #line 170 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = create_greater_operator(yymsp[-2].minor.yy0, yymsp[0].minor.yy0); add_to_temp_set(yygotominor.yy0, state->temp_set); remove_from_temp_set(yymsp[-2].minor.yy0, state->temp_set); remove_from_temp_set(yymsp[0].minor.yy0, state->temp_set); yy_destructor(yypParser,7,&yymsp[-1].minor); } #line 1004 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 22: /* expr ::= expr GREATER_OR_EQUAL expr */ #line 178 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = create_greater_or_equal_operator(yymsp[-2].minor.yy0, yymsp[0].minor.yy0); add_to_temp_set(yygotominor.yy0, state->temp_set); remove_from_temp_set(yymsp[-2].minor.yy0, state->temp_set); remove_from_temp_set(yymsp[0].minor.yy0, state->temp_set); yy_destructor(yypParser,8,&yymsp[-1].minor); } #line 1015 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 23: /* expr ::= expr EQUAL expr */ #line 186 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = create_equal_operator(yymsp[-2].minor.yy0, yymsp[0].minor.yy0); add_to_temp_set(yygotominor.yy0, state->temp_set); remove_from_temp_set(yymsp[-2].minor.yy0, state->temp_set); remove_from_temp_set(yymsp[0].minor.yy0, state->temp_set); yy_destructor(yypParser,9,&yymsp[-1].minor); } #line 1026 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 24: /* expr ::= expr NOT_EQUAL expr */ #line 194 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = create_not_equal_operator(yymsp[-2].minor.yy0, yymsp[0].minor.yy0); add_to_temp_set(yygotominor.yy0, state->temp_set); remove_from_temp_set(yymsp[-2].minor.yy0, state->temp_set); remove_from_temp_set(yymsp[0].minor.yy0, state->temp_set); yy_destructor(yypParser,10,&yymsp[-1].minor); } #line 1037 "/home/veei/git/calculator/expressions.grammar.cpp" break; case 25: /* expr ::= identifier ASSIGN expr */ #line 202 "/home/veei/git/calculator/expressions.grammar.y" { yygotominor.yy0 = create_assignment_operator(yymsp[-2].minor.yy0, yymsp[0].minor.yy0); add_to_temp_set(yygotominor.yy0, state->temp_set); remove_from_temp_set(yymsp[-2].minor.yy0, state->temp_set); remove_from_temp_set(yymsp[0].minor.yy0, state->temp_set); yy_destructor(yypParser,1,&yymsp[-1].minor); } #line 1048 "/home/veei/git/calculator/expressions.grammar.cpp" break; default: break; }; yygoto = yyRuleInfo[yyruleno].lhs; yysize = yyRuleInfo[yyruleno].nrhs; yypParser->yyidx -= yysize; yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto); if( yyact <= YY_MAX_SHIFTREDUCE ){ if( yyact>YY_MAX_SHIFT ) yyact += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE; /* If the reduce action popped at least ** one element off the stack, then we can push the new element back ** onto the stack here, and skip the stack overflow test in yy_shift(). ** That gives a significant speed improvement. */ if( yysize ){ yypParser->yyidx++; yymsp -= yysize-1; yymsp->stateno = (YYACTIONTYPE)yyact; yymsp->major = (YYCODETYPE)yygoto; yymsp->minor = yygotominor; yyTraceShift(yypParser, yyact); }else{ yy_shift(yypParser,yyact,yygoto,&yygotominor); } }else{ assert( yyact == YY_ACCEPT_ACTION ); yy_accept(yypParser); } }
void extract_stmt(abstractSyntaxTree temp,symbol_table_ptr st, int scope ) { identifier_list id; char name[10]; char temp_name[10]; //printf("Stmt is : %s\n" ,tokenName[temp->symbol]); if(temp->symbol == TK_ASSIGNOP) { //print_tree(temp); //printf("Sup\n"); //printf("%d\n",temp->childnum); if(temp->child[0]->child[0]->symbol == TK_EPSILON) { //print_tree(temp->child[1]); //printf("Child1 : %s\n",tokenName[temp->child[1]->symbol]); //printf("Searching for %s\n",temp->child[0]->value); id = get_symtable_ptr(" ",temp->child[0]->value, st, scope); //printf("Sym value : %s\n",id->id_name); //printf("HI\n"); create_quadruple(temp->symbol,eval_arithmetic(temp->child[1],st,scope),NULL,id); } else { //printf("Searching for %s.%s\n",temp->child[0]->value ,temp->child[0]->child[0]->value); id = get_symtable_ptr(temp->child[0]->value,temp->child[0]->child[0]->value, st, scope); //printf("Sym table value : %s.%s",id->record_name,id->id_name); create_quadruple(temp->symbol,eval_arithmetic(temp->child[1],st,scope),NULL,id); } } //printf("Hi\n");*/ if(temp->symbol == TK_READ) { if(temp->child[0]->child[0]->symbol == TK_EPSILON) { id = get_symtable_ptr(" ",temp->child[0]->value, st, scope); //printf("Sym value : %s\n",id->id_name); create_quadruple(temp->symbol,id,NULL,NULL); } else { //printf("The record is : %s\n",temp->child[0]->value); //printf("The id is : %s\n",temp->child[0]->child[0]->value); id = get_symtable_ptr(temp->child[0]->value,temp->child[0]->child[0]->value, st, scope); //printf("Sym table value : %s",id->record_name); create_quadruple(temp->symbol,id,NULL,NULL); } } else if(temp->symbol == TK_WRITE) { if(temp->child[0]->symbol == TK_NUM || temp->child[0]->symbol == TK_RNUM ) { //printf("Current expr is: %s\n",temp->value); sprintf(name , "%d", num_no); strcpy(temp_name,"num"); strcat(temp_name,name); id = create_identifier(3,temp->child[0]->value,temp_name,scope); num_no ++; create_quadruple(temp->symbol,id,NULL,NULL); } else if(temp->child[0]->child[0]->symbol == TK_EPSILON) { id = get_symtable_ptr(" ",temp->child[0]->value, st, scope); //printf("Sym value : %s\n",id->id_name); create_quadruple(temp->symbol,id,NULL,NULL); } else { //printf("The record is : %s\n",temp->child[0]->value); //printf("The id is : %s\n",temp->child[0]->child[0]->value); id = get_symtable_ptr(temp->child[0]->value,temp->child[0]->child[0]->value, st, scope); //printf("Sym table value : %s",id->record_name); create_quadruple(temp->symbol,id,NULL,NULL); } } else if(temp->symbol == TK_IF) { int jump1,jump2; //print_tree(temp->child[0]); id = eval_boolean(temp->child[0],st,scope); jump1 = quad_size; create_quadruple(temp->symbol,id,NULL,NULL); extract_stmt(temp->child[1],st,scope); extract_individual_stmts(temp->child[2],st,scope); if(temp->child[3]->symbol == TK_ENDIF) { sprintf(name , "%d", quad_size); quad[jump1].result = create_copy(quad[jump1].result,create_identifier(0," ",name,scope)); } else if(temp->child[3]->symbol == TK_ELSE) { jump2 = quad_size; extract_stmt(temp->child[3]->child[0],st,scope); extract_individual_stmts(temp->child[3]->child[1],st,scope); sprintf(name , "%d", quad_size); quad[jump1].result = create_copy(quad[jump1].result,create_identifier(0," ",name,scope)); sprintf(name , "%d", jump2); quad[jump1].arg2 = create_copy(quad[jump1].arg2,create_identifier(0," ",name,scope)); } } else if(temp->symbol == TK_WHILE) { int jump; create_quadruple(21,NULL,NULL,NULL); id = eval_boolean(temp->child[0],st,scope); create_quadruple(temp->symbol,id,NULL,NULL); jump = quad_size-1; extract_stmt(temp->child[1],st,scope); extract_individual_stmts(temp->child[2],st,scope); sprintf(name , "%d", quad_size); quad[jump].result = create_copy(quad[jump].result,create_identifier(0," ",name,scope)); } //printf("Bye"); }
identifier_list eval_arithmetic(abstractSyntaxTree temp, symbol_table_ptr st,int scope) { char temp_name[10]; char name[10]; identifier_list id1; identifier_list id2; identifier_list id3; if (temp->symbol == TK_EPSILON && temp->childnum == 0) { return NULL; } else if (temp->symbol == TK_EPSILON && temp->childnum>0) { return (eval_arithmetic(temp->child[0],st,scope)); } if(temp->symbol == TK_NUM || temp->symbol == TK_RNUM ) { //printf("Current expr is: %s\n",temp->value); sprintf(name , "%d", num_no); strcpy(temp_name,"num"); strcat(temp_name,name); id3 = create_identifier(3,temp->value,temp_name,scope); num_no ++; return (id3); } else if(temp->symbol == TK_ID) { if(temp->child[0]->symbol == TK_EPSILON) { //printf("Current expr is: %s\n",tokenName[temp->symbol]); return (get_symtable_ptr(" ",temp->value, st, scope)); } else { //printf("Current expr is: %s\n",tokenName[temp->symbol]); return (get_symtable_ptr(temp->value,temp->child[0]->value, st, scope)); } } else if(strcmp(tokenName[temp->symbol],"<term>") == 0 || strcmp(tokenName[temp->symbol],"<arithmeticExpression>") == 0 || temp->symbol == TK_PLUS || temp->symbol == TK_MINUS || temp->symbol == TK_MUL || temp->symbol == TK_DIV) { id1 = eval_arithmetic(temp->child[0],st,scope); id2 = eval_arithmetic(temp->child[1],st,scope); if(id1 == NULL && id2 != NULL) return (id2); else if(id1 !=NULL && id2 == NULL) return (id1); else { sprintf(name , "%d", temp_no); strcpy(temp_name,"temp"); strcat(temp_name,name); id3 = create_identifier(id1->id_type," ",temp_name,scope); create_quadruple(temp->child[1]->symbol,id1,id2,id3); temp_no++; return (id3); } } else; }