diff --git a/ass 5/Makefile b/ass 5/Makefile index 4e07f7a..a5a8af0 100644 --- a/ass 5/Makefile +++ b/ass 5/Makefile @@ -14,7 +14,7 @@ lex.yy.c: y.tab.h ass5_12CS10037.l ass5_12CS10037_translator.o flex ass5_12CS10037.l y.tab.c: ass5_12CS10037.y - yacc -dtv ass5_12CS10037.y + yacc -dt --warnings=none ass5_12CS10037.y diff --git a/ass 5/SymbolTable.cpp b/ass 5/SymbolTable.cpp index 35b709b..e3b562c 100644 --- a/ass 5/SymbolTable.cpp +++ b/ass 5/SymbolTable.cpp @@ -10,13 +10,19 @@ bool sortCompare(const SFields &s1,const SFields& s2) return true; return false; } +bool parCom(const SFields &s1,const SFields& s2) +{ + if(s1.parNum pars=getParamList(); + tr(pars,it) + printf("%s ",it->name.c_str() ); + printf("\n"); std::vector temp(table.begin(), table.end()); sort(temp.begin(), temp.end(),sortCompare); int c=1; @@ -79,7 +89,12 @@ void SymbolTable::print() } printf("-----------------------------------------------------------\n"); - + // tr(table, it) + // { + // printf("%u %s\n",&(*it),it->name.c_str() ); + // } + + // printf("-----------------------------------------------------------\n"); } void SymbolTable::update(Fields* f,const Type& t) @@ -121,7 +136,7 @@ Fields* SymbolTable::search(const string& s) std::vector SymbolTable::getParamList() { std::vector temp(table.begin(), table.end()); - sort(temp.begin(), temp.end(),sortCompare); + sort(temp.begin(), temp.end(),parCom); if(paramNum>0) return std::vector(temp.begin(),temp.begin()+paramNum); std::vector v; diff --git a/ass 5/SymbolTable.h b/ass 5/SymbolTable.h index 8e1b1af..615bef7 100644 --- a/ass 5/SymbolTable.h +++ b/ass 5/SymbolTable.h @@ -105,12 +105,14 @@ struct Fields{ bool isBoolExp; //if it is a boolean expression then t has true list and false lists Fields* arrSize; //if type is array, store the size while reducing grammar bool isArray; + bool isPointer; Fields* arrayBase; + int parNum; ListType tl,fl;//true list and false lists Fields():type(),loc(NULL),size(4),offset(0),nestedTable(NULL), isConst(false),isBoolExp(false),arrSize(NULL), - isArray(false) + isArray(false),isPointer(false),parNum(100000) {} diff --git a/ass 5/ass5_12CS10037.l b/ass 5/ass5_12CS10037.l index d0bd5ed..790f8c0 100644 --- a/ass 5/ass5_12CS10037.l +++ b/ass 5/ass5_12CS10037.l @@ -7,6 +7,7 @@ void count(); extern SymbolTable* st; + int isComment=0; %} @@ -192,7 +193,7 @@ STRING_LITERAL \"{S_CHAR_SEQUENCE}?\" BEGIN(INITIAL); } -\/\/.* {} +\/\/.* {isComment=1;} @@ -214,6 +215,8 @@ void count() column += 8 - (column % 8); else column++; - - ECHO; + if(!isComment) + ; + else + isComment=0; } diff --git a/ass 5/ass5_12CS10037.y b/ass 5/ass5_12CS10037.y index db55128..64ac9c3 100644 --- a/ass 5/ass5_12CS10037.y +++ b/ass 5/ass5_12CS10037.y @@ -7,15 +7,37 @@ struct Fields; extern int yylex(); extern void yyerror(char *s); + /* + All the quads will be stored and pushed to this vector. + All of them will be printed at once in the end + */ std::vector quadArray; + /* + It is the temporary symbol table to which names go into when scope is unknown + */ SymbolTable* _TEMPST=new SymbolTable(); + /* + st points to the current symbol table we're adding to + */ SymbolTable* st=_TEMPST; + /* + pointer to the global symbol table that has all the function names + */ extern SymbolTable* _GLOBST; extern char* yytext; using namespace quad; - //int offset=0; + /* + generates a tempory variable in symboltable + */ inline void GENTEMP(Fields* &f){ f=SymbolTable::gentemp(*st);} + + /* + Update functions has all overloads + update does the following + with the type specified, get the size of the symbol and initailize its offset + */ + inline void UPDATE(Fields* f){ st->update(f,f->type); } @@ -25,19 +47,52 @@ inline void UPDATE(Fields* f1,Fields* f2){ st->update(f1,f2); } + + /* + It provides the index of next emmited quad + */ inline int nextInst(){ return quadArray.size(); } + /* + Emit the last added quad + */ inline void EMIT(){ Quad::emit(quadArray[(int)quadArray.size()-1]); } + /* + explained at definition + */ Fields* checkTypesNAssign(Fields* f1,Fields* f2); - + /* + explained at definition + */ inline void getValueNBackpatch(Fields* f); + /* + It stores the type so that all idenitifiers declared using , operator + will be assigned with this type at the end + */ int _GLOBALTYPE; + /* + explained at definition + */ Fields* changeTypeNEmit(Fields* f1,Fields* f2,int op); + /* + explained at definition + */ pair changeTypeNReturn(Fields* f1,Fields* f2); + /* + explained at definition + */ void int2Bool(Fields* f); + /* + If function return value is found then it is set to 1 + */ + int funcRetSet=0; + /* + stores the return type of current function + */ + Type funcRetType; %} %union{ @@ -51,7 +106,6 @@ vector* sentryList; } - %token IDENTIFIER STRING_LITERAL SIZEOF INTEGER_CONSTANT FLOATING_CONSTANT CHARACTER_CONSTANT %token PTR_OP INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP %token AND_OP OR_OP MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN @@ -69,21 +123,31 @@ - - +/* + sentry--> pointer to a field in ST +*/ %type id_parser CONSTANT STRING_LITERAL %type primary_expression postfix_expression unary_expression cast_expression multiplicative_expression additive_expression shift_expression %type relational_expression equality_expression and_expression exclusive_or_expression inclusive_or_expression %type expression initializer init_declarator direct_declarator declarator logical_or_expression - logical_and_expression conditional_expression assignment_expression constant_expression changeBoolTemp logicalTempRule boolExpression + logical_and_expression conditional_expression assignment_expression constant_expression changeBoolTemp boolExpression boolExpressionStatement function_definition array_expression + %type type_specifier declaration_specifiers unary_operator %type pointer +/* + sentryList-->list of pointers to fields in ST +*/ %type identifier_list parameter_list argument_expression_list %type MIndex relop + +/* + This is the type to store indices of dangling gotos in quads +*/ %type NList statement compound_statement expression_statement selection_statement iteration_statement block_item block_item_list - + +%nonassoc HIGH_PREC %% @@ -125,21 +189,31 @@ init_declarator $$->type.push_back(ii(_GLOBALTYPE,0)); UPDATE($$); Fields* f=checkTypesNAssign($$,$3); - quadArray.push_back(Quad($$->name,f->name)); + + if($3->isBoolExp){ + backpatch($3->tl,nextInst()); + quadArray.push_back(Quad($1->name,"1")); + quadArray.push_back(Quad(QGOTO,nextInst()+2)); + backpatch($3->fl,nextInst()); + quadArray.push_back(Quad($1->name,"0")); + } + else + quadArray.push_back(Quad($1->name,f->name)); + $$=$1; } ; type_specifier - : VOID {_GLOBALTYPE=voidT;$$=voidT;} - | CHAR {_GLOBALTYPE=charT;$$=charT;} - | INT {_GLOBALTYPE=intT;$$=intT;} - | DOUBLE {_GLOBALTYPE=doubleT;$$=doubleT;} - | SIGNED {_GLOBALTYPE=intT;$$=intT;} - | UNSIGNED {_GLOBALTYPE=intT;$$=intT;} - | COMPLEX {_GLOBALTYPE=intT;$$=intT;} - | IMAGINARY {_GLOBALTYPE=intT;$$=intT;} + : VOID {_GLOBALTYPE=voidT;$$=voidT;funcRetSet=(funcRetSet==0)?$$:funcRetSet;} + | CHAR {_GLOBALTYPE=charT;$$=charT;funcRetSet=(funcRetSet==0)?$$:funcRetSet;} + | INT {_GLOBALTYPE=intT;$$=intT;funcRetSet=(funcRetSet==0)?$$:funcRetSet;} + | DOUBLE {_GLOBALTYPE=doubleT;$$=doubleT;funcRetSet=(funcRetSet==0)?$$:funcRetSet;} + | SIGNED {_GLOBALTYPE=intT;$$=intT;funcRetSet=(funcRetSet==0)?$$:funcRetSet;} + | UNSIGNED {_GLOBALTYPE=intT;$$=intT;funcRetSet=(funcRetSet==0)?$$:funcRetSet;} + | COMPLEX {_GLOBALTYPE=intT;$$=intT;funcRetSet=(funcRetSet==0)?$$:funcRetSet;} + | IMAGINARY {_GLOBALTYPE=intT;$$=intT;funcRetSet=(funcRetSet==0)?$$:funcRetSet;} ; @@ -157,7 +231,8 @@ declarator : pointer direct_declarator { $$=$2; - $$->type.push_back(ii(pointerT,$1.second)); + for(int i=0;i<$1.second;i++) + $$->type.push_back(ii(pointerT,0)); } | direct_declarator { @@ -181,12 +256,21 @@ direct_declarator } | direct_declarator '(' parameter_type_list ')' { - + funcRetType=$1->type; + funcRetType.push_back(ii(funcRetSet,0)); + Fields* f=_GLOBST->lookup($1->name); + UPDATE(f,funcRetType); + f->nestedTable=st; } | direct_declarator '(' identifier_list ')' | direct_declarator '(' ')' { st=new SymbolTable(); + funcRetType=$1->type; + funcRetType.push_back(ii(funcRetSet,0)); + Fields* f=_GLOBST->lookup($1->name); + UPDATE(f,funcRetType); + f->nestedTable=st; } ; @@ -221,6 +305,7 @@ parameter_list int s=f1.type.size(); f->type=f1.type; f->type.push_back(ii($1,0)); + f->parNum=st->paramNum; UPDATE(f); st->paramNum++; } @@ -228,6 +313,7 @@ parameter_list { $4->type.push_back(ii($3,0)); UPDATE($4); + $4->parNum=st->paramNum; st->paramNum++; } ; @@ -273,9 +359,26 @@ initializer +/* + this is to emit a goto while reducing and maintiain the index of + that quad in the list it has as attribute +*/ +NList + : %prec HIGH_PREC + { + $$=new vi(); + $$->push_back(nextInst()); + quadArray.push_back(Quad(QGOTO,"...",0)); + } + ; - - +/* +Index of the quad generated at MIndex will be stored when reducing this. +this is used for backpatching +*/ +MIndex + :{$$=nextInst();} + ; @@ -284,13 +387,13 @@ initializer statement : compound_statement {$$=$1;} | expression_statement{$$=$1;} - | selection_statement {$$=$1;printf("selection_statement");} + | selection_statement {$$=$1;} | iteration_statement { $$=$1; - printf("iteration_statement"); + } - | jump_statement + | jump_statement {$$=new vi();} ; @@ -332,18 +435,19 @@ expression_statement ; selection_statement - : IF '(' boolExpression ')' MIndex statement + :IF '(' boolExpression ')' MIndex statement %prec "then" { backpatch($3->tl,$5); $$=$6; - *$$=merge($3->fl,*$$); - } + *$$=merge($3->fl,*$$); + } | IF '(' boolExpression ')' MIndex statement NList ELSE MIndex statement { + $$=$10; backpatch($3->tl,$5); backpatch($3->fl,$9); vi temp=merge(*$6,*$7); - *$$=merge($3->fl,*$10); + *$$=merge(temp,*$10); } ; @@ -374,22 +478,36 @@ iteration_statement backpatch(*$8,$4); *$$=$5->fl; } - + | FOR '(' expression_statement MIndex boolExpressionStatement ')' + MIndex statement + { + backpatch(*$8,$4); + quadArray.push_back(Quad(QGOTO,$4)); + backpatch($5->tl,$7); + $$=new vi(); + *$$=$5->fl; + } + ; jump_statement : RETURN ';' + { + if(funcRetType.size()>0&&funcRetType[0].first==voidT) + quadArray.push_back(Quad(QRETURN_NULL,0)); + else + throw "return type not same"; + } | RETURN expression ';' + { + Fields*f;GENTEMP(f);f->type=funcRetType;UPDATE(f); + f= checkTypesNAssign(f,$2); + quadArray.push_back(Quad(QRETURN,f->name,"")); + } ; -NList - :{ - $$=new vi(); - $$->push_back(nextInst()); - quadArray.push_back(Quad(QGOTO,"...",0)); - } - ; + boolExpressionStatement : ';' { @@ -417,7 +535,7 @@ translation_unit ; external_declaration - : function_definition {printf(" function_definition ");} + : function_definition {} ; function_definition @@ -428,11 +546,15 @@ function_definition $$->type.push_back(ii($1,0)); _GLOBST->update($$,$$->type); $$->nestedTable=st; + printf("\n\n\tSymbolTable of function %s",$2->name.c_str()); st->print(); _TEMPST->clearTable(); st=_TEMPST; backpatch(*$4,nextInst()); - quadArray.push_back(Quad("end of function","end of function")); + //adds a default return at the end of every function + quadArray.push_back(Quad(QRETURN_NULL,0)); + funcRetType.clear(); + funcRetSet=0; } | declaration_specifiers declarator compound_statement @@ -442,12 +564,15 @@ function_definition $$->type.push_back(ii($1,0)); _GLOBST->update($$,$$->type); $$->nestedTable=st; + printf("\n\n\tSymbolTable of function %s",$2->name.c_str()); st->print(); _TEMPST->clearTable(); st=_TEMPST; backpatch(*$3,nextInst()); - quadArray.push_back(Quad("end of function","end of function")); - + //adds a default return at the end of every function + quadArray.push_back(Quad(QRETURN_NULL,0)); + funcRetType.clear(); + funcRetSet=0; } ; @@ -509,56 +634,56 @@ primary_expression ; -array_expression - :primary_expression '[' expression ']' - { - if($$->type[0].first!=arrayT) - throw "not an arrayT"; - GENTEMP($$); - $$->isArray=true; - vii temp($1->type.begin()+1,$1->type.end()); - int s=getSize(temp); - $$->type=temp; - UPDATE($$); - - char word[50]; - sprintf(word,"%d",s); - Fields *&f1=$$->arrSize; - GENTEMP(f1); - f1->type.push_back(ii(intT,0)); - UPDATE(f1); - - quadArray.push_back(Quad('*',f1->name,$3->name,word)); - $$->arrayBase=$1; - } - | array_expression '[' expression ']' - { - if($$->type[0].first!=arrayT) - throw "not an arrayT"; - GENTEMP($$); - $$->isArray=true; - vii temp($1->type.begin()+1,$1->type.end()); - int s=getSize(temp); - $$->type=temp; - UPDATE($$); - - char word[50];sprintf(word,"%d",s); - Fields *f1; - GENTEMP(f1); - GENTEMP($$->arrSize); - $$->arrSize->type=$1->arrSize->type; - UPDATE($$->arrSize); - - f1->type.push_back(ii(intT,0)); - UPDATE(f1); - - quadArray.push_back(Quad('*',f1->name,$3->name,word)); - $$->arrayBase=$1->arrayBase; - - quadArray.push_back(Quad('+',$$->arrSize->name, - $1->arrSize->name,f1->name)); - } - ; + array_expression + :primary_expression '[' expression ']' + { + if($$->type[0].first!=arrayT&&$$->type[0].first!=pointerT) + throw "not an arrayT and pointerT"; + GENTEMP($$); + $$->isArray=true; + vii temp($1->type.begin()+1,$1->type.end()); + int s=getSize(temp); + $$->type=temp; + UPDATE($$); + + char word[50]; + sprintf(word,"%d",s); + Fields *&f1=$$->arrSize; + GENTEMP(f1); + f1->type.push_back(ii(intT,0)); + UPDATE(f1); + + quadArray.push_back(Quad('*',f1->name,$3->name,word)); + $$->arrayBase=$1; + } + | array_expression '[' expression ']' + { + if($$->type[0].first!=arrayT&&$$->type[0].first!=pointerT) + throw "not an arrayT and pointerT"; + GENTEMP($$); + $$->isArray=true; + vii temp($1->type.begin()+1,$1->type.end()); + int s=getSize(temp); + $$->type=temp; + UPDATE($$); + + char word[50];sprintf(word,"%d",s); + Fields *f1; + GENTEMP(f1); + GENTEMP($$->arrSize); + $$->arrSize->type=$1->arrSize->type; + UPDATE($$->arrSize); + + f1->type.push_back(ii(intT,0)); + UPDATE(f1); + + quadArray.push_back(Quad('*',f1->name,$3->name,word)); + $$->arrayBase=$1->arrayBase; + + quadArray.push_back(Quad('+',$$->arrSize->name, + $1->arrSize->name,f1->name)); + } + ; postfix_expression : primary_expression {$$=$1;} @@ -675,21 +800,23 @@ unary_expression break; case '*': { - if($2->type[0].first!=pointerT) + if($2->type[0].first!=pointerT&&$2->type[0].first!=arrayT) throw "not a pointer"; vii temp($2->type.begin()+1,$2->type.end()); $$->type=temp; UPDATE($$); + $$->isPointer=true; + $$->arrayBase=$2; quadArray.push_back(Quad(QPOINTER,$$->name,$2->name)); break; } case '&': if($2->type.size()>0&&$2->type[0].first==pointerT){ $$->type=$2->type; - $$->type[0].second++; + $$->type.push_back(ii(pointerT,0)); } else { - $$->type.push_back(ii(pointerT,1)); + $$->type.push_back(ii(pointerT,0)); $$->type.insert($$->type.end(),$2->type.begin(), $2->type.end()); } @@ -698,7 +825,6 @@ unary_expression quadArray.push_back(Quad(QADDR,$$->name,$2->name)); break; default: - printf("default %c %d\n",$1,$1); throw "error in processing unary operators"; } } @@ -716,10 +842,10 @@ unary_expression ; unary_operator - : '&' {$$=yytext[0];} - | '*' {$$=yytext[0];} - | '+' {$$=yytext[0];} - | '-' {$$=yytext[0];} + : '&' %prec UAND {$$=yytext[0];} + | '*' %prec USTAR {$$=yytext[0];} + | '+' %prec UPLUS {$$=yytext[0];} + | '-' %prec UMINUS {$$=yytext[0];} | '~' {$$=yytext[0];} ; @@ -729,7 +855,7 @@ cast_expression ; multiplicative_expression - : cast_expression + : cast_expression %prec LEAST_PREC { $$=$1; } @@ -896,31 +1022,24 @@ inclusive_or_expression logical_and_expression : inclusive_or_expression{$$=$1;} - | logicalTempRule - ; - -logicalTempRule - : logicalTempRule AND_OP MIndex changeBoolTemp + | logical_and_expression NList AND_OP MIndex inclusive_or_expression { - GENTEMP($$); - $$->type.push_back(ii(intT,0)); - UPDATE($$); - - if($1->isBoolExp==false) - {int2Bool($1);$3++;} - if($4->isBoolExp==false) - int2Bool($4); + if(!$5->isBoolExp) + int2Bool($5); + + if(!$1->isBoolExp){ + backpatch(*$2,nextInst()); + int2Bool($1); + } + backpatch($1->tl,$4); $$->isBoolExp=true; - - - backpatch($1->tl,$3); - $$->fl=merge($1->fl,$4->fl); - $$->tl=$4->tl; - getValueNBackpatch($$); + $$->tl=$5->tl; + $$->fl=merge($1->fl,$5->fl); } - | changeBoolTemp{$$=$1;} ; + + changeBoolTemp :inclusive_or_expression { @@ -932,43 +1051,47 @@ changeBoolTemp logical_or_expression : logical_and_expression{$$=$1;} - | logical_or_expression OR_OP MIndex logical_and_expression + | logical_or_expression NList OR_OP MIndex logical_and_expression { + if(!$5->isBoolExp) + int2Bool($5); - GENTEMP($$); - $$->type.push_back(ii(intT,0)); - UPDATE($$); + if(!$1->isBoolExp){ + backpatch(*$2,nextInst()); + int2Bool($1); + } + backpatch($1->fl,$4); $$->isBoolExp=true; - - backpatch($1->fl,$3); - $$->tl=merge($1->tl,$4->tl); - $$->fl=$4->fl; - - - getValueNBackpatch($$); + $$->tl=merge($1->tl,$5->tl); + $$->fl=$5->fl; } ; conditional_expression : logical_or_expression{$$=$1;} - | logical_or_expression '?' MIndex expression NList ':' + | logical_or_expression NList '?' MIndex expression NList ':' MIndex conditional_expression { GENTEMP($$); - UPDATE($$,$4->type); + UPDATE($$,$5->type); - quadArray.push_back(Quad($$->name,$8->name)); + quadArray.push_back(Quad($$->name,$9->name)); vi I=makelist(nextInst()); quadArray.push_back(Quad(QGOTO,"...",0)); - backpatch(*$5,nextInst()); - quadArray.push_back(Quad($$->name,$4->name)); + backpatch(*$6,nextInst()); + quadArray.push_back(Quad($$->name,$5->name)); I=merge(I,makelist(nextInst())); quadArray.push_back(Quad(QGOTO,"...",0)); - - backpatch($1->tl,$3); - backpatch($1->fl,$7); + if(!$1->isBoolExp) + { + backpatch(*$2,nextInst()); + int2Bool($1); + } + + backpatch($1->tl,$4); + backpatch($1->fl,$8); backpatch(I,nextInst()); } ; @@ -985,11 +1108,24 @@ assignment_expression //quadArray.push_back(Quad($1->name,$3->name)); Fields* f=checkTypesNAssign($1,$3); + if($1->isArray) { quadArray.push_back(Quad(QARRDEREF,$1->arrayBase->name, $1->arrSize->name,f->name)); } + else if($1->isPointer) + { + quadArray.push_back(Quad(QPOINTERDER,$1->arrayBase->name, + f->name)); + } + else if($3->isBoolExp){ + backpatch($3->tl,nextInst()); + quadArray.push_back(Quad($1->name,"1")); + quadArray.push_back(Quad(QGOTO,nextInst()+2)); + backpatch($3->fl,nextInst()); + quadArray.push_back(Quad($1->name,"0")); + } else quadArray.push_back(Quad($1->name,f->name)); $$=$1; @@ -1010,11 +1146,6 @@ constant_expression ; -MIndex - :{$$=nextInst();} - ; - - @@ -1070,7 +1201,8 @@ CONSTANT UPDATE($$); char *temp=strdup(yytext); temp++; - temp[strlen(temp)-1]='\0'; + //temp[strlen(temp)-1]='\0'; + sprintf(temp,"%d",temp[0]); quadArray.push_back(Quad($$->name,temp)); } @@ -1161,6 +1293,18 @@ Fields* int2char(Fields* f) quadArray.push_back(Quad(res->name,val+string(f->name)+end)); return res; } + + +/* + This is for binary operations. checks the types and converts + one of them to other suitable type. + int,double ==> double,dpuble + int,char ==> int,int + char,double -->double,double + and so on + It returns the temporary of t=f1 op f2 + after chnaging f2 or f1 to suitable types +*/ Fields* changeTypeNEmit(Fields* f1,Fields* f2,int op) { if(f1->type.size()==0||f2->type.size()==0) @@ -1168,8 +1312,8 @@ Fields* changeTypeNEmit(Fields* f1,Fields* f2,int op) if(f1->type.size()>1||f2->type.size()>1) throw "invalid type Changing"; int check=typeCheck(f1->type,f2->type); - printf("\n"); - f1->print();f2->print(); + + //f1->print();f2->print(); Fields* arg1=f1,*arg2=f2,*res; GENTEMP(res); @@ -1217,6 +1361,10 @@ Fields* changeTypeNEmit(Fields* f1,Fields* f2,int op) } + +/* + same as above but returns both of them +*/ pair changeTypeNReturn(Fields* f1,Fields* f2) { if(f1->type.size()>1||f2->type.size()>1) @@ -1254,6 +1402,9 @@ pair changeTypeNReturn(Fields* f1,Fields* f2) return make_pair(arg1,arg2); } +/* + converts an integer expression to a boolExp with true and false lists +*/ void int2Bool(Fields* f) { Fields* arg=f; @@ -1285,6 +1436,12 @@ inline void getValueNBackpatch(Fields* f) //quadArray.push_back(Quad(QGOTO,"...",0)); } + +/* + This is for assigning types i.e f1=f2; + it converts f2 to suitable type and then assigns the value + to f1 +*/ Fields* checkTypesNAssign(Fields* f1,Fields* f2) { bool res=typeCheck(f1->type,f2->type); @@ -1296,6 +1453,7 @@ Fields* checkTypesNAssign(Fields* f1,Fields* f2) int t1=f1->type[0].first,t2=f2->type[0].first; Fields *temp; + if(t1==intT&&t2==doubleT) temp=double2int(f2); else if(t1==intT&&t2==charT) diff --git a/ass 5/ass5_12CS10037_test.c b/ass 5/ass5_12CS10037_test.c index 7503c70..1280108 100644 --- a/ass 5/ass5_12CS10037_test.c +++ b/ass 5/ass5_12CS10037_test.c @@ -1,43 +1,113 @@ -int main() +// This is the test file to test various compilers.You can add expressions here +// at check the output after doing make + +//By default it has KMP algorithm implemented and by running this function shows +// the 3-address quads for KMP algorithm + +//I've implemented almost all the things given except "function declarations" + + + + +int strlen(char* s) +{ + int i; + for ( i = 0; s[i]!='\0'; ++i){ + + } + return i; +} + +void computeLPSArray(char *pat, int M, int *lps) { + int len = 0; // lenght of the previous longest prefix suffix + int i; + + lps[0] = 0; // lps[0] is always 0 + i = 1; + + // the loop calculates lps[i] for i = 1 to M-1 + while(i < M) + { + if(pat[i] == pat[len]) + { + len++; + lps[i] = len; + i++; + } + else // (pat[i] != pat[len]) + { + if( len != 0 ) + { + // This is tricky. Consider the example AAACAAAA and i = 7. + len = lps[len-1]; + + // Also, note that we do not increment i here + } + else // if (len == 0) + { + lps[i] = 0; + i++; + } + } + } +} - //int i=0,a[10][10],j; - - // for(i=0;i<10;i++) - // { - // for(j=0;j<10;j++) - // a[i][j]=a[i][j]*200; - // j=0; - // if(j==1) - // while(i& v1,const ListType& v2){ quad’s on the list in v */ -void backpatch(const ListType &v,int index); +void backpatch(ListType &v,int index); /* A global function to check if E1 & E2 have same types diff --git a/ass 5/decl_test.c b/ass 5/decl_test.c new file mode 100644 index 0000000..506513d --- /dev/null +++ b/ass 5/decl_test.c @@ -0,0 +1,56 @@ + + +int main() +{ + + //all basic types + int i1=5,i2=34,i3=45.67; + char c1='a';//ASCII value of char is taken in quad + double d1=3,d2=56.7*9,d3=45.6+9; + + //arrays + int ia1[100],ia2[100]; + + //the shift is represeted as int value in quad + ia1[0]=i1*i2<<3; + + //pointers I've supported all possible types of pointer and array combination declarations + + // NOTE--> I've not suported pointer arithmetic but using pointers as arrays is supported + int *x=ia2; + + x[5]=(d1+d3)/i2; + + x=&i2; + *x=i2; + + //complex types + int *a[100][200]; //2-D array of int pointers + + a[10][20]=x; + + int (*aa)[100]; //pointer to an integer array of size 100 + + int **aa2=aa; + + + double da[10][10][10][10]; + double *p1=&da[0][0][0][0],**p2,***p3; + + double (*p4)[10][10][10]=da; + p3=da[9]; + p2=da[8][8]; + + int i,j,k,l; + for (i = 0; i < 10; ++i){ + for ( j = 0; j < 10; ++j){ + for ( k = 0; k < 10; ++k){ + for ( l = 0; l < 10; ++l){ + da[i][j][k][l]=i1*i2+3; + } + } + } + } + + return 0; +} \ No newline at end of file diff --git a/ass 5/decl_test_output b/ass 5/decl_test_output new file mode 100644 index 0000000..3393612 --- /dev/null +++ b/ass 5/decl_test_output @@ -0,0 +1,291 @@ + + + SymbolTable of function main +--------------------SymbolTable--------------------------- +no of params=0 + +1 __t0 4 4 (int 0) +2 i1 4 8 (int 0) +3 __t1 4 12 (int 0) +4 i2 4 16 (int 0) +5 __t2 8 20 (double 0) +6 i3 4 28 (int 0) +7 __t3 8 32 (double 0) +8 __t4 1 40 (char 0) +9 c1 1 41 (char 0) +10 __t5 4 42 (int 0) +11 d1 8 46 (double 0) +12 __t6 8 54 (double 0) +13 __t7 8 62 (double 0) +14 __t8 4 70 (int 0) +15 __t10 8 74 (double 0) +16 __t9 8 82 (double 0) +17 d2 8 90 (double 0) +18 __t11 8 98 (double 0) +19 __t12 4 106 (int 0) +20 __t14 8 110 (double 0) +21 __t13 8 118 (double 0) +22 d3 8 126 (double 0) +23 __t15 4 134 (int 0) +24 ia1 400 138 (array 100) (int 0) +25 __t16 4 538 (int 0) +26 ia2 400 542 (array 100) (int 0) +27 __t17 4 942 (int 0) +28 __t18 4 946 (int 0) +29 __t19 4 950 (int 0) +30 __t20 4 954 (int 0) +31 __t21 4 958 (int 0) +32 __t22 4 962 (int 0) +33 __t23 4 966 (int 0) +34 x 4 970 (pointer 0) (int 0) +35 __t24 4 974 (int 0) +36 __t25 4 978 (int 0) +37 __t26 4 982 (int 0) +38 __t27 4 986 (int 0) +39 __t28 8 990 (double 0) +40 __t30 8 998 (double 0) +41 __t29 8 1006 (double 0) +42 __t31 8 1014 (double 0) +43 __t32 4 1022 (pointer 0) (int 0) +44 __t33 4 1026 (int 0) +45 __t34 4 1030 (int 0) +46 __t35 4 1034 (int 0) +47 a 80000 1038 (array 100) (array 200) (pointer 0) (int 0) +48 __t36 4 81038 (int 0) +49 __t37 800 81042 (array 200) (pointer 0) (int 0) +50 __t38 4 81842 (int 0) +51 __t39 4 81846 (int 0) +52 __t40 4 81850 (pointer 0) (int 0) +53 __t42 4 81854 (int 0) +54 __t41 4 81858 (int 0) +55 __t43 4 81862 (pointer 0) (int 0) +56 __t44 4 81866 (int 0) +57 aa 4 81870 (pointer 0) (array 100) (int 0) +58 aa2 4 81874 (pointer 0) (pointer 0) (int 0) +59 __t45 4 81878 (int 0) +60 __t46 4 81882 (int 0) +61 __t47 4 81886 (int 0) +62 __t48 4 81890 (int 0) +63 da 80000 81894 (array 10) (array 10) (array 10) (array 10) (double 0) +64 __t49 4 161894 (int 0) +65 __t50 8000 161898 (array 10) (array 10) (array 10) (double 0) +66 __t51 4 169898 (int 0) +67 __t52 4 169902 (int 0) +68 __t53 800 169906 (array 10) (array 10) (double 0) +69 __t55 4 170706 (int 0) +70 __t54 4 170710 (int 0) +71 __t56 4 170714 (int 0) +72 __t57 80 170718 (array 10) (double 0) +73 __t59 4 170798 (int 0) +74 __t58 4 170802 (int 0) +75 __t60 4 170806 (int 0) +76 __t61 8 170810 (double 0) +77 __t63 4 170818 (int 0) +78 __t62 4 170822 (int 0) +79 __t64 8 170826 (double 0) +80 __t65 4 170834 (pointer 0) (double 0) +81 p1 4 170838 (pointer 0) (double 0) +82 p2 4 170842 (pointer 0) (pointer 0) (double 0) +83 p3 4 170846 (pointer 0) (pointer 0) (pointer 0) (double 0) +84 __t66 4 170850 (int 0) +85 __t67 4 170854 (int 0) +86 __t68 4 170858 (int 0) +87 p4 4 170862 (pointer 0) (array 10) (array 10) (array 10) (double 0) +88 __t69 4 170866 (int 0) +89 __t70 8000 170870 (array 10) (array 10) (array 10) (double 0) +90 __t71 4 178870 (int 0) +91 __t72 8000 178874 (array 10) (array 10) (array 10) (double 0) +92 __t73 4 186874 (int 0) +93 __t74 8000 186878 (array 10) (array 10) (array 10) (double 0) +94 __t75 4 194878 (int 0) +95 __t76 4 194882 (int 0) +96 __t77 800 194886 (array 10) (array 10) (double 0) +97 __t79 4 195686 (int 0) +98 __t78 4 195690 (int 0) +99 __t80 800 195694 (array 10) (array 10) (double 0) +100 i 4 196494 (int 0) +101 j 4 196498 (int 0) +102 k 4 196502 (int 0) +103 l 4 196506 (int 0) +104 __t81 4 196510 (int 0) +105 __t82 4 196514 (int 0) +106 __t83 4 196518 (int 0) +107 __t84 4 196522 (int 0) +108 __t85 4 196526 (int 0) +109 __t86 4 196530 (int 0) +110 __t87 4 196534 (int 0) +111 __t88 4 196538 (int 0) +112 __t89 4 196542 (int 0) +113 __t90 4 196546 (int 0) +114 __t91 4 196550 (int 0) +115 __t92 4 196554 (int 0) +116 __t93 4 196558 (int 0) +117 __t94 4 196562 (int 0) +118 __t95 4 196566 (int 0) +119 __t96 4 196570 (int 0) +120 __t97 8000 196574 (array 10) (array 10) (array 10) (double 0) +121 __t98 4 204574 (int 0) +122 __t99 800 204578 (array 10) (array 10) (double 0) +123 __t101 4 205378 (int 0) +124 __t100 4 205382 (int 0) +125 __t102 80 205386 (array 10) (double 0) +126 __t104 4 205466 (int 0) +127 __t103 4 205470 (int 0) +128 __t105 8 205474 (double 0) +129 __t107 4 205482 (int 0) +130 __t106 4 205486 (int 0) +131 __t108 8 205490 (double 0) +132 __t109 4 205498 (int 0) +133 __t110 4 205502 (int 0) +134 __t111 4 205506 (int 0) +135 __t112 8 205510 (double 0) +136 __t113 4 205518 (int 0) +137 __t114 4 205522 (int 0) +----------------------------------------------------------- +Global SymbolTable + +--------------------SymbolTable--------------------------- +no of params=0 + +1 main 4 0 (int 0) +----------------------------------------------------------- + 0 : __t0 = 5 + 1 : i1 = __t0 + 2 : __t1 = 34 + 3 : i2 = __t1 + 4 : __t2 = 45.67 + 5 : __t3 = double2int( __t2 ) + 6 : i3 = __t3 + 7 : __t4 = 97 + 8 : c1 = __t4 + 9 : __t5 = 3 + 10 : __t6 = int2double( __t5 ) + 11 : d1 = __t6 + 12 : __t7 = 56.7 + 13 : __t8 = 9 + 14 : __t10 = int2double( __t8 ) + 15 : __t9 = __t7 * __t10 + 16 : d2 = __t9 + 17 : __t11 = 45.6 + 18 : __t12 = 9 + 19 : __t14 = int2double( __t12 ) + 20 : __t13 = __t11 + __t14 + 21 : d3 = __t13 + 22 : __t15 = 100 + 23 : __t16 = 100 + 24 : __t17 = 0 + 25 : __t19 = __t17 * 4 + 26 : __t20 = ia1[__t19] + 27 : __t21 = i1 * i2 + 28 : __t22 = 3 + 29 : __t23 = __t21 << __t22 + 30 : ia1[__t19] = __t23 + 31 : x = ia2 + 32 : __t24 = 5 + 33 : __t26 = __t24 * 4 + 34 : __t27 = x[__t26] + 35 : __t28 = d1 + d3 + 36 : __t30 = int2double( i2 ) + 37 : __t29 = __t28 / __t30 + 38 : __t31 = double2int( __t29 ) + 39 : x[__t26] = __t31 + 40 : __t32=&i2 + 41 : x = __t32 + 42 : __t33=*x + 43 : *x=i2 + 44 : __t34 = 100 + 45 : __t35 = 200 + 46 : __t36 = 10 + 47 : __t38 = __t36 * 800 + 48 : __t39 = 20 + 49 : __t41 = __t39 * 4 + 50 : __t42 = __t38 + __t41 + 51 : __t43 = a[__t42] + 52 : a[__t42] = x + 53 : __t44 = 100 + 54 : aa2 = aa + 55 : __t45 = 10 + 56 : __t46 = 10 + 57 : __t47 = 10 + 58 : __t48 = 10 + 59 : __t49 = 0 + 60 : __t51 = __t49 * 8000 + 61 : __t52 = 0 + 62 : __t54 = __t52 * 800 + 63 : __t55 = __t51 + __t54 + 64 : __t56 = 0 + 65 : __t58 = __t56 * 80 + 66 : __t59 = __t55 + __t58 + 67 : __t60 = 0 + 68 : __t62 = __t60 * 8 + 69 : __t63 = __t59 + __t62 + 70 : __t64 = da[__t63] + 71 : __t65=&__t64 + 72 : p1 = __t65 + 73 : __t66 = 10 + 74 : __t67 = 10 + 75 : __t68 = 10 + 76 : p4 = da + 77 : __t69 = 9 + 78 : __t71 = __t69 * 8000 + 79 : __t72 = da[__t71] + 80 : p3 = __t72 + 81 : __t73 = 8 + 82 : __t75 = __t73 * 8000 + 83 : __t76 = 8 + 84 : __t78 = __t76 * 800 + 85 : __t79 = __t75 + __t78 + 86 : __t80 = da[__t79] + 87 : p2 = __t80 + 88 : __t81 = 0 + 89 : i = __t81 + 90 : __t82 = 10 + 91 : if i < __t82 goto 96 + 92 : goto 137 + 93 : i = i + 1 + 94 : __t84 = i + 95 : goto 90 + 96 : __t85 = 0 + 97 : j = __t85 + 98 : __t86 = 10 + 99 : if j < __t86 goto 104 +100 : goto 136 +101 : j = j + 1 +102 : __t88 = j +103 : goto 98 +104 : __t89 = 0 +105 : k = __t89 +106 : __t90 = 10 +107 : if k < __t90 goto 112 +108 : goto 135 +109 : k = k + 1 +110 : __t92 = k +111 : goto 106 +112 : __t93 = 0 +113 : l = __t93 +114 : __t94 = 10 +115 : if l < __t94 goto 120 +116 : goto 134 +117 : l = l + 1 +118 : __t96 = l +119 : goto 114 +120 : __t98 = i * 8000 +121 : __t100 = j * 800 +122 : __t101 = __t98 + __t100 +123 : __t103 = k * 80 +124 : __t104 = __t101 + __t103 +125 : __t106 = l * 8 +126 : __t107 = __t104 + __t106 +127 : __t108 = da[__t107] +128 : __t109 = i1 * i2 +129 : __t110 = 3 +130 : __t111 = __t109 + __t110 +131 : __t112 = int2double( __t111 ) +132 : da[__t107] = __t112 +133 : goto 117 +134 : goto 109 +135 : goto 101 +136 : goto 93 +137 : __t113 = 0 +138 : return __t113 +139 : return diff --git a/ass 5/divide_conquer.c b/ass 5/divide_conquer.c new file mode 100644 index 0000000..e72ffe0 --- /dev/null +++ b/ass 5/divide_conquer.c @@ -0,0 +1,65 @@ + + + // This function returns median of ar1[] and ar2[]. + // Assumptions in this function: + // Both ar1[] and ar2[] are sorted arrays + // Both have n elements +int getMedian(int *ar1, int *ar2, int n) +{ + int i = 0; + int j = 0; + int count; + int m1 = -1, m2 = -1; + + + for (count = 0; count <= n; count++) + { + // Below is to handle case where all elements of ar1[] are + // smaller than smallest(or first) element of ar2[] + if (i == n) + { + m1 = m2; + m2 = ar2[0]; + //break; removed break statemnet to avoid syntax error + } + + // Below is to handle case where all elements of ar2[] are + // smaller than smallest(or first) element of ar1[] + else if (j == n) + { + m1 = m2; + m2 = ar1[0]; + // break;removed break statemnet to avoid syntax error + } + + if (ar1[i] < ar2[j]) + { + m1 = m2; //Store the prev median + m2 = ar1[i]; + i++; + } + else + { + m1 = m2; // Store the prev median + m2 = ar2[j]; + j++; + } + } + + return (m1 + m2)/2; +} + + // Driver program to test above function +int main() +{ + int ar1[100]; + int ar2[100]; + int i; + for ( i = 0; i < 100; ++i){ + ar1[i]=i*7/34; + } + + getMedian(ar1, ar2, 100); + + return 0; +} \ No newline at end of file diff --git a/ass 5/divide_conquer_output b/ass 5/divide_conquer_output new file mode 100644 index 0000000..2f11e20 --- /dev/null +++ b/ass 5/divide_conquer_output @@ -0,0 +1,172 @@ + + + SymbolTable of function getMedian +--------------------SymbolTable--------------------------- +no of params=3 +ar1 ar2 n +1 ar1 4 0 (pointer 0) (int 0) +2 ar2 4 4 (pointer 0) (int 0) +3 n 4 8 (int 0) +4 __t0 4 16 (int 0) +5 i 4 20 (int 0) +6 __t1 4 24 (int 0) +7 j 4 28 (int 0) +8 count 4 32 (int 0) +9 __t2 4 36 (int 0) +10 __t3 4 40 (int 0) +11 m1 4 44 (int 0) +12 __t4 4 48 (int 0) +13 __t5 4 52 (int 0) +14 m2 4 56 (int 0) +15 __t6 4 60 (int 0) +16 __t7 4 64 (int 0) +17 __t8 4 68 (int 0) +18 __t9 4 72 (int 0) +19 __t10 4 76 (int 0) +20 __t11 4 80 (int 0) +21 __t12 4 84 (int 0) +22 __t13 4 88 (int 0) +23 __t14 4 92 (int 0) +24 __t15 4 96 (int 0) +25 __t16 4 100 (int 0) +26 __t17 4 104 (int 0) +27 __t18 4 108 (int 0) +28 __t19 4 112 (int 0) +29 __t20 4 116 (int 0) +30 __t21 4 120 (int 0) +31 __t22 4 124 (int 0) +32 __t23 4 128 (int 0) +33 __t24 4 132 (int 0) +34 __t25 4 136 (int 0) +35 __t26 4 140 (int 0) +36 __t27 4 144 (int 0) +37 __t28 4 148 (int 0) +38 __t29 4 152 (int 0) +39 __t30 4 156 (int 0) +40 __t31 4 160 (int 0) +41 __t32 4 164 (int 0) +42 __t33 4 168 (int 0) +43 __t34 4 172 (int 0) +44 __t35 4 176 (int 0) +45 __t36 4 180 (int 0) +46 __t37 4 184 (int 0) +----------------------------------------------------------- + + + SymbolTable of function main +--------------------SymbolTable--------------------------- +no of params=0 + +1 getMedian 4 0 +2 __t38 4 4 (int 0) +3 ar1 400 8 (array 100) (int 0) +4 __t39 4 408 (int 0) +5 ar2 400 412 (array 100) (int 0) +6 i 4 812 (int 0) +7 __t40 4 816 (int 0) +8 __t41 4 820 (int 0) +9 __t42 4 824 (int 0) +10 __t43 4 828 (int 0) +11 __t44 4 832 (int 0) +12 __t45 4 836 (int 0) +13 __t46 4 840 (int 0) +14 __t47 4 844 (int 0) +15 __t48 4 848 (int 0) +16 __t49 4 852 (int 0) +17 __t50 4 856 (int 0) +18 __t51 4 860 (int 0) +19 __t52 4 864 (int 0) +20 __t53 4 868 (int 0) +21 __t54 4 872 (int 0) +----------------------------------------------------------- +Global SymbolTable + +--------------------SymbolTable--------------------------- +no of params=0 + +1 getMedian 4 0 (int 0) +2 main 4 4 (int 0) +----------------------------------------------------------- + 0 : __t0 = 0 + 1 : i = __t0 + 2 : __t1 = 0 + 3 : j = __t1 + 4 : __t2 = 1 + 5 : __t3 = - __t2 + 6 : m1 = __t3 + 7 : __t4 = 1 + 8 : __t5 = - __t4 + 9 : m2 = __t5 + 10 : __t6 = 0 + 11 : count = __t6 + 12 : if count <= n goto 17 + 13 : goto 52 + 14 : __t8 = count + 15 : count = count + 1 + 16 : goto 12 + 17 : if i = n goto 19 + 18 : goto 25 + 19 : m1 = m2 + 20 : __t10 = 0 + 21 : __t12 = __t10 * 4 + 22 : __t13 = ar2[__t12] + 23 : m2 = __t13 + 24 : goto 32 + 25 : if j = n goto 27 + 26 : goto 32 + 27 : m1 = m2 + 28 : __t15 = 0 + 29 : __t17 = __t15 * 4 + 30 : __t18 = ar1[__t17] + 31 : m2 = __t18 + 32 : __t20 = i * 4 + 33 : __t21 = ar1[__t20] + 34 : __t23 = j * 4 + 35 : __t24 = ar2[__t23] + 36 : if __t21 < __t24 goto 38 + 37 : goto 45 + 38 : m1 = m2 + 39 : __t27 = i * 4 + 40 : __t28 = ar1[__t27] + 41 : m2 = __t28 + 42 : __t29 = i + 43 : i = i + 1 + 44 : goto 51 + 45 : m1 = m2 + 46 : __t31 = j * 4 + 47 : __t32 = ar2[__t31] + 48 : m2 = __t32 + 49 : __t33 = j + 50 : j = j + 1 + 51 : goto 14 + 52 : __t34 = m1 + m2 + 53 : __t35 = 2 + 54 : __t36 = __t34 / __t35 + 55 : return __t36 + 56 : return + 57 : __t38 = 100 + 58 : __t39 = 100 + 59 : __t40 = 0 + 60 : i = __t40 + 61 : __t41 = 100 + 62 : if i < __t41 goto 67 + 63 : goto 75 + 64 : i = i + 1 + 65 : __t43 = i + 66 : goto 61 + 67 : __t45 = i * 4 + 68 : __t46 = ar1[__t45] + 69 : __t47 = 7 + 70 : __t48 = i * __t47 + 71 : __t49 = 34 + 72 : __t50 = __t48 / __t49 + 73 : ar1[__t45] = __t50 + 74 : goto 64 + 75 : __t51 = 100 + 76 : param ar1 + 77 : param ar2 + 78 : param __t51 + 79 : __t52=call getMedian,3 + 80 : __t53 = 0 + 81 : return __t53 + 82 : return diff --git a/ass 5/expres_test.c b/ass 5/expres_test.c new file mode 100644 index 0000000..4884e3a --- /dev/null +++ b/ass 5/expres_test.c @@ -0,0 +1,54 @@ + + +// testing the file for expressions + + +int main() +{ + int a=10; + + int i1,i2,i3; + + //unary operators + i1=a++*++a; + + i1=-i1; + + + //binary + i2=i1%a*a+456+'c'; + i3= i1<<3 + 2>>3; + + double d1,d2,d3; + + d3=d2=d1=i2+i3; //here conversion from int to double takes place + + d2=i2*(34.56+'c')/i2; + + char c1=5; + c1=d2;//double2char + + //testing relational expressions + //every relational expression has a value true(1) or false(0) + + int r1,r2,r3,r4; + r1=i1&&i2; //the value of r1 is 1 if the boolean expression is true else r2=0 + r2= i3<=75&&i3>=6; //there will a goto which is unreachable and not backpatched when + //evaluating this expression + + r3= 1&&3||1&&2; + + r2=(r1==r3)||(1!=2); + + //teranary operator + r1=(r2==r3)?i1*2938:i2*34+98.67; + + r1= (1)?2:3; + + + //if statement + + + + return 0; +} \ No newline at end of file diff --git a/ass 5/expres_test_output b/ass 5/expres_test_output new file mode 100644 index 0000000..a8bfdf0 --- /dev/null +++ b/ass 5/expres_test_output @@ -0,0 +1,205 @@ + + + SymbolTable of function main +--------------------SymbolTable--------------------------- +no of params=0 + +1 __t0 4 4 (int 0) +2 a 4 8 (int 0) +3 i1 4 12 (int 0) +4 i2 4 16 (int 0) +5 i3 4 20 (int 0) +6 __t1 4 24 (int 0) +7 __t2 4 28 (int 0) +8 __t3 4 32 (int 0) +9 __t4 4 36 (int 0) +10 __t5 4 40 (int 0) +11 __t6 4 44 (int 0) +12 __t7 4 48 (int 0) +13 __t8 4 52 (int 0) +14 __t9 1 56 (char 0) +15 __t11 4 57 (int 0) +16 __t10 4 61 (int 0) +17 __t12 4 65 (int 0) +18 __t13 4 69 (int 0) +19 __t14 4 73 (int 0) +20 __t15 4 77 (int 0) +21 __t16 4 81 (int 0) +22 __t17 4 85 (int 0) +23 d1 8 89 (double 0) +24 d2 8 97 (double 0) +25 d3 8 105 (double 0) +26 __t18 4 113 (int 0) +27 __t19 8 117 (double 0) +28 __t20 8 125 (double 0) +29 __t21 1 133 (char 0) +30 __t23 8 134 (double 0) +31 __t22 8 142 (double 0) +32 __t25 8 150 (double 0) +33 __t24 8 158 (double 0) +34 __t27 8 166 (double 0) +35 __t26 8 174 (double 0) +36 __t28 4 182 (int 0) +37 c1 1 186 (char 0) +38 __t29 8 187 (double 0) +39 __t30 8 195 (double 0) +40 r1 4 203 (int 0) +41 r2 4 207 (int 0) +42 r3 4 211 (int 0) +43 r4 4 215 (int 0) +44 __t31 4 219 (int 0) +45 __t32 4 223 (int 0) +46 __t33 4 227 (int 0) +47 __t34 4 231 (int 0) +48 __t35 4 235 (int 0) +49 __t36 4 239 (int 0) +50 __t37 4 243 (int 0) +51 __t38 4 247 (int 0) +52 __t39 4 251 (int 0) +53 __t40 4 255 (int 0) +54 __t41 4 259 (int 0) +55 __t42 4 263 (int 0) +56 __t43 4 267 (int 0) +57 __t44 4 271 (int 0) +58 __t45 4 275 (int 0) +59 __t46 4 279 (int 0) +60 __t47 4 283 (int 0) +61 __t48 8 287 (double 0) +62 __t50 8 295 (double 0) +63 __t49 8 303 (double 0) +64 __t51 4 311 (int 0) +65 __t52 4 315 (int 0) +66 __t53 4 319 (int 0) +67 __t54 4 323 (int 0) +68 __t55 4 327 (int 0) +69 __t56 4 331 (int 0) +70 __t57 4 335 (int 0) +----------------------------------------------------------- +Global SymbolTable + +--------------------SymbolTable--------------------------- +no of params=0 + +1 main 4 0 (int 0) +----------------------------------------------------------- + 0 : __t0 = 10 + 1 : a = __t0 + 2 : __t1 = a + 3 : a = a + 1 + 4 : a = a + 1 + 5 : __t2 = a + 6 : __t3 = __t1 * __t2 + 7 : i1 = __t3 + 8 : __t4 = - i1 + 9 : i1 = __t4 + 10 : __t5 = i1 % a + 11 : __t6 = __t5 * a + 12 : __t7 = 456 + 13 : __t8 = __t6 + __t7 + 14 : __t9 = 99 + 15 : __t11 = char2int( __t9 ) + 16 : __t10 = __t8 + __t11 + 17 : i2 = __t10 + 18 : __t12 = 3 + 19 : __t13 = 2 + 20 : __t14 = __t12 + __t13 + 21 : __t15 = i1 << __t14 + 22 : __t16 = 3 + 23 : __t17 = __t15 >> __t16 + 24 : i3 = __t17 + 25 : __t18 = i2 + i3 + 26 : __t19 = int2double( __t18 ) + 27 : d1 = __t19 + 28 : d2 = d1 + 29 : d3 = d2 + 30 : __t20 = 34.56 + 31 : __t21 = 99 + 32 : __t23 = char2double( __t21 ) + 33 : __t22 = __t20 + __t23 + 34 : __t25 = int2double( i2 ) + 35 : __t24 = __t22 * __t25 + 36 : __t27 = int2double( i2 ) + 37 : __t26 = __t24 / __t27 + 38 : d2 = __t26 + 39 : __t28 = 5 + 40 : __t29 = int2char( __t28 ) + 41 : c1 = __t29 + 42 : __t30 = double2char( d2 ) + 43 : c1 = __t30 + 44 : goto 47 + 45 : if i2 = 0 goto 51 + 46 : goto 49 + 47 : if i1 = 0 goto 51 + 48 : goto 45 + 49 : r1 = 1 + 50 : goto 52 + 51 : r1 = 0 + 52 : __t31 = 75 + 53 : if i3 <= __t31 goto 56 + 54 : goto 61 + 55 : goto ... + 56 : __t33 = 6 + 57 : if i3 >= __t33 goto 59 + 58 : goto 61 + 59 : r2 = 1 + 60 : goto 62 + 61 : r2 = 0 + 62 : __t35 = 1 + 63 : goto 67 + 64 : __t36 = 3 + 65 : if __t36 = 0 goto 70 + 66 : goto 77 + 67 : if __t35 = 0 goto 70 + 68 : goto 64 + 69 : goto ... + 70 : __t37 = 1 + 71 : goto 75 + 72 : __t38 = 2 + 73 : if __t38 = 0 goto 79 + 74 : goto 77 + 75 : if __t37 = 0 goto 79 + 76 : goto 72 + 77 : r3 = 1 + 78 : goto 80 + 79 : r3 = 0 + 80 : if r1 = r3 goto 87 + 81 : goto 83 + 82 : goto ... + 83 : __t40 = 1 + 84 : __t41 = 2 + 85 : if __t40 != __t41 goto 87 + 86 : goto 89 + 87 : r2 = 1 + 88 : goto 90 + 89 : r2 = 0 + 90 : if r2 = r3 goto 93 + 91 : goto 96 + 92 : goto ... + 93 : __t44 = 2938 + 94 : __t45 = i1 * __t44 + 95 : goto 103 + 96 : __t46 = 34 + 97 : __t47 = i2 * __t46 + 98 : __t48 = 98.67 + 99 : __t50 = int2double( __t47 ) +100 : __t49 = __t48 + __t50 +101 : __t51 = __t49 +102 : goto 105 +103 : __t51 = __t45 +104 : goto 105 +105 : r1 = __t51 +106 : __t52 = 1 +107 : goto 115 +108 : __t53 = 2 +109 : goto 113 +110 : __t54 = 3 +111 : __t55 = __t54 +112 : goto 117 +113 : __t55 = __t53 +114 : goto 117 +115 : if __t52 = 0 goto 110 +116 : goto 108 +117 : r1 = __t55 +118 : __t56 = 0 +119 : return __t56 +120 : return diff --git a/ass 5/gcd_test.c b/ass 5/gcd_test.c new file mode 100644 index 0000000..4ca8288 --- /dev/null +++ b/ass 5/gcd_test.c @@ -0,0 +1,34 @@ +int gcd ( int a, int b ) +{ + int c; + while ( a != 0 ) { + c = a; + a = b%a; + b = c; + } + return b; +} + +/* Recursive Standard C Function: Greatest Common Divisor */ +int gcdr ( int a, int b ) +{ + if ( a==0 ) return b; + return gcdr ( b%a, a ); +} + +int gcdv() +{ + return 0; +} + +int main() +{ + int a,b,c; + a = 299792458; + b = 6447287; + c = 256964964; + + gcd(a,b); + gcdr(gcd(a, a+b), gcdr(a*b,b*gcdv())); + return 0; +} \ No newline at end of file diff --git a/ass 5/gcd_test_output b/ass 5/gcd_test_output new file mode 100644 index 0000000..51a525a --- /dev/null +++ b/ass 5/gcd_test_output @@ -0,0 +1,123 @@ + + + SymbolTable of function gcd +--------------------SymbolTable--------------------------- +no of params=2 +a b +1 a 4 0 (int 0) +2 b 4 4 (int 0) +3 c 4 12 (int 0) +4 __t0 4 16 (int 0) +5 __t1 4 20 (int 0) +6 __t2 4 24 (int 0) +7 __t3 4 28 (int 0) +----------------------------------------------------------- + + + SymbolTable of function gcdr +--------------------SymbolTable--------------------------- +no of params=2 +a b +1 gcdr 4 0 +2 a 4 0 (int 0) +3 b 4 4 (int 0) +4 __t4 4 12 (int 0) +5 __t5 4 16 (int 0) +6 __t6 4 20 (int 0) +7 __t7 4 24 (int 0) +8 __t8 4 28 (int 0) +9 __t9 4 32 (int 0) +----------------------------------------------------------- + + + SymbolTable of function gcdv +--------------------SymbolTable--------------------------- +no of params=0 + +1 __t10 4 4 (int 0) +2 __t11 4 8 (int 0) +----------------------------------------------------------- + + + SymbolTable of function main +--------------------SymbolTable--------------------------- +no of params=0 + +1 gcdv 4 0 +2 gcdr 4 0 +3 gcd 4 0 +4 a 4 4 (int 0) +5 b 4 8 (int 0) +6 c 4 12 (int 0) +7 __t12 4 16 (int 0) +8 __t13 4 20 (int 0) +9 __t14 4 24 (int 0) +10 __t15 4 28 (int 0) +11 __t16 4 32 (int 0) +12 __t17 4 36 (int 0) +13 __t18 4 40 (int 0) +14 __t19 4 44 (int 0) +15 __t20 4 48 (int 0) +16 __t21 4 52 (int 0) +17 __t22 4 56 (int 0) +18 __t23 4 60 (int 0) +19 __t24 4 64 (int 0) +----------------------------------------------------------- +Global SymbolTable + +--------------------SymbolTable--------------------------- +no of params=0 + +1 gcd 4 0 (int 0) +2 gcdr 4 4 (int 0) +3 gcdv 4 8 (int 0) +4 main 4 12 (int 0) +----------------------------------------------------------- + 0 : __t0 = 0 + 1 : if a != __t0 goto 3 + 2 : goto 8 + 3 : c = a + 4 : __t2 = b % a + 5 : a = __t2 + 6 : b = c + 7 : goto 0 + 8 : return b + 9 : return + 10 : __t4 = 0 + 11 : if a = __t4 goto 13 + 12 : goto 14 + 13 : return b + 14 : __t7 = b % a + 15 : param __t7 + 16 : param a + 17 : __t8=call gcdr,2 + 18 : return __t8 + 19 : return + 20 : __t10 = 0 + 21 : return __t10 + 22 : return + 23 : __t12 = 299792458 + 24 : a = __t12 + 25 : __t13 = 6447287 + 26 : b = __t13 + 27 : __t14 = 256964964 + 28 : c = __t14 + 29 : param a + 30 : param b + 31 : __t15=call gcd,2 + 32 : __t16 = a + b + 33 : param a + 34 : param __t16 + 35 : __t17=call gcd,2 + 36 : __t18 = a * b + 37 : __t19=call gcdv,0 + 38 : __t20 = b * __t19 + 39 : param __t18 + 40 : param __t20 + 41 : __t21=call gcdr,2 + 42 : param __t17 + 43 : param __t21 + 44 : __t22=call gcdr,2 + 45 : __t23 = 0 + 46 : return __t23 + 47 : return diff --git a/ass 5/myfile.c b/ass 5/myfile.c index d510f78..a1ebab4 100644 --- a/ass 5/myfile.c +++ b/ass 5/myfile.c @@ -12,11 +12,12 @@ extern SymbolTable* st; void yyerror(char* s) { - printf("%s",s); + printf("%s at line_number %d\n",s,line_number); } int main() { freopen("ass5_12CS10037_test.c", "r", stdin); + // freopen("stat_test_output","w",stdout); int token; std::string s; try{ @@ -27,7 +28,7 @@ int main() printf("\nERROR: %s on line_number %d\n",p,line_number+1 ); st->print(); } - + printf("Global SymbolTable\n"); _GLOBST->print(); for (int i = 0; i > %s\n",q.res,q.arg1,q.arg2 ); + else + printf("%s = %s %d %s\n",q.res,q.arg1,q.op,q.arg2 ); + } + } @@ -97,8 +104,15 @@ void Quad::emit(const Quad &q){ break; case QPOINTER:printf("%s=*%s\n",q.res,q.arg1 ); break; + case QPOINTERDER:printf("*%s=%s\n",q.res,q.arg1); + break; case QADDR:printf("%s=&%s\n",q.res,q.arg1 ); break; + case QRETURN:printf("return %s\n",q.res); + break; + case QRETURN_NULL:printf("return\n"); + break; + default: if(q.op<=255) printf("%s = %c %s\n",q.res,q.op,q.arg1 ); diff --git a/ass 5/quad.h b/ass 5/quad.h index 7da0c8f..1748921 100644 --- a/ass 5/quad.h +++ b/ass 5/quad.h @@ -28,7 +28,10 @@ enum{ QPARAM, //function params QCALL, //function call QPOINTER, - QADDR + QPOINTERDER, + QADDR, + QRETURN_NULL, + QRETURN }; /* diff --git a/ass 5/stat_test.c b/ass 5/stat_test.c new file mode 100644 index 0000000..c8469b8 --- /dev/null +++ b/ass 5/stat_test.c @@ -0,0 +1,44 @@ +int main() +{ + int a=10,r1=5,r2=3,r3=8; + + //if statement simple if + if(r1==r2&&r2<=5&&r1+100*a>=56.746) + { + r3++; + } + + //if else complex + if(r1>=r2) + { + if(a+98<=908) + a++; + else { + if(a>5) + a--; + else + a=a+283*37.45; + } + } + else + r1++; + + + + //for while do while all in one + int i,j=29,k=88; + for (i = 0; i < 20; ++i){ + for (; j!=9; ){ + j++; + do{ + while(k<=90) + k++; + }while(k>20); + + } + + } + + + return 0; +} \ No newline at end of file diff --git a/ass 5/stat_test_output b/ass 5/stat_test_output new file mode 100644 index 0000000..4edb07d --- /dev/null +++ b/ass 5/stat_test_output @@ -0,0 +1,150 @@ + + + SymbolTable of function main +--------------------SymbolTable--------------------------- +no of params=0 + +1 __t0 4 4 (int 0) +2 a 4 8 (int 0) +3 __t1 4 12 (int 0) +4 r1 4 16 (int 0) +5 __t2 4 20 (int 0) +6 r2 4 24 (int 0) +7 __t3 4 28 (int 0) +8 r3 4 32 (int 0) +9 __t4 4 36 (int 0) +10 __t5 4 40 (int 0) +11 __t6 4 44 (int 0) +12 __t7 4 48 (int 0) +13 __t8 4 52 (int 0) +14 __t9 4 56 (int 0) +15 __t10 8 60 (double 0) +16 __t11 4 68 (int 0) +17 __t12 8 72 (double 0) +18 __t13 4 80 (int 0) +19 __t14 4 84 (int 0) +20 __t15 4 88 (int 0) +21 __t16 4 92 (int 0) +22 __t17 4 96 (int 0) +23 __t18 4 100 (int 0) +24 __t19 4 104 (int 0) +25 __t20 4 108 (int 0) +26 __t21 4 112 (int 0) +27 __t22 4 116 (int 0) +28 __t23 4 120 (int 0) +29 __t24 8 124 (double 0) +30 __t26 8 132 (double 0) +31 __t25 8 140 (double 0) +32 __t28 8 148 (double 0) +33 __t27 8 156 (double 0) +34 __t29 8 164 (double 0) +35 __t30 4 172 (int 0) +36 i 4 176 (int 0) +37 __t31 4 180 (int 0) +38 j 4 184 (int 0) +39 __t32 4 188 (int 0) +40 k 4 192 (int 0) +41 __t33 4 196 (int 0) +42 __t34 4 200 (int 0) +43 __t35 4 204 (int 0) +44 __t36 4 208 (int 0) +45 __t37 4 212 (int 0) +46 __t38 4 216 (int 0) +47 __t39 4 220 (int 0) +48 __t40 4 224 (int 0) +49 __t41 4 228 (int 0) +50 __t42 4 232 (int 0) +51 __t43 4 236 (int 0) +52 __t44 4 240 (int 0) +53 __t45 4 244 (int 0) +54 __t46 4 248 (int 0) +----------------------------------------------------------- +Global SymbolTable + +--------------------SymbolTable--------------------------- +no of params=0 + +1 main 4 0 (int 0) +----------------------------------------------------------- + 0 : __t0 = 10 + 1 : a = __t0 + 2 : __t1 = 5 + 3 : r1 = __t1 + 4 : __t2 = 3 + 5 : r2 = __t2 + 6 : __t3 = 8 + 7 : r3 = __t3 + 8 : if r1 = r2 goto 11 + 9 : goto 24 + 10 : goto ... + 11 : __t5 = 5 + 12 : if r2 <= __t5 goto 15 + 13 : goto 24 + 14 : goto ... + 15 : __t7 = 100 + 16 : __t8 = __t7 * a + 17 : __t9 = r1 + __t8 + 18 : __t10 = 56.746 + 19 : __t12 = int2double( __t9 ) + 20 : if __t10 >= __t12 goto 22 + 21 : goto 24 + 22 : __t13 = r3 + 23 : r3 = r3 + 1 + 24 : if r1 >= r2 goto 26 + 25 : goto 49 + 26 : __t15 = 98 + 27 : __t16 = a + __t15 + 28 : __t17 = 908 + 29 : if __t16 <= __t17 goto 31 + 30 : goto 34 + 31 : __t19 = a + 32 : a = a + 1 + 33 : goto 51 + 34 : __t20 = 5 + 35 : if a > __t20 goto 37 + 36 : goto 40 + 37 : __t22 = a + 38 : a = a - 1 + 39 : goto 51 + 40 : __t23 = 283 + 41 : __t24 = 37.45 + 42 : __t26 = int2double( __t23 ) + 43 : __t25 = __t24 * __t26 + 44 : __t28 = int2double( a ) + 45 : __t27 = __t25 + __t28 + 46 : __t29 = double2int( __t27 ) + 47 : a = __t29 + 48 : goto 51 + 49 : __t30 = r1 + 50 : r1 = r1 + 1 + 51 : __t31 = 29 + 52 : j = __t31 + 53 : __t32 = 88 + 54 : k = __t32 + 55 : __t33 = 0 + 56 : i = __t33 + 57 : __t34 = 20 + 58 : if i < __t34 goto 63 + 59 : goto 79 + 60 : i = i + 1 + 61 : __t36 = i + 62 : goto 57 + 63 : __t37 = 9 + 64 : if j != __t37 goto 66 + 65 : goto 78 + 66 : __t39 = j + 67 : j = j + 1 + 68 : __t40 = 90 + 69 : if k <= __t40 goto 71 + 70 : goto 74 + 71 : __t42 = k + 72 : k = k + 1 + 73 : goto 68 + 74 : __t43 = 20 + 75 : if k > __t43 goto 68 + 76 : goto 63 + 77 : goto 63 + 78 : goto 60 + 79 : __t45 = 0 + 80 : return __t45 + 81 : return diff --git a/ass 5/test2.c b/ass 5/test2.c new file mode 100644 index 0000000..1f747d6 --- /dev/null +++ b/ass 5/test2.c @@ -0,0 +1,92 @@ +int strlen(char* x) +{ + return 0; +} + +void computeLPSArray(char *pat, int M, int *lps) +{ + int len = 0; // lenght of the previous longest prefix suffix + int i; + + lps[0] = 0; // lps[0] is always 0 + i = 1; + + // the loop calculates lps[i] for i = 1 to M-1 + while(i < M) + { + if(pat[i] == pat[len]) + { + len++; + lps[i] = len; + i++; + } + else // (pat[i] != pat[len]) + { + if( len != 0 ) + { + // This is tricky. Consider the example AAACAAAA and i = 7. + len = lps[len-1]; + + // Also, note that we do not increment i here + } + else // if (len == 0) + { + lps[i] = 0; + i++; + } + } + } +} + + + +void KMPSearch(char *pat, char *txt) +{ + int M = strlen(pat); + int N = strlen(txt); + + // create lps[] that will hold the longest prefix suffix values for pattern + int *lps ; + int j = 0; // index for pat[] + + // Preprocess the pattern (calculate lps[] array) + computeLPSArray(pat, M, lps); + + int i = 0; // index for txt[] + while(i < N) + { + if(pat[j] == txt[i]) + { + j++; + i++; + } + + if (j == M) + { + // printf("Found pattern at index %d \n", i-j); + j = lps[j-1]; + } + + // mismatch after j matches + else if(pat[j] != txt[i]) + { + // Do not match lps[0..lps[j-1]] characters, + // they will match anyway + if(j != 0) + j = lps[j-1]; + else + i = i+1; + } + } + // free(lps); // to avoid memory leak +} + + +// Driver program to test above function +int main() +{ + char *txt ; + char *pat ; + KMPSearch(pat, txt); + return 0; +} \ No newline at end of file diff --git a/ass 5/tests and outputs/decl_test.c b/ass 5/tests and outputs/decl_test.c new file mode 100644 index 0000000..506513d --- /dev/null +++ b/ass 5/tests and outputs/decl_test.c @@ -0,0 +1,56 @@ + + +int main() +{ + + //all basic types + int i1=5,i2=34,i3=45.67; + char c1='a';//ASCII value of char is taken in quad + double d1=3,d2=56.7*9,d3=45.6+9; + + //arrays + int ia1[100],ia2[100]; + + //the shift is represeted as int value in quad + ia1[0]=i1*i2<<3; + + //pointers I've supported all possible types of pointer and array combination declarations + + // NOTE--> I've not suported pointer arithmetic but using pointers as arrays is supported + int *x=ia2; + + x[5]=(d1+d3)/i2; + + x=&i2; + *x=i2; + + //complex types + int *a[100][200]; //2-D array of int pointers + + a[10][20]=x; + + int (*aa)[100]; //pointer to an integer array of size 100 + + int **aa2=aa; + + + double da[10][10][10][10]; + double *p1=&da[0][0][0][0],**p2,***p3; + + double (*p4)[10][10][10]=da; + p3=da[9]; + p2=da[8][8]; + + int i,j,k,l; + for (i = 0; i < 10; ++i){ + for ( j = 0; j < 10; ++j){ + for ( k = 0; k < 10; ++k){ + for ( l = 0; l < 10; ++l){ + da[i][j][k][l]=i1*i2+3; + } + } + } + } + + return 0; +} \ No newline at end of file diff --git a/ass 5/tests and outputs/decl_test_output b/ass 5/tests and outputs/decl_test_output new file mode 100644 index 0000000..3393612 --- /dev/null +++ b/ass 5/tests and outputs/decl_test_output @@ -0,0 +1,291 @@ + + + SymbolTable of function main +--------------------SymbolTable--------------------------- +no of params=0 + +1 __t0 4 4 (int 0) +2 i1 4 8 (int 0) +3 __t1 4 12 (int 0) +4 i2 4 16 (int 0) +5 __t2 8 20 (double 0) +6 i3 4 28 (int 0) +7 __t3 8 32 (double 0) +8 __t4 1 40 (char 0) +9 c1 1 41 (char 0) +10 __t5 4 42 (int 0) +11 d1 8 46 (double 0) +12 __t6 8 54 (double 0) +13 __t7 8 62 (double 0) +14 __t8 4 70 (int 0) +15 __t10 8 74 (double 0) +16 __t9 8 82 (double 0) +17 d2 8 90 (double 0) +18 __t11 8 98 (double 0) +19 __t12 4 106 (int 0) +20 __t14 8 110 (double 0) +21 __t13 8 118 (double 0) +22 d3 8 126 (double 0) +23 __t15 4 134 (int 0) +24 ia1 400 138 (array 100) (int 0) +25 __t16 4 538 (int 0) +26 ia2 400 542 (array 100) (int 0) +27 __t17 4 942 (int 0) +28 __t18 4 946 (int 0) +29 __t19 4 950 (int 0) +30 __t20 4 954 (int 0) +31 __t21 4 958 (int 0) +32 __t22 4 962 (int 0) +33 __t23 4 966 (int 0) +34 x 4 970 (pointer 0) (int 0) +35 __t24 4 974 (int 0) +36 __t25 4 978 (int 0) +37 __t26 4 982 (int 0) +38 __t27 4 986 (int 0) +39 __t28 8 990 (double 0) +40 __t30 8 998 (double 0) +41 __t29 8 1006 (double 0) +42 __t31 8 1014 (double 0) +43 __t32 4 1022 (pointer 0) (int 0) +44 __t33 4 1026 (int 0) +45 __t34 4 1030 (int 0) +46 __t35 4 1034 (int 0) +47 a 80000 1038 (array 100) (array 200) (pointer 0) (int 0) +48 __t36 4 81038 (int 0) +49 __t37 800 81042 (array 200) (pointer 0) (int 0) +50 __t38 4 81842 (int 0) +51 __t39 4 81846 (int 0) +52 __t40 4 81850 (pointer 0) (int 0) +53 __t42 4 81854 (int 0) +54 __t41 4 81858 (int 0) +55 __t43 4 81862 (pointer 0) (int 0) +56 __t44 4 81866 (int 0) +57 aa 4 81870 (pointer 0) (array 100) (int 0) +58 aa2 4 81874 (pointer 0) (pointer 0) (int 0) +59 __t45 4 81878 (int 0) +60 __t46 4 81882 (int 0) +61 __t47 4 81886 (int 0) +62 __t48 4 81890 (int 0) +63 da 80000 81894 (array 10) (array 10) (array 10) (array 10) (double 0) +64 __t49 4 161894 (int 0) +65 __t50 8000 161898 (array 10) (array 10) (array 10) (double 0) +66 __t51 4 169898 (int 0) +67 __t52 4 169902 (int 0) +68 __t53 800 169906 (array 10) (array 10) (double 0) +69 __t55 4 170706 (int 0) +70 __t54 4 170710 (int 0) +71 __t56 4 170714 (int 0) +72 __t57 80 170718 (array 10) (double 0) +73 __t59 4 170798 (int 0) +74 __t58 4 170802 (int 0) +75 __t60 4 170806 (int 0) +76 __t61 8 170810 (double 0) +77 __t63 4 170818 (int 0) +78 __t62 4 170822 (int 0) +79 __t64 8 170826 (double 0) +80 __t65 4 170834 (pointer 0) (double 0) +81 p1 4 170838 (pointer 0) (double 0) +82 p2 4 170842 (pointer 0) (pointer 0) (double 0) +83 p3 4 170846 (pointer 0) (pointer 0) (pointer 0) (double 0) +84 __t66 4 170850 (int 0) +85 __t67 4 170854 (int 0) +86 __t68 4 170858 (int 0) +87 p4 4 170862 (pointer 0) (array 10) (array 10) (array 10) (double 0) +88 __t69 4 170866 (int 0) +89 __t70 8000 170870 (array 10) (array 10) (array 10) (double 0) +90 __t71 4 178870 (int 0) +91 __t72 8000 178874 (array 10) (array 10) (array 10) (double 0) +92 __t73 4 186874 (int 0) +93 __t74 8000 186878 (array 10) (array 10) (array 10) (double 0) +94 __t75 4 194878 (int 0) +95 __t76 4 194882 (int 0) +96 __t77 800 194886 (array 10) (array 10) (double 0) +97 __t79 4 195686 (int 0) +98 __t78 4 195690 (int 0) +99 __t80 800 195694 (array 10) (array 10) (double 0) +100 i 4 196494 (int 0) +101 j 4 196498 (int 0) +102 k 4 196502 (int 0) +103 l 4 196506 (int 0) +104 __t81 4 196510 (int 0) +105 __t82 4 196514 (int 0) +106 __t83 4 196518 (int 0) +107 __t84 4 196522 (int 0) +108 __t85 4 196526 (int 0) +109 __t86 4 196530 (int 0) +110 __t87 4 196534 (int 0) +111 __t88 4 196538 (int 0) +112 __t89 4 196542 (int 0) +113 __t90 4 196546 (int 0) +114 __t91 4 196550 (int 0) +115 __t92 4 196554 (int 0) +116 __t93 4 196558 (int 0) +117 __t94 4 196562 (int 0) +118 __t95 4 196566 (int 0) +119 __t96 4 196570 (int 0) +120 __t97 8000 196574 (array 10) (array 10) (array 10) (double 0) +121 __t98 4 204574 (int 0) +122 __t99 800 204578 (array 10) (array 10) (double 0) +123 __t101 4 205378 (int 0) +124 __t100 4 205382 (int 0) +125 __t102 80 205386 (array 10) (double 0) +126 __t104 4 205466 (int 0) +127 __t103 4 205470 (int 0) +128 __t105 8 205474 (double 0) +129 __t107 4 205482 (int 0) +130 __t106 4 205486 (int 0) +131 __t108 8 205490 (double 0) +132 __t109 4 205498 (int 0) +133 __t110 4 205502 (int 0) +134 __t111 4 205506 (int 0) +135 __t112 8 205510 (double 0) +136 __t113 4 205518 (int 0) +137 __t114 4 205522 (int 0) +----------------------------------------------------------- +Global SymbolTable + +--------------------SymbolTable--------------------------- +no of params=0 + +1 main 4 0 (int 0) +----------------------------------------------------------- + 0 : __t0 = 5 + 1 : i1 = __t0 + 2 : __t1 = 34 + 3 : i2 = __t1 + 4 : __t2 = 45.67 + 5 : __t3 = double2int( __t2 ) + 6 : i3 = __t3 + 7 : __t4 = 97 + 8 : c1 = __t4 + 9 : __t5 = 3 + 10 : __t6 = int2double( __t5 ) + 11 : d1 = __t6 + 12 : __t7 = 56.7 + 13 : __t8 = 9 + 14 : __t10 = int2double( __t8 ) + 15 : __t9 = __t7 * __t10 + 16 : d2 = __t9 + 17 : __t11 = 45.6 + 18 : __t12 = 9 + 19 : __t14 = int2double( __t12 ) + 20 : __t13 = __t11 + __t14 + 21 : d3 = __t13 + 22 : __t15 = 100 + 23 : __t16 = 100 + 24 : __t17 = 0 + 25 : __t19 = __t17 * 4 + 26 : __t20 = ia1[__t19] + 27 : __t21 = i1 * i2 + 28 : __t22 = 3 + 29 : __t23 = __t21 << __t22 + 30 : ia1[__t19] = __t23 + 31 : x = ia2 + 32 : __t24 = 5 + 33 : __t26 = __t24 * 4 + 34 : __t27 = x[__t26] + 35 : __t28 = d1 + d3 + 36 : __t30 = int2double( i2 ) + 37 : __t29 = __t28 / __t30 + 38 : __t31 = double2int( __t29 ) + 39 : x[__t26] = __t31 + 40 : __t32=&i2 + 41 : x = __t32 + 42 : __t33=*x + 43 : *x=i2 + 44 : __t34 = 100 + 45 : __t35 = 200 + 46 : __t36 = 10 + 47 : __t38 = __t36 * 800 + 48 : __t39 = 20 + 49 : __t41 = __t39 * 4 + 50 : __t42 = __t38 + __t41 + 51 : __t43 = a[__t42] + 52 : a[__t42] = x + 53 : __t44 = 100 + 54 : aa2 = aa + 55 : __t45 = 10 + 56 : __t46 = 10 + 57 : __t47 = 10 + 58 : __t48 = 10 + 59 : __t49 = 0 + 60 : __t51 = __t49 * 8000 + 61 : __t52 = 0 + 62 : __t54 = __t52 * 800 + 63 : __t55 = __t51 + __t54 + 64 : __t56 = 0 + 65 : __t58 = __t56 * 80 + 66 : __t59 = __t55 + __t58 + 67 : __t60 = 0 + 68 : __t62 = __t60 * 8 + 69 : __t63 = __t59 + __t62 + 70 : __t64 = da[__t63] + 71 : __t65=&__t64 + 72 : p1 = __t65 + 73 : __t66 = 10 + 74 : __t67 = 10 + 75 : __t68 = 10 + 76 : p4 = da + 77 : __t69 = 9 + 78 : __t71 = __t69 * 8000 + 79 : __t72 = da[__t71] + 80 : p3 = __t72 + 81 : __t73 = 8 + 82 : __t75 = __t73 * 8000 + 83 : __t76 = 8 + 84 : __t78 = __t76 * 800 + 85 : __t79 = __t75 + __t78 + 86 : __t80 = da[__t79] + 87 : p2 = __t80 + 88 : __t81 = 0 + 89 : i = __t81 + 90 : __t82 = 10 + 91 : if i < __t82 goto 96 + 92 : goto 137 + 93 : i = i + 1 + 94 : __t84 = i + 95 : goto 90 + 96 : __t85 = 0 + 97 : j = __t85 + 98 : __t86 = 10 + 99 : if j < __t86 goto 104 +100 : goto 136 +101 : j = j + 1 +102 : __t88 = j +103 : goto 98 +104 : __t89 = 0 +105 : k = __t89 +106 : __t90 = 10 +107 : if k < __t90 goto 112 +108 : goto 135 +109 : k = k + 1 +110 : __t92 = k +111 : goto 106 +112 : __t93 = 0 +113 : l = __t93 +114 : __t94 = 10 +115 : if l < __t94 goto 120 +116 : goto 134 +117 : l = l + 1 +118 : __t96 = l +119 : goto 114 +120 : __t98 = i * 8000 +121 : __t100 = j * 800 +122 : __t101 = __t98 + __t100 +123 : __t103 = k * 80 +124 : __t104 = __t101 + __t103 +125 : __t106 = l * 8 +126 : __t107 = __t104 + __t106 +127 : __t108 = da[__t107] +128 : __t109 = i1 * i2 +129 : __t110 = 3 +130 : __t111 = __t109 + __t110 +131 : __t112 = int2double( __t111 ) +132 : da[__t107] = __t112 +133 : goto 117 +134 : goto 109 +135 : goto 101 +136 : goto 93 +137 : __t113 = 0 +138 : return __t113 +139 : return diff --git a/ass 5/tests and outputs/divide_conquer.c b/ass 5/tests and outputs/divide_conquer.c new file mode 100644 index 0000000..e72ffe0 --- /dev/null +++ b/ass 5/tests and outputs/divide_conquer.c @@ -0,0 +1,65 @@ + + + // This function returns median of ar1[] and ar2[]. + // Assumptions in this function: + // Both ar1[] and ar2[] are sorted arrays + // Both have n elements +int getMedian(int *ar1, int *ar2, int n) +{ + int i = 0; + int j = 0; + int count; + int m1 = -1, m2 = -1; + + + for (count = 0; count <= n; count++) + { + // Below is to handle case where all elements of ar1[] are + // smaller than smallest(or first) element of ar2[] + if (i == n) + { + m1 = m2; + m2 = ar2[0]; + //break; removed break statemnet to avoid syntax error + } + + // Below is to handle case where all elements of ar2[] are + // smaller than smallest(or first) element of ar1[] + else if (j == n) + { + m1 = m2; + m2 = ar1[0]; + // break;removed break statemnet to avoid syntax error + } + + if (ar1[i] < ar2[j]) + { + m1 = m2; //Store the prev median + m2 = ar1[i]; + i++; + } + else + { + m1 = m2; // Store the prev median + m2 = ar2[j]; + j++; + } + } + + return (m1 + m2)/2; +} + + // Driver program to test above function +int main() +{ + int ar1[100]; + int ar2[100]; + int i; + for ( i = 0; i < 100; ++i){ + ar1[i]=i*7/34; + } + + getMedian(ar1, ar2, 100); + + return 0; +} \ No newline at end of file diff --git a/ass 5/tests and outputs/divide_conquer_output b/ass 5/tests and outputs/divide_conquer_output new file mode 100644 index 0000000..2f11e20 --- /dev/null +++ b/ass 5/tests and outputs/divide_conquer_output @@ -0,0 +1,172 @@ + + + SymbolTable of function getMedian +--------------------SymbolTable--------------------------- +no of params=3 +ar1 ar2 n +1 ar1 4 0 (pointer 0) (int 0) +2 ar2 4 4 (pointer 0) (int 0) +3 n 4 8 (int 0) +4 __t0 4 16 (int 0) +5 i 4 20 (int 0) +6 __t1 4 24 (int 0) +7 j 4 28 (int 0) +8 count 4 32 (int 0) +9 __t2 4 36 (int 0) +10 __t3 4 40 (int 0) +11 m1 4 44 (int 0) +12 __t4 4 48 (int 0) +13 __t5 4 52 (int 0) +14 m2 4 56 (int 0) +15 __t6 4 60 (int 0) +16 __t7 4 64 (int 0) +17 __t8 4 68 (int 0) +18 __t9 4 72 (int 0) +19 __t10 4 76 (int 0) +20 __t11 4 80 (int 0) +21 __t12 4 84 (int 0) +22 __t13 4 88 (int 0) +23 __t14 4 92 (int 0) +24 __t15 4 96 (int 0) +25 __t16 4 100 (int 0) +26 __t17 4 104 (int 0) +27 __t18 4 108 (int 0) +28 __t19 4 112 (int 0) +29 __t20 4 116 (int 0) +30 __t21 4 120 (int 0) +31 __t22 4 124 (int 0) +32 __t23 4 128 (int 0) +33 __t24 4 132 (int 0) +34 __t25 4 136 (int 0) +35 __t26 4 140 (int 0) +36 __t27 4 144 (int 0) +37 __t28 4 148 (int 0) +38 __t29 4 152 (int 0) +39 __t30 4 156 (int 0) +40 __t31 4 160 (int 0) +41 __t32 4 164 (int 0) +42 __t33 4 168 (int 0) +43 __t34 4 172 (int 0) +44 __t35 4 176 (int 0) +45 __t36 4 180 (int 0) +46 __t37 4 184 (int 0) +----------------------------------------------------------- + + + SymbolTable of function main +--------------------SymbolTable--------------------------- +no of params=0 + +1 getMedian 4 0 +2 __t38 4 4 (int 0) +3 ar1 400 8 (array 100) (int 0) +4 __t39 4 408 (int 0) +5 ar2 400 412 (array 100) (int 0) +6 i 4 812 (int 0) +7 __t40 4 816 (int 0) +8 __t41 4 820 (int 0) +9 __t42 4 824 (int 0) +10 __t43 4 828 (int 0) +11 __t44 4 832 (int 0) +12 __t45 4 836 (int 0) +13 __t46 4 840 (int 0) +14 __t47 4 844 (int 0) +15 __t48 4 848 (int 0) +16 __t49 4 852 (int 0) +17 __t50 4 856 (int 0) +18 __t51 4 860 (int 0) +19 __t52 4 864 (int 0) +20 __t53 4 868 (int 0) +21 __t54 4 872 (int 0) +----------------------------------------------------------- +Global SymbolTable + +--------------------SymbolTable--------------------------- +no of params=0 + +1 getMedian 4 0 (int 0) +2 main 4 4 (int 0) +----------------------------------------------------------- + 0 : __t0 = 0 + 1 : i = __t0 + 2 : __t1 = 0 + 3 : j = __t1 + 4 : __t2 = 1 + 5 : __t3 = - __t2 + 6 : m1 = __t3 + 7 : __t4 = 1 + 8 : __t5 = - __t4 + 9 : m2 = __t5 + 10 : __t6 = 0 + 11 : count = __t6 + 12 : if count <= n goto 17 + 13 : goto 52 + 14 : __t8 = count + 15 : count = count + 1 + 16 : goto 12 + 17 : if i = n goto 19 + 18 : goto 25 + 19 : m1 = m2 + 20 : __t10 = 0 + 21 : __t12 = __t10 * 4 + 22 : __t13 = ar2[__t12] + 23 : m2 = __t13 + 24 : goto 32 + 25 : if j = n goto 27 + 26 : goto 32 + 27 : m1 = m2 + 28 : __t15 = 0 + 29 : __t17 = __t15 * 4 + 30 : __t18 = ar1[__t17] + 31 : m2 = __t18 + 32 : __t20 = i * 4 + 33 : __t21 = ar1[__t20] + 34 : __t23 = j * 4 + 35 : __t24 = ar2[__t23] + 36 : if __t21 < __t24 goto 38 + 37 : goto 45 + 38 : m1 = m2 + 39 : __t27 = i * 4 + 40 : __t28 = ar1[__t27] + 41 : m2 = __t28 + 42 : __t29 = i + 43 : i = i + 1 + 44 : goto 51 + 45 : m1 = m2 + 46 : __t31 = j * 4 + 47 : __t32 = ar2[__t31] + 48 : m2 = __t32 + 49 : __t33 = j + 50 : j = j + 1 + 51 : goto 14 + 52 : __t34 = m1 + m2 + 53 : __t35 = 2 + 54 : __t36 = __t34 / __t35 + 55 : return __t36 + 56 : return + 57 : __t38 = 100 + 58 : __t39 = 100 + 59 : __t40 = 0 + 60 : i = __t40 + 61 : __t41 = 100 + 62 : if i < __t41 goto 67 + 63 : goto 75 + 64 : i = i + 1 + 65 : __t43 = i + 66 : goto 61 + 67 : __t45 = i * 4 + 68 : __t46 = ar1[__t45] + 69 : __t47 = 7 + 70 : __t48 = i * __t47 + 71 : __t49 = 34 + 72 : __t50 = __t48 / __t49 + 73 : ar1[__t45] = __t50 + 74 : goto 64 + 75 : __t51 = 100 + 76 : param ar1 + 77 : param ar2 + 78 : param __t51 + 79 : __t52=call getMedian,3 + 80 : __t53 = 0 + 81 : return __t53 + 82 : return diff --git a/ass 5/tests and outputs/expres_test.c b/ass 5/tests and outputs/expres_test.c new file mode 100644 index 0000000..4884e3a --- /dev/null +++ b/ass 5/tests and outputs/expres_test.c @@ -0,0 +1,54 @@ + + +// testing the file for expressions + + +int main() +{ + int a=10; + + int i1,i2,i3; + + //unary operators + i1=a++*++a; + + i1=-i1; + + + //binary + i2=i1%a*a+456+'c'; + i3= i1<<3 + 2>>3; + + double d1,d2,d3; + + d3=d2=d1=i2+i3; //here conversion from int to double takes place + + d2=i2*(34.56+'c')/i2; + + char c1=5; + c1=d2;//double2char + + //testing relational expressions + //every relational expression has a value true(1) or false(0) + + int r1,r2,r3,r4; + r1=i1&&i2; //the value of r1 is 1 if the boolean expression is true else r2=0 + r2= i3<=75&&i3>=6; //there will a goto which is unreachable and not backpatched when + //evaluating this expression + + r3= 1&&3||1&&2; + + r2=(r1==r3)||(1!=2); + + //teranary operator + r1=(r2==r3)?i1*2938:i2*34+98.67; + + r1= (1)?2:3; + + + //if statement + + + + return 0; +} \ No newline at end of file diff --git a/ass 5/tests and outputs/expres_test_output b/ass 5/tests and outputs/expres_test_output new file mode 100644 index 0000000..a8bfdf0 --- /dev/null +++ b/ass 5/tests and outputs/expres_test_output @@ -0,0 +1,205 @@ + + + SymbolTable of function main +--------------------SymbolTable--------------------------- +no of params=0 + +1 __t0 4 4 (int 0) +2 a 4 8 (int 0) +3 i1 4 12 (int 0) +4 i2 4 16 (int 0) +5 i3 4 20 (int 0) +6 __t1 4 24 (int 0) +7 __t2 4 28 (int 0) +8 __t3 4 32 (int 0) +9 __t4 4 36 (int 0) +10 __t5 4 40 (int 0) +11 __t6 4 44 (int 0) +12 __t7 4 48 (int 0) +13 __t8 4 52 (int 0) +14 __t9 1 56 (char 0) +15 __t11 4 57 (int 0) +16 __t10 4 61 (int 0) +17 __t12 4 65 (int 0) +18 __t13 4 69 (int 0) +19 __t14 4 73 (int 0) +20 __t15 4 77 (int 0) +21 __t16 4 81 (int 0) +22 __t17 4 85 (int 0) +23 d1 8 89 (double 0) +24 d2 8 97 (double 0) +25 d3 8 105 (double 0) +26 __t18 4 113 (int 0) +27 __t19 8 117 (double 0) +28 __t20 8 125 (double 0) +29 __t21 1 133 (char 0) +30 __t23 8 134 (double 0) +31 __t22 8 142 (double 0) +32 __t25 8 150 (double 0) +33 __t24 8 158 (double 0) +34 __t27 8 166 (double 0) +35 __t26 8 174 (double 0) +36 __t28 4 182 (int 0) +37 c1 1 186 (char 0) +38 __t29 8 187 (double 0) +39 __t30 8 195 (double 0) +40 r1 4 203 (int 0) +41 r2 4 207 (int 0) +42 r3 4 211 (int 0) +43 r4 4 215 (int 0) +44 __t31 4 219 (int 0) +45 __t32 4 223 (int 0) +46 __t33 4 227 (int 0) +47 __t34 4 231 (int 0) +48 __t35 4 235 (int 0) +49 __t36 4 239 (int 0) +50 __t37 4 243 (int 0) +51 __t38 4 247 (int 0) +52 __t39 4 251 (int 0) +53 __t40 4 255 (int 0) +54 __t41 4 259 (int 0) +55 __t42 4 263 (int 0) +56 __t43 4 267 (int 0) +57 __t44 4 271 (int 0) +58 __t45 4 275 (int 0) +59 __t46 4 279 (int 0) +60 __t47 4 283 (int 0) +61 __t48 8 287 (double 0) +62 __t50 8 295 (double 0) +63 __t49 8 303 (double 0) +64 __t51 4 311 (int 0) +65 __t52 4 315 (int 0) +66 __t53 4 319 (int 0) +67 __t54 4 323 (int 0) +68 __t55 4 327 (int 0) +69 __t56 4 331 (int 0) +70 __t57 4 335 (int 0) +----------------------------------------------------------- +Global SymbolTable + +--------------------SymbolTable--------------------------- +no of params=0 + +1 main 4 0 (int 0) +----------------------------------------------------------- + 0 : __t0 = 10 + 1 : a = __t0 + 2 : __t1 = a + 3 : a = a + 1 + 4 : a = a + 1 + 5 : __t2 = a + 6 : __t3 = __t1 * __t2 + 7 : i1 = __t3 + 8 : __t4 = - i1 + 9 : i1 = __t4 + 10 : __t5 = i1 % a + 11 : __t6 = __t5 * a + 12 : __t7 = 456 + 13 : __t8 = __t6 + __t7 + 14 : __t9 = 99 + 15 : __t11 = char2int( __t9 ) + 16 : __t10 = __t8 + __t11 + 17 : i2 = __t10 + 18 : __t12 = 3 + 19 : __t13 = 2 + 20 : __t14 = __t12 + __t13 + 21 : __t15 = i1 << __t14 + 22 : __t16 = 3 + 23 : __t17 = __t15 >> __t16 + 24 : i3 = __t17 + 25 : __t18 = i2 + i3 + 26 : __t19 = int2double( __t18 ) + 27 : d1 = __t19 + 28 : d2 = d1 + 29 : d3 = d2 + 30 : __t20 = 34.56 + 31 : __t21 = 99 + 32 : __t23 = char2double( __t21 ) + 33 : __t22 = __t20 + __t23 + 34 : __t25 = int2double( i2 ) + 35 : __t24 = __t22 * __t25 + 36 : __t27 = int2double( i2 ) + 37 : __t26 = __t24 / __t27 + 38 : d2 = __t26 + 39 : __t28 = 5 + 40 : __t29 = int2char( __t28 ) + 41 : c1 = __t29 + 42 : __t30 = double2char( d2 ) + 43 : c1 = __t30 + 44 : goto 47 + 45 : if i2 = 0 goto 51 + 46 : goto 49 + 47 : if i1 = 0 goto 51 + 48 : goto 45 + 49 : r1 = 1 + 50 : goto 52 + 51 : r1 = 0 + 52 : __t31 = 75 + 53 : if i3 <= __t31 goto 56 + 54 : goto 61 + 55 : goto ... + 56 : __t33 = 6 + 57 : if i3 >= __t33 goto 59 + 58 : goto 61 + 59 : r2 = 1 + 60 : goto 62 + 61 : r2 = 0 + 62 : __t35 = 1 + 63 : goto 67 + 64 : __t36 = 3 + 65 : if __t36 = 0 goto 70 + 66 : goto 77 + 67 : if __t35 = 0 goto 70 + 68 : goto 64 + 69 : goto ... + 70 : __t37 = 1 + 71 : goto 75 + 72 : __t38 = 2 + 73 : if __t38 = 0 goto 79 + 74 : goto 77 + 75 : if __t37 = 0 goto 79 + 76 : goto 72 + 77 : r3 = 1 + 78 : goto 80 + 79 : r3 = 0 + 80 : if r1 = r3 goto 87 + 81 : goto 83 + 82 : goto ... + 83 : __t40 = 1 + 84 : __t41 = 2 + 85 : if __t40 != __t41 goto 87 + 86 : goto 89 + 87 : r2 = 1 + 88 : goto 90 + 89 : r2 = 0 + 90 : if r2 = r3 goto 93 + 91 : goto 96 + 92 : goto ... + 93 : __t44 = 2938 + 94 : __t45 = i1 * __t44 + 95 : goto 103 + 96 : __t46 = 34 + 97 : __t47 = i2 * __t46 + 98 : __t48 = 98.67 + 99 : __t50 = int2double( __t47 ) +100 : __t49 = __t48 + __t50 +101 : __t51 = __t49 +102 : goto 105 +103 : __t51 = __t45 +104 : goto 105 +105 : r1 = __t51 +106 : __t52 = 1 +107 : goto 115 +108 : __t53 = 2 +109 : goto 113 +110 : __t54 = 3 +111 : __t55 = __t54 +112 : goto 117 +113 : __t55 = __t53 +114 : goto 117 +115 : if __t52 = 0 goto 110 +116 : goto 108 +117 : r1 = __t55 +118 : __t56 = 0 +119 : return __t56 +120 : return diff --git a/ass 5/tests and outputs/gcd_test.c b/ass 5/tests and outputs/gcd_test.c new file mode 100644 index 0000000..4ca8288 --- /dev/null +++ b/ass 5/tests and outputs/gcd_test.c @@ -0,0 +1,34 @@ +int gcd ( int a, int b ) +{ + int c; + while ( a != 0 ) { + c = a; + a = b%a; + b = c; + } + return b; +} + +/* Recursive Standard C Function: Greatest Common Divisor */ +int gcdr ( int a, int b ) +{ + if ( a==0 ) return b; + return gcdr ( b%a, a ); +} + +int gcdv() +{ + return 0; +} + +int main() +{ + int a,b,c; + a = 299792458; + b = 6447287; + c = 256964964; + + gcd(a,b); + gcdr(gcd(a, a+b), gcdr(a*b,b*gcdv())); + return 0; +} \ No newline at end of file diff --git a/ass 5/tests and outputs/gcd_test_output b/ass 5/tests and outputs/gcd_test_output new file mode 100644 index 0000000..51a525a --- /dev/null +++ b/ass 5/tests and outputs/gcd_test_output @@ -0,0 +1,123 @@ + + + SymbolTable of function gcd +--------------------SymbolTable--------------------------- +no of params=2 +a b +1 a 4 0 (int 0) +2 b 4 4 (int 0) +3 c 4 12 (int 0) +4 __t0 4 16 (int 0) +5 __t1 4 20 (int 0) +6 __t2 4 24 (int 0) +7 __t3 4 28 (int 0) +----------------------------------------------------------- + + + SymbolTable of function gcdr +--------------------SymbolTable--------------------------- +no of params=2 +a b +1 gcdr 4 0 +2 a 4 0 (int 0) +3 b 4 4 (int 0) +4 __t4 4 12 (int 0) +5 __t5 4 16 (int 0) +6 __t6 4 20 (int 0) +7 __t7 4 24 (int 0) +8 __t8 4 28 (int 0) +9 __t9 4 32 (int 0) +----------------------------------------------------------- + + + SymbolTable of function gcdv +--------------------SymbolTable--------------------------- +no of params=0 + +1 __t10 4 4 (int 0) +2 __t11 4 8 (int 0) +----------------------------------------------------------- + + + SymbolTable of function main +--------------------SymbolTable--------------------------- +no of params=0 + +1 gcdv 4 0 +2 gcdr 4 0 +3 gcd 4 0 +4 a 4 4 (int 0) +5 b 4 8 (int 0) +6 c 4 12 (int 0) +7 __t12 4 16 (int 0) +8 __t13 4 20 (int 0) +9 __t14 4 24 (int 0) +10 __t15 4 28 (int 0) +11 __t16 4 32 (int 0) +12 __t17 4 36 (int 0) +13 __t18 4 40 (int 0) +14 __t19 4 44 (int 0) +15 __t20 4 48 (int 0) +16 __t21 4 52 (int 0) +17 __t22 4 56 (int 0) +18 __t23 4 60 (int 0) +19 __t24 4 64 (int 0) +----------------------------------------------------------- +Global SymbolTable + +--------------------SymbolTable--------------------------- +no of params=0 + +1 gcd 4 0 (int 0) +2 gcdr 4 4 (int 0) +3 gcdv 4 8 (int 0) +4 main 4 12 (int 0) +----------------------------------------------------------- + 0 : __t0 = 0 + 1 : if a != __t0 goto 3 + 2 : goto 8 + 3 : c = a + 4 : __t2 = b % a + 5 : a = __t2 + 6 : b = c + 7 : goto 0 + 8 : return b + 9 : return + 10 : __t4 = 0 + 11 : if a = __t4 goto 13 + 12 : goto 14 + 13 : return b + 14 : __t7 = b % a + 15 : param __t7 + 16 : param a + 17 : __t8=call gcdr,2 + 18 : return __t8 + 19 : return + 20 : __t10 = 0 + 21 : return __t10 + 22 : return + 23 : __t12 = 299792458 + 24 : a = __t12 + 25 : __t13 = 6447287 + 26 : b = __t13 + 27 : __t14 = 256964964 + 28 : c = __t14 + 29 : param a + 30 : param b + 31 : __t15=call gcd,2 + 32 : __t16 = a + b + 33 : param a + 34 : param __t16 + 35 : __t17=call gcd,2 + 36 : __t18 = a * b + 37 : __t19=call gcdv,0 + 38 : __t20 = b * __t19 + 39 : param __t18 + 40 : param __t20 + 41 : __t21=call gcdr,2 + 42 : param __t17 + 43 : param __t21 + 44 : __t22=call gcdr,2 + 45 : __t23 = 0 + 46 : return __t23 + 47 : return diff --git a/ass 5/tests and outputs/stat_test.c b/ass 5/tests and outputs/stat_test.c new file mode 100644 index 0000000..c8469b8 --- /dev/null +++ b/ass 5/tests and outputs/stat_test.c @@ -0,0 +1,44 @@ +int main() +{ + int a=10,r1=5,r2=3,r3=8; + + //if statement simple if + if(r1==r2&&r2<=5&&r1+100*a>=56.746) + { + r3++; + } + + //if else complex + if(r1>=r2) + { + if(a+98<=908) + a++; + else { + if(a>5) + a--; + else + a=a+283*37.45; + } + } + else + r1++; + + + + //for while do while all in one + int i,j=29,k=88; + for (i = 0; i < 20; ++i){ + for (; j!=9; ){ + j++; + do{ + while(k<=90) + k++; + }while(k>20); + + } + + } + + + return 0; +} \ No newline at end of file diff --git a/ass 5/tests and outputs/stat_test_output b/ass 5/tests and outputs/stat_test_output new file mode 100644 index 0000000..4edb07d --- /dev/null +++ b/ass 5/tests and outputs/stat_test_output @@ -0,0 +1,150 @@ + + + SymbolTable of function main +--------------------SymbolTable--------------------------- +no of params=0 + +1 __t0 4 4 (int 0) +2 a 4 8 (int 0) +3 __t1 4 12 (int 0) +4 r1 4 16 (int 0) +5 __t2 4 20 (int 0) +6 r2 4 24 (int 0) +7 __t3 4 28 (int 0) +8 r3 4 32 (int 0) +9 __t4 4 36 (int 0) +10 __t5 4 40 (int 0) +11 __t6 4 44 (int 0) +12 __t7 4 48 (int 0) +13 __t8 4 52 (int 0) +14 __t9 4 56 (int 0) +15 __t10 8 60 (double 0) +16 __t11 4 68 (int 0) +17 __t12 8 72 (double 0) +18 __t13 4 80 (int 0) +19 __t14 4 84 (int 0) +20 __t15 4 88 (int 0) +21 __t16 4 92 (int 0) +22 __t17 4 96 (int 0) +23 __t18 4 100 (int 0) +24 __t19 4 104 (int 0) +25 __t20 4 108 (int 0) +26 __t21 4 112 (int 0) +27 __t22 4 116 (int 0) +28 __t23 4 120 (int 0) +29 __t24 8 124 (double 0) +30 __t26 8 132 (double 0) +31 __t25 8 140 (double 0) +32 __t28 8 148 (double 0) +33 __t27 8 156 (double 0) +34 __t29 8 164 (double 0) +35 __t30 4 172 (int 0) +36 i 4 176 (int 0) +37 __t31 4 180 (int 0) +38 j 4 184 (int 0) +39 __t32 4 188 (int 0) +40 k 4 192 (int 0) +41 __t33 4 196 (int 0) +42 __t34 4 200 (int 0) +43 __t35 4 204 (int 0) +44 __t36 4 208 (int 0) +45 __t37 4 212 (int 0) +46 __t38 4 216 (int 0) +47 __t39 4 220 (int 0) +48 __t40 4 224 (int 0) +49 __t41 4 228 (int 0) +50 __t42 4 232 (int 0) +51 __t43 4 236 (int 0) +52 __t44 4 240 (int 0) +53 __t45 4 244 (int 0) +54 __t46 4 248 (int 0) +----------------------------------------------------------- +Global SymbolTable + +--------------------SymbolTable--------------------------- +no of params=0 + +1 main 4 0 (int 0) +----------------------------------------------------------- + 0 : __t0 = 10 + 1 : a = __t0 + 2 : __t1 = 5 + 3 : r1 = __t1 + 4 : __t2 = 3 + 5 : r2 = __t2 + 6 : __t3 = 8 + 7 : r3 = __t3 + 8 : if r1 = r2 goto 11 + 9 : goto 24 + 10 : goto ... + 11 : __t5 = 5 + 12 : if r2 <= __t5 goto 15 + 13 : goto 24 + 14 : goto ... + 15 : __t7 = 100 + 16 : __t8 = __t7 * a + 17 : __t9 = r1 + __t8 + 18 : __t10 = 56.746 + 19 : __t12 = int2double( __t9 ) + 20 : if __t10 >= __t12 goto 22 + 21 : goto 24 + 22 : __t13 = r3 + 23 : r3 = r3 + 1 + 24 : if r1 >= r2 goto 26 + 25 : goto 49 + 26 : __t15 = 98 + 27 : __t16 = a + __t15 + 28 : __t17 = 908 + 29 : if __t16 <= __t17 goto 31 + 30 : goto 34 + 31 : __t19 = a + 32 : a = a + 1 + 33 : goto 51 + 34 : __t20 = 5 + 35 : if a > __t20 goto 37 + 36 : goto 40 + 37 : __t22 = a + 38 : a = a - 1 + 39 : goto 51 + 40 : __t23 = 283 + 41 : __t24 = 37.45 + 42 : __t26 = int2double( __t23 ) + 43 : __t25 = __t24 * __t26 + 44 : __t28 = int2double( a ) + 45 : __t27 = __t25 + __t28 + 46 : __t29 = double2int( __t27 ) + 47 : a = __t29 + 48 : goto 51 + 49 : __t30 = r1 + 50 : r1 = r1 + 1 + 51 : __t31 = 29 + 52 : j = __t31 + 53 : __t32 = 88 + 54 : k = __t32 + 55 : __t33 = 0 + 56 : i = __t33 + 57 : __t34 = 20 + 58 : if i < __t34 goto 63 + 59 : goto 79 + 60 : i = i + 1 + 61 : __t36 = i + 62 : goto 57 + 63 : __t37 = 9 + 64 : if j != __t37 goto 66 + 65 : goto 78 + 66 : __t39 = j + 67 : j = j + 1 + 68 : __t40 = 90 + 69 : if k <= __t40 goto 71 + 70 : goto 74 + 71 : __t42 = k + 72 : k = k + 1 + 73 : goto 68 + 74 : __t43 = 20 + 75 : if k > __t43 goto 68 + 76 : goto 63 + 77 : goto 63 + 78 : goto 60 + 79 : __t45 = 0 + 80 : return __t45 + 81 : return diff --git a/ass 6/Makefile b/ass 6/Makefile new file mode 100644 index 0000000..5590ca4 --- /dev/null +++ b/ass 6/Makefile @@ -0,0 +1,43 @@ +# link_stp:compiler asm_file +# gcc -m32 out.s -L. -lmyl + +asm_file:compiler libmyl.a + ./compiler + gcc -m32 -fno-asynchronous-unwind-tables -S ass5_12CS10037_test.c + +compiler: y.tab.o lex.yy.o target_generate.o + g++ -o compiler lex.yy.o y.tab.o ass5_12CS10037_translator.o quad.o SymbolTable.o target_generate.o -lfl + +target_generate.o: target_generate.cpp target_generate.h + g++ -g -c target_generate.cpp -lfl + +y.tab.o: y.tab.c y.tab.h + g++ -g -c -Wno-write-strings y.tab.c + +lex.yy.o: lex.yy.c ass5_12CS10037_translator.o + g++ -g -c lex.yy.c + +lex.yy.c: y.tab.h ass5_12CS10037.l ass5_12CS10037_translator.o + flex ass5_12CS10037.l + +y.tab.c: ass5_12CS10037.y + yacc -dtv ass5_12CS10037.y + + + +ass5_12CS10037_translator.o:ass5_12CS10037_translator.h ass5_12CS10037_translator.cpp quad.o SymbolTable.o + g++ -g -c ass5_12CS10037_translator.cpp + +quad.o: quad.cpp quad.h y.tab.h + g++ -g -c quad.cpp + +SymbolTable.o: SymbolTable.cpp SymbolTable.h + g++ -g -c SymbolTable.cpp + +libmyl.a: myl.o + ar -rcs libmyl.a myl.o +myl.o: myl.c myl.h + cc -Wall -m32 -c myl.c + +clean: + rm y.tab.* lex.yy.c *.o out.s y.output *.a 3ADC a.out \ No newline at end of file diff --git a/ass 6/SymbolTable.cpp b/ass 6/SymbolTable.cpp new file mode 100644 index 0000000..6b67d51 --- /dev/null +++ b/ass 6/SymbolTable.cpp @@ -0,0 +1,264 @@ +#include "SymbolTable.h" +#include "bits/stdc++.h" +using namespace std; + +int SymbolTable::tcount=0; + +bool sortCompare(const SFields &s1,const SFields& s2) +{ + if(s1.offset<=s2.offset) + return true; + return false; +} +bool parCom(const SFields &s1,const SFields& s2) +{ + if(s1.parNumtype=f.type; + fn->offset=f.offset; + fn->nestedTable=f.nestedTable; + fn->loc=f.loc; + fn->size=f.size; + + offset+=f.size; +} + +void SymbolTable::print() +{ + + + printf("\n--------------------SymbolTable---------------------------\n"); + printf("no of params=%d and stackOffset=%d\n",paramNum,stackOffset ); + std::vector pars=getParamList(); + tr(pars,it) + printf("%s ",it->name.c_str() ); + printf("\n"); + std::vector temp(table.begin(), table.end()); + sort(temp.begin(), temp.end(),sortCompare); + int c=1; + tr(temp,it) { + printf("%d\t%s\t%d\t%d\t%d\t%d\t",c, it->name.c_str(),it->size,it->offset, + it->actOffset,it->isConst); + For(i, 0, it->type.size()){ + printf("(%s %d) ",nameSizeArray[it->type[i].first].c_str(), + it->type[i].second ); + } + printf("\n"); + c++; + + } + printf("-----------------------------------------------------------\n"); + + // tr(table, it) + // { + // printf("%u %s\n",&(*it),it->name.c_str() ); + // } + + // printf("-----------------------------------------------------------\n"); +} + +void SymbolTable::update(Fields* f,const Type& t) +{ + if(f==NULL) + throw "error in update function"; + f->type=t; + f->size=getSize(t); + f->offset=offset; + offset+=f->size; +} + +void SymbolTable::update(Fields* f1,Fields* f2) +{ + if(f1==NULL||f2==NULL) + throw "error in update function"; + f1->type=f2->type; + f1->size=f2->size; + f1->offset=offset; + offset+=f2->size; +} + +void SymbolTable::clearTable() +{ + this->nameindex.clear(); + this->table.clear(); + this->n=0; + this->offset=0; +} + +Fields* SymbolTable::search(const string& s) +{ + if(nameindex.find(s)!=nameindex.end()) + { + return &table[nameindex[s]]; + } + return NULL; +} +std::vector SymbolTable::getParamList() +{ + std::vector temp(table.begin(), table.end()); + sort(temp.begin(), temp.end(),parCom); + if(paramNum>0) + return std::vector(temp.begin(),temp.begin()+paramNum); + std::vector v; + return v; +} + +void SymbolTable::activationRecords() +{ + sort(table.begin(),table.end(),parCom); + nameindex.clear(); + For(i, 0, table.size()) + { + if(table[i].parNum!=DEFAULT_PAR_NO){ + table[i].actOffset=parOff; + parOff+=table[i].size; + } + else{ + localOff-=table[i].size; + table[i].actOffset=localOff; + } + nameindex[table[i].name]=i; + } + + For(i, 0, table.size()){ + if(table[i].isfname==true){ + Fields* f=parent->lookup(table[i].name); + std::vector v=f->nestedTable->getParamList(); + int sS=0; + For(j, 0, v.size()) + { + sS+=v[j].size; + } + if(sS>stackOffset) + stackOffset=sS; + } + } +} + +std::vector SymbolTable::getConstStrings() +{ + vector temp; + For(i, 0, table.size()){ + if(table[i].isStringConst){ + temp.push_back(&table[i]); + } + } + return temp; +} + +int getSize(const Type &t) +{ + int val=0; + int vsize=t.size(); + if(t[0].first==pointerT) + return SIZE_OF_POINTER; + + + for (int i = vsize-1; i >=0 ; --i){ + int ff=t[i].first,ss=t[i].second; + switch(ff){ + case intT: + case charT: + case doubleT: + val+=sizeArrayNOD[ff]; + break; + case arrayT: + val=val*ss; + break; + case pointerT: + val=4; + } + } + return val; +} + + + + + + + + + + + + + +// void solve() +// { + +// } + + + + + +// int main() +// { + +// freopen("input", "r", stdin); + +// SymbolTable st; +// int tc=10; +// S(tc); +// for (int i = 0; i < tc; ++i){ +// INS(x); +// string word; +// if(x){ +// cin>>word; +// st.lookup(word); +// } + +// else +// SymbolTable::gentemp(st); +// } +// st.print(); +// return 0; +// } \ No newline at end of file diff --git a/ass 6/SymbolTable.h b/ass 6/SymbolTable.h new file mode 100644 index 0000000..a967e07 --- /dev/null +++ b/ass 6/SymbolTable.h @@ -0,0 +1,250 @@ +#ifndef _SYMBOL_TABLE_H +#define _SYMBOL_TABLE_H + +#include + +using namespace std; + + + +typedef long long ll; +typedef unsigned long long ull; +typedef pair ii; +typedef vector vi; +typedef vector vii; +typedef pair pll; +typedef vector vll; +typedef vector vpll; + + + +#define FI(n) for(int i=0;ithe type of type 2->size + soo + int x[30][40] === array(30,array(40,int)) + === {(arrayT,30),(arrayT,40),(intT,0)} + + int **x[20]; + + ==={(arrayT,20),(pointerT,2),(intT,0)} + +*/ +typedef std::vector > Type; + +/* + ListType is array type to store the indices of quads having dangling gotos +*/ +typedef std::vector ListType; + +//returns the size of a specific type +int getSize(const Type &t); + + +/* + This is the type present in the symbol table as a row. +*/ +struct Fields{ + public: + Type type; //type of symbol present + char* loc; //loc that it will be stored + unsigned int size; //size of the element + int offset; //offset in the symbol table + SymbolTable* nestedTable; //if it is the global symbolTable then it stores the + //symboltable pointer of that function + string name; //name of the symbol + + //used for constant folding val contains the val of the contant + union Val{ + int intVal; + double doubleVal; + char* stringVal; + char charVal; + }val; + bool isConst; //if the symbol present is actually a constant + bool isBoolExp; //if it is a boolean expression then t has true list and false lists + Fields* arrSize; //if type is array, store the size while reducing grammar + bool isArray; + bool isPointer; + Fields* arrayBase; + int parNum; + int actOffset; + bool isfname; + bool isStringConst; + int stringLabel; + ListType tl,fl;//true list and false lists + Fields():type(),loc(NULL),size(0),offset(0),nestedTable(NULL), + isConst(false),isBoolExp(false),arrSize(NULL), + isArray(false),isPointer(false),parNum(DEFAULT_PAR_NO), + isfname(false),isStringConst(false),stringLabel(0) + + {} + + //all posible updates available to change the values in ST using pointers + void update(int type1,unsigned int size1,int offset1) + { + type.push_back(make_pair(type1, 0)); + size=size1; + offset=offset1; + } + + void update(const Type& type1,unsigned int size1,int offset1) + { + // printf("updating %s\n",name.c_str() ); + type=type1; + size=size1; + offset=offset1; + } + void update(Fields* f) + { + type=f->type; + this->size=f->size; + } + void print() + { + printf("%s\t%d\t%d\t",name.c_str(),size,offset); + printf("type="); + tr(type, it){ + + printf("(%s %d) ",nameSizeArray[it->first].c_str(),it->second ); + } + printf("\n"); + } +}; + + + +class SFields:public Fields{ + public: + + SFields():Fields(){} +}; + + +/* + Class to represetnt the symoltable +*/ +class SymbolTable{ + + /* + The tabe is stored in form of array where every symbol is mapped to an + index in this array + */ + public:std::vector table; + + /* + hashmap to store symbolnames to indices in the symboltable + */ + map nameindex; + /* + number of symbols present + */ + int n; + +public: + /* + The current offset of ST + */ + int offset; + int paramNum; //the number of parameters that it has + int parOff; + int localOff; + int stackOffset; + SymbolTable* parent; + SymbolTable(); + /* + A method to lookup an id (given its name or lexeme) + in the Symbol Table. If the id exists, the entry is + returned, otherwise a new entry is created. + */ + Fields* lookup(const string &s); + + static int tcount;//temporary count + /* + A static method to generate a new temporary, insert + it to the Symbol Table, and return a pointer to the + entry. + */ + static Fields* gentemp(SymbolTable &st); + + /* + prints all symbols and their data in tabular form + */ + void print(); + /* + A method to update different fields of an existing + entry.All are overloaded forms for the same function + */ + void update(const string &s,const Fields &f); + void update(Fields* f,const Type& t); + void update(Fields* f1,Fields* f2); + /* + erase all contents of the current symboltable + */ + void clearTable(); + /* + given a string searches if it is present in symboltable. Unlike + lookup it doesnt add the given string to ST if it is not present + */ + Fields* search(const string& s); + /* + for a ST of function return all the list of params + */ + std::vector getParamList(); + + + void activationRecords(); + + inline bool isTemp(char*s) + { + if(strlen(s)<3) + return false; + if(s[0]=='_'&&s[1]=='_'&&s[2]=='t') + return true; + return false; + } + + std::vector getConstStrings(); +}; + +#endif \ No newline at end of file diff --git a/ass 6/asmcheatsheet.pdf b/ass 6/asmcheatsheet.pdf new file mode 100644 index 0000000..c1a2101 Binary files /dev/null and b/ass 6/asmcheatsheet.pdf differ diff --git a/ass 6/ass 6.tar.gz b/ass 6/ass 6.tar.gz new file mode 100644 index 0000000..6698a3c Binary files /dev/null and b/ass 6/ass 6.tar.gz differ diff --git a/ass 6/ass 6_12CS10037.tar.gz b/ass 6/ass 6_12CS10037.tar.gz new file mode 100644 index 0000000..bfbca32 Binary files /dev/null and b/ass 6/ass 6_12CS10037.tar.gz differ diff --git a/ass 6/ass5_12CS10037.l b/ass 6/ass5_12CS10037.l new file mode 100644 index 0000000..790f8c0 --- /dev/null +++ b/ass 6/ass5_12CS10037.l @@ -0,0 +1,222 @@ +%{ + #include + #include "ass5_12CS10037_translator.h" + #include "y.tab.h" + char comment_word[1000];int comment_index=0; + int line_number=0; + void count(); + + extern SymbolTable* st; + int isComment=0; +%} + + + +WHITE_SPACE [ \n\v\t] +DIGIT [0-9] +IDENTIFIER_NON_DIGIT [_a-zA-Z] +IDENTIFIER {IDENTIFIER_NON_DIGIT}({IDENTIFIER_NON_DIGIT}?{DIGIT}?)* + + + + + + + +NON_ZERO_DIGIT [1-9] +DIGIT_SEQUENCE {DIGIT}+ +SIGN [\+-] +EXPONENT_PART [eE]{SIGN}?{DIGIT}+ +ESCAPE_SEQUENCE \\[\'\"?\\abfnrtv] +C_CHAR [^\'\\\n]|{ESCAPE_SEQUENCE} +C_CHAR_SEQUENCE {C_CHAR}+ + +FRACTIONAL_CONSTANT {DIGIT_SEQUENCE}?\.{DIGIT_SEQUENCE}|{DIGIT_SEQUENCE}\. +FLOATING_CONSTANT {FRACTIONAL_CONSTANT}{EXPONENT_PART}?|{DIGIT_SEQUENCE}{EXPONENT_PART} + +INTEGER_CONSTANT {NON_ZERO_DIGIT}{DIGIT}*|0 + +ENUMERATION_CONSTANT {IDENTIFIER} + +CHARACTER_CONSTANT \'{C_CHAR_SEQUENCE}\' + +CONSTANT {INTEGER_CONSTANT}|{FLOATING_CONSTANT}|{ENUMERATION_CONSTANT}|{CHARACTER_CONSTANT} + + + + + + + +S_CHAR [^\"\\\n]|{ESCAPE_SEQUENCE} +S_CHAR_SEQUENCE {S_CHAR}+ +STRING_LITERAL \"{S_CHAR_SEQUENCE}?\" + + + +%x comment +%% + + +"auto" { count();return(AUTO); } +"break" { count();return(BREAK); } +"case" { count();return(CASE); } +"char" { count();return(CHAR); } +"const" { count();return(CONST); } +"continue" { count();return(CONTINUE); } +"default" { count();return(DEFAULT); } +"do" { count();return(DO); } +"double" { count();return(DOUBLE); } +"else" { count();return(ELSE); } +"enum" { count();return(ENUM); } +"extern" { count();return(EXTERN); } +"float" { count();return(FLOAT); } +"for" { count();return(FOR); } +"goto" { count();return(GOTO); } +"if" { count();return(IF); } +"int" { count();return(INT); } +"long" { count();return(LONG); } +"register" { count();return(REGISTER); } +"return" { count();return(RETURN); } +"short" { count();return(SHORT); } +"signed" { count();return(SIGNED); } +"sizeof" { count();return(SIZEOF); } +"static" { count();return(STATIC); } +"struct" { count();return(STRUCT); } +"switch" { count();return(SWITCH); } +"typedef" { count();return(TYPEDEF); } +"union" { count();return(UNION); } +"unsigned" { count();return(UNSIGNED); } +"void" { count();return(VOID); } +"volatile" { count();return(VOLATILE); } +"while" { count();return(WHILE); } +"_Bool" {count();return(BOOL);} +"_Complex" {count();return(COMPLEX);} +"_Imaginary" {count();return(IMAGINARY);} +"restrict" {count();return(RESTRICT);} +"inline" {count();return(INLINE);} + +"..." { count();return(ELLIPSIS); } +">>=" { count();return(RIGHT_ASSIGN); } +"<<=" { count();return(LEFT_ASSIGN); } +"+=" { count();return(ADD_ASSIGN); } +"-=" { count();return(SUB_ASSIGN); } +"*=" { count();return(MUL_ASSIGN); } +"/=" { count();return(DIV_ASSIGN); } +"%=" { count();return(MOD_ASSIGN); } +"&=" { count();return(AND_ASSIGN); } +"^=" { count();return(XOR_ASSIGN); } +"|=" { count();return(OR_ASSIGN); } +">>" { count();return(RIGHT_OP); } +"<<" { count();return(LEFT_OP); } +"++" { count();return(INC_OP); } +"--" { count();return(DEC_OP); } +"->" { count();return(PTR_OP); } +"&&" { count();return(AND_OP); } +"||" { count();return(OR_OP); } +"<=" { count();return(LE_OP); } +">=" { count();return(GE_OP); } +"==" { count();return(EQ_OP); } +"!=" { count();return(NE_OP); } +";" { count();return(';'); } +("{"|"<%") { count();return('{'); } +("}"|"%>") { count();return('}'); } +"," { count();return(','); } +":" { count();return(':'); } +"=" { count();return('='); } +"(" { count();return('('); } +")" { count();return(')'); } +("["|"<:") { count();return('['); } +("]"|":>") { count();return(']'); } +"." { count();return('.'); } +"&" { count();return('&'); } +"!" { count();return('!'); } +"~" { count();return('~'); } +"-" { count();return('-'); } +"+" { count();return('+'); } +"*" { count();return('*'); } +"/" { count();return('/'); } +"%" { count();return('%'); } +"<" { count();return('<'); } +">" { count();return('>'); } +"^" { count();return('^'); } +"|" { count();return('|'); } +"?" { count();return('?'); } + + + + +{INTEGER_CONSTANT} {count();return INTEGER_CONSTANT;} +{FLOATING_CONSTANT} {count();return FLOATING_CONSTANT;} +{CHARACTER_CONSTANT} {count();return CHARACTER_CONSTANT;} + + +{STRING_LITERAL} count();return STRING_LITERAL; + +{IDENTIFIER} {st->lookup(string(yytext));count();return IDENTIFIER;} + + + +{WHITE_SPACE} count(); + + + + + + + +"$" return 0; +. + + + + + + + + + + + + + + + +\/\* BEGIN(comment); +[^*]* | +"*"+[^*/\n]* { + for(int i=0;i"*"+"/" { + yytext=comment_word; + comment_index=0; + BEGIN(INITIAL); + } + +\/\/.* {isComment=1;} + + + + + +%% + + +int column = 0; + +void count() +{ + int i; + + for (i = 0; yytext[i] != '\0'; i++) + if (yytext[i] == '\n') + {column = 0;line_number++;} + else if (yytext[i] == '\t') + column += 8 - (column % 8); + else + column++; + if(!isComment) + ; + else + isComment=0; +} diff --git a/ass 6/ass5_12CS10037.y b/ass 6/ass5_12CS10037.y new file mode 100644 index 0000000..d14f7ae --- /dev/null +++ b/ass 6/ass5_12CS10037.y @@ -0,0 +1,1543 @@ +%{ + #include + #include + #include + #include "stdio.h" + #include "ass5_12CS10037_translator.h" + struct Fields; + extern int yylex(); + extern void yyerror(char *s); + /* + All the quads will be stored and pushed to this vector. + All of them will be printed at once in the end + */ + std::vector quadArray; + /* + It is the temporary symbol table to which names go into when scope is unknown + */ + SymbolTable* _TEMPST=new SymbolTable(); + /* + st points to the current symbol table we're adding to + */ + SymbolTable* st=_TEMPST; + /* + pointer to the global symbol table that has all the function names + */ + extern SymbolTable* _GLOBST; + + extern char* yytext; + using namespace quad; + /* + generates a tempory variable in symboltable + */ + inline void GENTEMP(Fields* &f){ f=SymbolTable::gentemp(*st);} + + /* + Update functions has all overloads + update does the following + with the type specified, get the size of the symbol and initailize its offset + */ + + inline void UPDATE(Fields* f){ + st->update(f,f->type); + } + inline void UPDATE(Fields* f,Type& t){ + st->update(f,t); + } + inline void UPDATE(Fields* f1,Fields* f2){ + st->update(f1,f2); + } + + /* + It provides the index of next emmited quad + */ + inline int nextInst(){ + return quadArray.size(); + } + /* + Emit the last added quad + */ + inline void EMIT(){ + Quad::emit(quadArray[(int)quadArray.size()-1]); + } + /* + explained at definition + */ + Fields* checkTypesNAssign(Fields* f1,Fields* f2); + /* + explained at definition + */ + inline void getValueNBackpatch(Fields* f); + /* + It stores the type so that all idenitifiers declared using , operator + will be assigned with this type at the end + */ + int _GLOBALTYPE; + /* + explained at definition + */ + Fields* changeTypeNEmit(Fields* f1,Fields* f2,int op); + /* + explained at definition + */ + pair changeTypeNReturn(Fields* f1,Fields* f2); + /* + explained at definition + */ + void int2Bool(Fields* f); + /* + If function return value is found then it is set to 1 + */ + int funcRetSet=0; + /* + stores the return type of current function + */ + Type funcRetType; +%} + +%union{ + Fields* sentry; + Type *type; + int intval; + struct Int_pair{ + int first,second; + }int_pair; + ListType* listType; + vector* sentryList; +} + +%nonassoc LEAST_PREC +%token IDENTIFIER STRING_LITERAL SIZEOF INTEGER_CONSTANT FLOATING_CONSTANT CHARACTER_CONSTANT +%token PTR_OP INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP +%token AND_OP OR_OP MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN +%token SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN +%token XOR_ASSIGN OR_ASSIGN TYPE_NAME + +%token TYPEDEF EXTERN STATIC AUTO REGISTER INLINE +%token CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE CONST VOLATILE VOID BOOL COMPLEX IMAGINARY RESTRICT +%token STRUCT UNION ENUM ELLIPSIS + +%token CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN + +%start translation_unit + + + + +/* + sentry--> pointer to a field in ST +*/ +%type id_parser CONSTANT STRING_LITERAL +%type primary_expression postfix_expression unary_expression cast_expression multiplicative_expression additive_expression shift_expression +%type relational_expression equality_expression and_expression exclusive_or_expression inclusive_or_expression +%type expression initializer init_declarator direct_declarator declarator logical_or_expression + logical_and_expression conditional_expression assignment_expression constant_expression changeBoolTemp boolExpression + boolExpressionStatement function_definition array_expression + +%type type_specifier declaration_specifiers unary_operator +%type pointer +/* + sentryList-->list of pointers to fields in ST +*/ +%type identifier_list parameter_list argument_expression_list +%type MIndex relop + +/* + This is the type to store indices of dangling gotos in quads +*/ +%type NList statement compound_statement expression_statement + selection_statement iteration_statement block_item block_item_list + +%nonassoc UAND USTAR UPLUS UMINUS + +%nonassoc HIGH_PREC + +%% + + + +declaration + : declaration_specifiers ';' + | declaration_specifiers init_declarator_list ';' + { + } + ; + +declaration_specifiers + : type_specifier {$$=$1;} + ; + +init_declarator_list + : init_declarator + { + + } + | init_declarator_list ',' init_declarator + { + + } + ; + +init_declarator + : declarator + { + $$=$1; + $$->type.push_back(ii(_GLOBALTYPE,0)); + UPDATE($$); + + } + | declarator '=' initializer + { + $$=$1; + $$->type.push_back(ii(_GLOBALTYPE,0)); + UPDATE($$); + Fields* f=checkTypesNAssign($$,$3); + + if($3->isBoolExp){ + backpatch($3->tl,nextInst()); + quadArray.push_back(Quad($1->name,"1")); + quadArray.push_back(Quad(QGOTO,nextInst()+2)); + backpatch($3->fl,nextInst()); + quadArray.push_back(Quad($1->name,"0")); + } + else + quadArray.push_back(Quad($1->name,f->name)); + $$=$1; + } + ; + + + +type_specifier + : VOID {_GLOBALTYPE=voidT;$$=voidT;funcRetSet=(funcRetSet==0)?$$:funcRetSet;} + | CHAR {_GLOBALTYPE=charT;$$=charT;funcRetSet=(funcRetSet==0)?$$:funcRetSet;} + | INT {_GLOBALTYPE=intT;$$=intT;funcRetSet=(funcRetSet==0)?$$:funcRetSet;} + | DOUBLE {_GLOBALTYPE=doubleT;$$=doubleT;funcRetSet=(funcRetSet==0)?$$:funcRetSet;} + | SIGNED {_GLOBALTYPE=intT;$$=intT;funcRetSet=(funcRetSet==0)?$$:funcRetSet;} + | UNSIGNED {_GLOBALTYPE=intT;$$=intT;funcRetSet=(funcRetSet==0)?$$:funcRetSet;} + | COMPLEX {_GLOBALTYPE=intT;$$=intT;funcRetSet=(funcRetSet==0)?$$:funcRetSet;} + | IMAGINARY {_GLOBALTYPE=intT;$$=intT;funcRetSet=(funcRetSet==0)?$$:funcRetSet;} + ; + + + +specifier_qualifier_list + : type_specifier specifier_qualifier_list + | type_specifier + ; + + + + + +declarator + : pointer direct_declarator + { + $$=$2; + for(int i=0;i<$1.second;i++) + $$->type.push_back(ii(pointerT,0)); + } + | direct_declarator + { + $$=$1; + } + ; + +direct_declarator + : id_parser { + $$=$1; + } + | '(' declarator ')' + { + $$=$2; + } + | direct_declarator '[' assignment_expression ']' + { + $$=$1; + int x=$3->val.intVal; + $$->type.push_back(ii(arrayT,x)); + } + | direct_declarator '(' parameter_type_list ')' + { + quadArray.push_back(Quad(QFUNC,$1->name,"")); + funcRetType=$1->type; + funcRetType.push_back(ii(funcRetSet,0)); + Fields* f=_GLOBST->lookup($1->name); + UPDATE(f,funcRetType); + f->nestedTable=st; + } + | direct_declarator '(' identifier_list ')' + | direct_declarator '(' ')' + { + quadArray.push_back(Quad(QFUNC,$1->name,"")); + st=new SymbolTable(); + st->parent=_GLOBST; + funcRetType=$1->type; + funcRetType.push_back(ii(funcRetSet,0)); + Fields* f=_GLOBST->lookup($1->name); + UPDATE(f,funcRetType); + f->nestedTable=st; + } + ; + + + +pointer + : '*' + { + $$.first=pointerT; + $$.second=1; + } + | '*' pointer + { + $$=$2; + $$.second++; + } + ; + + + +parameter_type_list + : parameter_list + | parameter_list ',' ELLIPSIS + ; + +parameter_list + : declaration_specifiers declarator + { + Fields f1=*$2; + st=new SymbolTable(); + st->parent=_GLOBST; + Fields *f=st->lookup(f1.name); + int s=f1.type.size(); + f->type=f1.type; + f->type.push_back(ii($1,0)); + f->parNum=st->paramNum; + UPDATE(f); + st->paramNum++; + } + | parameter_list ',' declaration_specifiers declarator + { + $4->type.push_back(ii($3,0)); + UPDATE($4); + $4->parNum=st->paramNum; + st->paramNum++; + } + ; + + +identifier_list + : id_parser + { + $$=new vector(); + $$->push_back($1); + } + | identifier_list ',' id_parser + { + $$=$1; + $$->push_back($3); + } + ; + +type_name + : specifier_qualifier_list + ; + + + + +initializer + : assignment_expression + { + $$=$1; + } + ; + + + + + + + + + + + + + + +/* + this is to emit a goto while reducing and maintiain the index of + that quad in the list it has as attribute +*/ +NList + : %prec HIGH_PREC + { + $$=new vi(); + $$->push_back(nextInst()); + quadArray.push_back(Quad(QGOTO,"...",0)); + } + ; + +/* +Index of the quad generated at MIndex will be stored when reducing this. +this is used for backpatching +*/ +MIndex + :{$$=nextInst();} + ; + + + + + +statement + : compound_statement {$$=$1;} + | expression_statement{$$=$1;} + | selection_statement {$$=$1;} + | iteration_statement + { + $$=$1; + + } + | jump_statement {$$=new vi();} + ; + + +compound_statement + : '{' '}' {$$=new vi();} + | '{' block_item_list '}' {$$=$2;} + ; + +block_item_list + : block_item + { + *$$=merge(*$$,*$1); + } + | block_item_list MIndex block_item + { + backpatch(*$1,$2); + $$=$3; + } + ; + +block_item + : declaration {$$=new vi();} + | statement{ + $$=$1; + } + ; + + +expression_statement + : ';' {$$=new vi();} + | expression ';' + { + if($1->isBoolExp){ + $$=&($1->fl); + } + else + $$=new vi(); + } + ; + +selection_statement + :IF '(' boolExpression ')' MIndex statement %prec "then" + { + backpatch($3->tl,$5); + $$=$6; + *$$=merge($3->fl,*$$); + } + | IF '(' boolExpression ')' MIndex statement NList ELSE MIndex statement + { + $$=$10; + backpatch($3->tl,$5); + backpatch($3->fl,$9); + vi temp=merge(*$6,*$7); + *$$=merge(temp,*$10); + } + ; + +iteration_statement + : WHILE '(' MIndex boolExpression ')' MIndex statement + { + $$=new vi(); + backpatch(*$7,$3); + backpatch($4->tl,$6); + *$$=$4->fl; + quadArray.push_back((Quad(QGOTO,$3))); + } + | DO MIndex statement WHILE '('MIndex boolExpression ')' ';' + { + $$=new vi(); + *$$=$7->fl; + backpatch($7->tl,$2); + backpatch(*$3,$6); + } + + | FOR '(' expression_statement MIndex boolExpressionStatement + MIndex expression NList ')' MIndex statement + { + backpatch($5->tl,$10); + $$=new vi(); + backpatch(*$11,nextInst()); + quadArray.push_back(Quad(QGOTO,$6)); + backpatch(*$8,$4); + *$$=$5->fl; + } + | FOR '(' expression_statement MIndex boolExpressionStatement ')' + MIndex statement + { + backpatch(*$8,$4); + quadArray.push_back(Quad(QGOTO,$4)); + backpatch($5->tl,$7); + $$=new vi(); + *$$=$5->fl; + } + + ; + +jump_statement + : RETURN ';' + { + if(funcRetType.size()>0&&funcRetType[0].first==voidT) + quadArray.push_back(Quad(QRETURN_NULL,0)); + else + throw "return type not same"; + } + | RETURN expression ';' + { + Fields*f;GENTEMP(f);f->type=funcRetType;UPDATE(f); + f= checkTypesNAssign(f,$2); + quadArray.push_back(Quad(QRETURN,f->name,"")); + } + ; + + + +boolExpressionStatement + : ';' + { + GENTEMP($$); + $$->isBoolExp=true; + $$->tl=makelist(nextInst()); + quadArray.push_back((Quad(QGOTO,"...",0))); + $$->fl=makelist(nextInst()); + quadArray.push_back((Quad(QGOTO,"...",0))); + } + | boolExpression ';' {$$=$1;} + ; +boolExpression + :expression + { + if($1->isBoolExp==false) + int2Bool($1); + $$->isBoolExp=true; + $$=$1; + } + ; +translation_unit + : external_declaration + | translation_unit external_declaration + ; + +external_declaration + : function_definition {} + | declaration + { + quadArray.pop_back(); + st->activationRecords(); + st->print(); + _TEMPST->clearTable(); + st=_TEMPST; + funcRetType.clear(); + funcRetSet=0; + } + ; + +function_definition + : declaration_specifiers declarator declaration_list compound_statement + { + $$=_GLOBST->lookup($2->name); + $$->type=$2->type; + $$->type.push_back(ii($1,0)); + _GLOBST->update($$,$$->type); + $$->nestedTable=st; + printf("\n\n\tSymbolTable of function %s",$2->name.c_str()); + st->activationRecords(); + st->print(); + _TEMPST->clearTable(); + st=_TEMPST; + backpatch(*$4,nextInst()); + //adds a default return at the end of every function + quadArray.push_back(Quad(QFUNCEND,0)); + funcRetType.clear(); + funcRetSet=0; + + } + | declaration_specifiers declarator compound_statement + { + $$=_GLOBST->lookup($2->name); + $$->type=$2->type; + $$->type.push_back(ii($1,0)); + _GLOBST->update($$,$$->type); + $$->nestedTable=st; + printf("\n\n\tSymbolTable of function %s",$2->name.c_str()); + st->activationRecords(); + st->print(); + _TEMPST->clearTable(); + st=_TEMPST; + backpatch(*$3,nextInst()); + //adds a default return at the end of every function + quadArray.push_back(Quad(QFUNCEND,0)); + funcRetType.clear(); + funcRetSet=0; + } + ; + +declaration_list + : declaration + | declaration_list declaration + ; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +primary_expression + : id_parser { $$=$1;} + | CONSTANT {$$=$1;} + | STRING_LITERAL + { + char* temp=strdup(yytext); + temp[strlen(yytext)-1]='\0'; + temp++; + GENTEMP($$); + $$->type.push_back(ii(pointerT,0)); + $$->type.push_back(ii(charT,0)); + UPDATE($$); + $$->isStringConst=true; + $$->val.stringVal=temp; + quadArray.push_back(Quad(QSTRING,$$->name,temp)); + } + | '(' expression ')' + { + $$=$2; + } + ; + + + array_expression + :primary_expression '[' expression ']' + { + if($$->type[0].first!=arrayT&&$$->type[0].first!=pointerT) + throw "not an arrayT and pointerT"; + GENTEMP($$); + $$->isArray=true; + vii temp($1->type.begin()+1,$1->type.end()); + int s=getSize(temp); + $$->type=temp; + UPDATE($$); + + char word[50]; + sprintf(word,"%d",s); + Fields *&f1=$$->arrSize; + GENTEMP(f1); + f1->type.push_back(ii(intT,0)); + UPDATE(f1); + + quadArray.push_back(Quad('*',f1->name,$3->name,word)); + $$->arrayBase=$1; + } + | array_expression '[' expression ']' + { + if($$->type[0].first!=arrayT&&$$->type[0].first!=pointerT) + throw "not an arrayT and pointerT"; + GENTEMP($$); + $$->isArray=true; + vii temp($1->type.begin()+1,$1->type.end()); + int s=getSize(temp); + $$->type=temp; + UPDATE($$); + + char word[50];sprintf(word,"%d",s); + Fields *f1; + GENTEMP(f1); + GENTEMP($$->arrSize); + $$->arrSize->type=$1->arrSize->type; + UPDATE($$->arrSize); + + f1->type.push_back(ii(intT,0)); + UPDATE(f1); + + quadArray.push_back(Quad('*',f1->name,$3->name,word)); + $$->arrayBase=$1->arrayBase; + + quadArray.push_back(Quad('+',$$->arrSize->name, + $1->arrSize->name,f1->name)); + } + ; + +postfix_expression + : primary_expression {$$=$1;} + |array_expression + { + GENTEMP($$); + $$->type=$1->type; + $$->isArray=true; + $$->arrayBase=$1->arrayBase; + $$->arrSize=$1->arrSize; + UPDATE($$); + quadArray.push_back(Quad(QARRVAL,$$->name,$1->arrayBase->name, + $1->arrSize->name)); + } + | postfix_expression '(' ')' + { + Fields* f=_GLOBST->search($1->name); + if(f==NULL) + throw "no function found with that name"; + vector v=f->nestedTable->getParamList(); + if(v.size()!=0) + throw "no arguments given"; + GENTEMP($$); + f->isfname=true; + $$->type=f->type; + UPDATE($$); + quadArray.push_back(Quad(QCALL,$$->name,$1->name,"0")); + } + | postfix_expression '(' argument_expression_list ')' + { + Fields* f=_GLOBST->search($1->name); + if(f==NULL) + throw "no function found with that name"; + vector v=f->nestedTable->getParamList(); + if(v.size()!=$3->size()) + throw "number of arguments not matching"; + + f->isfname=true; + vector quadTemp; + For(i,0,v.size()) + { + //int check=typeCheck(v[i].type,$3->at(i)->type); + //if(check==0) + // throw "argument type not matching to call"; + + Fields*f= checkTypesNAssign(&v[i],($3->at(i))); + quadTemp.push_back(Quad(QPARAM,f->name,"")); + } + quadArray.insert(quadArray.end(),quadTemp.begin(),quadTemp.end()); + GENTEMP($$); + $$->type=f->type; + UPDATE($$); + char word[20]; + sprintf(word,"%ld",v.size()); + quadArray.push_back(Quad(QCALL,$$->name,$1->name,word)); + } + | postfix_expression INC_OP + { + GENTEMP($$); + $$->type=$1->type; + UPDATE($$); + quadArray.push_back(Quad($$->name,$1->name)); + quadArray.push_back(Quad('+',$1->name,$1->name,"1")); + } + | postfix_expression DEC_OP + { + GENTEMP($$); + $$->type=$1->type; + UPDATE($$); + quadArray.push_back(Quad($$->name,$1->name)); + quadArray.push_back(Quad('-',$1->name,$1->name,"1")); + } + ; + +argument_expression_list + : assignment_expression + { + $$=new vector(); + $$->push_back($1); + } + | argument_expression_list ',' assignment_expression + { + $$=$1; + $$->push_back($3); + } + ; + +unary_expression + : postfix_expression {$$=$1;} + | INC_OP unary_expression + { + GENTEMP($$); + UPDATE($$,$2); + quadArray.push_back(Quad('+',$2->name,$2->name,"1")); + quadArray.push_back((Quad($$->name,$2->name))); + + + } + | DEC_OP unary_expression + { + GENTEMP($$);UPDATE($$,$2); + quadArray.push_back(Quad('-',$2->name,$2->name,"1")); + quadArray.push_back((Quad($$->name,$2->name))); + } + | unary_operator cast_expression + { + GENTEMP($$); + switch($1) + { + case '+': + case '-': + case '~': + UPDATE($$,$2); + quadArray.push_back(Quad($1,$$->name, + $2->name)); + break; + case '*': + { + if($2->type[0].first!=pointerT&&$2->type[0].first!=arrayT) + throw "not a pointer"; + vii temp($2->type.begin()+1,$2->type.end()); + $$->type=temp; + UPDATE($$); + $$->isPointer=true; + $$->arrayBase=$2; + quadArray.push_back(Quad(QPOINTER,$$->name,$2->name)); + break; + } + case '&': + $$->type.push_back(ii(pointerT,0)); + $$->type.insert($$->type.end(),$2->type.begin(), + $2->type.end()); + UPDATE($$); + if($2->isArray){ + quadArray.push_back(Quad('+',$$->name, + $2->arrayBase->name + ,$2->arrSize->name)); + } + else + quadArray.push_back(Quad(QADDR,$$->name,$2->name)); + + break; + default: + throw "error in processing unary operators"; + } + } + | '!' changeBoolTemp + { + GENTEMP($$); + $$->type.push_back(ii(intT,0)); + UPDATE($$); + $$->isBoolExp=true; + $$->tl=$2->fl; + $$->fl=$2->tl; + getValueNBackpatch($$); + } + + ; + +unary_operator + : '&' %prec UAND {$$=yytext[0];} + | '*' %prec USTAR {$$=yytext[0];} + | '+' %prec UPLUS {$$=yytext[0];} + | '-' %prec UMINUS {$$=yytext[0];} + | '~' {$$=yytext[0];} + ; + +cast_expression + : unary_expression{$$=$1;} + | '(' type_name ')' cast_expression {$$=$4;} + ; + +multiplicative_expression + : cast_expression %prec LEAST_PREC + { + $$=$1; + } + | multiplicative_expression '*' cast_expression + { + $$=changeTypeNEmit($1,$3,'*'); + } + | multiplicative_expression '/' cast_expression + { + $$=changeTypeNEmit($1,$3,'/'); + } + | multiplicative_expression '%' cast_expression + { + $$=changeTypeNEmit($1,$3,'%'); + } + ; + +additive_expression + : multiplicative_expression + { + $$=$1; + } + | additive_expression '+' multiplicative_expression + { + $$=changeTypeNEmit($1,$3,'+'); + } + | additive_expression '-' multiplicative_expression + { + $$=changeTypeNEmit($1,$3,'-'); + } + ; + +shift_expression + : additive_expression + { + $$=$1; + } + | shift_expression LEFT_OP additive_expression + { + $$=changeTypeNEmit($1,$3,LEFT_OP); + } + | shift_expression RIGHT_OP additive_expression + { + $$=changeTypeNEmit($1,$3,RIGHT_OP); + } + ; + +relational_expression + : shift_expression + { + $$=$1; + } + | relational_expression relop shift_expression + { + GENTEMP($$); + $$->type.push_back(ii(intT,0)); + UPDATE($$); + pair p=changeTypeNReturn($1,$3); + $$->tl=makelist(nextInst()); + switch($2) + { + case '<': + quadArray.push_back(Quad(QRELIFLT,"...", + p.first->name,p.second->name)); + break; + case '>': + quadArray.push_back(Quad(QRELIFGT,"...", + p.first->name,p.second->name)); + break; + case LE_OP: + quadArray.push_back(Quad(QRELIFLTE,"...", + p.first->name,p.second->name)); + break; + case GE_OP: + quadArray.push_back(Quad(QRELIFGTE,"...", + p.first->name,p.second->name)); + break; + } + $$->fl=makelist(nextInst()); + quadArray.push_back(Quad(QGOTO,"...",0)); + $$->isBoolExp=true; + + getValueNBackpatch($$); + + } + ; +relop + :'<' {$$='<';} + | '>' {$$='>';} + | LE_OP {$$=LE_OP;} + | GE_OP {$$=GE_OP;} + ; +equality_expression + : relational_expression + { + $$=$1; + } + | equality_expression EQ_OP relational_expression + { + GENTEMP($$); + $$->type.push_back(ii(intT,0)); + UPDATE($$); + pair p=changeTypeNReturn($1,$3); + $$->tl=makelist(nextInst()); + + quadArray.push_back(Quad(QRELIFEQ,"...", + p.first->name,p.second->name)); + + $$->fl=makelist(nextInst()); + quadArray.push_back(Quad(QGOTO,"...",0)); + $$->isBoolExp=true; + getValueNBackpatch($$); + } + | equality_expression NE_OP relational_expression + { + GENTEMP($$); + $$->type.push_back(ii(intT,0)); + UPDATE($$); + pair p=changeTypeNReturn($1,$3); + $$->tl=makelist(nextInst()); + + quadArray.push_back(Quad(QRENOTE,"...", + p.first->name,p.second->name)); + + $$->fl=makelist(nextInst()); + quadArray.push_back(Quad(QGOTO,"...",0)); + $$->isBoolExp=true; + getValueNBackpatch($$); + } + ; + +and_expression + : equality_expression + { + $$=$1; + } + | and_expression '&' equality_expression + { + $$=changeTypeNEmit($1,$3,'&'); + } + ; + +exclusive_or_expression + : and_expression + { + $$=$1; + } + | exclusive_or_expression '^' and_expression + { + $$=changeTypeNEmit($1,$3,'^'); + } + ; + +inclusive_or_expression + : exclusive_or_expression + { + $$=$1; + } + | inclusive_or_expression '|' exclusive_or_expression + { + $$=changeTypeNEmit($1,$3,'|'); + } + ; + +logical_and_expression + : inclusive_or_expression{$$=$1;} + | logical_and_expression NList AND_OP MIndex inclusive_or_expression + { + if(!$5->isBoolExp) + int2Bool($5); + + if(!$1->isBoolExp){ + backpatch(*$2,nextInst()); + int2Bool($1); + } + backpatch($1->tl,$4); + $$->isBoolExp=true; + $$->tl=$5->tl; + $$->fl=merge($1->fl,$5->fl); + } + ; + + + +changeBoolTemp + :inclusive_or_expression + { + if($1->isBoolExp==false) + int2Bool($1); + $$=$1; + } + ; + +logical_or_expression + : logical_and_expression{$$=$1;} + | logical_or_expression NList OR_OP MIndex logical_and_expression + { + if(!$5->isBoolExp) + int2Bool($5); + + if(!$1->isBoolExp){ + backpatch(*$2,nextInst()); + int2Bool($1); + } + backpatch($1->fl,$4); + $$->isBoolExp=true; + $$->tl=merge($1->tl,$5->tl); + $$->fl=$5->fl; + } + ; + +conditional_expression + : logical_or_expression{$$=$1;} + | logical_or_expression NList '?' MIndex expression NList ':' + MIndex conditional_expression + { + GENTEMP($$); + UPDATE($$,$5->type); + + quadArray.push_back(Quad($$->name,$9->name)); + vi I=makelist(nextInst()); + quadArray.push_back(Quad(QGOTO,"...",0)); + + backpatch(*$6,nextInst()); + quadArray.push_back(Quad($$->name,$5->name)); + I=merge(I,makelist(nextInst())); + quadArray.push_back(Quad(QGOTO,"...",0)); + + if(!$1->isBoolExp) + { + backpatch(*$2,nextInst()); + int2Bool($1); + } + + backpatch($1->tl,$4); + backpatch($1->fl,$8); + backpatch(I,nextInst()); + } + ; + +assignment_expression + : conditional_expression{$$=$1;} + | unary_expression assignment_operator assignment_expression + { + //quadArray.push_back(Quad($$)) + //$$=changeTypeNEmit($1,$3,'='); + // int check=typeCheck($$->type,$1->type)&&typeCheck($1->type,$3->type); + //if(check==0) + // throw "type mismatch"; + //quadArray.push_back(Quad($1->name,$3->name)); + + Fields* f=checkTypesNAssign($1,$3); + + if($1->isArray) + { + quadArray.push_back(Quad(QARRDEREF,$1->arrayBase->name, + $1->arrSize->name,f->name)); + } + else if($1->isPointer) + { + quadArray.push_back(Quad(QPOINTERDER,$1->arrayBase->name, + f->name)); + } + else if($3->isBoolExp){ + backpatch($3->tl,nextInst()); + quadArray.push_back(Quad($1->name,"1")); + quadArray.push_back(Quad(QGOTO,nextInst()+2)); + backpatch($3->fl,nextInst()); + quadArray.push_back(Quad($1->name,"0")); + } + else + quadArray.push_back(Quad($1->name,f->name)); + $$=$1; + } + ; + +assignment_operator + : '=' + ; + +expression + : assignment_expression{$$=$1;} + | expression ',' assignment_expression + ; + +constant_expression + : conditional_expression{$$=$1;} + ; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +id_parser + :IDENTIFIER + { + $$=st->lookup(yytext); + } +CONSTANT + : INTEGER_CONSTANT + { + $$=SymbolTable::gentemp(*st); + $$->type.push_back(ii(intT,0)); + UPDATE($$); + $$->isConst=true; + $$->val.intVal=atoi(yytext); + quadArray.push_back(Quad($$->name,yytext)); + + } + | CHARACTER_CONSTANT + { + $$=SymbolTable::gentemp(*st); + $$->type.push_back(ii(charT,0)); + UPDATE($$); + char *temp=strdup(yytext); + temp++; + temp[strlen(temp)-1]='\0'; + sprintf(temp,"%d",temp[0]); + $$->isConst=true; + $$->val.intVal=yytext[1]; + $$->val.charVal=yytext[1]; + quadArray.push_back(Quad($$->name,temp)); + + } + | FLOATING_CONSTANT + { + $$=SymbolTable::gentemp(*st); + $$->type.push_back(ii(doubleT,0)); + UPDATE($$); + $$->isConst=true; + $$->val.doubleVal=atof(yytext); + quadArray.push_back(Quad($$->name,yytext)); + } + + + +%% + + + + +Fields* char2int(Fields* f) +{ + Fields* res; + GENTEMP(res); + res->type.push_back(ii(intT,0)); + UPDATE(res); + quadArray.push_back(Quad(QCHAR2INT,res->name,f->name)); + return res; +} + +Fields* int2double(Fields* f) +{ + Fields* res; + GENTEMP(res); + res->type.push_back(ii(doubleT,0)); + UPDATE(res); + string val="int2double( "; + string end=" )"; + quadArray.push_back(Quad(res->name,val+string(f->name)+end)); + return res; +} +Fields* char2double(Fields* f) +{ + Fields* res; + GENTEMP(res); + res->type.push_back(ii(doubleT,0)); + UPDATE(res); + string val="char2double( "; + string end=" )"; + quadArray.push_back(Quad(res->name,val+string(f->name)+end)); + return res; +} + + +Fields* double2char(Fields* f) +{ + Fields* res; + GENTEMP(res); + res->type.push_back(ii(doubleT,0)); + UPDATE(res); + string val="double2char( "; + string end=" )"; + quadArray.push_back(Quad(res->name,val+string(f->name)+end)); + return res; +} + +Fields* double2int(Fields* f) +{ + Fields* res; + GENTEMP(res); + res->type.push_back(ii(doubleT,0)); + UPDATE(res); + string val="double2int( "; + string end=" )"; + quadArray.push_back(Quad(res->name,val+string(f->name)+end)); + return res; +} +Fields* int2char(Fields* f) +{ + Fields* res; + GENTEMP(res); + res->type.push_back(ii(doubleT,0)); + UPDATE(res); + quadArray.push_back(Quad(QINT2CHAR,res->name,f->name)); + return res; +} + + +/* + This is for binary operations. checks the types and converts + one of them to other suitable type. + int,double ==> double,dpuble + int,char ==> int,int + char,double -->double,double + and so on + It returns the temporary of t=f1 op f2 + after chnaging f2 or f1 to suitable types +*/ +Fields* changeTypeNEmit(Fields* f1,Fields* f2,int op) +{ + if(f1->type.size()==0||f2->type.size()==0) + throw "non initialized types"; + if((f1->type.size()>1||f2->type.size()>1)&& + !f1->isArray&&!f1->isPointer&&f1->type[0].first!=pointerT&& + f1->type[0].first!=arrayT) + throw "invalid type Changing changeTypeNEmit"; + int check=typeCheck(f1->type,f2->type); + + //f1->print();f2->print(); + Fields* arg1=f1,*arg2=f2,*res; + GENTEMP(res); + + int t1=f1->type[0].first; + int t2=f2->type[0].first; + + int greaterT=min(t1,t2),lesserT=max(t1,t2); + + if(lesserT==arrayT||lesserT==pointerT) + { + swap(lesserT,greaterT); + } + + //by def arg1 holds the greater type + if(greaterT!=t1) + swap(arg1,arg2); + + + if(greaterT==lesserT){ + //both are char + if(greaterT==charT){ + arg1=char2int(arg1); + arg2=char2int(arg2); + res->type.push_back(ii(intT,0)); + UPDATE(res); + } + else{ + res->type.push_back(ii(greaterT,0)); + UPDATE(res); + } + } + else if(greaterT==intT){ + + arg2=char2int(arg2); + res->type.push_back(ii(intT,0)); + UPDATE(res); + } + else if(greaterT==doubleT) + { + if(lesserT==intT) + arg2=int2double(arg2); + else + arg2=char2double(arg2); + res->type.push_back(ii(doubleT,0)); + UPDATE(res); + } + else if(greaterT==arrayT||greaterT==pointerT) + { + if(lesserT>charT||lesserT==doubleT) + throw "unsupprted array operations"; + + if(lesserT==charT) + arg2=char2int(arg2); + + + if(op=='+'||op=='-') + { + Fields* f;GENTEMP(f); + f->type.push_back(ii(intT,0)); + UPDATE(f); + Type temp(arg1->type.begin()+1,arg1->type.end()); + int s=getSize(temp); + char word[20];sprintf(word,"%d",s); + quadArray.push_back(Quad('*',f->name,arg2->name, + word)); + quadArray.push_back(Quad('+',res->name,arg1->name, + f->name)); + res->type.clear(); + Type temp2; + temp2.push_back(ii(pointerT,0)); + temp2.insert(temp2.begin()+1,temp.begin(),temp.end()); + res->type=(greaterT==pointerT)?arg1->type:temp2; + UPDATE(res); + return res; + } + else + throw "unsupprted array operations"; + } + quadArray.push_back(Quad(op,res->name,arg1->name + ,arg2->name)); + + return res; + +} + + +/* + same as above but returns both of them +*/ +pair changeTypeNReturn(Fields* f1,Fields* f2) +{ + if(f1->type.size()>1||f2->type.size()>1) + throw "invalid type Changing changeTypeNReturn"; + + Fields* arg1=f1,*arg2=f2; + + int t1=f1->type[0].first; + int t2=f2->type[0].first; + + int greaterT=min(t1,t2),lesserT=max(t1,t2); + + //by def arg1 holds the greater type + if(greaterT!=t1) + swap(arg1,arg2); + + + if(greaterT==lesserT){ + //both are char + if(greaterT==charT){ + arg1=char2int(arg1); + arg2=char2int(arg2); + } + } + else if(greaterT==intT){ + arg2=char2int(arg2); + } + else if(greaterT==doubleT) + { + if(lesserT==intT) + arg2=int2double(arg2); + else + arg2=char2double(arg2); + } + return make_pair(arg1,arg2); +} + +/* + converts an integer expression to a boolExp with true and false lists +*/ +void int2Bool(Fields* f) +{ + Fields* arg=f; + if(f->type[0].first==charT) + arg=char2int(f); + if(f->type[0].first==doubleT) + arg=double2int(f); + + f->isBoolExp=true; + + f->fl.push_back(nextInst()); + quadArray.push_back(Quad(QRELIFEQ,"...",arg->name,"0")); + + f->tl.push_back(nextInst()); + quadArray.push_back(Quad(QGOTO,"...",0)); + +} + +inline void getValueNBackpatch(Fields* f) +{ + //backpatch(f->tl,nextInst()); + //quadArray.push_back(Quad(f->name,"1")); + //f->tl=makelist(nextInst()); + //quadArray.push_back(Quad(QGOTO,"...",0)); + + //backpatch(f->fl,nextInst()); + //quadArray.push_back(Quad(f->name,"0")); + //f->fl=makelist(nextInst()); + //quadArray.push_back(Quad(QGOTO,"...",0)); +} + + +/* + This is for assigning types i.e f1=f2; + it converts f2 to suitable type and then assigns the value + to f1 +*/ +Fields* checkTypesNAssign(Fields* f1,Fields* f2) +{ + bool res=typeCheck(f1->type,f2->type); + + + if(!res){ + if(f1->type.size()>1||f2->type.size()>1) + throw "assigning types dont match"; + int t1=f1->type[0].first,t2=f2->type[0].first; + + Fields *temp; + + if(t1==intT&&t2==doubleT) + temp=double2int(f2); + else if(t1==intT&&t2==charT) + temp=char2int(f2); + else if(t1==charT&&t2==intT) + temp=int2char(f2); + else if(t1==charT&&t2==doubleT) + temp=double2char(f2); + else if(t1==doubleT&&t2==intT) + temp=int2double(f2); + else if(t1==doubleT&&t2==charT) + temp=char2double(f2); + return temp; + } + return f2; +} + + diff --git a/ass 6/ass5_12CS10037_test.c b/ass 6/ass5_12CS10037_test.c new file mode 100644 index 0000000..2e9a068 --- /dev/null +++ b/ass 6/ass5_12CS10037_test.c @@ -0,0 +1,115 @@ + +// This is the test file to test various compilers.You can add expressions here +// at check the output after doing make + +//By default it has KMP algorithm implemented and by running this function shows +// the 4-address quads for KMP algorithm + + + + + + +int prints(char *x); +int printi(int p); +int readi(int *eP); +int printi2(int a,int b); +int printc(char a); +int reads(char*a); +int printf(char*s,int a) +{ + return prints(s)+printi(a); +} +int printf2(char*s,int a,int b) +{ + return prints(s)+printi(a)+printi(b); +} + +int strlen(char* s) +{ + int i=0; + while(s[i]!='\0')i++; + return i; +} + +int determinant(int (*f)[20],int x) +{ + int pr,c[20],d=0,b[20][20],j,p,q,t; + if(x==2) + { + d=0; + d=(f[1][1]*f[2][2])-(f[1][2]*f[2][1]); + return(d); + } + else + { + for(j=1;j<=x;j++) + { + int r=1,s=1; + for(p=1;p<=x;p++) + { + for(q=1;q<=x;q++) + { + if(p!=1&&q!=j) + { + b[r][s]=f[p][q]; + s++; + if(s>x-1) + { + r++; + s=1; + } + } + } + } + for(t=1,pr=1;t<=(1+j);t++) + pr=(-1)*pr; + c[j]=pr*determinant(b,x-1); + } + for(j=1,d=0;j<=x;j++) + { + d=d+(f[1][j]*c[j]); + } + return(d); + } +} + + +int adn(int a,int b) +{ + +} + + +int main() +{ + int i,j,m; + int a[20][20]; + + prints("\n\nEnter order of matrix : "); + readi(&m); + prints("\nEnter the elements of matrix\n"); + for(i=1;i<=m;i++) + { + for(j=1;j<=m;j++) + { + printf("a[",i); printf("][",j);prints("] "); + readi(&a[i][j]); + } + } + prints("\n\n---------- Matrix A is --------------\n"); + for(i=1;i<=m;i++) + { + prints("\n"); + for(j=1;j<=m;j++) + { + printf("\t",a[i][j]);prints("\t"); + } + } + prints("\n \n"); + int res=determinant(&a[0],m); + + printf("\n Determinant of Matrix A is ",res);prints("\n"); + +} + diff --git a/ass 6/ass5_12CS10037_test.s b/ass 6/ass5_12CS10037_test.s new file mode 100644 index 0000000..8afc125 --- /dev/null +++ b/ass 6/ass5_12CS10037_test.s @@ -0,0 +1,334 @@ + .file "ass5_12CS10037_test.c" + .text + .globl printf + .type printf, @function +printf: + pushl %ebp + movl %esp, %ebp + pushl %ebx + subl $20, %esp + movl 8(%ebp), %eax + movl %eax, (%esp) + call prints + movl %eax, %ebx + movl 12(%ebp), %eax + movl %eax, (%esp) + call printi + addl %ebx, %eax + addl $20, %esp + popl %ebx + popl %ebp + ret + .size printf, .-printf + .globl printf2 + .type printf2, @function +printf2: + pushl %ebp + movl %esp, %ebp + pushl %ebx + subl $20, %esp + movl 8(%ebp), %eax + movl %eax, (%esp) + call prints + movl %eax, %ebx + movl 12(%ebp), %eax + movl %eax, (%esp) + call printi + addl %eax, %ebx + movl 16(%ebp), %eax + movl %eax, (%esp) + call printi + addl %ebx, %eax + addl $20, %esp + popl %ebx + popl %ebp + ret + .size printf2, .-printf2 + .globl strlen + .type strlen, @function +strlen: + pushl %ebp + movl %esp, %ebp + subl $16, %esp + movl $0, -4(%ebp) + jmp .L6 +.L7: + addl $1, -4(%ebp) +.L6: + movl -4(%ebp), %edx + movl 8(%ebp), %eax + addl %edx, %eax + movzbl (%eax), %eax + testb %al, %al + jne .L7 + movl -4(%ebp), %eax + leave + ret + .size strlen, .-strlen + .globl determinant + .type determinant, @function +determinant: + pushl %ebp + movl %esp, %ebp + subl $1736, %esp + movl $0, -1716(%ebp) + cmpl $2, 12(%ebp) + jne .L10 + movl $0, -1716(%ebp) + movl 8(%ebp), %eax + addl $80, %eax + movl 4(%eax), %edx + movl 8(%ebp), %eax + addl $160, %eax + movl 8(%eax), %eax + imull %eax, %edx + movl 8(%ebp), %eax + addl $80, %eax + movl 8(%eax), %ecx + movl 8(%ebp), %eax + addl $160, %eax + movl 4(%eax), %eax + imull %ecx, %eax + subl %eax, %edx + movl %edx, %eax + movl %eax, -1716(%ebp) + movl -1716(%ebp), %eax + jmp .L23 +.L10: + movl $1, -1712(%ebp) + jmp .L12 +.L20: + movl $1, -1696(%ebp) + movl $1, -1692(%ebp) + movl $1, -1708(%ebp) + jmp .L13 +.L17: + movl $1, -1704(%ebp) + jmp .L14 +.L16: + cmpl $1, -1708(%ebp) + je .L15 + movl -1704(%ebp), %eax + cmpl -1712(%ebp), %eax + je .L15 + movl -1708(%ebp), %edx + movl %edx, %eax + sall $2, %eax + addl %edx, %eax + sall $4, %eax + movl %eax, %edx + movl 8(%ebp), %eax + addl %eax, %edx + movl -1704(%ebp), %eax + movl (%edx,%eax,4), %ecx + movl -1696(%ebp), %edx + movl %edx, %eax + sall $2, %eax + addl %edx, %eax + sall $2, %eax + movl -1692(%ebp), %edx + addl %edx, %eax + movl %ecx, -1608(%ebp,%eax,4) + addl $1, -1692(%ebp) + movl 12(%ebp), %eax + subl $1, %eax + cmpl -1692(%ebp), %eax + jge .L15 + addl $1, -1696(%ebp) + movl $1, -1692(%ebp) +.L15: + addl $1, -1704(%ebp) +.L14: + movl -1704(%ebp), %eax + cmpl 12(%ebp), %eax + jle .L16 + addl $1, -1708(%ebp) +.L13: + movl -1708(%ebp), %eax + cmpl 12(%ebp), %eax + jle .L17 + movl $1, -1700(%ebp) + movl $1, -1720(%ebp) + jmp .L18 +.L19: + negl -1720(%ebp) + addl $1, -1700(%ebp) +.L18: + movl -1712(%ebp), %eax + addl $1, %eax + cmpl -1700(%ebp), %eax + jge .L19 + movl 12(%ebp), %eax + subl $1, %eax + movl %eax, 4(%esp) + leal -1608(%ebp), %eax + movl %eax, (%esp) + call determinant + imull -1720(%ebp), %eax + movl %eax, %edx + movl -1712(%ebp), %eax + movl %edx, -1688(%ebp,%eax,4) + addl $1, -1712(%ebp) +.L12: + movl -1712(%ebp), %eax + cmpl 12(%ebp), %eax + jle .L20 + movl $1, -1712(%ebp) + movl $0, -1716(%ebp) + jmp .L21 +.L22: + movl 8(%ebp), %eax + leal 80(%eax), %edx + movl -1712(%ebp), %eax + movl (%edx,%eax,4), %edx + movl -1712(%ebp), %eax + movl -1688(%ebp,%eax,4), %eax + imull %edx, %eax + addl %eax, -1716(%ebp) + addl $1, -1712(%ebp) +.L21: + movl -1712(%ebp), %eax + cmpl 12(%ebp), %eax + jle .L22 + movl -1716(%ebp), %eax +.L23: + leave + ret + .size determinant, .-determinant + .globl adn + .type adn, @function +adn: + pushl %ebp + movl %esp, %ebp + popl %ebp + ret + .size adn, .-adn + .section .rodata +.LC0: + .string "\n\nEnter order of matrix : " + .align 4 +.LC1: + .string "\nEnter the elements of matrix\n" +.LC2: + .string "a[" +.LC3: + .string "][" +.LC4: + .string "] " + .align 4 +.LC5: + .string "\n\n---------- Matrix A is --------------\n" +.LC6: + .string "\n" +.LC7: + .string "\t" +.LC8: + .string "\n \n" + .align 4 +.LC9: + .string "\n Determinant of Matrix A is " + .text + .globl main + .type main, @function +main: + pushl %ebp + movl %esp, %ebp + andl $-16, %esp + subl $1632, %esp + movl $.LC0, (%esp) + call prints + leal 16(%esp), %eax + movl %eax, (%esp) + call readi + movl $.LC1, (%esp) + call prints + movl $1, 20(%esp) + jmp .L26 +.L29: + movl $1, 24(%esp) + jmp .L27 +.L28: + movl 20(%esp), %eax + movl %eax, 4(%esp) + movl $.LC2, (%esp) + call printf + movl 24(%esp), %eax + movl %eax, 4(%esp) + movl $.LC3, (%esp) + call printf + movl $.LC4, (%esp) + call prints + leal 32(%esp), %ecx + movl 20(%esp), %edx + movl %edx, %eax + sall $2, %eax + addl %edx, %eax + sall $2, %eax + movl 24(%esp), %edx + addl %edx, %eax + sall $2, %eax + addl %ecx, %eax + movl %eax, (%esp) + call readi + addl $1, 24(%esp) +.L27: + movl 16(%esp), %eax + cmpl %eax, 24(%esp) + jle .L28 + addl $1, 20(%esp) +.L26: + movl 16(%esp), %eax + cmpl %eax, 20(%esp) + jle .L29 + movl $.LC5, (%esp) + call prints + movl $1, 20(%esp) + jmp .L30 +.L33: + movl $.LC6, (%esp) + call prints + movl $1, 24(%esp) + jmp .L31 +.L32: + movl 20(%esp), %edx + movl %edx, %eax + sall $2, %eax + addl %edx, %eax + sall $2, %eax + movl 24(%esp), %edx + addl %edx, %eax + movl 32(%esp,%eax,4), %eax + movl %eax, 4(%esp) + movl $.LC7, (%esp) + call printf + movl $.LC7, (%esp) + call prints + addl $1, 24(%esp) +.L31: + movl 16(%esp), %eax + cmpl %eax, 24(%esp) + jle .L32 + addl $1, 20(%esp) +.L30: + movl 16(%esp), %eax + cmpl %eax, 20(%esp) + jle .L33 + movl $.LC8, (%esp) + call prints + movl 16(%esp), %eax + movl %eax, 4(%esp) + leal 32(%esp), %eax + movl %eax, (%esp) + call determinant + movl %eax, 28(%esp) + movl 28(%esp), %eax + movl %eax, 4(%esp) + movl $.LC9, (%esp) + call printf + movl $.LC6, (%esp) + call prints + leave + ret + .size main, .-main + .ident "GCC: (Ubuntu 4.8.1-2ubuntu1~12.04) 4.8.1" + .section .note.GNU-stack,"",@progbits diff --git a/ass 6/ass5_12CS10037_translator.cpp b/ass 6/ass5_12CS10037_translator.cpp new file mode 100644 index 0000000..2f13fc5 --- /dev/null +++ b/ass 6/ass5_12CS10037_translator.cpp @@ -0,0 +1,68 @@ +#include "ass5_12CS10037_translator.h" + + +void backpatch(ListType &v,int index) +{ + char word[20]; + sprintf(word, "%d",index); + for (int i = 0; i < v.size(); ++i){ + quadArray[v[i]].setRes(word); + } + v.clear(); +} + + + +int typeCheck(Type &t1,Type &t2) +{ + // int x=E1[0].first,y=E2[0].first; + // if(x==y){ + // if(x>=intT&&x<=voidT) + // return true; + // else if(x==arrayT){ + // int l1=E1.array.size(),l2=E2.array.size(); + // if(l1!=l2) + // return false; + // for (int i = 1; i < l1; ++i){ + // if(E1.array[i]!=E2.array[i]) + // return false; + // } + // } + // else if(x==pointerT) + // { + // int l1=E1.pointer.size(),l2=E2.pointer.size(); + // if(l1!=l2) + // return false; + // for (int i = 1; i < l1; ++i){ + // if(E1.pointer[i]!=E2.pointer[i]) + // return false; + // } + // } + // } + if(t1.size()!=t2.size()) + return false; + int s=t1.size(); + for (int i = 0; i + + + + + + + + + + + +extern std::vector quadArray; +using namespace std; + +class SymbolTable; + + + +typedef std::vector ListType; +/* + A global function to create a new list containing only index, an index + into the array of quad’s, and to returns the newly created list. +*/ +inline ListType makelist(int index){ + ListType v; + v.push_back(index); + return v; +} + +/* + A global function to concatenate two lists v1 v2 and to + return the concatenated list. +*/ +inline ListType merge(const vector& v1,const ListType& v2){ + ListType v(v1); + v.insert(v.begin(), v2.begin(), v2.end()); + return v; +} + +/* + A global function to insert index as the target label for each of the + quad’s on the list in v + +*/ +void backpatch(ListType &v,int index); + +/* + A global function to check if E1 & E2 have same types + (that is, if = (E)). +*/ +int typeCheck(Type &E1,Type &E2); +inline int typeCheck(Fields* f1,Fields* f2){ + return typeCheck(f1->type,f2->type); +} + + + + +#endif \ No newline at end of file diff --git a/ass 6/assignment6.pdf b/ass 6/assignment6.pdf new file mode 100644 index 0000000..1a69ef1 Binary files /dev/null and b/ass 6/assignment6.pdf differ diff --git a/ass 6/libmyl.a b/ass 6/libmyl.a new file mode 100644 index 0000000..bd5d3ad Binary files /dev/null and b/ass 6/libmyl.a differ diff --git a/ass 6/look into target/a.out b/ass 6/look into target/a.out new file mode 100755 index 0000000..0e0a9e9 Binary files /dev/null and b/ass 6/look into target/a.out differ diff --git a/ass 6/myfile.c b/ass 6/myfile.c new file mode 100644 index 0000000..116ac9f --- /dev/null +++ b/ass 6/myfile.c @@ -0,0 +1,92 @@ +#include "stdio.h" +#include "SymbolTable.h" +#include "ass5_12CS10037_translator.h" +#include "y.tab.h" +#include +extern int yylex(); +extern int yyparse(); +extern char* yytext; +SymbolTable* _GLOBST=new SymbolTable(); +extern int line_number; +extern SymbolTable* st; + +void yyerror(char* s) +{ + printf("%s at line_number %d\n",s,line_number); +} +int main() +{ + freopen("ass5_12CS10037_test.c", "r", stdin); + // freopen("stat_test_output","w",stdout); + int token; + std::string s; + try{ + yyparse(); + } + catch(const char* p) + { + printf("\nERROR: %s on line_number %d\n",p,line_number+1 ); + st->print(); + } + printf("Global SymbolTable\n"); + _GLOBST->print(); + + for (int i = 0; i ",s.c_str()); + // } + // printf("%s\n", yytext); \ No newline at end of file diff --git a/ass 6/myl.c b/ass 6/myl.c new file mode 100644 index 0000000..5654081 --- /dev/null +++ b/ass 6/myl.c @@ -0,0 +1,202 @@ +#include "myl.h" +#define BUF 20 +#define ERR 1 +#define OK 0 +#include "stdio.h" +void asmprint(char* buff,int bytes) +{ + __asm__ __volatile__ ( + "movl $4, %%eax \n\t" + "movl $1, %%ebx \n\t" + "int $128 \n\t" + : + :"c"(buff), "d"(bytes) + ) ; +} +int asmread(char* buff,int count) +{ + int readBytes=1; + while(readBytes==1){ + __asm__ __volatile__ ( + "movl $3, %%eax \n\t" + "movl $1, %%ebx \n\t" + "int $128 \n\t" + "movl %%eax,%0" + :"=r"(readBytes) + :"c"(buff), "d"(count) + ) ;} + + return readBytes-1; +} +int prints(char *word) +{ + int len=0; + while(word[len]!='\0')len++; + asmprint(word,len); + return len; +} + +int printi(int n) +{ + int len=0; + char buffer[BUF]; + int neg=0; + if(n==0){ + prints("0"); + return 1; + } + + if(n<0){ + n*=-1; + neg=1; + } + + int rev=0,rem; + while(n!=0){ + rem=n%10; + n=n/10; + buffer[len++]=rem+'0'; + rev=rev*10+rem; + } + if(neg) + buffer[len++]='-'; + int i=0; + for ( ; i < len/2; ++i){ + char t=buffer[i]; + buffer[i]=buffer[len-i-1]; + buffer[len-i-1]=t; + } + + buffer[len]='\0'; + asmprint(buffer, len); + + return len; +} + +int readi(int* eP) +{ + char buffer[BUF]; + int bytes=asmread(buffer, BUF); + int num=0; + if(bytes<=0) + return ERR; + int i,neg=0; + if(buffer[0]=='-') + neg=1; + for (i=((neg==1) ?1:0); i < bytes; ++i){ + if( buffer[i]>='0'&&buffer[i]<='9') + num=num*10+(buffer[i]-'0'); + else { + if(neg) + num*=-1; + *eP=num; + return ERR; + } + + } + if(neg) + num*=-1; + *eP=num; + return OK; +} + +int readf(float* eP) +{ + char buffer[BUF]; + int bytes=asmread(buffer,BUF); + int i; + int n,neg=0; + float res,tens=1; + + + if(buffer[0]=='-') + neg=1; + for ( i=((neg==1) ?1:0); i < bytes&&buffer[i]!='.'; ++i){ + if( buffer[i]>='0'&&buffer[i]<='9') + n=n*10+(buffer[i]-'0'); + else { + if(neg) + n*=-1; + *eP=n; + return ERR; + } + + } + res=n; + for ( ++i; i < bytes; ++i){ + if( buffer[i]>='0'&&buffer[i]<='9') + n=(buffer[i]-'0'); + else { + res+=n/tens; + if(neg) + res*=-1; + *eP=res; + return ERR; + } + + tens*=10; + res+=n/tens; + } + + if(neg) + res*=-1; + + *eP=res; + return OK; +} + +int printd(float f) +{ + float x=f-0; + int printLen=0; + char buffer[BUF]; + if(x<0) + x*=-1; + if(x<0.0000001) + { + buffer[0]='0'; + asmprint(buffer, 1); + return 1; + } + + + int PRECISION=6; + int num=(int)f; + if(num==0) + printLen+=prints("-"); + printLen+=printi(num); + + f=f-num; + + printLen+=prints("."); + while(PRECISION--) + { + f*=10; + num=(int)f; + f-=num; + if(num<0) + num*=-1; + printLen+=printi(num); + } + return printLen; +} + + +int printi2(int a,int b) +{ + return printi(a)+prints(" ")+printi(b)+prints("\n"); +} +int printc(char a) +{ + char word[2]; + word[0]=a; + word[1]='\0'; + prints(word); + return 1; +} +int reads(char*a) +{ + int x= asmread(a, 200); + a[x]='\0'; + return x; +} \ No newline at end of file diff --git a/ass 6/myl.h b/ass 6/myl.h new file mode 100644 index 0000000..e1186a6 --- /dev/null +++ b/ass 6/myl.h @@ -0,0 +1,14 @@ +#ifndef _MYL_H +#define _MYL_H +#define ERR 1 +#define OK 0 +int prints(char *); +int printi(int); +int readi(int *eP); // *eP is for error, if the input is not an integer +int readf(float *); // return value is error or OK +int printd(float); +int prints(char *); +int printi2(int a,int b); +int printc(char a); +int reads(char*a); +#endif diff --git a/ass 6/myl.o b/ass 6/myl.o new file mode 100644 index 0000000..8ecc754 Binary files /dev/null and b/ass 6/myl.o differ diff --git a/ass 6/quad.cpp b/ass 6/quad.cpp new file mode 100644 index 0000000..453ef6d --- /dev/null +++ b/ass 6/quad.cpp @@ -0,0 +1,167 @@ +#include "quad.h" +#include +namespace quad + +{ + +Quad::Quad(int op1, char *s1, char *s2, char *s3) +:op(op1), res(strdup(s1)), arg1(strdup(s2)), arg2(strdup(s3)) { } + +Quad::Quad(int op1,const string &s1,const string &s2,const string& s3) +{ + op=op1; + res=strdup(s1.c_str()); + arg1=strdup(s2.c_str()); + arg2=strdup(s3.c_str()); +} + +Quad::Quad(int op1,const string &s1,const string &s2) +{ + op=op1; + res=strdup(s1.c_str()); + arg1=strdup(s2.c_str()); + arg2=NULL; +} + + +Quad::Quad(int op1, char *s, int num) +:op(op1), res(strdup(s)),arg2(NULL) +{ + arg1 = new char[15]; + sprintf(arg1, "%d", num); +} +Quad::Quad(int op1, const string &s, int num){ + Quad(op1,s.c_str(),num); +} + +Quad::Quad(char* s1,char* s2) +:op(0),res(strdup(s1)),arg1(strdup(s2)),arg2(NULL){} + +Quad::Quad(const string & s1,const string & s2) +:op(0),res(strdup(s1.c_str())),arg1(strdup(s2.c_str())),arg2(NULL) +{ +} + + +Quad::Quad(int op,int index) +:op(op) +{ + res = new char[15];arg1=arg2=NULL; + sprintf(res, "%d", index); +} + + +void Quad::emit(Quad &q){ + if(q.arg2!=NULL&&q.arg1!=NULL){ + //---arithmetic bit logical shift + switch(q.op){ + case QARRDEREF:printf("%s[%s] = %s\n", q.res,q.arg1,q.arg2); + break; + case QARRVAL:printf("%s = %s[%s]\n",q.res,q.arg1,q.arg2 ); + break; + case QRELIFEQ:q.gotoIndex=atof(q.res ); + printf("if %s = %s goto %d\n",q.arg1,q.arg2,q.gotoIndex); + + break; + case QRENOTE:q.gotoIndex=atof(q.res ); + printf("if %s != %s goto %d\n",q.arg1,q.arg2,q.gotoIndex); + + break; + case QRELIFGT:q.gotoIndex=atof(q.res ); + printf("if %s %c %s goto %d\n",q.arg1,'>',q.arg2,q.gotoIndex); + + break; + case QRELIFGTE:q.gotoIndex=atof(q.res ); + printf("if %s >= %s goto %d\n",q.arg1,q.arg2,q.gotoIndex); + + break; + case QRELIFLT:q.gotoIndex=atof(q.res ); + printf("if %s %c %s goto %d\n",q.arg1,'<',q.arg2,q.gotoIndex); + + break; + case QRELIFLTE:q.gotoIndex=atof(q.res ); + printf("if %s <= %s goto %d\n",q.arg1,q.arg2,q.gotoIndex); + + break; + + case QCALL:printf("%s=call %s,%s\n",q.res,q.arg1,q.arg2 ); + break; + case QPASS:printf("pass\n"); + break; + default: + if(q.op<=255) + printf("%s = %s %c %s\n",q.res,q.arg1,q.op,q.arg2 ); + else{ + if(q.op==267) + printf("%s = %s << %s\n",q.res,q.arg1,q.arg2 ); + else if(q.op==268) + printf("%s = %s >> %s\n",q.res,q.arg1,q.arg2 ); + else + printf("%s = %s %d %s\n",q.res,q.arg1,q.op,q.arg2 ); + } + + } + + + } + else if(q.arg2==NULL&&q.op!=0){ + + switch(q.op) + { + case QGOTO:q.gotoIndex=atof(q.res ); + printf("goto %d\n", q.gotoIndex); + + break; + case QIF:q.gotoIndex=atof(q.res ); + printf("if %s goto %d\n",q.arg1,q.gotoIndex); + + break; + case QIFFALSE:q.gotoIndex=atof(q.res ); + printf("ifFalse %s goto %d\n",q.arg1,q.gotoIndex); + + break; + case QPARAM:printf("param %s\n",q.res ); + break; + case QPOINTER:printf("%s=*%s\n",q.res,q.arg1 ); + break; + case QPOINTERDER:printf("*%s=%s\n",q.res,q.arg1); + break; + case QADDR:printf("%s=&%s\n",q.res,q.arg1 ); + break; + case QRETURN:printf("return %s\n",q.res); + break; + case QRETURN_NULL:printf("return\n"); + break; + case QFUNCEND:printf("function end\n"); + break; + case QFUNC:printf("function %s\n",q.res ); + break; + case QINT2CHAR:printf("%s=INT2CHAR(%s)\n",q.res,q.arg1 ); + break; + case QCHAR2INT:printf("%s=CHAR2INT(%s)\n",q.res,q.arg1 ); + break; + case QPASS:printf("pass\n"); + break; + case QSTRING:printf("%s=\"%s\"\n",q.res,q.arg1 ); + break; + default: + if(q.op<=255) + printf("%s = %c %s\n",q.res,q.op,q.arg1 ); + else + printf("%s = %d %s\n",q.res,q.op,q.arg1 ); + + } + + + } + else + { + if(q.op==QPASS) + printf("pass\n"); + else + printf("%s = %s\n",q.res,q.arg1 ); + } + +} + +}//end of quad namespace \ No newline at end of file diff --git a/ass 6/quad.h b/ass 6/quad.h new file mode 100644 index 0000000..cb32857 --- /dev/null +++ b/ass 6/quad.h @@ -0,0 +1,78 @@ +#ifndef _QUAD_H +#define _QUAD_H +#include +using namespace std; +namespace quad +{ + +/* + These are all macros to be used so that we can know what type of quad it represents + +*/ +enum{ + QIF=1, //used for simple IF + QIFFALSE, //used for ifFalse + QRELIFEQ, // if == + QRELIFLT, //if < + QRELIFGT, // if > + QRELIFLTE, // if <= + QRELIFGTE, //if >= + QRENOTE, //if != + QGOTO, //goto + + + QARRVAL, //a[x] + QARRDEREF,// + QEQ_OP, //== + QNE_OP, //!= + QGE_OP, //>= + QLE_OP, //<= + QPARAM, //function params + QCALL, //function call + QPOINTER, + QPOINTERDER, + QADDR, + QRETURN_NULL, + QRETURN, + QFUNC, + QFUNCEND, + QCHAR2INT, + QINT2CHAR, + QPASS, + QSTRING +}; + +/* + Quad class to represnt all possible type of quads we wish to emit. +*/ +class Quad{ + +public: + int op; //the operator present in quad + char* res,*arg1,*arg2; //all of the locations are stored as strings + int gotoIndex; + Quad(int op1, char *s1, char *s2, char *s3=0); //for binary operations and unary when s3=0 + Quad(int op1, char *s, int num); //unary operations + Quad(char* s1,char* s2); //assignment operations + Quad(int op,int index); //goto labels + + Quad(int op1, const string &s1, const string &s2, const string &s3); + Quad(int op1,const string &s1,const string &s2); + Quad(int op1, const string &s, int num); + Quad(const string & s1,const string & s2); + + + inline void setArg1(const string &s){ + this->arg1=strdup(s.c_str()); + }//to set arg1 operand + inline void setRes(const string &s){ + this->res=strdup(s.c_str()); + }//to set res operand + static void emit(Quad& q);//prints the current quad accordingly + +}; + +} + + +#endif \ No newline at end of file diff --git a/ass 6/quicksort_test.c b/ass 6/quicksort_test.c new file mode 100644 index 0000000..17dbc04 --- /dev/null +++ b/ass 6/quicksort_test.c @@ -0,0 +1,101 @@ + +// This is the test file to test various compilers.You can add expressions here +// at check the output after doing make + +//By default it has KMP algorithm implemented and by running this function shows +// the 3-address quads for KMP algorithm + + +//I've implemented almost all the things given except "function declarations" + + + +int prints(char *x); +int printi(int p); +int readi(int *eP); +int printi2(int a,int b); +int printc(char a); +int reads(char*a); +int printf(char*s,int a) +{ + return prints(s)+printi(a); +} + +// int strlen(char* s) +// { +// int i=0; +// while(s[i]!='\0')i++; +// return i; +// } + +void swap(int* a, int* b) +{ + int t = *a; + *a = *b; + *b = t; +} + +/* This function takes last element as pivot, places the pivot element at its + correct position in sorted array, and places all smaller (smaller than pivot) + to left of pivot and all greater elements to right of pivot */ +int partition (int *arr, int l, int h) +{ + int x = arr[h]; // pivot + int i = (l - 1),j; // Index of smaller element + + for ( j = l; j <= h- 1; j++) + { + // If current element is smaller than or equal to pivot + if (arr[j] <= x) + { + i++; // increment index of smaller element + swap(arr+i, arr+j); // Swap current element with index + } + } + swap(arr+i + 1, arr+h); + return (i + 1); +} + +/* *arr --> Array to be sorted, l --> Starting index, h --> Ending index */ +void quickSort(int *arr, int l, int h) +{ + if (l < h) + { + int p = partition(arr, l, h); /* Partitioning index */ + // printi2(p, l); + quickSort(arr, l, p - 1); + // printi2(p, h); + quickSort(arr, p + 1, h); + // prints("here12\n"); + } + // prints("here1\n"); + return; +} + +/* Function to print an array */ +void printArray(int *arr, int size) +{ + int i; + for (i=0; i < size; i++) + printf(" ", arr[i]); + prints("\n"); +} + +// Driver program to test above functions +int main() +{ + int arr[10]; + int n,i; + prints("enter the number of elements and all the elements:"); + readi(&n); + for (i = 0; i < n; ++i){ + readi(&arr[i]); + } + // swap(&n, &i); + prints("array before sorting"); + printArray(arr, n); + quickSort(arr, 0, n-1); + prints("Sorted array: \n"); + printArray(arr, n); + return 0; +} \ No newline at end of file diff --git a/ass 6/target_generate.cpp b/ass 6/target_generate.cpp new file mode 100644 index 0000000..2238e6e --- /dev/null +++ b/ass 6/target_generate.cpp @@ -0,0 +1,615 @@ +#include "ass5_12CS10037_translator.h" +#include "target_generate.h" + +#include "y.tab.h" + +extern int yylex(); +extern int yyparse(); +extern char* yytext; +SymbolTable* _GLOBST=new SymbolTable(); +extern int line_number; +extern SymbolTable* st; +const int NO_LABEL=-100; + +using namespace quad; +void yyerror(char* s) +{ + printf("%s at line_number %d\n",s,line_number); +} +int labelNo=0; +extern std::vector quadArray; +int labelArray[10000]={NO_LABEL}; +int paramESPoffset=0; +int stringLabelNo=0; + + + +SymbolTable* currst; + + + + + + + +int recursiveRemove(int index) +{ + if(quadArray[index].op!=QGOTO) + { + return index; + } + // printf("changed a goto!!! of %d\n",index); + return quadArray[index].gotoIndex=recursiveRemove(quadArray[index].gotoIndex); +} +void removeGotoNLabel() +{ + // for (int i = 0; i < quadArray.size(); ++i){ + // // printf("%d\n",quadArray[i].op ); + // if(quadArray[i].op<=QGOTO&&quadArray[i].op!=0) + // recursiveRemove(i); + // } + + for (int i = 0; i < quadArray.size(); ++i){ + if(quadArray[i].op==QGOTO&& quadArray[i].gotoIndex==i+1) + quadArray[i].op=QPASS; + } + + for (int i = 0; i < quadArray.size(); ++i){ + if(quadArray[i].op<=QGOTO&&quadArray[i].op!=0){ + int index=quadArray[i].gotoIndex; + if(labelArray[quadArray[i].gotoIndex]==NO_LABEL){ + labelArray[quadArray[i].gotoIndex]=labelNo++; + } + + } + } + + // for (int i = 0; i < quadArray.size(); ++i){ + // Quad&q=quadArray[i]; + // if(q.op!=QPARAM&&q.op!=QRETURN&&q.op!=QFUNC + // &&q.op!=QFUNCEND&&q.op!=QPASS&&q.op!=QCALL){ + + + // if(q.res!=NULL&&currst->isTemp(q.res)){ + // bool qpassF=true; + // For(j, i+1, quadArray.size()){ + + // Quad&q2=quadArray[j]; + // int boolVal; + // if(q2.op==QPARAM||q.op==QRETURN||q.op==QFUNC) + // boolVal=(strcmp(q2.res,q.res)==0)?1:0; + // else if(q2.arg1==NULL&&q2.arg2==NULL) + // continue; + // else if(q2.arg2==NULL) + // boolVal=(strcmp(q.res, q2.arg1)==0)?1:0; + // else if(q.arg1==NULL) + // boolVal=(strcmp(q.res, q2.arg2)==0)?1:0; + // else + // boolVal=(strcmp(q.res, q2.arg2)==0)||(strcmp(q.res, q2.arg1)==0); + + // if(boolVal){ + // qpassF=false; + // break; + // } + // } + // if(qpassF) + // q.op=QPASS; + // } + // } + + // } + + +} + + + + +void convOp(const Quad& q) +{ + Fields* res,*arg; + res=currst->search(q.res); + arg=currst->search(q.arg1); + if(q.op==QCHAR2INT){ + if(arg->isConst){ + emit(MOVB, arg->val.intVal,AL); + } + else + emit(MOVB,arg,AL); + emit(MOVSBL,AL,EaX); + emit(MOVL,EaX,res); + } + else if(q.op==QINT2CHAR){ + if(arg->isConst){ + emit(MOVL, arg->val.intVal,EaX); + } + else + emit(MOVL,arg,EaX); + emit(MOVB, AL,res); + } + else + throw "such a type conv not supported"; +} + +void functionStart(const Quad& q) +{ + /* + pushl %ebp + movl %esp, %ebp + andl $-16, %esp + subl $32, %esp + */ + + SymbolTable* ftable=_GLOBST->lookup(q.res)->nestedTable; + std::vector v=ftable->getConstStrings(); + For(i, 0, v.size()){ + v[i]->stringLabel=stringLabelNo; + string label=getStringLabelName(stringLabelNo); + emitStringLabel(label, v[i]->val.stringVal); + stringLabelNo++; + } + + emitLabel(q.res); + emit(PUSHL,EBP); + emit(MOVL,ESP,EBP); + if(currst->localOff-currst->stackOffset-4<0) + emit(ADDL,currst->localOff-currst->stackOffset-4,ESP); + paramESPoffset=0; +} + + +void functionEnd(const Quad &q) +{ + emit(LEAVE); + emit(RET); +} + + + + + + +void assignmentOpe(const Quad& q) +{ + Fields* res,*arg; + res=currst->search(q.res); + if(!res) + return; + + if(res->isConst) + { + return;} + + arg=currst->search(q.arg1); + + if(arg->type[0].first==arrayT&&res->type[0].first==pointerT) + { + emit(LEAL,arg,EaX); + emit(MOVL,EaX,res); + } + else + { + int s=getSize(res->type); + + string mov,reg; + mov=(s==1)?MOVB:MOVL; + reg=(s==1)?AL:EaX; + if(arg->isConst) + { + emit(mov,arg->val.intVal,res); + } + else{ + emit(mov,arg,reg); + emit(mov,reg,res); + } + } + +} + + +void binaryOp(const Quad& q) +{ + Fields* res,*arg1,*arg2; + res=currst->search(q.res); + arg1=currst->search(q.arg1); + arg2=currst->search(q.arg2); + + int op=q.op; + switch(op) + { + case '%': + case '/': + { + emit(MOVL,arg1,EaX); + emit(MOVL,arg2,EbX); + emit(CLTD); + emit(DIV,EbX); + string reg=(q.op=='%')?EdX:EaX; + emit(MOVL,reg,res); + + } + break; + + case '+': + case '-': + case '*': + { + + if(arg2==NULL){ + if(op=='+'&&strcmp(q.arg2,"1" )==0){ + emit(INCL,arg1); + } + else if(op=='-'&&strcmp(q.arg2,"1" )==0){ + emit(DECL,arg1); + } + else if(op=='*') + { + emit(MOVL,arg1,EaX); + emit(MUL,atoi(q.arg2),EaX); + emit(MOVL,EaX,res); + } + else + throw "gone again!!"; + + } + else{ + if(arg1->type[0].first==arrayT) + emit(LEAL,arg1,EaX); + else + emit(MOVL,arg1,EaX); + emit(MOVL,arg2,EbX); + string oper=(op=='+')?ADDL:((op=='-')?SUBL:MUL); + emit(oper,EbX,EaX); + emit(MOVL,EaX,res); + } + + } + break; + + case '>': + case QGE_OP: + case '<': + case QLE_OP: + case QEQ_OP: + case QNE_OP: + { + emit(MOVL,arg1,EaX); + emit(MOVL,arg2,EbX); + emit(CMP,EbX,EaX); + string oper; + switch(op) + { + case '>': + oper=SETG; + break; + case '<': + oper=SETL; + break; + case QGE_OP: + oper=SETGE; + break; + case QLE_OP: + oper=SETLE; + break; + case QEQ_OP: + oper=SETE; + break; + case QNE_OP: + oper=SETNE; + break; + } + emit(oper,AL); + emit(MOVZBL,AL,EaX); + emit(MOVL,EaX,res); + } + break; + + case QRENOTE: + case QRELIFEQ: + case QRELIFGT: + case QRELIFGTE: + case QRELIFLT: + case QRELIFLTE: + { + emit(MOVL,arg1,EaX); + if(arg2!=NULL) + emit(MOVL,arg2,EbX); + else + emit(MOVL,atoi(q.arg2),EbX); + emit(CMP,EbX,EaX); + string oper; + switch(op) + { + case QRELIFGT: + oper=JG; + break; + case QRELIFLT: + oper=JL; + break; + case QRELIFGTE: + oper=JGE; + break; + case QRELIFLTE: + oper=JLE; + break; + case QRELIFEQ: + oper=JE; + break; + case QRENOTE: + oper=JNE; + break; + } + + emit(oper,getLabelName(labelArray[q.gotoIndex])); + } + break; + + + + case QCALL: + { + emit(CALL,q.arg1); + paramESPoffset=0; + int s=getSize(res->type); + if(s==0) + break; + //stroing the return value in res + string reg=(s==1)?AL:EaX; + string op=(s==1)?MOVB:MOVL; + emit(op,reg,res); + } + break; + case QARRDEREF: + { + //res[arg1]=arg2 + int s=getSize(currst->search(q.arg2)->type); + string reg=(s==1)?AL:EaX; + string reg2=(s==1)?BL:EbX; + string op=(s==1)?MOVB:MOVL; + string addop=(s==1)?ADDB:ADDL; + if(res->type[0].first!=pointerT) + emit(LEAL,res,EaX); + else + emit(MOVL,res,EaX); + + emit(ADDL,arg1,EaX); + + emit(op,arg2,reg2); + emit(op,reg2,0,EaX); + } + break; + case QARRVAL: + { + //res=arg1[arg2] + int s=getSize(currst->search(q.res)->type); + string reg=(s==1)?AL:EaX; + string reg2=(s==1)?BL:EbX; + string op=(s==1)?MOVB:MOVL; + string addop=(s==1)?ADDB:ADDL; + if(arg1->type[0].first!=pointerT) + emit(LEAL,arg1,EaX); + else + emit(MOVL,arg1,EaX); + emit(ADDL,arg2,EaX); + emit(op,0,EaX,reg2); + emit(op,reg2,res); + + } + break; + } +} + +void unaryOp(const Quad&q) +{ + // Quad q1=q; + // Quad::emit(q1); + Fields *res,*arg; + if(q.op!=QRETURN_NULL&&q.op!=QFUNCEND&&q.op!=QGOTO) + res=currst->search(q.res); + if(q.op!=QRETURN_NULL&&q.op!=QFUNCEND&&q.op!=QRETURN + &&q.op!=QPARAM&&q.op!=QFUNC&&q.op!=QGOTO) + arg=currst->search(q.arg1); + int op=q.op; + + switch(op) + { + case QGOTO: + emit(JMP,getLabelName(labelArray[q.gotoIndex])); + break; + case QFUNC: + functionStart(q); + break; + case QFUNCEND: + functionEnd(q); + break; + + case QRETURN: + { + Fields *res=currst->search(q.res); + int s=getSize(res->type); + string reg=(s==1)?AL:EaX; + string op=(s==1)?MOVB:MOVL; + emit(op,res,EaX); + functionEnd(q); + } + + break; + case QRETURN_NULL: + functionEnd(q); + break; + + case QINT2CHAR: + case QCHAR2INT: + convOp(q); + break; + + case QPARAM: + { + if(res->type[0].first==arrayT&&res->type[0].first!=pointerT){ + emit(LEAL,res,EaX); + emit(MOVL,EaX,paramESPoffset,ESP); + paramESPoffset+=4; + } + else{ + int s=getSize(res->type); + string reg=(s==1)?AL:EaX; + string op=(s==1)?MOVB:MOVL; + + emit(op,res,reg); + emit(op,reg,paramESPoffset,ESP); + paramESPoffset+=(s==1)?1:4; + } + } + break; + + case QADDR: + { + emit(LEAL,arg,EaX); + emit(MOVL,EaX,res); + } + break; + + case QPOINTER: + { + //a=*b + int s=getSize(res->type); + string reg=(s==1)?AL:EaX; + string op=(s==1)?MOVB:MOVL; + emit(MOVL,arg,EaX); + emit(op,0,EaX,reg); + emit(op,reg,res); + } + break; + + case QPOINTERDER: + { + int s=getSize(arg->type); + string reg=(s==1)?AL:EaX; + string op=(s==1)?MOVB:MOVL; + string reg2=(s==1)?BL:EbX; + emit(op,arg,reg2); + emit(MOVL,res,EaX); + emit(op,reg2,0,EaX); + } + break; + + case QSTRING: + { + + } + break; + + case '-': + { + //unary minus + int s=getSize(res->type); + string reg=(s==1)?AL:EaX; + string op=(s==1)?MOVB:MOVL; + string negop=(s==1)?NEGB:NEGL; + emit(op,arg,reg); + emit(negop,reg); + emit(op,reg,res); + } + break; + } + +} + + + + + +void globalsEmit(string filename) +{ + + /* + .file "sum.c" + .text + .globl add + .type add, @function + */ + char word[40]; + sprintf(word, "\"%s\"",filename.c_str()); + emit(".file", word); + emit(".text"); + for (int i = 0; i < _GLOBST->table.size(); ++i){ + emit(".globl", _GLOBST->table[i].name.c_str()); + emit(".type", _GLOBST->table[i].name.c_str(),"@function"); + } +} + + + + + + + + +int main(int argc, char const *argv[]) +{ + const char* infile=(argc==1)?"ass5_12CS10037_test.c":argv[1]; + + freopen(infile, "r", stdin); + + // freopen("stat_test_output","w",stdout); + FILE *fp= fopen("out.s", "w"); + freopen("3ADC", "w", stdout); + int token; + std::string s; + try{ + yyparse(); + } + catch(const char* p) + { + fprintf(stdout,"\nERROR: %s on line_number %d\n",p,line_number+1 ); + st->print(); + } + + for (int i = 0; i < quadArray.size(); ++i){ + /* code */ + Quad &q=quadArray[i]; + printf("%3d:",i ); + Quad::emit(q); + } + _GLOBST->print(); + globalsEmit(infile); + for (int i = 0; i < quadArray.size(); ++i){ + labelArray[i]=NO_LABEL; + } + removeGotoNLabel(); + + + try{ + + for (int i = 0; i < quadArray.size(); ++i){ + // printf("\t"); + + Quad &q=quadArray[i]; + if(labelArray[i]!=NO_LABEL){ + emitLabel(getLabelName(labelArray[i])); + } + if(q.op==QPASS) + continue; + if(q.op==QFUNC){ + currst=_GLOBST->lookup(q.res)->nestedTable; + } + if(q.arg2!=NULL&&q.arg1!=NULL){ + binaryOp(q); + } + else if(q.arg2==NULL&&q.op!=0){ + unaryOp(q); + } + else + assignmentOpe(q); + } + } + catch(const char* s){ + printf("%s\n",s ); + } + fprintf(fp, "%s\n", outs); + fclose(fp); + + system("gcc -m32 out.s -L. -lmyl"); + return 0; + +} \ No newline at end of file diff --git a/ass 6/target_generate.h b/ass 6/target_generate.h new file mode 100644 index 0000000..714d219 --- /dev/null +++ b/ass 6/target_generate.h @@ -0,0 +1,132 @@ +#ifndef _TARGET_GEN_H +#define _TARGET_GEN_H +#include "ass5_12CS10037_translator.h" +char outs[1002020];int outIndex=0; +const string + EaX="%eax",A_EaX="(%eax)", + EbX="%ebx",A_EbX="(%ebx)", + EcX="%ecx",A_EcX="(%ecx)", + EdX="%edx",A_EdX="(%edx)", + EBP="%ebp",ESP="%esp", + AL="%al",BL="%bl", + + PUSHL="pushl",POPL="popl",PUSHB="pushb",POPB="popb", + + + ADDB="addb",ADDL="addl", + SUBB="subb",SUBL="subl", + MOVL="movl",MOVB="movb",MOVZBL="movzbl",MOVSBL="movsbl", + MUL="imull", + DIV="idivl",CLTD="cltd", + INCL="incl",DECL="decl",INCB="incb",DECB="decb", + + LEAL="leal",LEAB="leab", + + + CALL="call",LEAVE="leave",RET="ret", + + + CMP="cmp", + JE="je",JNE="jne",JL="jl",JLE="jle",JG="jg",JGE="jge", + JMP="jmp",JZ="jz",JNZ="jnz", + SETE="sete",SETNE="setne",SETL="setl",SETLE="setle",SETG="setg",SETGE="setge", + TEST="test",NEGL="negl",NEGB="negb" + ; + +inline const string getLabelName(int no) +{ + char w[10]; + sprintf(w, ".L%d",no); + return string(w); +} + +inline const string getStringLabelName(int no) +{ + char w[10]; + sprintf(w, ".string%d",no); + return string(w); +} + + + +//pushl eax +//popl ebx +inline void emit(const string x,const string y) +{ + outIndex+= sprintf(outs+outIndex,"\t%s\t%s\n",x.c_str(),y.c_str() ); +} +//pushl (x) +inline void emit(const string x,Fields* f) +{ + outIndex+= sprintf(outs+outIndex,"\t%s\t%d(%s)\n",x.c_str(),f->actOffset,EBP.c_str()); +} + +//addl $32,eax +inline void emit(const string x,int i,const string y) +{ + outIndex+= sprintf(outs+outIndex,"\t%s\t$%d,%s\n",x.c_str(),i,y.c_str() ); +} +//add $32,$56(X) +inline void emit(const string x,int i,Fields* f) +{ + outIndex+= sprintf(outs+outIndex,"\t%s\t$%d,%d(%s)\n",x.c_str(),i,f->actOffset,EBP.c_str()); +} + +//addl %eax,%eax +inline void emit(const string x,const string y,const string z) +{ + outIndex+= sprintf(outs+outIndex,"\t%s\t%s,%s\n",x.c_str(),y.c_str(),z.c_str() ); +} + +//addl $32(%eax),%eax +inline void emit(const string x,int i,const string y,const string z) +{ + outIndex+= sprintf(outs+outIndex,"\t%s\t%d(%s),%s\n",x.c_str(),i,y.c_str(),z.c_str() ); +} +//addl $32(X),%eax +inline void emit(const string x,Fields* f,const string z) +{ + if(f->isStringConst) + outIndex+=sprintf(outIndex+outs,"\t%s\t$%s,%s\n",x.c_str(), + getStringLabelName(f->stringLabel).c_str(),z.c_str()); + + else if(!f->isConst) + outIndex+= sprintf(outs+outIndex,"\t%s\t%d(%s),%s\n",x.c_str(),f->actOffset,EBP.c_str(),z.c_str() ); + else{ + int t=f->type[0].first,val; + if(t==charT) + val=f->val.charVal; + else + val=f->val.intVal; + outIndex+= sprintf(outs+outIndex,"\t%s\t$%d,%s\n",x.c_str(),val,z.c_str() ); + } + +} + +//addl %eax,$32(%eax) +inline void emit(const string x,const string y,int i,const string z) +{ + outIndex+= sprintf(outs+outIndex,"\t%s\t%s,%d(%s)\n",x.c_str(),y.c_str(),i,z.c_str() ); +} +//addl %eax,$32(%eax) +inline void emit(const string x,const string y,Fields* f) +{ + outIndex+= sprintf(outs+outIndex,"\t%s\t%s,%d(%s)\n",x.c_str(),y.c_str(),f->actOffset,EBP.c_str() ); +} +inline void emit(const string s) +{ + outIndex+= sprintf(outs+outIndex,"\t%s\n",s.c_str() ); +} +//labels: +inline void emitLabel(const string s) +{ + outIndex+= sprintf(outs+outIndex,"%s:\n",s.c_str() ); +} +inline void emitStringLabel(const string &labelName,const string & strdat) +{ + emitLabel(labelName); + outIndex+= sprintf(outs+outIndex,"\t.string\t\"%s\"\n",strdat.c_str() ); +} + + +#endif \ No newline at end of file diff --git a/ass 6/test b/ass 6/test new file mode 100644 index 0000000..b1d015b --- /dev/null +++ b/ass 6/test @@ -0,0 +1,115 @@ +./compiler ass5_12CS10037_test.c + +--------------------SymbolTable--------------------------- +no of params=1 +p +1 p 4 0 8 0 0 (int 0) +----------------------------------------------------------- + +--------------------SymbolTable--------------------------- +no of params=1 +eP +1 eP 4 0 8 0 0 (pointer 0) (int 0) +----------------------------------------------------------- + +--------------------SymbolTable--------------------------- +no of params=2 +a b +1 a 4 0 8 0 0 (int 0) +2 b 4 4 12 0 1 (int 0) +----------------------------------------------------------- + + + SymbolTable of function add +--------------------SymbolTable--------------------------- +no of params=2 +x y +1 x 4 0 8 0 0 (int 0) +2 y 4 4 12 0 1 (int 0) +3 __t0 4 12 -4 0 2 (int 0) +4 __t1 4 16 -8 0 3 (int 0) +----------------------------------------------------------- + + + SymbolTable of function mainstackOffset=0 +stackOffset=4 + +--------------------SymbolTable--------------------------- +no of params=0 + +1 printi 0 0 0 0 0 +2 readi 0 0 0 0 1 +3 x1 4 4 -4 0 2 (int 0) +4 y1 4 8 -8 0 3 (int 0) +5 __t2 4 12 -12 0 4 (pointer 0) (int 0) +6 __t3 4 16 -16 0 5 (int 0) +7 __t4 4 20 -20 0 6 (pointer 0) (int 0) +8 __t5 4 24 -24 0 7 (int 0) +9 __t6 4 28 -28 1 8 (int 0) +10 __t7 4 32 -32 1 9 (int 0) +11 __t8 4 36 -36 0 10 (int 0) +12 i 4 40 -40 0 11 (int 0) +13 __t9 4 44 -44 0 12 (int 0) +14 __t10 4 48 -48 1 13 (int 0) +15 __t11 4 52 -52 1 14 (int 0) +16 __t12 4 56 -56 0 15 (int 0) +17 __t13 4 60 -60 0 16 (int 0) +18 __t14 4 64 -64 0 17 (int 0) +19 __t15 4 68 -68 1 18 (int 0) +20 __t16 4 72 -72 0 19 (int 0) +----------------------------------------------------------- + 0:function add + 1:__t0 = x + y + 2:return __t0 + 3:function end + 4:function main + 5:__t2=&x1 + 6:param __t2 + 7:__t3=call readi,1 + 8:__t4=&y1 + 9:param __t4 + 10:__t5=call readi,1 + 11:__t6 = 4 + 12:__t7 = 5 + 13:__t8 = __t6 + __t7 + 14:i = __t8 + 15:__t9 = i + 16:i = i + 1 + 17:__t10 = 0 + 18:i = __t10 + 19:__t11 = 100 + 20:if i < __t11 goto 25 + 21:goto 28 + 22:__t13 = i + 23:i = i + 1 + 24:goto 19 + 25:param i + 26:__t14=call printi,1 + 27:goto 22 + 28:__t15 = 0 + 29:return __t15 + 30:function end + +--------------------SymbolTable--------------------------- +no of params=0 + +1 add 4 0 0 0 3 (int 0) +2 main 4 4 0 0 4 (int 0) +3 readi 4 4 0 0 1 (int 0) +4 printi 4 4 0 0 0 (int 0) +5 printi2 4 8 0 0 2 (int 0) +----------------------------------------------------------- +function add +return __t0 +function end +function main +__t2=&x1 +param __t2 +__t4=&y1 +param __t4 +goto 28 +goto 19 +param i +goto 22 +return __t15 +function end diff --git a/ass 6/test files/editditance_test.c b/ass 6/test files/editditance_test.c new file mode 100644 index 0000000..ba57cb3 --- /dev/null +++ b/ass 6/test files/editditance_test.c @@ -0,0 +1,102 @@ + +// This is the test file to test various compilers.You can add expressions here +// at check the output after doing make + +//By default it has KMP algorithm implemented and by running this function shows +// the 3-address quads for KMP algorithm + + +//I've implemented almost all the things given except "function declarations" + + + +int prints(char *x); +int printi(int p); +int readi(int *eP); +int printi2(int a,int b); +int printc(char a); +int reads(char*a); +int printf(char*s,int a) +{ + return prints(s)+printi(a); +} + +int strlen(char* s) +{ + int i=0; + while(s[i]!='\0')i++; + return i; +} + + + + +int min(int a, int b) { + return a < b ? a : b; +} + +// Returns Minimum among a, b, c +int Minimum(int a, int b, int c) +{ + return min(min(a, b), c); +} + + +// Recursive implementation +int EditDistanceRecursion( char *X, char *Y, int m, int n ) +{ + // Base cases + if( m == 0 && n == 0 ) + return 0; + + if( m == 0 ) + return n; + + if( n == 0 ) + return m; + + // Recurse + int left = EditDistanceRecursion(X, Y, m-1, n) + 1; + int right = EditDistanceRecursion(X, Y, m, n-1) + 1; + int corner = EditDistanceRecursion(X, Y, m-1, n-1) + ((X[m-1] != Y[n-1])?1:0); + + return Minimum(left, right, corner); +} + + +int levenshtein(char *s1, char *s2) +{ + int x, y, s1len, s2len; + s1len = strlen(s1); + s2len = strlen(s2); + int matrix[100][100]; + matrix[0][0] = 0; + for (x = 1; x <= s2len; x++) + matrix[x][0] = matrix[x-1][0] + 1; + for (y = 1; y <= s1len; y++) + matrix[0][y] = matrix[0][y-1] + 1; + for (x = 1; x <= s2len; x++) + for (y = 1; y <= s1len; y++) + matrix[x][y] = Minimum(matrix[x-1][y] + 1, matrix[x][y-1] + 1, matrix[x-1][y-1] + (s1[y-1] == s2[x-1] ? 0 : 1)); + + return(matrix[s2len][s1len]); +} +int main() +{ + + char Y[1000]; + + char X[1000]; + + reads(X);reads(&Y[0]); + + + prints("Minimum edits required to convert "); + prints(X);prints(" to ");prints(Y); + printf(" is ",levenshtein(X, Y) ); + prints("\n"); + + + + return 0; +} \ No newline at end of file diff --git a/ass 6/test files/regular_expression_match.c b/ass 6/test files/regular_expression_match.c new file mode 100644 index 0000000..e1448a8 --- /dev/null +++ b/ass 6/test files/regular_expression_match.c @@ -0,0 +1,73 @@ + +// This is the test file to test various compilers.You can add expressions here +// at check the output after doing make + +//By default it has KMP algorithm implemented and by running this function shows +// the 3-address quads for KMP algorithm + + +//I've implemented almost all the things given except "function declarations" + + + +int prints(char *x); +int printi(int p); +int readi(int *eP); +int printi2(int a,int b); +int printc(char a); +int reads(char*a); +int printf(char*s,int a) +{ + return prints(s)+printi(a); +} + +int strlen(char* s) +{ + int i=0; + while(s[i]!='\0')i++; + return i; +} + + + + +int isMatch( char *s, char *p) +{ + + if (p[0] == '\0') return + ((s[0] == '\0')?1:0); + + // next char is not '*': must match current character + if ((p[1]) != '*') { + + return (((p[0] == s[0]) || (p[0] == '.' && s[0] != '\0')) && isMatch(s+1, p+1))?1:0; + } + // next char is '*' + while ((*p == *s) || (*p == '.' && *s != '\0')) { + if (isMatch(s, p+2)) return 1; + s++; + } + return isMatch(s, p+2); +} +int main() +{ + + char Y[1000]; + + char X[1000]; + + reads(X);reads(&Y[0]); + + + prints("The string "); + prints(X);prints(" and ");prints(Y); + int res=isMatch(X, Y) ; + if(res) + prints(" can be matched via regular expressions"); + else + prints(" cannot be matched via regular expressions"); + prints("\n"); + + + return 0; +} \ No newline at end of file diff --git a/ass 6/test files/string_permutation.c b/ass 6/test files/string_permutation.c new file mode 100644 index 0000000..037a0d5 --- /dev/null +++ b/ass 6/test files/string_permutation.c @@ -0,0 +1,78 @@ + +// This is the test file to test various compilers.You can add expressions here +// at check the output after doing make + +//By default it has KMP algorithm implemented and by running this function shows +// the 3-address quads for KMP algorithm + + +//I've implemented almost all the things given except "function declarations" + + + +int prints(char *x); +int printi(int p); +int readi(int *eP); +int printi2(int a,int b); +int printc(char a); +int reads(char*a); +int printf(char*s,int a) +{ + return prints(s)+printi(a); +} + +int strlen(char* s) +{ + int i=0; + while(s[i]!='\0')i++; + return i; +} + + + + +void swap (char *x, char *y) +{ + char temp; + temp = *x; + *x = *y; + *y = temp; +} + + // Function to print permutations of string + // This function takes three parameters: + // 1. String + // 2. Starting index of the string + // 3. Ending index of the string. +void permute(char *a, int i, int n) +{ + int j; + if (i == n-1) + {prints(a);prints("\n");} + else + { + for (j = i; j < n; j++) + { + swap((a+i), (a+j)); + permute(a, i+1, n); + swap((a+i), (a+j)); //backtrack + } + } +} + + +int main() +{ + + + + char X[1000]; + + reads(X); + + permute(X, 0, strlen(X)); + + + + return 0; +} \ No newline at end of file diff --git a/ass 6/test files/tic_tac_toe.c b/ass 6/test files/tic_tac_toe.c new file mode 100644 index 0000000..3f0c6e6 --- /dev/null +++ b/ass 6/test files/tic_tac_toe.c @@ -0,0 +1,166 @@ + +// This is the test file to test various compilers.You can add expressions here +// at check the output after doing make + +//By default it has KMP algorithm implemented and by running this function shows +// the 4-address quads for KMP algorithm + + +//I've implemented almost all the things given except "function declarations" + + + +int prints(char *x); +int printi(int p); +int readi(int *eP); +int printi2(int a,int b); +int printc(char a); +int reads(char*a); +int printf(char*s,int a) +{ + return prints(s)+printi(a); +} + +int strlen(char* s) +{ + int i=0; + while(s[i]!='\0')i++; + return i; +} + + +void print_grid(char (*board)[4]); +int didwin(char (*board)[4]); + + + + + + //Purpose: Two players battle in the classic game tic tac toe until one is proclaimed victor! + + + + +int main() +{ + int i=0,j=0,player=0,lead=0,nrows=0,ncols=0,winner=0; + int again='q'; + char four_X_four[4][4]; + while(winner == '\0') { + for(i = 0; i < 4; i++) { + for(j = 0; j<4; j++) { + four_X_four[i][j] = '\0'; + } + } + for( i = 0; i<16 && winner==0; i++) + { + print_grid(four_X_four); + player = i%2 + 1; + if(player==1){ + printf("\n Player ", player); + prints (" : "); + printc('X');prints(" "); + } + else if(player==2) { + printf("\n Player ", player); + prints (" : "); + printc('0');prints(" "); + } + readi( &lead); + lead--; + ncols = lead%4; + lead = lead - ncols; + nrows = lead/4; + if(lead<0 || lead>16 || four_X_four[nrows][ncols]=='X' || four_X_four[nrows][ncols]=='O') { + prints("Space is already taken, please try again"); + i--; + } + else { + four_X_four[nrows][ncols] = (player == 1) ? 'X' : 'O'; + } + winner = didwin(four_X_four); + } + if(winner != '\0') { + prints("Winner was ");printc(winner); prints(" Good job.\n"); + } + else { + prints("No winner this round. Try again."); + } + } + return 0; +} +void print_grid(char (*board)[4]) { + int i,j; + prints("\n\n"); + for(i = 0; i < 4; i++) { + for(j = 0; j < 4; j++) { + if(board[i][j] == '\0') { + printf(" ", 4*(i)+(j+1));prints(" "); + } + else { + printc(' '); + printc(board[i][j]);printc(' '); + } + if(j!=4) { prints("|"); } + } + if(i != 4) { + prints("\n-------------------\n"); + } + } +} + +int didwin(char (*board)[4]) { + int i,j; + char current; + char winner = '\0'; + + //Iter over rows to check for winner + for(i = 0; i<4; i++) { + current = board[i][0]; + for(j = 0; j < 4; j++) { + if(board[i][j] != current) { + current = '\0'; + } + } + if(current != '\0') { + winner = current; + } + } + + //Iter over columns + for(i = 0; i<4; i++) { + current = board[0][i]; + for(j = 0; j < 4; j++) { + if(board[j][i] != current) { + current = '\0'; + } + } + if(current != '\0') { + winner = current; + } + } + + //Iter over diagonals + current = board[0][0]; + for(i = 0; i < 4; i++) { + if(board[i][i] != current) { + current = '\0'; + } + } + if(current != '\0') { + winner = current; + } + + current = board[0][3]; + for(i = 0; i <4; i++) { + if(board[i][4-i-1] != current) { + current = '\0'; + } + } + if(current != '\0') { + winner = current; + } + return winner; +} + + diff --git a/ass 6/test_editditance.c b/ass 6/test_editditance.c new file mode 100644 index 0000000..ba57cb3 --- /dev/null +++ b/ass 6/test_editditance.c @@ -0,0 +1,102 @@ + +// This is the test file to test various compilers.You can add expressions here +// at check the output after doing make + +//By default it has KMP algorithm implemented and by running this function shows +// the 3-address quads for KMP algorithm + + +//I've implemented almost all the things given except "function declarations" + + + +int prints(char *x); +int printi(int p); +int readi(int *eP); +int printi2(int a,int b); +int printc(char a); +int reads(char*a); +int printf(char*s,int a) +{ + return prints(s)+printi(a); +} + +int strlen(char* s) +{ + int i=0; + while(s[i]!='\0')i++; + return i; +} + + + + +int min(int a, int b) { + return a < b ? a : b; +} + +// Returns Minimum among a, b, c +int Minimum(int a, int b, int c) +{ + return min(min(a, b), c); +} + + +// Recursive implementation +int EditDistanceRecursion( char *X, char *Y, int m, int n ) +{ + // Base cases + if( m == 0 && n == 0 ) + return 0; + + if( m == 0 ) + return n; + + if( n == 0 ) + return m; + + // Recurse + int left = EditDistanceRecursion(X, Y, m-1, n) + 1; + int right = EditDistanceRecursion(X, Y, m, n-1) + 1; + int corner = EditDistanceRecursion(X, Y, m-1, n-1) + ((X[m-1] != Y[n-1])?1:0); + + return Minimum(left, right, corner); +} + + +int levenshtein(char *s1, char *s2) +{ + int x, y, s1len, s2len; + s1len = strlen(s1); + s2len = strlen(s2); + int matrix[100][100]; + matrix[0][0] = 0; + for (x = 1; x <= s2len; x++) + matrix[x][0] = matrix[x-1][0] + 1; + for (y = 1; y <= s1len; y++) + matrix[0][y] = matrix[0][y-1] + 1; + for (x = 1; x <= s2len; x++) + for (y = 1; y <= s1len; y++) + matrix[x][y] = Minimum(matrix[x-1][y] + 1, matrix[x][y-1] + 1, matrix[x-1][y-1] + (s1[y-1] == s2[x-1] ? 0 : 1)); + + return(matrix[s2len][s1len]); +} +int main() +{ + + char Y[1000]; + + char X[1000]; + + reads(X);reads(&Y[0]); + + + prints("Minimum edits required to convert "); + prints(X);prints(" to ");prints(Y); + printf(" is ",levenshtein(X, Y) ); + prints("\n"); + + + + return 0; +} \ No newline at end of file diff --git a/ass 6/test_primefactorization.c b/ass 6/test_primefactorization.c new file mode 100644 index 0000000..677b99a --- /dev/null +++ b/ass 6/test_primefactorization.c @@ -0,0 +1,71 @@ + +// This is the test file to test various compilers.You can add expressions here +// at check the output after doing make + +//By default it has KMP algorithm implemented and by running this function shows +// the 4-address quads for KMP algorithm + + + + + + +int prints(char *x); +int printi(int p); +int readi(int *eP); +int printi2(int a,int b); +int printc(char a); +int reads(char*a); +int printf(char*s,int a) +{ + return prints(s)+printi(a); +} + + +int strlen(char* s) +{ + int i=0; + while(s[i]!='\0')i++; + return i; +} + + +void primeFactors(int n) +{ + // Print the number of 2s that divide n + while (n%2 == 0) + { + printf("\n", 2); + n = n/2; + } + int i; + // n must be odd at this point. So we can skip one element (Note i = i +2) + for ( i = 3; i <= (n); i = i+2) + { + // While i divides n, print i and divide n + while (n%i == 0) + { + printf("\n", i); + n = n/i; + } + } + + // This condition is to handle the case whien n is a prime number + // greater than 2 + if (n > 2) + printf ("\n", n); +} + +/* Driver program to test above function */ +int main() +{ + int n = 315; + prints("Enter a number:"); + readi(&n); + prints("The primefactorization of this number"); + primeFactors(n); + prints("\n"); + return 0; +} + + diff --git a/ass 6/test_quicksort.c b/ass 6/test_quicksort.c new file mode 100644 index 0000000..17dbc04 --- /dev/null +++ b/ass 6/test_quicksort.c @@ -0,0 +1,101 @@ + +// This is the test file to test various compilers.You can add expressions here +// at check the output after doing make + +//By default it has KMP algorithm implemented and by running this function shows +// the 3-address quads for KMP algorithm + + +//I've implemented almost all the things given except "function declarations" + + + +int prints(char *x); +int printi(int p); +int readi(int *eP); +int printi2(int a,int b); +int printc(char a); +int reads(char*a); +int printf(char*s,int a) +{ + return prints(s)+printi(a); +} + +// int strlen(char* s) +// { +// int i=0; +// while(s[i]!='\0')i++; +// return i; +// } + +void swap(int* a, int* b) +{ + int t = *a; + *a = *b; + *b = t; +} + +/* This function takes last element as pivot, places the pivot element at its + correct position in sorted array, and places all smaller (smaller than pivot) + to left of pivot and all greater elements to right of pivot */ +int partition (int *arr, int l, int h) +{ + int x = arr[h]; // pivot + int i = (l - 1),j; // Index of smaller element + + for ( j = l; j <= h- 1; j++) + { + // If current element is smaller than or equal to pivot + if (arr[j] <= x) + { + i++; // increment index of smaller element + swap(arr+i, arr+j); // Swap current element with index + } + } + swap(arr+i + 1, arr+h); + return (i + 1); +} + +/* *arr --> Array to be sorted, l --> Starting index, h --> Ending index */ +void quickSort(int *arr, int l, int h) +{ + if (l < h) + { + int p = partition(arr, l, h); /* Partitioning index */ + // printi2(p, l); + quickSort(arr, l, p - 1); + // printi2(p, h); + quickSort(arr, p + 1, h); + // prints("here12\n"); + } + // prints("here1\n"); + return; +} + +/* Function to print an array */ +void printArray(int *arr, int size) +{ + int i; + for (i=0; i < size; i++) + printf(" ", arr[i]); + prints("\n"); +} + +// Driver program to test above functions +int main() +{ + int arr[10]; + int n,i; + prints("enter the number of elements and all the elements:"); + readi(&n); + for (i = 0; i < n; ++i){ + readi(&arr[i]); + } + // swap(&n, &i); + prints("array before sorting"); + printArray(arr, n); + quickSort(arr, 0, n-1); + prints("Sorted array: \n"); + printArray(arr, n); + return 0; +} \ No newline at end of file diff --git a/ass 6/test_regular_expression_match.c b/ass 6/test_regular_expression_match.c new file mode 100644 index 0000000..e1448a8 --- /dev/null +++ b/ass 6/test_regular_expression_match.c @@ -0,0 +1,73 @@ + +// This is the test file to test various compilers.You can add expressions here +// at check the output after doing make + +//By default it has KMP algorithm implemented and by running this function shows +// the 3-address quads for KMP algorithm + + +//I've implemented almost all the things given except "function declarations" + + + +int prints(char *x); +int printi(int p); +int readi(int *eP); +int printi2(int a,int b); +int printc(char a); +int reads(char*a); +int printf(char*s,int a) +{ + return prints(s)+printi(a); +} + +int strlen(char* s) +{ + int i=0; + while(s[i]!='\0')i++; + return i; +} + + + + +int isMatch( char *s, char *p) +{ + + if (p[0] == '\0') return + ((s[0] == '\0')?1:0); + + // next char is not '*': must match current character + if ((p[1]) != '*') { + + return (((p[0] == s[0]) || (p[0] == '.' && s[0] != '\0')) && isMatch(s+1, p+1))?1:0; + } + // next char is '*' + while ((*p == *s) || (*p == '.' && *s != '\0')) { + if (isMatch(s, p+2)) return 1; + s++; + } + return isMatch(s, p+2); +} +int main() +{ + + char Y[1000]; + + char X[1000]; + + reads(X);reads(&Y[0]); + + + prints("The string "); + prints(X);prints(" and ");prints(Y); + int res=isMatch(X, Y) ; + if(res) + prints(" can be matched via regular expressions"); + else + prints(" cannot be matched via regular expressions"); + prints("\n"); + + + return 0; +} \ No newline at end of file diff --git a/ass 6/test_string_permutation.c b/ass 6/test_string_permutation.c new file mode 100644 index 0000000..037a0d5 --- /dev/null +++ b/ass 6/test_string_permutation.c @@ -0,0 +1,78 @@ + +// This is the test file to test various compilers.You can add expressions here +// at check the output after doing make + +//By default it has KMP algorithm implemented and by running this function shows +// the 3-address quads for KMP algorithm + + +//I've implemented almost all the things given except "function declarations" + + + +int prints(char *x); +int printi(int p); +int readi(int *eP); +int printi2(int a,int b); +int printc(char a); +int reads(char*a); +int printf(char*s,int a) +{ + return prints(s)+printi(a); +} + +int strlen(char* s) +{ + int i=0; + while(s[i]!='\0')i++; + return i; +} + + + + +void swap (char *x, char *y) +{ + char temp; + temp = *x; + *x = *y; + *y = temp; +} + + // Function to print permutations of string + // This function takes three parameters: + // 1. String + // 2. Starting index of the string + // 3. Ending index of the string. +void permute(char *a, int i, int n) +{ + int j; + if (i == n-1) + {prints(a);prints("\n");} + else + { + for (j = i; j < n; j++) + { + swap((a+i), (a+j)); + permute(a, i+1, n); + swap((a+i), (a+j)); //backtrack + } + } +} + + +int main() +{ + + + + char X[1000]; + + reads(X); + + permute(X, 0, strlen(X)); + + + + return 0; +} \ No newline at end of file diff --git a/ass 6/test_tic_tac_toe.c b/ass 6/test_tic_tac_toe.c new file mode 100644 index 0000000..3f0c6e6 --- /dev/null +++ b/ass 6/test_tic_tac_toe.c @@ -0,0 +1,166 @@ + +// This is the test file to test various compilers.You can add expressions here +// at check the output after doing make + +//By default it has KMP algorithm implemented and by running this function shows +// the 4-address quads for KMP algorithm + + +//I've implemented almost all the things given except "function declarations" + + + +int prints(char *x); +int printi(int p); +int readi(int *eP); +int printi2(int a,int b); +int printc(char a); +int reads(char*a); +int printf(char*s,int a) +{ + return prints(s)+printi(a); +} + +int strlen(char* s) +{ + int i=0; + while(s[i]!='\0')i++; + return i; +} + + +void print_grid(char (*board)[4]); +int didwin(char (*board)[4]); + + + + + + //Purpose: Two players battle in the classic game tic tac toe until one is proclaimed victor! + + + + +int main() +{ + int i=0,j=0,player=0,lead=0,nrows=0,ncols=0,winner=0; + int again='q'; + char four_X_four[4][4]; + while(winner == '\0') { + for(i = 0; i < 4; i++) { + for(j = 0; j<4; j++) { + four_X_four[i][j] = '\0'; + } + } + for( i = 0; i<16 && winner==0; i++) + { + print_grid(four_X_four); + player = i%2 + 1; + if(player==1){ + printf("\n Player ", player); + prints (" : "); + printc('X');prints(" "); + } + else if(player==2) { + printf("\n Player ", player); + prints (" : "); + printc('0');prints(" "); + } + readi( &lead); + lead--; + ncols = lead%4; + lead = lead - ncols; + nrows = lead/4; + if(lead<0 || lead>16 || four_X_four[nrows][ncols]=='X' || four_X_four[nrows][ncols]=='O') { + prints("Space is already taken, please try again"); + i--; + } + else { + four_X_four[nrows][ncols] = (player == 1) ? 'X' : 'O'; + } + winner = didwin(four_X_four); + } + if(winner != '\0') { + prints("Winner was ");printc(winner); prints(" Good job.\n"); + } + else { + prints("No winner this round. Try again."); + } + } + return 0; +} +void print_grid(char (*board)[4]) { + int i,j; + prints("\n\n"); + for(i = 0; i < 4; i++) { + for(j = 0; j < 4; j++) { + if(board[i][j] == '\0') { + printf(" ", 4*(i)+(j+1));prints(" "); + } + else { + printc(' '); + printc(board[i][j]);printc(' '); + } + if(j!=4) { prints("|"); } + } + if(i != 4) { + prints("\n-------------------\n"); + } + } +} + +int didwin(char (*board)[4]) { + int i,j; + char current; + char winner = '\0'; + + //Iter over rows to check for winner + for(i = 0; i<4; i++) { + current = board[i][0]; + for(j = 0; j < 4; j++) { + if(board[i][j] != current) { + current = '\0'; + } + } + if(current != '\0') { + winner = current; + } + } + + //Iter over columns + for(i = 0; i<4; i++) { + current = board[0][i]; + for(j = 0; j < 4; j++) { + if(board[j][i] != current) { + current = '\0'; + } + } + if(current != '\0') { + winner = current; + } + } + + //Iter over diagonals + current = board[0][0]; + for(i = 0; i < 4; i++) { + if(board[i][i] != current) { + current = '\0'; + } + } + if(current != '\0') { + winner = current; + } + + current = board[0][3]; + for(i = 0; i <4; i++) { + if(board[i][4-i-1] != current) { + current = '\0'; + } + } + if(current != '\0') { + winner = current; + } + return winner; +} + +