├── 01 Symbol Table ├── README.md ├── input.cpp └── symboltable.cpp ├── 02 Lexical Analyzer ├── README.md ├── input.cpp └── lexicalanalyzer.cpp ├── 03 Lexcal Analyzer using LEX Tool ├── README.md ├── input.c └── lex.l ├── 04(a) Valid Arithmetic Expression ├── README.md └── program.y ├── 04(b) Valid Variable ├── README.md ├── file.l └── file.y ├── 04(c) Calculator using LEX and YACC ├── README.md ├── input.y └── lex.l ├── 05 BNF Rules into YACC ├── README.md ├── input.c ├── lex.l └── yacc.y ├── 06 Type Checking ├── README.md └── type_checking.c ├── 07 Control Flow & Data Flow Analysis ├── README.md └── control_flow.jpg ├── 08 Storage Allocation ├── README.md ├── stack.c └── storage_allocation.c ├── 09 DAG ├── README.md └── program.c ├── 10 Backend Compiler ├── README.md └── program.c ├── 11 Simple Code Optimization ├── README.md └── program.c ├── CODE_OF_CONDUCT.md ├── LICENSE └── README.md /01 Symbol Table/README.md: -------------------------------------------------------------------------------- 1 | Steps to compile 2 | 3 | ``` 4 | g++ symboltable.cpp 5 | ./a.out 6 | ``` 7 | -------------------------------------------------------------------------------- /01 Symbol Table/input.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a,b,c; 5 | char x; 6 | float x1,x2; 7 | 8 | } 9 | -------------------------------------------------------------------------------- /01 Symbol Table/symboltable.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Jacob Samro 3 | * @Date: 2016-04-06 08:44:18 4 | * @Last Modified by: Jacob Samro 5 | * @Last Modified time: 2016-04-06 09:20:38 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace std; 15 | 16 | int main(){ 17 | 18 | char key[32][10]={"break","auto","case","const","char","continue","default","do","double","else","enum","extern","float","for","goto","if","int","long","register","return","short","signed","sizeof","static","struct","switch","typedef","union","unsigned","void","volatile","while"}; 19 | 20 | char c; 21 | 22 | int i,count=0,k,d; 23 | 24 | char s[20],id[25],u[10]; 25 | 26 | ifstream fin("input.cpp", ios::app); 27 | 28 | int index = 0,break_index = 0 , chunk_index = 0 ; 29 | 30 | char temp[10000]; 31 | 32 | int memory = 10245; 33 | 34 | cout<<"Variable DataType Memory\n"; 35 | 36 | while(fin) 37 | { 38 | fin.get(c); 39 | 40 | temp[break_index] = c; 41 | 42 | if(c==' ' || c == '\n' || c == ',' || c == '}') { 43 | 44 | //Clear the space 45 | char newtemp[10000]; 46 | int newIndex = 0; 47 | 48 | for(int _i = 0 ; _i < strlen(temp) ; _i++){ 49 | if(temp[_i] != ' '){ 50 | newtemp[newIndex] = temp[_i]; 51 | newIndex++; 52 | } 53 | } 54 | 55 | 56 | for(int __i = 0 ; __i < strlen(newtemp) ; __i++){ 57 | if(newtemp[__i] == ',' ||newtemp[__i] == ' ' || newtemp[__i] == ';' || newtemp[__i] == '('){ 58 | newtemp[__i] = '\0'; 59 | break; 60 | } 61 | } 62 | 63 | char lastDataType[10]; 64 | 65 | for(int _i = 0 ; _i < 32; _i++){ 66 | if(strcmp(key[_i], newtemp) == 0){ 67 | 68 | strcpy(lastDataType,key[_i]); 69 | break; 70 | } 71 | } 72 | 73 | if(strcmp(newtemp,"int") == 0 || strcmp(newtemp,"char") == 0 || strcmp(newtemp,"float") == 0|| strcmp(newtemp,"double") == 0|| strcmp(newtemp,"long") == 0){ 74 | 75 | } 76 | else if(newtemp[0] == '#'){ 77 | }else if(strcmp(newtemp,"main")==0 || strcmp(newtemp,"return")==0){ 78 | } 79 | else if(newtemp[0] == ' '){ } 80 | else if(newtemp[0] == '<' || newtemp[0] == '}' || newtemp[0] == '{'){ 81 | } 82 | else if(newtemp[0] == '\n'){ 83 | }else if(strcmp(lastDataType,"int")==0){ 84 | memory += 2 ; 85 | cout<< " "< 2 | 3 | int main(){ 4 | int a=10,c = a+ 10; 5 | char x; 6 | float x1,x2; 7 | 8 | } 9 | -------------------------------------------------------------------------------- /02 Lexical Analyzer/lexicalanalyzer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Compiled with GCC Compiler 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | int main(){ 14 | 15 | char key[32][10]={"break","auto","case","const","char","continue","default","do","double","else","enum","extern","float","for","goto","if","int","long","register","return","short","signed","sizeof","static","struct","switch","typedef","union","unsigned","void","volatile","while"}; 16 | 17 | char c; 18 | 19 | int i,count=0,k,d; 20 | 21 | char s[20],id[25],u[10]; 22 | 23 | ifstream fin("input.cpp", ios::app); 24 | 25 | int index = 0,break_index = 0 , chunk_index = 0 ; 26 | 27 | char temp[10000]; 28 | 29 | int memory = 10245; 30 | 31 | cout<<"Variable DataType Memory\n"; 32 | 33 | while(fin) 34 | { 35 | 36 | fin.get(c); 37 | 38 | temp[break_index] = c; 39 | 40 | if(c==' ' || c == '\n' || c == ',' || c == '}' || c == '=' || c == '+') { 41 | 42 | 43 | if(c=='(' || c == ')'|| c == '{'|| c == '}'|| c == ';'|| c == ','|| c == '"'|| c == '\'' || c == ')'){ 44 | cout<< " =" << " \t " << "Punctuation" << "\t " << 0 << "\n"; 45 | }else if( c == '+' || c == '+'|| c == '-'|| c == '*'|| c == '/' || c == '='|| c == '<' || c == '>' || c == '.' || c == ':'){ 46 | cout<< " "<< c << " \t " << "Operator" << "\t " << 0 << "\n"; 47 | } 48 | 49 | //Clear the space 50 | char newtemp[10000]; 51 | int newIndex = 0; 52 | 53 | for(int _i = 0 ; _i < strlen(temp) ; _i++){ 54 | if(temp[_i] != ' '){ 55 | newtemp[newIndex] = temp[_i]; 56 | newIndex++; 57 | } 58 | } 59 | 60 | 61 | for(int __i = 0 ; __i < strlen(newtemp) ; __i++){ 62 | if(newtemp[__i] == '=' || newtemp[__i] == ',' ||newtemp[__i] == ' ' || newtemp[__i] == ';' || newtemp[__i] == '('|| newtemp[__i] == '+'|| newtemp[__i] == '-'|| newtemp[__i] == '*'|| newtemp[__i] == '/'|| newtemp[__i] == '|' || newtemp[__i] == '[' || newtemp[__i] == '"'|| newtemp[__i] == '{'|| newtemp[__i] == ')'|| newtemp[__i] == '}'|| newtemp[__i] == '<'|| newtemp[__i] == '>'){ 63 | newtemp[__i] = '\0'; 64 | break; 65 | }else if(isdigit(newtemp[__i]) && !isalpha(newtemp[__i-1 ])){ 66 | newtemp[__i] = '\0'; 67 | 68 | break; 69 | 70 | } 71 | } 72 | 73 | char lastDataType[10]; 74 | 75 | for(int _i = 0 ; _i < 32; _i++){ 76 | if(strcmp(key[_i], newtemp) == 0){ 77 | 78 | strcpy(lastDataType,key[_i]); 79 | break; 80 | } 81 | } 82 | 83 | if(strcmp(newtemp,"int") == 0 || strcmp(newtemp,"char") == 0 || strcmp(newtemp,"float") == 0|| strcmp(newtemp,"double") == 0|| strcmp(newtemp,"long") == 0){ 84 | 85 | } 86 | else if(newtemp[0] == '#'){ 87 | }else if(strcmp(newtemp,"main")==0 || strcmp(newtemp,"return")==0){ 88 | } 89 | else if(newtemp[0] == ' ' || newtemp[0] == '\0'){ } 90 | 91 | else if(newtemp[0] == '<' || newtemp[0] == '}' || newtemp[0] == '{'){ 92 | } 93 | else if(newtemp[0] == '\n'){ 94 | }else if(strcmp(lastDataType,"int")==0){ 95 | memory += 2 ; 96 | cout<< " "< 2 | 3 | int main() 4 | { 5 | int a,b,c; 6 | 7 | printf("enter the value for a,b"); 8 | 9 | scanf("%d%d",&a,&b); 10 | 11 | c=a+b; 12 | 13 | printf("the value of c:%d",&c); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /03 Lexcal Analyzer using LEX Tool/lex.l: -------------------------------------------------------------------------------- 1 | %{ 2 | int COMMENT=0; 3 | %} 4 | identifier[a-z][A-Z0-9]* 5 | digit[0-9] 6 | %% 7 | #.* {printf("\n%s is a peprocessor diective",yytext);} 8 | "int" | 9 | "float" | 10 | "char" | 11 | "double" | 12 | "for" | 13 | "do" | 14 | "if" | 15 | "else" | 16 | "break" | 17 | "continue" | 18 | "void" | 19 | "case" | 20 | "long" | 21 | "struct" | 22 | "typedef" | 23 | "goto" | 24 | "const" | 25 | "while" | 26 | "printf" | 27 | "scanf" | 28 | "switch" | 29 | "main()" | 30 | "return" {printf("\n\t%s is a keyword",yytext);} 31 | "\*" {COMMENT=1;} 32 | "*/" {COMMENT=0;} 33 | \{ {if(!COMMENT) printf("\nblocks starts");} 34 | \} {if(!COMMENT) printf("\n block end");} 35 | {identifier} {if(!COMMENT) printf("\n\t%s is an identifier",yytext);} 36 | \".*\" {if(!COMMENT) printf("\n\t%s is a string",yytext);} 37 | {digit} {if(!COMMENT) printf("\n\t%s is a number",yytext);} 38 | \)(\;)? {if(!COMMENT) printf("\n\t"); ECHO; printf("\n");} 39 | \ECHO; 40 | = {if(!COMMENT) printf("\n\t= is an assignment operator");} 41 | \<= | 42 | \>= | 43 | \< | 44 | == | 45 | \> {if(!COMMENT) printf("\n\t%s is an relational operator",yytext);} 46 | %% 47 | int main(int argc,char **argv) 48 | { 49 | if(argc>1) 50 | { 51 | FILE *f1; 52 | f1=fopen("file.c","r"); 53 | if(!f1) 54 | { 55 | printf("could not open%s\n",argv[1]); 56 | exit(0); 57 | } 58 | yyin=f1; 59 | } 60 | yylex(); 61 | printf("\n"); 62 | return 1; 63 | } 64 | int yywrap() 65 | { 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /04(a) Valid Arithmetic Expression/README.md: -------------------------------------------------------------------------------- 1 | ## Compiling this Program 2 | 3 | ```sh 4 | yacc -d -o program.c program.y 5 | gcc program.c 6 | ./a.out 7 | ``` 8 | -------------------------------------------------------------------------------- /04(a) Valid Arithmetic Expression/program.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include 4 | %} 5 | %token LETTER DIGIT 6 | %left '+' '-' 7 | %left '*' '/' 8 | %% 9 | st:st expr '\n' { 10 | printf("\n valid expression \n"); 11 | } 12 | | st '\n' 13 | | 14 | ; 15 | expr:expr '+' expr 16 | |expr '-' expr 17 | |expr '*' expr 18 | |expr '/' expr 19 | |'(' expr ')' 20 | | NUM 21 | | LETTER 22 | ; 23 | NUM: DIGIT 24 | |NUM DIGIT 25 | ; 26 | %% 27 | 28 | int yylex() 29 | { 30 | char c; 31 | while((c=getchar())==' '); 32 | if(isalpha(c)) return LETTER; 33 | if(isdigit(c)) return DIGIT; 34 | return(c); 35 | } 36 | int main() 37 | { 38 | printf("\n enter an expression\n"); 39 | yyparse(); 40 | } 41 | 42 | int yyerror() 43 | { 44 | printf("invalid\n"); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /04(b) Valid Variable/README.md: -------------------------------------------------------------------------------- 1 | ## Compiling this Program 2 | 3 | ```sh 4 | lex file.l 5 | yacc -d file.y 6 | gcc lex.yy.c y.tab.c -ll -ly 7 | ./a.out 8 | ``` 9 | -------------------------------------------------------------------------------- /04(b) Valid Variable/file.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include "y.tab.h" 3 | %} 4 | %% 5 | [0-9]+ {return DIGIT;} 6 | [a-zA-Z]+ {return LETTER;} 7 | [ \t] {;} 8 | \n { return 0;} 9 | . {return yytext[0];} 10 | %% 11 | -------------------------------------------------------------------------------- /04(b) Valid Variable/file.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | %} 4 | %token DIGIT LETTER 5 | %% 6 | stmt:A 7 | ; 8 | A: LETTER B 9 | ; 10 | B: LETTER B 11 | | DIGIT B 12 | | LETTER 13 | | DIGIT 14 | ; 15 | %% 16 | void main(){ 17 | printf("enter string \n"); 18 | yyparse(); 19 | printf("valid"); 20 | exit(0); 21 | } 22 | void yyerror() 23 | { 24 | printf("invalid"); 25 | exit(0); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /04(c) Calculator using LEX and YACC/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JacobSamro/Compiler-Design-Lab/0fecd2271dceba1ab8f2b28fbd174e8729a22b2b/04(c) Calculator using LEX and YACC/README.md -------------------------------------------------------------------------------- /04(c) Calculator using LEX and YACC/input.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include "y.tab.h" 3 | #include 4 | #include 5 | #define PI 3.141592 6 | int i,j; 7 | %} 8 | %union 9 | { 10 | double dval; 11 | } 12 | %token NUMBER 13 | %token SIN COS TAN SQRT SQUARE RECI EXP MOD CUBE FACT 14 | %left LN LOG 15 | %left '+''-''*''/' 16 | %right '^' 17 | %nonassoc NEG 18 | %type E 19 | %% 20 | SL : S '\n' 21 | | SL S '\n' 22 | ; 23 | S : E {printf("=%g\n",$1);} 24 | ; 25 | 26 | E : E '+' E {$$=$1+$3; 27 | printf("Addition is");} 28 | |E '-' E {$$=$1-$3; 29 | printf("substraction is"); 30 | } 31 | |E '*' E {$$=$1*$3; 32 | printf("Multiplication is"); 33 | } 34 | |E '/' E { 35 | if($3==0) 36 | printf("Error! Divide by zero!!"); 37 | else 38 | $$=$1/$3; 39 | printf("Division is"); 40 | } 41 | |E '^' E {$$=pow($1,$3); 42 | printf("Power is="); 43 | } 44 | |SIN '(' E ')' {$$=sin($3/180*PI); 45 | printf("SIN is="); 46 | } 47 | |COS '(' E ')' {$$=cos($3/180*PI); 48 | printf("COS is"); 49 | } 50 | |TAN '(' E ')' { if($3==90) 51 | printf("Undefined (Infinity)"); 52 | else 53 | $$=tan($3/180*PI); 54 | printf("TAN is"); 55 | } 56 | |SQRT '(' E ')' {$$=sqrt($3); 57 | printf("Square Root is"); 58 | } 59 | |SQUARE '(' E ')' {$$=$3*$3; 60 | printf("square is="); 61 | } 62 | |EXP '(' E ')' {$$=exp($3); 63 | printf("EXPONENTIAL is"); 64 | } 65 | |RECI '(' E ')' {$$=1/($3); 66 | printf("Reciprocal is");} 67 | |CUBE '(' E ')' {$$=$3*$3*$3; 68 | printf("Cube is");} 69 | |FACT '(' E ')' { 70 | $$=1; 71 | for(j=1;j<=$3;j++) 72 | 73 | $$=$$*j; 74 | printf("Factorial is"); 75 | } 76 | |'(' E ')' {$$=$2; 77 | printf("The simple number is");} 78 | |'-' E %prec NEG {$$=-$2; 79 | printf("The number with Negative sign is "); 80 | } 81 | |LOG E {$$=log($2)/log(10); 82 | printf("LOG of base 10 is"); 83 | } 84 | |LN E { $$=log($2); 85 | printf("LOG with base 2 is"); 86 | } 87 | |MOD '(' E','E')' {$$=fmod($3,$5); 88 | } 89 | |NUMBER 90 | ; 91 | %% 92 | extern FILE*yyin; 93 | int main() 94 | { 95 | do{ 96 | yyparse(); 97 | }while(!feof(yyin)); 98 | } 99 | yyerror(char*a) 100 | { 101 | fprintf(stderr,"parse error!!!"); 102 | } 103 | -------------------------------------------------------------------------------- /04(c) Calculator using LEX and YACC/lex.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include "y.tab.h" 3 | #include 4 | %} 5 | NUMBER [0-9]+|([0-9]*"."[0-9]+) 6 | %% 7 | {NUMBER} {yylval.dval=atof(yytext); 8 | return NUMBER;} 9 | sin|SIN {return SIN;} 10 | cos|COS {return COS;} 11 | 12 | tan|TAN {return TAN;} 13 | sqrt|SQRT {return SQRT;} 14 | square|SQUARE {return SQUARE;} 15 | exp|EXP {return EXP;} 16 | reci|RECI {return RECI;} 17 | cube|CUBE {return CUBE;} 18 | fact|FACT {return FACT;} 19 | LOG { return LOG;} 20 | ln|LN {return LN;} 21 | MOD|mod {return MOD;} 22 | [\t]+ ; 23 | \n|. return yytext[0]; 24 | %% 25 | int yywrap(void) 26 | { return 1; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /05 BNF Rules into YACC/README.md: -------------------------------------------------------------------------------- 1 | ## Compiling this Program 2 | 3 | ```sh 4 | lex lex.l 5 | yacc –d yacc.y 6 | gcc lex.yy.c y.tab.c –ll –lm 7 | ./a.out 8 | 9 | ``` 10 | -------------------------------------------------------------------------------- /05 BNF Rules into YACC/input.c: -------------------------------------------------------------------------------- 1 | main() 2 | { 3 | int a,b,c; 4 | if(a 4 | #include 5 | int LineNo=1; 6 | %} 7 | identifier [a-zA-Z][_a-zA-Z0-9]* 8 | number [0-9]+|([0-9]*\.[0-9]+) 9 | %% 10 | main\(\) return MAIN; 11 | if return IF; 12 | else return ELSE; 13 | while return WHILE; 14 | int | 15 | char | 16 | float return TYPE; 17 | {identifier} {strcpy(yylval.var,yytext); 18 | return VAR;} 19 | {number} {strcpy(yylval.var,yytext); 20 | return NUM;} 21 | 22 | \< | 23 | \> | 24 | \>= | 25 | \<= | 26 | == {strcpy(yylval.var,yytext); 27 | return RELOP;} 28 | [ \t] ; 29 | \n LineNo++; 30 | . return yytext[0]; 31 | %% 32 | -------------------------------------------------------------------------------- /05 BNF Rules into YACC/yacc.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include 4 | struct quad 5 | { 6 | char op[5]; 7 | char arg1[10]; 8 | char arg2[10]; 9 | char result[10]; 10 | }QUAD[30]; 11 | struct stack 12 | { 13 | int items[100]; 14 | int top; 15 | }stk; 16 | int Index=0,tIndex=0,StNo,Ind,tInd; 17 | extern int LineNo; 18 | %} 19 | %union 20 | { 21 | char var[10]; 22 | } 23 | %token NUM VAR RELOP 24 | %token MAIN IF ELSE WHILE TYPE 25 | %type EXPR ASSIGNMENT CONDITION IFST ELSEST WHILELOOP 26 | %left '-' '+' 27 | %left '*' '/' 28 | %% 29 | PROGRAM : MAIN BLOCK 30 | ; 31 | BLOCK: '{' CODE '}' 32 | ; 33 | CODE: BLOCK 34 | | STATEMENT CODE 35 | | STATEMENT 36 | ; 37 | STATEMENT: DESCT ';' 38 | | ASSIGNMENT ';' 39 | 40 | | CONDST 41 | | WHILEST 42 | ; 43 | DESCT: TYPE VARLIST 44 | ; 45 | VARLIST: VAR ',' VARLIST 46 | | VAR 47 | ; 48 | ASSIGNMENT: VAR '=' EXPR{ 49 | strcpy(QUAD[Index].op,"="); 50 | strcpy(QUAD[Index].arg1,$3); 51 | strcpy(QUAD[Index].arg2,""); 52 | strcpy(QUAD[Index].result,$1); 53 | strcpy($$,QUAD[Index++].result); 54 | } 55 | ; 56 | EXPR: EXPR '+' EXPR {AddQuadruple("+",$1,$3,$$);} 57 | | EXPR '-' EXPR {AddQuadruple("-",$1,$3,$$);} 58 | | EXPR '*' EXPR {AddQuadruple("*",$1,$3,$$);} 59 | | EXPR '/' EXPR {AddQuadruple("/",$1,$3,$$);} 60 | | '-' EXPR {AddQuadruple("UMIN",$2,"",$$);} 61 | | '(' EXPR ')' {strcpy($$,$2);} 62 | | VAR 63 | | NUM 64 | ; 65 | CONDST: IFST{ 66 | Ind=pop(); 67 | sprintf(QUAD[Ind].result,"%d",Index); 68 | Ind=pop(); 69 | sprintf(QUAD[Ind].result,"%d",Index); 70 | } 71 | | IFST ELSEST 72 | ; 73 | IFST: IF '(' CONDITION ')' { 74 | strcpy(QUAD[Index].op,"=="); 75 | strcpy(QUAD[Index].arg1,$3); 76 | strcpy(QUAD[Index].arg2,"FALSE"); 77 | strcpy(QUAD[Index].result,"-1"); 78 | push(Index); 79 | Index++; 80 | } 81 | BLOCK { 82 | strcpy(QUAD[Index].op,"GOTO"); 83 | strcpy(QUAD[Index].arg1,""); 84 | strcpy(QUAD[Index].arg2,""); 85 | strcpy(QUAD[Index].result,"-1"); 86 | push(Index); 87 | Index++; 88 | }; 89 | ELSEST: ELSE{ 90 | 91 | tInd=pop(); 92 | Ind=pop(); 93 | push(tInd); 94 | sprintf(QUAD[Ind].result,"%d",Index); 95 | } 96 | BLOCK{ 97 | Ind=pop(); 98 | sprintf(QUAD[Ind].result,"%d",Index); 99 | }; 100 | CONDITION: VAR RELOP VAR {AddQuadruple($2,$1,$3,$$); 101 | StNo=Index-1; 102 | } 103 | | VAR 104 | | NUM 105 | ; 106 | WHILEST: WHILELOOP{ 107 | Ind=pop(); 108 | sprintf(QUAD[Ind].result,"%d",StNo); 109 | Ind=pop(); 110 | sprintf(QUAD[Ind].result,"%d",Index); 111 | } 112 | ; 113 | WHILELOOP: WHILE '(' CONDITION ')' { 114 | strcpy(QUAD[Index].op,"=="); 115 | strcpy(QUAD[Index].arg1,$3); 116 | strcpy(QUAD[Index].arg2,"FALSE"); 117 | strcpy(QUAD[Index].result,"-1"); 118 | push(Index); 119 | Index++; 120 | } 121 | BLOCK { 122 | strcpy(QUAD[Index].op,"GOTO"); 123 | strcpy(QUAD[Index].arg1,""); 124 | strcpy(QUAD[Index].arg2,""); 125 | strcpy(QUAD[Index].result,"-1"); 126 | push(Index); 127 | Index++; 128 | } 129 | ; 130 | %% 131 | extern FILE *yyin; 132 | int main(int argc,char *argv[]) 133 | { 134 | FILE *fp; 135 | int i; 136 | if(argc>1) 137 | { 138 | fp=fopen(argv[1],"r"); 139 | if(!fp) 140 | { 141 | 142 | printf("\n File not found"); 143 | exit(0); 144 | } 145 | yyin=fp; 146 | } 147 | yyparse(); 148 | printf("\n\n\t\t ----------------------------""\n\t\t Pos Operator Arg1 Arg2 Result" "\n\t\t 149 | --------------------"); 150 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | #define SIZE 128 6 | #define NONE -1 7 | #define EOS '\0' 8 | #define NUM 257 9 | #define KEYWORD 258 10 | #define ID 259 11 | #define DONE 260 12 | #define MAX 999 13 | char lexemes[MAX]; 14 | char buffer[SIZE]; 15 | int lastchar=-1; 16 | int lastentry=0; 17 | int tokenval=DONE; 18 | int lineno=1; 19 | int lookahead; 20 | struct entry 21 | { 22 | char *lexptr; 23 | int token; 24 | } 25 | symtable[100]; 26 | struct entry 27 | keywords[]={"if",KEYWORD,"else",KEYWORD,"for",KEYWORD,"int",KEYWORD,"float",KEYWORD,"double",KEYWORD,"char",KEYWORD,"struct",KEYWORD,"return",KEYWORD,0,0}; 28 | void Error_Message(char *m) 29 | 30 | { 31 | fprintf(stderr,"line %d, %s \n",lineno,m); 32 | exit(1); 33 | } 34 | int look_up(char s[ ]) 35 | { 36 | int k; 37 | for(k=lastentry;k>0;k--) 38 | if(strcmp(symtable[k].lexptr,s)==0) 39 | return k; 40 | return 0; 41 | } 42 | int insert(char s[ ],int tok) 43 | { 44 | int len; 45 | len=strlen(s); 46 | if(lastentry+1>=MAX) 47 | Error_Message("Symbpl table is full"); 48 | if(lastchar+len+1>=MAX) 49 | Error_Message("Lexemes array is full"); 50 | lastentry=lastentry+1; 51 | symtable[lastentry].token=tok; 52 | symtable[lastentry].lexptr=&lexemes[lastchar+1]; 53 | lastchar=lastchar+len+1; 54 | strcpy(symtable[lastentry].lexptr,s); 55 | return lastentry; 56 | } 57 | /*void Initialize() 58 | { 59 | struct entry *ptr; 60 | for(ptr=keywords;ptr->token;ptr+1) 61 | insert(ptr->lexptr,ptr->token); 62 | }*/ 63 | int lexer() 64 | { 65 | int t; 66 | int val,i=0; 67 | while(1) 68 | { 69 | t=getchar(); 70 | if(t==' '||t=='\t'); 71 | else 72 | if(t=='\n') 73 | lineno=lineno+1; 74 | else 75 | if(isdigit(t)) 76 | { 77 | ungetc(t,stdin); 78 | scanf("%d",&tokenval); 79 | return NUM; 80 | 81 | } 82 | else 83 | if(isalpha(t)) 84 | { 85 | while(isalnum(t)) 86 | { 87 | buffer[i]=t; 88 | t=getchar(); 89 | i=i+1; 90 | if(i>=SIZE) 91 | Error_Message("Compiler error"); 92 | } 93 | buffer[i]=EOS; 94 | if(t!=EOF) 95 | ungetc(t,stdin); 96 | val=look_up(buffer); 97 | if(val==0) 98 | val=insert(buffer,ID); 99 | tokenval=val; 100 | return symtable[val].token; 101 | } 102 | else 103 | if(t==EOF) 104 | return DONE; 105 | else 106 | { 107 | tokenval=NONE; 108 | return t; 109 | } 110 | } 111 | } 112 | void Match(int t) 113 | { 114 | if(lookahead==t) 115 | lookahead=lexer(); 116 | else 117 | Error_Message("Syntax error"); 118 | } 119 | void display(int t,int tval) 120 | { 121 | if(t=='+'||t=='-'||t=='*'||t=='/') 122 | printf("\nArithmetic Operator: %c",t); 123 | else if(t==NUM) 124 | printf("\n Number: %d",tval); 125 | else if(t==ID) 126 | printf("\n Identifier: %s",symtable[tval].lexptr); 127 | else 128 | printf("\n Token %d tokenval %d",t,tokenval); 129 | } 130 | void F() 131 | 132 | { 133 | void E(); 134 | switch(lookahead) 135 | { 136 | case '(' : Match('('); 137 | E(); 138 | Match(')'); 139 | break; 140 | case NUM : display(NUM,tokenval); 141 | Match(NUM); 142 | break; 143 | case ID : display(ID,tokenval); 144 | Match(ID); 145 | break; 146 | default : Error_Message("Syntax error"); 147 | } 148 | } 149 | void T() 150 | { 151 | int t; 152 | F(); 153 | while(1) 154 | { 155 | switch(lookahead) 156 | { 157 | case '*' : t=lookahead; 158 | Match(lookahead); 159 | F(); 160 | display(t,NONE); 161 | continue; 162 | case '/' : t=lookahead; 163 | Match(lookahead); 164 | display(t,NONE); 165 | continue; 166 | default : return; 167 | } 168 | } 169 | } 170 | void E() 171 | { 172 | int t; 173 | T(); 174 | while(1) 175 | { 176 | switch(lookahead) 177 | { 178 | case '+' : t=lookahead; 179 | Match(lookahead); 180 | T(); 181 | display(t,NONE); 182 | 183 | continue; 184 | case '-' : t=lookahead; 185 | Match(lookahead); 186 | T(); 187 | display(t,NONE); 188 | continue; 189 | default : return; 190 | } 191 | } 192 | } 193 | void parser() 194 | { 195 | lookahead=lexer(); 196 | while(lookahead!=DONE) 197 | { 198 | E(); 199 | Match(';'); 200 | } 201 | } 202 | int main() 203 | { 204 | char ans[10]; 205 | printf("\n Enter the expression "); 206 | printf("And place ; at the end\n"); 207 | printf("Press Ctrl-Z to terminate\n"); 208 | parser(); 209 | return 0; 210 | } 211 | -------------------------------------------------------------------------------- /07 Control Flow & Data Flow Analysis/README.md: -------------------------------------------------------------------------------- 1 | # Control Flow Analysis & Data Flow Analysis 2 | 3 | based on the lecture by V. Krishna Nandivada from IIT Madras 4 | 5 | Code optimization requires that the compiler has a global understanding of how programs use the available resources.It has to understand the control flows in the program and how the data is manipulated (data-flow analysis) 6 | 7 | Control-flow analysis: flow of control within each procedure. 8 | 9 | Data-flow analysis: how the data is manipulated in the program. 10 | 11 | ## Control Flow Analysis 12 | 13 | Control-flow analysis discovers the flow of control within a procedure 14 | (e.g., builds a CFG, identifies loops) 15 | 16 | ```sh 17 | 1 a := 0 18 | 2 b := a * b 19 | 3 L1: c := b/d 20 | 4 if c < x goto L2 21 | 5 e := b / c 22 | 6 f := e + 1 23 | 7 L2: g := f 24 | 8 h := t - g 25 | 9 if e > 0 goto L3 26 | 10 goto L1 27 | 11 L3: return 28 | ``` 29 | ![alt text](https://raw.githubusercontent.com/JacobSamro/Compiler-Design-Lab/master/07%20Control%20Flow%20%26%20Data%20Flow%20Analysis/control_flow.jpg) 30 | ### C Version 31 | 32 | ```c 33 | #include 34 | 35 | ``` 36 | 37 | ## Data Flow Analysis 38 | 39 | Data-flow analysis derives information about the dynamic 40 | behavior of a program by only examining the static code 41 | 42 | (ie, How many registers do we need for the program ) 43 | 44 | ```c 45 | 46 | #include 47 | 48 | int main(){ 49 | int a = 10; 50 | int b = 20; 51 | printf("The Sum is %d",a+b); 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /07 Control Flow & Data Flow Analysis/control_flow.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JacobSamro/Compiler-Design-Lab/0fecd2271dceba1ab8f2b28fbd174e8729a22b2b/07 Control Flow & Data Flow Analysis/control_flow.jpg -------------------------------------------------------------------------------- /08 Storage Allocation/README.md: -------------------------------------------------------------------------------- 1 | ### Steps to compile 2 | 3 | #### Compiling the Stack Program 4 | 5 | ``` 6 | gcc stack.c 7 | ./a.out 8 | ``` 9 | 10 | #### Compiling the Storage Allocation Program 11 | ``` 12 | gcc storage_allocation.c 13 | ./a.out 14 | ``` 15 | -------------------------------------------------------------------------------- /08 Storage Allocation/stack.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | struct stack 5 | { 6 | int no; 7 | struct stack *next; 8 | } 9 | *start=NULL; 10 | typedef struct stack st; 11 | void push(); 12 | int pop(); 13 | void display(); 14 | void main() 15 | { 16 | char ch; 17 | int choice,item; 18 | do 19 | { 20 | clrscr(); 21 | printf("\n 1: push"); 22 | printf("\n 2: pop"); 23 | printf("\n 3: display"); 24 | printf("\n Enter your choice"); 25 | scanf("%d",&choice); 26 | switch (choice) 27 | { 28 | case 1: push(); 29 | break; 30 | case 2: item=pop(); 31 | printf("The delete element in %d",item); 32 | break; 33 | case 3: display(); 34 | break; 35 | default : printf("\n Wrong choice"); 36 | }; 37 | 38 | printf("\n do you want to continue(Y/N)"); 39 | fflush(stdin); 40 | scanf("%c",&ch); 41 | } 42 | while (ch=='Y'||ch=='y'); 43 | } 44 | void push() 45 | { 46 | st *node; 47 | node=(st *)malloc(sizeof(st)); 48 | printf("\n Enter the number to be insert"); 49 | scanf("%d",&node->no); 50 | node->next=start; 51 | start=node; 52 | } 53 | int pop() 54 | { 55 | st *temp; 56 | temp=start; 57 | if(start==NULL) 58 | { 59 | printf("stack is already empty"); 60 | getch(); 61 | exit(); 62 | } 63 | else 64 | { 65 | start=start->next; 66 | free(temp); 67 | } 68 | return(temp->no); 69 | } 70 | void display() 71 | { 72 | 73 | st *temp; 74 | temp=start; 75 | while(temp->next!=NULL) 76 | { 77 | printf("\nno=%d",temp->no); 78 | temp=temp->next; 79 | } 80 | printf("\nno=%d",temp->no); 81 | } 82 | -------------------------------------------------------------------------------- /08 Storage Allocation/storage_allocation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define TRUE 1 5 | #define FALSE 0 6 | typedef struct Heap 7 | { 8 | int data; 9 | struct Heap *next; 10 | }node; 11 | node *create(); 12 | void main() 13 | { 14 | int choice, val; 15 | char ans; 16 | node *head; 17 | void display(node *); 18 | node *search(node *,int); 19 | node *insert(node *); 20 | void dele(node **); 21 | head=NULL; 22 | do 23 | 24 | { 25 | clrscr(); 26 | printf("\n Program to perform various operations on heap using dynamic memory 27 | management"); 28 | printf ("\n1.Create"); 29 | printf ("\n2.Display"); 30 | printf ("\n3.Insert an element in a list"); 31 | printf ("\n4.Delete an element from list"); 32 | printf ("\n5.Quit"); 33 | printf ("\n Enter Your Choice(1-5)"); 34 | scanf("%d",&choice); 35 | switch(choice) 36 | { 37 | case 1:head=create(); 38 | break; 39 | case 2:display(head); 40 | break; 41 | case 3:head=insert(head); 42 | break; 43 | case 4:dele(&head); 44 | break; 45 | case 5:exit(0); 46 | default:clrscr(); 47 | printf("Invalid Choice,Try again"); 48 | getch(); 49 | } 50 | } 51 | while(choice!=5); 52 | } 53 | node *create() 54 | { 55 | node *temp,*new1,*head; 56 | int val,flag; 57 | char ans='y'; 58 | node *get_node(); 59 | temp=NULL; 60 | flag=TRUE; 61 | do 62 | { 63 | printf("\n Enter the Element"); 64 | scanf("%d",&val); 65 | /*allocate new node*/ 66 | new1=get_node(); 67 | if(new1==NULL) 68 | printf("\n Memory is not allocated"); 69 | new1-> data=val; 70 | if (flag==TRUE)/* Executed only for the first time*/ 71 | { 72 | head=new1; 73 | temp=head; /*head is the first node in the heap*/ 74 | 75 | flag=FALSE; 76 | } 77 | else 78 | { 79 | temp->next=new1; 80 | temp=new1; 81 | } 82 | printf("\nDo you want to enter more elements?(y/n)"); 83 | ans=getch(); 84 | } 85 | while(ans=='y'); 86 | printf("\nThe list is created"); 87 | getch(); 88 | clrscr(); 89 | return head; 90 | } 91 | node *get_node() 92 | { 93 | node *temp; 94 | temp=(node*)malloc(sizeof(node)); 95 | //using the mem. Allocation function 96 | temp->next=NULL; 97 | return temp; 98 | } 99 | void display(node*head) 100 | { 101 | node *temp; 102 | temp=head; 103 | if(temp==NULL) 104 | { 105 | printf("\n The list is empty\n"); 106 | getch(); 107 | clrscr(); 108 | return; 109 | } 110 | while(temp!= NULL) 111 | { 112 | printf("%d->",temp-> data); 113 | temp=temp->next; 114 | } 115 | printf("NULL"); 116 | getch(); 117 | clrscr(); 118 | } 119 | node *search(node *head,int key) 120 | { 121 | node *temp; 122 | int found; 123 | temp=head; 124 | if (temp==NULL) 125 | 126 | { 127 | printf("The linked list is empty\n"); 128 | getch(); 129 | clrscr(); 130 | return NULL; 131 | } 132 | found=FALSE; 133 | while(temp!=NULL&&found==FALSE) 134 | { 135 | if(temp->data != key) 136 | temp = temp->next; 137 | else 138 | found=TRUE; 139 | } 140 | if(found == TRUE) 141 | { 142 | printf("\n The Elements is present in the list\n"); 143 | getch(); 144 | return temp; 145 | } 146 | else 147 | printf("\n The Element is not present in the list\n"); 148 | getch(); 149 | return NULL; 150 | } 151 | node *insert(node *head) 152 | { 153 | int choice; 154 | node *insert_head(node*); 155 | void insert_after(node*); 156 | void insert_last(node*); 157 | printf("\n 1.Insert a node as a head node"); 158 | printf("\n 1.Insert a node as a last node"); 159 | printf("\n 1.Insert a node as at the intermediate position in the list "); 160 | printf("\n 1.Enter your choice for insertion of node "); 161 | scanf("%d",&choice); 162 | switch(choice) 163 | { 164 | case 1:head = insert_head(head); 165 | break; 166 | case 2:insert_last(head); 167 | break; 168 | case 3:insert_after (head); 169 | break; 170 | } 171 | return head; 172 | } 173 | node *insert_head(node*head) 174 | { 175 | node *New,*temp; 176 | 177 | New=get_node(); 178 | printf ("\n Enter the element which you want to insert "); 179 | scanf("%d",&New->data); 180 | if(head == NULL) 181 | head = New; 182 | else 183 | { 184 | temp=head; 185 | New->next = temp; 186 | head= New; 187 | } 188 | return head; 189 | } 190 | void insert_last(node *head) 191 | { 192 | node *New,*temp; 193 | New = get_node(); 194 | printf ("\n Enter the element which you want to insert "); 195 | scanf("%d",&New->data); 196 | if(head == NULL) 197 | { 198 | head = New; 199 | } 200 | else 201 | { 202 | temp=head; 203 | while(temp->next!=NULL) 204 | temp=temp->next; 205 | temp->next=New; 206 | New->next=NULL; 207 | } 208 | } 209 | void insert_after(node *head) 210 | { 211 | int key; 212 | node *New,*temp; 213 | New = get_node(); 214 | printf("Enter the element after which you want to insert "); 215 | scanf("%d",&key); 216 | temp=head; 217 | do 218 | { 219 | if(temp->data==key) 220 | { 221 | printf ("Enter element which you want to insert "); 222 | scanf("%d",&New->data); 223 | New->next=temp->next; 224 | temp->next=New; 225 | return; 226 | } 227 | 228 | else 229 | temp=temp->next; 230 | } 231 | while(temp!=NULL); 232 | } 233 | node *get_prev(node *head,int val) 234 | { 235 | node *temp,*prev; 236 | int flag; 237 | temp = head; 238 | if(temp == NULL) 239 | return NULL; 240 | flag=FALSE; 241 | prev=NULL; 242 | while(temp!=NULL && !flag) 243 | { 244 | if(temp->data!=val) 245 | { 246 | prev = temp; 247 | temp = temp->next; 248 | } 249 | else 250 | flag = TRUE; 251 | } 252 | if(flag) /*if Flag is true*/ 253 | return prev; 254 | else 255 | return NULL; 256 | } 257 | void dele(node **head) 258 | { 259 | int key; 260 | node *New,*temp,*prev; 261 | temp=*head; 262 | if (temp== NULL) 263 | { 264 | printf ("\n The list is empty\n "); 265 | getch(); 266 | clrscr(); 267 | return; 268 | } 269 | clrscr(); 270 | printf("\nENTER the Element you want to delete:"); 271 | scanf("%d",&key); 272 | temp=search(*head,key); 273 | if(temp!=NULL) 274 | { 275 | prev=get_prev(*head,key); 276 | if(prev!=NULL) 277 | { 278 | 279 | prev ->next = temp-> next; 280 | free(temp); 281 | } 282 | else 283 | { 284 | *head = temp->next; 285 | free(temp); // using the mem. Dellocation function 286 | } 287 | printf("\n The Element is deleted\n"); 288 | getch(); 289 | clrscr(); 290 | } 291 | } 292 | -------------------------------------------------------------------------------- /09 DAG/README.md: -------------------------------------------------------------------------------- 1 | Steps to compile 2 | 3 | ``` 4 | gcc program.c 5 | ./a.out 6 | ``` 7 | -------------------------------------------------------------------------------- /09 DAG/program.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define size 20 5 | typedef struct node 6 | { 7 | char data; 8 | struct node *left; 9 | struct node *right; 10 | }btree; 11 | btree *stack[size]; 12 | int top; 13 | void main() 14 | { 15 | btree *root; 16 | char exp[80]; 17 | btree *create(char exp[80]); 18 | void dag(btree *root); 19 | clrscr(); 20 | printf("\nEnter the postfix expression:\n"); 21 | scanf("%s",exp); 22 | top=-1; 23 | root=create(exp); 24 | printf("\nThe tree is created.....\n"); 25 | printf("\nInorder DAG is : \n\n"); 26 | dag(root); 27 | getch(); 28 | } 29 | btree *create(char exp[]) 30 | { 31 | btree *temp; 32 | 33 | int pos; 34 | char ch; 35 | void push(btree*); 36 | btree *pop(); 37 | pos=0; 38 | ch=exp[pos]; 39 | while(ch!='\0') 40 | { 41 | temp=((btree*)malloc(sizeof(btree))); 42 | temp->left=temp->right=NULL; 43 | temp->data=ch; 44 | if(isalpha(ch)) 45 | push(temp); 46 | else if(ch=='+' ||ch=='-' || ch=='*' || ch=='/') 47 | { 48 | temp->right=pop(); 49 | temp->left=pop(); 50 | push(temp); 51 | } 52 | else 53 | printf("\n Invalid char Expression\n"); 54 | pos++; 55 | ch=exp[pos]; 56 | } 57 | temp=pop(); 58 | return(temp); 59 | } 60 | void push(btree *Node) 61 | { 62 | if(top+1 >=size) 63 | printf("Error:Stack is full\n"); 64 | top++; 65 | stack[top]=Node; 66 | } 67 | btree* pop() 68 | { 69 | btree *Node; 70 | if(top==-1) 71 | printf("\nerror: stack is empty..\n"); 72 | Node=stack[top]; 73 | top--; 74 | return(Node); 75 | } 76 | void dag(btree *root) 77 | { 78 | btree *temp; 79 | temp=root; 80 | 81 | if(temp!=NULL) 82 | { 83 | dag(temp->left); 84 | printf("%c",temp->data); 85 | dag(temp->right); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /10 Backend Compiler/README.md: -------------------------------------------------------------------------------- 1 | Steps to compile 2 | 3 | ``` 4 | gcc program.c 5 | ./a.out 6 | ``` 7 | -------------------------------------------------------------------------------- /10 Backend Compiler/program.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | //#include 4 | #include 5 | void main() 6 | { 7 | char icode[10][30],str[20],opr[10]; 8 | int i=0; 9 | //clrscr(); 10 | printf("\n Enter the set of intermediate code (terminated by exit):\n"); 11 | do 12 | { 13 | scanf("%s",icode[i]); 14 | } while(strcmp(icode[i++],"exit")!=0); 15 | printf("\n target code generation"); 16 | printf("\n************************"); 17 | i=0; 18 | do 19 | { 20 | strcpy(str,icode[i]); 21 | switch(str[3]) 22 | { 23 | case '+': 24 | strcpy(opr,"ADD"); 25 | break; 26 | case '-': 27 | strcpy(opr,"SUB"); 28 | break; 29 | case '*': 30 | strcpy(opr,"MUL"); 31 | break; 32 | case '/': 33 | strcpy(opr,"DIV"); 34 | break; 35 | } 36 | printf("\n\tMov %c,R%d",str[2],i); 37 | 38 | printf("\n\t%s%c,R%d",opr,str[4],i); 39 | printf("\n\tMov R%d,%c",i,str[0]); 40 | }while(strcmp(icode[++i],"exit")!=0); 41 | //getch(); 42 | } 43 | -------------------------------------------------------------------------------- /11 Simple Code Optimization/README.md: -------------------------------------------------------------------------------- 1 | Steps to compile 2 | 3 | ``` 4 | gcc program.c 5 | ./a..out 6 | ``` 7 | -------------------------------------------------------------------------------- /11 Simple Code Optimization/program.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | char s[20],o[20]; 5 | void main() 6 | { 7 | int i=0,j=0,k,f1=1,f=1,k1=0; 8 | void part(); 9 | clrscr(); 10 | printf("\n Enter the input string\n"); 11 | scanf("%s",o); 12 | strlen(o); 13 | while(o[k1]!='\0') 14 | { 15 | if((o[k1]=='=')==1) 16 | 17 | { 18 | break; 19 | } 20 | k1++; 21 | } 22 | for(j=k1+1;j3) 32 | { 33 | while(s[j]!='\0') 34 | { 35 | if((s[j]=='*')==1||(s[j]=='/')==1) 36 | { 37 | k=j; 38 | if(f1!=0) 39 | { 40 | printf("t1=%c\n",s[k+1]); 41 | printf("t2=%c%ct1",s[k-1],s[k]); 42 | } 43 | else 44 | { 45 | if(k>3) 46 | { 47 | printf("t2=t1%c%c\n",s[k],s[k+1]); 48 | } 49 | else 50 | { 51 | printf("\t2=t1%c%c\n",s[k],s[k-1]); 52 | } 53 | } 54 | f=0; 55 | break; 56 | } 57 | j++; 58 | } 59 | j=0; 60 | while(s[j]!='\0') 61 | { 62 | if((s[j]=='+')==1||(s[j]=='-')==1) 63 | { 64 | k=j; 65 | if(f==0) 66 | { 67 | 68 | if(k<3) 69 | { 70 | printf("\nt3=t2%c%c\n",s[k],s[k-1]); 71 | } 72 | else 73 | { 74 | printf("\nt3=t2%c%c\n",s[k],s[k+1]) 75 | } 76 | } 77 | else 78 | { 79 | printf("t1=%c%c%c\n",s[k-1],s[k],s[k+1]); 80 | } 81 | f1=0; 82 | } 83 | j++; 84 | } 85 | printf("%c=t3",o[0]); 86 | } 87 | else 88 | { 89 | printf("t1=%s\n",s); 90 | printf("%c=t1",o[0]); 91 | } 92 | part(); 93 | getch(); 94 | } 95 | void part() 96 | { 97 | int i=0,j=0,k,f1=1,f=1,k1=0; 98 | while(o[k1]!='\0') 99 | { 100 | if((o[k1]=='=')==1) 101 | { 102 | break; 103 | } 104 | k1++; 105 | } 106 | for(j=k1+1;j3) 116 | { 117 | while(s[j]!='\0') 118 | 119 | { 120 | if((s[j]=='*')==1||(s[j]=='/')==1) 121 | { 122 | k=j; 123 | if(f1!=0) 124 | { 125 | printf("t1=%c%c%c\n",s[k-1],s[k],s[k+1]); 126 | } 127 | else 128 | { 129 | if(k>3) 130 | { 131 | printf("t2=t1%c%c\n",s[k],s[k+1]); 132 | } 133 | else 134 | { 135 | printf("\t2=t1%c%c\n",s[k],s[k-1]); 136 | } 137 | } 138 | f=0; 139 | break; 140 | } 141 | j++; 142 | } 143 | j=0; 144 | while(s[j]!='\0') 145 | { 146 | if((s[j]=='+')==1||(s[j]=='-')==1) 147 | { 148 | k=j; 149 | if(f==0) 150 | { 151 | if(k<3) 152 | { 153 | printf("t2=t1%c%c\n",s[k],s[k-1]); 154 | } 155 | else 156 | { 157 | printf("t2=t1%c%c\n",s[k],s[k+1]); 158 | } 159 | } 160 | else 161 | { 162 | printf("t1=%c%c%c\n",s[k-1],s[k],s[k+1]); 163 | } 164 | f1=0; 165 | } 166 | j++; 167 | } 168 | printf("%c=t2",o[0]); 169 | 170 | } 171 | else 172 | { 173 | printf("t1=%s\n",s); 174 | printf("%c=t1",o[0]); 175 | } 176 | } 177 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at jacobsamro@outlook.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Jacob Samro 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Compiler Laboratory 2 | 3 | Lab Programs for CS6612 Compiler Laboratory 4 | 5 | 1. Implementation of Symbol Table 6 | 2. Develop a lexical analyzer to recognize a few patterns in C. (Ex. identifiers, constants, comments, operators etc.) 7 | 3. Implementation of Lexical Analyzer using Lex Tool 8 | 4. Generate YACC specification for a few syntactic categories. 9 | a. Program to recognize a valid arithmetic expression that usesoperator +, – , * and /. 10 | b. Program to recognize a valid variable which starts with a letter followed by any number of letters or digits. 11 | c. Implementation of Calculator using LEX and YACC 12 | 5. Convert the BNF rules into Yacc form and write code to generate Abstract Syntax Tree. 13 | 6. Implement type checking 14 | 7. Implement control flow analysis and Data flow Analysis 15 | 8. Implement any one storage allocation strategies (Heap, Stack, Static) 16 | 9. Construction of DAG 17 | 10. Implement the back end of the compiler which takes the three address code and produces the 8086 assembly language instructions that can be assembled and run using a 8086 assembler. The target assembly instructions can be simple move, add, sub, jump. Also simple addressing modes are used. 18 | 11. Implementation of Simple Code Optimization Techniques 19 | --------------------------------------------------------------------------------