├── CD Lab manual.doc ├── README.md └── cd ├── README.md ├── p10_recursice_descend_parser ├── 11anaghasethu-p10.c └── 11anaghasethu-p10.pdf ├── p11_shift_reduce_parser ├── 11anaghasethu-p11.c └── 11anaghasethu-p11.pdf ├── p12_loopunrolling ├── 11anaghasethu-p12.c └── 11anaghasethu-p12.pdf ├── p13_constant_propagation ├── 11anaghasethu-p13.c └── 11anaghasethu-p13.pdf ├── p14_intermediate_code ├── 11anaghasethu-p14.c ├── 11anaghasethu-p14.pdf └── input.txt ├── p15_compiler_backend ├── 11anaghasethu-p15.c └── 11anaghasethu-p15.pdf ├── p1_lexical_analyzer ├── 11anaghasethu-p1.c ├── 11anaghasethu-p1.docx ├── a.out └── input.txt ├── p2_lexicalanalyzer_lex ├── 11anaghasethu-p2.docx ├── 11anaghasethu-p2.l └── input.c ├── p3a_expression_grammer ├── 11anaghasethu-p3.docx ├── 11anaghasethu-p3.l └── 11anaghasethu-p3.y ├── p3b_calculator_in_yacc ├── 11anaghasethu-p3b.docx ├── 11anaghasethu-p3b.l └── 11anaghasethu-p3b.y ├── p3c_abstract_syntax_tree ├── 11anaghasethu-P3c.l ├── 11anaghasethu-P3c.y └── 11anaghasethu-p3c.pdf ├── p4_Eclosure ├── 11anaghasethu-p4.c └── 11anaghasethu-p4.pdf ├── p5_epsilon_nfa_to_nfa ├── 11anaghasethu-p5.c └── 11anaghasethu-p5.pdf ├── p6_nfa_dfa ├── 11anaghasethu-p6.c └── 11anaghasethu-p6.pdf ├── p7_dfa_minimisation ├── 11anaghasethu-p7.c └── 11anaghasethu-p7.pdf ├── p8_operator_precedence ├── 11anaghasethu-p8.c └── 11anaghasethu-p8.pdf └── p9_firstandfollow ├── 11anaghasethu-p9.c └── 11anaghasethu-p9.pdf /CD Lab manual.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/abc1657a1369196c62f78ba2afb37cce07407f20/CD Lab manual.doc -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Compiler Design Lab (KTU S7) 2 | 3 | ------------ 4 | 5 | The following repository contains the programs associated with compiler design lab of the seventh semester as specified by APJAKTU. 6 | 7 | ## Programs 8 | 9 | 10 | #### 1. Lexical Analyzer - [[Source Code](https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/main/cd/p1_lexical_analyzer/11anaghasethu-p1.c "Source Code")] | [[Files](https://github.com/anaghasethu/KTU-sem7-compilerdesign-programs/tree/main/cd/p1_lexical_analyzer "Files")] 11 | 12 | #### 2. Lexical Analyzer using Lex Tool - [[Source Code](https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/main/cd/p2_lexicalanalyzer_lex/11anaghasethu-p2.l "Source Code")] | [[Files](https://github.com/anaghasethu/KTU-sem7-compilerdesign-programs/tree/main/cd/p2_lexicalanalyzer_lex "Files")] 13 | 14 | #### 3.  a) Recognition of Arithmetic Expressions - [[Files](https://github.com/anaghasethu/KTU-sem7-compilerdesign-programs/tree/main/cd/p3a_expression_grammer "Files")] 15 | ####      b) Calculator(Using LEX and YACC) - [[Files](https://github.com/anaghasethu/KTU-sem7-compilerdesign-programs/tree/main/cd/p3b_calculator_in_yacc "Files")] 16 | ####      c) Abstract Syntax Tree(BNF to YACC) - [[Files](https://github.com/anaghasethu/KTU-sem7-compilerdesign-programs/tree/main/cd/p3c_abstract_syntax_tree "Files")] 17 | 18 | #### 4. ε-Closure of all states - [[Source Code](https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/main/cd/p4_Eclosure/11anaghasethu-p4.c "Source Code")] | [[Files](https://github.com/anaghasethu/KTU-sem7-compilerdesign-programs/tree/main/cd/p4_Eclosure "Files")] 19 | 20 | #### 5. Remove ε-Transition from NFA - [[Source Code](https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/main/cd/p5_epsilon_nfa_to_nfa/11anaghasethu-p5.c "Source Code")] | [[Files](https://github.com/anaghasethu/KTU-sem7-compilerdesign-programs/tree/main/cd/p5_epsilon_nfa_to_nfa "Files")] 21 | 22 | #### 6. NFA to DFA Conversion - [[Source Code](https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/main/cd/p6_nfa_dfa/11anaghasethu-p6.c "Source Code")] | [[Files](https://github.com/anaghasethu/KTU-sem7-compilerdesign-programs/tree/main/cd/p6_nfa_dfa "Files")] 23 | 24 | #### 7. Minimization of DFA - [[Source Code](https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/main/cd/p7_dfa_minimisation/11anaghasethu-p7.c "Source Code")] | [[Files](https://github.com/anaghasethu/KTU-sem7-compilerdesign-programs/tree/main/cd/p7_dfa_minimisation "Files")] 25 | 26 | #### 8. Operator Precedence Parser - [[Source Code](https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/main/cd/p8_operator_precedence/11anaghasethu-p8.c "Source Code")] | [[Files](https://github.com/anaghasethu/KTU-sem7-compilerdesign-programs/tree/main/cd/p8_operator_precedence "Files")] 27 | 28 | #### 9. Simulating First and Follow - [[Source Code](https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/main/cd/p9_firstandfollow/11anaghasethu-p9.c "Source Code")] | [[Files](https://github.com/anaghasethu/KTU-sem7-compilerdesign-programs/tree/main/cd/p9_firstandfollow "Files")] 29 | 30 | #### 10. Recursive Descent Parser - [[Source Code](https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/main/cd/p10_recursice_descend_parser/11anaghasethu-p10.c "Source Code")] | [[Files](https://github.com/anaghasethu/KTU-sem7-compilerdesign-programs/tree/main/cd/p10_recursice_descend_parser "Files")] 31 | 32 | #### 11. Shift Reduce Parser - [[Source Code](https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/main/cd/p11_shift_reduce_parser/11anaghasethu-p11.c "Source Code")] | [[Files](https://github.com/anaghasethu/KTU-sem7-compilerdesign-programs/tree/main/cd/p11_shift_reduce_parser "Files")] 33 | 34 | #### 12. Loop Unrolling - [[Source Code](https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/main/cd/p12_loopunrolling/11anaghasethu-p12.c "Source Code")] | [[Files](https://github.com/anaghasethu/KTU-sem7-compilerdesign-programs/tree/main/cd/p12_loopunrolling "Files")] 35 | 36 | #### 13. Constant Propagation - [[Source Code](https://github.com/anaghasethu/KTU-sem7-compilerdesign-programs/tree/main/cd/p13_constant_propagation "Source Code")] | [[Files](https://github.com/anaghasethu/KTU-sem7-compilerdesign-programs/tree/main/cd/p13_constant_propagation "Files")] 37 | 38 | #### 14. Intermediate Code Generation(Expression to Assembly) - [[Source Code](https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/main/cd/p14_intermediate_code/11anaghasethu-p14.c "Source Code")] | [[Files](https://github.com/anaghasethu/KTU-sem7-compilerdesign-programs/tree/main/cd/p14_intermediate_code "Files")] 39 | 40 | #### 15. Compiler Integration(Backend) - [[Source Code](https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/main/cd/p15_compiler_backend/11anaghasethu-p15.c "Source Code")] | [[Files](https://github.com/anaghasethu/KTU-sem7-compilerdesign-programs/tree/main/cd/p15_compiler_backend "Files")] 41 | 42 | 43 | ------------ 44 | 45 | 46 | Each program comes with the documentation, source code and the input files (wherever applicable). You can find this as a webpage [here](https://anaghasethu.github.io/KTU-sem7-compilerdesign-programs/ "here") or use this shortened link 47 | `https://bit.ly/cds7ktu` 48 | 49 | ------------ 50 | 51 | -------------------------------------------------------------------------------- /cd/README.md: -------------------------------------------------------------------------------- 1 | # CD-lab 2 | -------------------------------------------------------------------------------- /cd/p10_recursice_descend_parser/11anaghasethu-p10.c: -------------------------------------------------------------------------------- 1 | #include"stdio.h" 2 | #include"string.h" 3 | #include"stdlib.h" 4 | #include"ctype.h" 5 | 6 | char ip_sym[15],ip_ptr=0,op[50],tmp[50]; 7 | void e_prime(); 8 | void e(); 9 | void t_prime(); 10 | void t(); 11 | void f(); 12 | void advance(); 13 | int n=0; 14 | void e() 15 | { 16 | strcpy(op,"TE'"); 17 | printf("E=%-25s",op); 18 | printf("E->TE'\n"); 19 | t(); 20 | e_prime(); 21 | } 22 | 23 | void e_prime() 24 | { 25 | int i,n=0,l; 26 | for(i=0;i<=strlen(op);i++) 27 | if(op[i]!='e') 28 | tmp[n++]=op[i]; 29 | strcpy(op,tmp); 30 | l=strlen(op); 31 | for(n=0;n < l && op[n]!='E';n++); 32 | if(ip_sym[ip_ptr]=='+') 33 | { 34 | i=n+2; 35 | do 36 | { 37 | op[i+2]=op[i]; 38 | i++; 39 | }while(i<=l); 40 | op[n++]='+'; 41 | op[n++]='T'; 42 | op[n++]='E'; 43 | op[n++]=39; 44 | printf("E=%-25s",op); 45 | printf("E'->+TE'\n"); 46 | advance(); 47 | t(); 48 | e_prime(); 49 | } 50 | else 51 | { 52 | op[n]='e'; 53 | for(i=n+1;i<=strlen(op);i++) 54 | op[i]=op[i+1]; 55 | printf("E=%-25s",op); 56 | printf("E'->e"); 57 | } 58 | } 59 | void t() 60 | { 61 | int i,n=0,l; 62 | for(i=0;i<=strlen(op);i++) 63 | if(op[i]!='e') 64 | tmp[n++]=op[i]; 65 | strcpy(op,tmp); 66 | l=strlen(op); 67 | for(n=0;n < l && op[n]!='T';n++); 68 | 69 | i=n+1; 70 | do 71 | { 72 | op[i+2]=op[i]; 73 | i++; 74 | }while(i < l); 75 | op[n++]='F'; 76 | op[n++]='T'; 77 | op[n++]=39; 78 | printf("E=%-25s",op); 79 | printf("T->FT'\n"); 80 | f(); 81 | t_prime(); 82 | } 83 | 84 | void t_prime() 85 | { 86 | int i,n=0,l; 87 | for(i=0;i<=strlen(op);i++) 88 | if(op[i]!='e') 89 | tmp[n++]=op[i]; 90 | strcpy(op,tmp); 91 | l=strlen(op); 92 | for(n=0;n < l && op[n]!='T';n++); 93 | if(ip_sym[ip_ptr]=='*') 94 | { 95 | i=n+2; 96 | do 97 | { 98 | op[i+2]=op[i]; 99 | i++; 100 | }while(i < l); 101 | op[n++]='*'; 102 | op[n++]='F'; 103 | op[n++]='T'; 104 | op[n++]=39; 105 | printf("E=%-25s",op); 106 | printf("T'->*FT'\n"); 107 | advance(); 108 | f(); 109 | t_prime(); 110 | } 111 | else 112 | { 113 | op[n]='e'; 114 | for(i=n+1;i<=strlen(op);i++) 115 | op[i]=op[i+1]; 116 | printf("E=%-25s",op); 117 | printf("T'->e\n"); 118 | } 119 | } 120 | 121 | void f() 122 | { 123 | int i,n=0,l; 124 | for(i=0;i<=strlen(op);i++) 125 | if(op[i]!='e') 126 | tmp[n++]=op[i]; 127 | strcpy(op,tmp); 128 | l=strlen(op); 129 | for(n=0;n < l && op[n]!='F';n++); 130 | if((ip_sym[ip_ptr]=='i')||(ip_sym[ip_ptr]=='I')) 131 | { 132 | op[n]='i'; 133 | printf("E=%-25s",op); 134 | printf("F->i\n"); 135 | advance(); 136 | } 137 | else 138 | { 139 | if(ip_sym[ip_ptr]=='(') 140 | { 141 | advance(); 142 | e(); 143 | if(ip_sym[ip_ptr]==')') 144 | { 145 | advance(); 146 | i=n+2; 147 | do 148 | { 149 | op[i+2]=op[i]; 150 | i++; 151 | }while(i<=l); 152 | op[n++]='('; 153 | op[n++]='E'; 154 | op[n++]=')'; 155 | printf("E=%-25s",op); 156 | printf("F->(E)\n"); 157 | } 158 | } 159 | else 160 | { 161 | printf("\n\t syntax error"); 162 | 163 | exit(1); 164 | } 165 | } 166 | } 167 | 168 | void advance() 169 | { 170 | ip_ptr++; 171 | } 172 | 173 | void main() 174 | { 175 | int i; 176 | 177 | printf("\nGrammar without left recursion"); 178 | printf("\n\t\t E->TE' \n\t\t E'->+TE'|e \n\t\t T->FT' "); 179 | printf("\n\t\t T'->*FT'|e \n\t\t F->(E)|i"); 180 | printf("\n Enter the input expression:"); 181 | scanf("%s",ip_sym); 182 | printf("Expressions"); 183 | printf("\t Sequence of production rules\n"); 184 | e(); 185 | for(i=0;i < strlen(ip_sym);i++) 186 | { 187 | if(ip_sym[i]!='+'&&ip_sym[i]!='*'&&ip_sym[i]!='('&& 188 | ip_sym[i]!=')'&&ip_sym[i]!='i'&&ip_sym[i]!='I') 189 | { 190 | printf("\nSyntax error"); 191 | break; 192 | } 193 | for(i=0;i<=strlen(op);i++) 194 | if(op[i]!='e') 195 | tmp[n++]=op[i]; 196 | strcpy(op,tmp); 197 | printf("\nE=%-25s",op); 198 | } 199 | 200 | } 201 | -------------------------------------------------------------------------------- /cd/p10_recursice_descend_parser/11anaghasethu-p10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/abc1657a1369196c62f78ba2afb37cce07407f20/cd/p10_recursice_descend_parser/11anaghasethu-p10.pdf -------------------------------------------------------------------------------- /cd/p11_shift_reduce_parser/11anaghasethu-p11.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int k=0,z=0,i=0,j=0,c=0; 4 | char a[16],ac[20],stk[15],act[10]; 5 | void check(); 6 | int main() 7 | { 8 | 9 | puts("GRAMMAR is E->E+E \n E->E*E \n E->(E) \n E->id"); 10 | puts("enter input string "); 11 | scanf("%s",a); 12 | c=strlen(a); 13 | strcpy(act,"SHIFT->"); 14 | puts("stack \t input \t action"); 15 | for(k=0,i=0; j 2 | void main() 3 | { 4 | unsigned int n; 5 | int x; 6 | char ch; 7 | 8 | printf("\nEnter N\n"); 9 | scanf("%u",&n); 10 | printf("\n1. Loop Roll\n2. Loop UnRoll\n"); 11 | printf("\nEnter ur choice\n"); 12 | scanf(" %c",&ch); 13 | switch(ch) 14 | { 15 | case '1': 16 | x=countbit1(n); 17 | printf("\nLoop Roll: Count of 1's : %d" ,x); 18 | break; 19 | case '2': 20 | x=countbit2(n); 21 | printf("\nLoop UnRoll: Count of 1's : %d" ,x); 22 | break; 23 | default: 24 | printf("\n Wrong Choice\n"); 25 | 26 | } 27 | } 28 | int countbit1(unsigned int n) 29 | { 30 | int bits = 0,i=0; 31 | while (n != 0) 32 | { 33 | if (n & 1) bits++; 34 | n >>= 1; 35 | i++; 36 | } 37 | printf("Number of iterations %d \n",i); 38 | return bits; 39 | } 40 | int countbit2(unsigned int n) 41 | { 42 | int bits = 0,i=0; 43 | while (n != 0) 44 | { 45 | if (n & 1) bits++; 46 | if (n & 2) bits++; 47 | if (n & 4) bits++; 48 | if (n & 8) bits++; 49 | n >>= 4; 50 | i++; 51 | } 52 | printf("Number of iterations %d \n",i); 53 | return bits; 54 | } 55 | -------------------------------------------------------------------------------- /cd/p12_loopunrolling/11anaghasethu-p12.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/abc1657a1369196c62f78ba2afb37cce07407f20/cd/p12_loopunrolling/11anaghasethu-p12.pdf -------------------------------------------------------------------------------- /cd/p13_constant_propagation/11anaghasethu-p13.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void input(); 6 | void output(); 7 | void change(int p,char *res); 8 | void constant(); 9 | 10 | struct expr{ 11 | char op[2],op1[5],op2[5],res[5]; 12 | int flag; 13 | }arr[10]; 14 | int n; 15 | void main(){ 16 | 17 | input(); 18 | constant(); 19 | output(); 20 | } 21 | 22 | void input(){ 23 | 24 | int i; 25 | printf("\n\nEnter the maximum number of expressions : "); 26 | scanf("%d",&n); 27 | printf("\nEnter the input : \n"); 28 | for(i=0;i 2 | #include 3 | char op[2],arg1[5],arg2[5],result[5]; 4 | void main() 5 | { 6 | FILE *fp1,*fp2; 7 | fp1=fopen("input.txt","r"); 8 | fp2=fopen("output.txt","w"); 9 | while(!feof(fp1)) 10 | { 11 | 12 | fscanf(fp1,"%s%s%s%s",op,arg1,arg2,result); 13 | if(strcmp(op,"+")==0) 14 | { 15 | fprintf(fp2,"\nMOV R0,%s",arg1); 16 | fprintf(fp2,"\nADD R0,%s",arg2); 17 | fprintf(fp2,"\nMOV %s,R0",result); 18 | } 19 | if(strcmp(op,"*")==0) 20 | { 21 | fprintf(fp2,"\nMOV R0,%s",arg1); 22 | fprintf(fp2,"\nMUL R0,%s",arg2); 23 | fprintf(fp2,"\nMOV %s,R0",result); 24 | } 25 | if(strcmp(op,"-")==0) 26 | { 27 | fprintf(fp2,"\nMOV R0,%s",arg1); 28 | fprintf(fp2,"\nSUB R0,%s",arg2); 29 | fprintf(fp2,"\nMOV %s,R0",result); 30 | } 31 | if(strcmp(op,"/")==0) 32 | { 33 | fprintf(fp2,"\nMOV R0,%s",arg1); 34 | fprintf(fp2,"\nDIV R0,%s",arg2); 35 | fprintf(fp2,"\nMOV %s,R0",result); 36 | } 37 | if(strcmp(op,"=")==0) 38 | { 39 | fprintf(fp2,"\nMOV R0,%s",arg1); 40 | fprintf(fp2,"\nMOV %s,R0",result); 41 | } 42 | } 43 | fclose(fp1); 44 | fclose(fp2); 45 | 46 | } 47 | 48 | -------------------------------------------------------------------------------- /cd/p14_intermediate_code/11anaghasethu-p14.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/abc1657a1369196c62f78ba2afb37cce07407f20/cd/p14_intermediate_code/11anaghasethu-p14.pdf -------------------------------------------------------------------------------- /cd/p14_intermediate_code/input.txt: -------------------------------------------------------------------------------- 1 | + a b t1 2 | * c d t2 3 | - t1 t2 t 4 | = t ? x 5 | 6 | 7 | -------------------------------------------------------------------------------- /cd/p15_compiler_backend/11anaghasethu-p15.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void main(){ 4 | char icode[10][30], str[20], opr[10]; 5 | int i=0; 6 | printf("\nEnter the set of intermediate code (terminated by exit):\n"); 7 | do{ 8 | scanf("%s", icode[i]); 9 | } 10 | while(strcmp(icode[i++],"exit")!=0); 11 | printf("\nTarget code generation"); 12 | printf("\n*******************"); 13 | i=0; 14 | do{ 15 | strcpy(str,icode[i]); 16 | switch(str[3]){ 17 | case '+': 18 | strcpy(opr,"ADD"); 19 | break; 20 | case '-': 21 | strcpy(opr,"SUB"); 22 | break; 23 | case '*': 24 | strcpy(opr,"MUL"); 25 | break; 26 | case '/': 27 | strcpy(opr,"DIV"); 28 | break; 29 | 30 | } 31 | 32 | printf("\n\tMov %c,R%d", str[2],i); 33 | printf("\n\%s%c,,R%d", opr,str[4],i); 34 | printf("\n\tMov R%d%c", i,str[0]); 35 | }while(strcmp(icode[++i],"exit")!=0); } 36 | -------------------------------------------------------------------------------- /cd/p15_compiler_backend/11anaghasethu-p15.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/abc1657a1369196c62f78ba2afb37cce07407f20/cd/p15_compiler_backend/11anaghasethu-p15.pdf -------------------------------------------------------------------------------- /cd/p1_lexical_analyzer/11anaghasethu-p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int kwd(char buffer[]); 7 | 8 | int main(){ 9 | char ch, buffer[15], buf[15], operators[] = "+-*/%=,;()"; 10 | FILE *fp; 11 | int i,j=0; 12 | int ido=0; 13 | 14 | char ids[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; 15 | 16 | 17 | 18 | fp = fopen("input.txt","r"); 19 | 20 | if(fp == NULL){ 21 | printf("error while opening the file\n"); 22 | exit(0); 23 | } 24 | 25 | while((ch = fgetc(fp)) != EOF){ 26 | 27 | for(i=0;i<10;i++) 28 | { 29 | if(ch == operators[i] && kwd(buffer) == 0) 30 | printf("id "); 31 | 32 | } 33 | 34 | for(i = 0; i < 10; ++i){ 35 | 36 | 37 | if(ch == operators[i]) 38 | // printf("operator"); 39 | if(operators[i] == '+') 40 | printf("op-plus "); 41 | else if(operators[i] == '-') 42 | printf("op-sub "); 43 | else if(operators[i] == '*') 44 | printf("op-mul "); 45 | else if(operators[i] == '/') 46 | printf("op-div "); 47 | else if(operators[i] == '%') 48 | printf("op-mod "); 49 | else if(operators[i] == '=') 50 | printf("op-equ "); 51 | else if(operators[i] == ';') 52 | printf(";"); 53 | else if(operators[i] == ',') 54 | printf(","); 55 | else if(operators[i] == '(') 56 | printf("."); 57 | } 58 | 59 | 60 | 61 | if(isalnum(ch)) 62 | { 63 | 64 | buffer[j++] = ch; 65 | 66 | 67 | } 68 | else if((ch == ' ' || ch == '\n') && (j != 0)) 69 | { 70 | 71 | buffer[j] = '\0'; 72 | 73 | j = 0; 74 | 75 | if(kwd(buffer) == 1) 76 | printf("kwd "); 77 | 78 | 79 | } 80 | 81 | 82 | 83 | } 84 | 85 | fclose(fp); 86 | 87 | return 0; 88 | } 89 | 90 | 91 | 92 | int kwd(char buffer[]){ 93 | char keywords[32][10] = {"auto","break","case","char","const","continue","default", 94 | "do","double","else","enum","extern","float","for","goto", 95 | "if","int","long","register","return","short","signed", 96 | "sizeof","static","struct","switch","typedef","union", 97 | "unsigned","void","volatile","while"}; 98 | int i, flag = 0; 99 | 100 | for(i = 0; i < 32; ++i){ 101 | if(strcmp(keywords[i], buffer) == 0){ 102 | flag = 1; 103 | break; 104 | } 105 | } 106 | 107 | return flag; 108 | 109 | } 110 | 111 | 112 | 113 | /*int id( char buf[]){ 114 | 115 | char ids[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; 116 | 117 | int i, flag = 0; 118 | 119 | for(i = 0; i < 62; ++i){ 120 | if(strcmp(ids[i], buf) == 0){ 121 | flag = 1; 122 | break; 123 | } 124 | } 125 | 126 | return flag; 127 | } 128 | 129 | */ 130 | 131 | 132 | -------------------------------------------------------------------------------- /cd/p1_lexical_analyzer/11anaghasethu-p1.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/abc1657a1369196c62f78ba2afb37cce07407f20/cd/p1_lexical_analyzer/11anaghasethu-p1.docx -------------------------------------------------------------------------------- /cd/p1_lexical_analyzer/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/abc1657a1369196c62f78ba2afb37cce07407f20/cd/p1_lexical_analyzer/a.out -------------------------------------------------------------------------------- /cd/p1_lexical_analyzer/input.txt: -------------------------------------------------------------------------------- 1 | int a,b,c; 2 | 3 | a=b+c; 4 | -------------------------------------------------------------------------------- /cd/p2_lexicalanalyzer_lex/11anaghasethu-p2.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/abc1657a1369196c62f78ba2afb37cce07407f20/cd/p2_lexicalanalyzer_lex/11anaghasethu-p2.docx -------------------------------------------------------------------------------- /cd/p2_lexicalanalyzer_lex/11anaghasethu-p2.l: -------------------------------------------------------------------------------- 1 | //Implementation of Lexical Analyzer using Lex tool 2 | %{ 3 | int COMMENT=0; 4 | %} 5 | identifier [a-zA-Z][a-zA-Z0-9]* 6 | %% 7 | #.* {printf("\n%s is a preprocessor directive",yytext);} 8 | int | 9 | float | 10 | char | 11 | double | 12 | while | 13 | for | 14 | struct | 15 | typedef | 16 | do | 17 | if | 18 | break | 19 | continue | 20 | void | 21 | switch | 22 | return | 23 | else | 24 | goto {printf(" kwd");} 25 | "/*" {COMMENT=1;}{printf("comment");} 26 | \+ {if(!COMMENT)printf(" op-plus");} 27 | \- {if(!COMMENT)printf(" op-sub");} 28 | \* {if(!COMMENT)printf(" op-mul");} 29 | \/ {if(!COMMENT)printf(" op-div");} 30 | {identifier}\( {if(!COMMENT)printf("fun");} 31 | \{ {if(!COMMENT)printf("block begins");} 32 | \} {if(!COMMENT)printf("block ends");} 33 | {identifier}(\[[0-9]*\])? {if(!COMMENT) printf(" id");} 34 | 35 | \".*\" {if(!COMMENT)printf("str");} 36 | [0-9]+ {if(!COMMENT) printf("num");} 37 | \)(\:)? {if(!COMMENT)printf("\n\t");ECHO;printf("\n");} 38 | \( ECHO; 39 | = {if(!COMMENT)printf(" op-equ");} 40 | \<= | 41 | \>= | 42 | \< | 43 | == | 44 | \> {if(!COMMENT) printf("rel-op");} 45 | 46 | %% 47 | 48 | 49 | int main(int argc, char **argv) 50 | { 51 | FILE *file; 52 | file=fopen("input.c","r"); 53 | if(!file) 54 | { 55 | printf("could not open the file"); 56 | exit(0); 57 | } 58 | yyin=file; 59 | yylex(); 60 | printf("\n"); 61 | return(0); 62 | } 63 | int yywrap() 64 | { 65 | return(1); 66 | } 67 | -------------------------------------------------------------------------------- /cd/p2_lexicalanalyzer_lex/input.c: -------------------------------------------------------------------------------- 1 | int a,b,c; 2 | 3 | a=b+c; 4 | -------------------------------------------------------------------------------- /cd/p3a_expression_grammer/11anaghasethu-p3.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/abc1657a1369196c62f78ba2afb37cce07407f20/cd/p3a_expression_grammer/11anaghasethu-p3.docx -------------------------------------------------------------------------------- /cd/p3a_expression_grammer/11anaghasethu-p3.l: -------------------------------------------------------------------------------- 1 | %{ 2 | 3 | #include "y.tab.h" 4 | 5 | %} 6 | 7 | %% 8 | 9 | [a-zA-Z_][a-zA-Z_0-9]* return id; 10 | 11 | [0-9]+(\.[0-9]*)? return num; 12 | 13 | [+/*] return op; 14 | 15 | . return yytext[0]; 16 | 17 | \n return 0; 18 | 19 | %% 20 | 21 | int yywrap() 22 | 23 | { 24 | 25 | return 1; 26 | 27 | } 28 | -------------------------------------------------------------------------------- /cd/p3a_expression_grammer/11anaghasethu-p3.y: -------------------------------------------------------------------------------- 1 | %{ 2 | 3 | #include 4 | 5 | int valid=1; 6 | 7 | %} 8 | 9 | %token num id op 10 | 11 | %% 12 | 13 | start :id '=' s ; | s ; 14 | 15 | s : id x 16 | 17 | | num x 18 | 19 | | '-' num x 20 | 21 | | '(' s ')' x 22 | 23 | ; 24 | 25 | x : op s 26 | 27 | | '-' s 28 | 29 | | 30 | 31 | ; 32 | 33 | %% 34 | 35 | int yyerror() 36 | 37 | { 38 | 39 | valid=0; 40 | 41 | printf("\nInvalid expression!\n"); 42 | 43 | return 0; 44 | 45 | } 46 | 47 | int main() 48 | 49 | { 50 | 51 | printf("\nEnter the expression:\n"); 52 | 53 | yyparse(); 54 | 55 | if(valid) 56 | 57 | { 58 | 59 | printf("\nValid expression!\n"); 60 | 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /cd/p3b_calculator_in_yacc/11anaghasethu-p3b.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/abc1657a1369196c62f78ba2afb37cce07407f20/cd/p3b_calculator_in_yacc/11anaghasethu-p3b.docx -------------------------------------------------------------------------------- /cd/p3b_calculator_in_yacc/11anaghasethu-p3b.l: -------------------------------------------------------------------------------- 1 | %{ 2 | /* Definition section */ 3 | #include 4 | #include "y.tab.h" 5 | extern int yylval; 6 | %} 7 | 8 | /* Rule Section */ 9 | %% 10 | [0-9]+ { 11 | yylval=atoi(yytext); 12 | return NUMBER; 13 | 14 | } 15 | [\t] ; 16 | 17 | [\n] return 0; 18 | 19 | . return yytext[0]; 20 | 21 | %% 22 | 23 | int yywrap() 24 | { 25 | return 1; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /cd/p3b_calculator_in_yacc/11anaghasethu-p3b.y: -------------------------------------------------------------------------------- 1 | %{ 2 | 3 | #include 4 | 5 | int flag=0; 6 | 7 | 8 | 9 | %} 10 | 11 | %token NUMBER 12 | 13 | 14 | 15 | %left '+' '-' 16 | 17 | %left '*' '/' '%' 18 | 19 | %left '(' ')' 20 | 21 | %% 22 | 23 | ArithmeticExpression: E{ 24 | 25 | printf("\nResult=%d\n",$$); 26 | 27 | return 0; 28 | 29 | }; 30 | 31 | E:E'+'E {$$=$1+$3;} 32 | 33 | |E'-'E {$$=$1-$3;} 34 | 35 | |E'*'E {$$=$1*$3;} 36 | 37 | |E'/'E {$$=$1/$3;} 38 | 39 | |E'%'E {$$=$1%$3;} 40 | 41 | |'('E')' {$$=$2;} 42 | 43 | | NUMBER {$$=$1;} 44 | 45 | ; 46 | 47 | %% 48 | 49 | 50 | 51 | void main() 52 | 53 | { 54 | 55 | printf("\nEnter Any Arithmetic Expression:\n"); 56 | 57 | yyparse(); 58 | 59 | if(flag==0) 60 | 61 | printf("\n\n"); 62 | 63 | 64 | 65 | } 66 | 67 | void yyerror() 68 | 69 | { 70 | 71 | printf("\nInvalid\n\n"); 72 | 73 | flag=1; 74 | 75 | } 76 | -------------------------------------------------------------------------------- /cd/p3c_abstract_syntax_tree/11anaghasethu-P3c.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include"y.tab.h" 3 | #include 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 | == {strcpy(yylval.var,yytext); 26 | return RELOP;} 27 | [ \t] ; 28 | \n LineNo++; 29 | . return yytext[0]; 30 | %% 31 | int yywrap() 32 | { 33 | return 1; 34 | } 35 | -------------------------------------------------------------------------------- /cd/p3c_abstract_syntax_tree/11anaghasethu-P3c.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 | | CONDST 40 | | WHILEST 41 | ; 42 | DESCT: TYPE VARLIST 43 | ; 44 | VARLIST: VAR ',' VARLIST 45 | | VAR 46 | ; 47 | ASSIGNMENT: VAR '=' EXPR{ 48 | strcpy(QUAD[Index].op,"="); 49 | strcpy(QUAD[Index].arg1,$3); 50 | strcpy(QUAD[Index].arg2,""); 51 | strcpy(QUAD[Index].result,$1); 52 | strcpy($$,QUAD[Index++].result); 53 | } 54 | ; 55 | EXPR: EXPR '+' EXPR {AddQuadruple("+",$1,$3,$$);} 56 | | EXPR '-' EXPR {AddQuadruple("-",$1,$3,$$);} 57 | | EXPR '*' EXPR {AddQuadruple("*",$1,$3,$$);} 58 | | EXPR '/' EXPR {AddQuadruple("/",$1,$3,$$);} 59 | | '-' EXPR {AddQuadruple("UMIN",$2,"",$$);} 60 | | '(' EXPR ')' {strcpy($$,$2);} 61 | | VAR 62 | | NUM 63 | ; 64 | CONDST: IFST{ 65 | Ind=pop(); 66 | sprintf(QUAD[Ind].result,"%d",Index); 67 | Ind=pop(); 68 | sprintf(QUAD[Ind].result,"%d",Index); 69 | } 70 | | IFST ELSEST 71 | ; 72 | IFST: IF '(' CONDITION ')' { 73 | strcpy(QUAD[Index].op,"=="); 74 | strcpy(QUAD[Index].arg1,$3); 75 | strcpy(QUAD[Index].arg2,"FALSE"); 76 | strcpy(QUAD[Index].result,"-1"); 77 | push(Index); 78 | Index++; 79 | } 80 | BLOCK { 81 | strcpy(QUAD[Index].op,"GOTO"); 82 | strcpy(QUAD[Index].arg1,""); 83 | strcpy(QUAD[Index].arg2,""); 84 | strcpy(QUAD[Index].result,"-1"); 85 | push(Index); 86 | Index++; 87 | }; 88 | ELSEST: ELSE{ 89 | tInd=pop(); 90 | Ind=pop(); 91 | push(tInd); 92 | sprintf(QUAD[Ind].result,"%d",Index); 93 | } 94 | BLOCK{ 95 | Ind=pop(); 96 | sprintf(QUAD[Ind].result,"%d",Index); 97 | }; 98 | CONDITION: VAR RELOP VAR {AddQuadruple($2,$1,$3,$$); 99 | StNo=Index-1; 100 | } 101 | | VAR 102 | | NUM 103 | ; 104 | WHILEST: WHILELOOP{ 105 | Ind=pop(); 106 | sprintf(QUAD[Ind].result,"%d",StNo); 107 | Ind=pop(); 108 | sprintf(QUAD[Ind].result,"%d",Index); 109 | } 110 | ; 111 | WHILELOOP: WHILE '(' CONDITION ')' { 112 | strcpy(QUAD[Index].op,"=="); 113 | strcpy(QUAD[Index].arg1,$3); 114 | strcpy(QUAD[Index].arg2,"FALSE"); 115 | strcpy(QUAD[Index].result,"-1"); 116 | push(Index); 117 | Index++; 118 | } 119 | BLOCK { 120 | strcpy(QUAD[Index].op,"GOTO"); 121 | strcpy(QUAD[Index].arg1,""); 122 | strcpy(QUAD[Index].arg2,""); 123 | strcpy(QUAD[Index].result,"-1"); 124 | push(Index); 125 | Index++; 126 | } 127 | ; 128 | %% 129 | extern FILE *yyin; 130 | int main(int argc,char *argv[]) 131 | { 132 | FILE *fp; 133 | int i; 134 | if(argc>1) 135 | { 136 | fp=fopen(argv[1],"r"); 137 | if(!fp) 138 | { 139 | printf("\n File not found"); 140 | exit(0); 141 | } 142 | yyin=fp; 143 | } 144 | yyparse(); 145 | printf("\n\n\t\t -----------------------------------""\n\t\t Pos Operator Arg1 Arg2 Result" "\n\t\t -----------------------------------"); 146 | for(i=0;i 2 | #include 3 | struct node 4 | { 5 | int st; 6 | struct node *link; 7 | }; 8 | 9 | void findclosure(int,int); 10 | void insert_trantbl(int ,char, int); 11 | int findalpha(char); 12 | void print_e_closure(int); 13 | 14 | static int set[20],nostate,noalpha,s,notransition,c,r,buffer[20]; 15 | char alphabet[20]; 16 | static int e_closure[20][20]={0}; 17 | struct node * transition[20][20]={NULL}; 18 | 19 | void main() 20 | { 21 | int i,j,k,m,t,n; 22 | struct node *temp; 23 | printf("Enter the number of alphabets?\n"); 24 | scanf("%d",&noalpha); 25 | getchar(); 26 | printf("NOTE:- [ use letter e as epsilon]\n"); 27 | printf("NOTE:- [e must be last character ,if it is present]\n"); 28 | printf("\nEnter alphabets?\n"); 29 | for(i=0;i qno alphabet qno]\n",notransition); 39 | printf("NOTE:- [States number must be greater than zero]\n"); 40 | printf("\nEnter transition?\n"); 41 | for(i=0;ist,sta); 77 | temp=temp->link; 78 | } 79 | } 80 | } 81 | 82 | void insert_trantbl(int r,char c,int s) 83 | { 84 | int j; 85 | struct node *temp; 86 | j=findalpha(c); 87 | if(j==999) 88 | { 89 | printf("error\n"); 90 | exit(0); 91 | } 92 | temp=(struct node *)malloc(sizeof(struct node)); 93 | temp->st=s; 94 | temp->link=transition[r][j]; 95 | transition[r][j]=temp; 96 | } 97 | 98 | int findalpha(char c) 99 | { 100 | int i; 101 | for(i=0;i 2 | #include 3 | struct node 4 | { 5 | int st; 6 | struct node *link; 7 | }; 8 | 9 | void findclosure(int,int); 10 | void insert_trantbl(int ,char, int); 11 | int findalpha(char); 12 | void findfinalstate(void); 13 | void unionclosure(int); 14 | void print_e_closure(int); 15 | static int set[20],nostate,noalpha,s,notransition,nofinal,start,finalstate[20],c,r,buffer[20]; 16 | char alphabet[20]; 17 | static int e_closure[20][20]={0}; 18 | struct node * transition[20][20]={NULL}; 19 | void main() 20 | { 21 | int i,j,k,m,t,n; 22 | 23 | struct node *temp; 24 | printf("enter the number of alphabets?\n"); 25 | scanf("%d",&noalpha); 26 | getchar(); 27 | printf("NOTE:- [ use letter e as epsilon]\n"); 28 | 29 | printf("NOTE:- [e must be last character ,if it is present]\n"); 30 | 31 | printf("\nEnter alphabets?\n"); 32 | for(i=0;i qno alphabet qno]\n",notransition); 50 | printf("NOTE:- [States number must be greater than zero]\n"); 51 | printf("\nEnter transition?\n"); 52 | for(i=0;ist); 103 | temp=temp->link; 104 | } 105 | } 106 | printf("\n"); 107 | print_e_closure(i); 108 | printf("%c\t",alphabet[j] ); 109 | printf("{"); 110 | for(n=1;n<=nostate;n++) 111 | { 112 | if(set[n]!=0) 113 | printf("q%d,",n); 114 | } 115 | printf("}"); 116 | } 117 | } 118 | printf("\nFinal states:"); 119 | findfinalstate(); 120 | 121 | 122 | 123 | 124 | } 125 | 126 | void findclosure(int x,int sta) 127 | { 128 | struct node *temp; 129 | int i; 130 | if(buffer[x]) 131 | return; 132 | e_closure[sta][c++]=x; 133 | buffer[x]=1; 134 | if(alphabet[noalpha-1]=='e' && transition[x][noalpha-1]!=NULL) 135 | { 136 | temp=transition[x][noalpha-1]; 137 | while(temp!=NULL) 138 | { 139 | findclosure(temp->st,sta); 140 | temp=temp->link; 141 | } 142 | } 143 | } 144 | 145 | void insert_trantbl(int r,char c,int s) 146 | { 147 | int j; 148 | struct node *temp; 149 | j=findalpha(c); 150 | if(j==999) 151 | { 152 | printf("error\n"); 153 | exit(0); 154 | } 155 | temp=(struct node *) malloc(sizeof(struct node)); 156 | temp->st=s; 157 | temp->link=transition[r][j]; 158 | transition[r][j]=temp; 159 | } 160 | 161 | int findalpha(char c) 162 | { 163 | int i; 164 | for(i=0;i 2 | #include 3 | struct node 4 | { 5 | int st; 6 | struct node *link; 7 | }; 8 | struct node1 9 | { 10 | 11 | int nst[20]; 12 | }; 13 | 14 | void insert(int ,char, int); 15 | int findalpha(char); 16 | void findfinalstate(void); 17 | int insertdfastate(struct node1); 18 | int compare(struct node1,struct node1); 19 | void printnewstate(struct node1); 20 | static int set[20],nostate,noalpha,s,notransition,nofinal,start,finalstate[20],c,r,buffer[20]; 21 | int complete=-1; 22 | char alphabet[20]; 23 | static int eclosure[20][20]={0}; 24 | struct node1 hash[20]; 25 | struct node * transition[20][20]={NULL}; 26 | void main() 27 | { 28 | int i,j,k,m,t,n,l; 29 | struct node *temp; 30 | struct node1 newstate={0},tmpstate={0}; 31 | 32 | printf("Enter the number of alphabets?\n"); 33 | printf("NOTE:- [ use letter e as epsilon]\n"); 34 | printf("NOTE:- [e must be last character ,if it is present]\n"); 35 | printf("\nEnter No of alphabets and alphabets?\n"); 36 | scanf("%d",&noalpha); 37 | getchar(); 38 | for(i=0;i qno alphabet qno]\n",notransition); 57 | printf("NOTE:- [States number must be greater than zero]\n"); 58 | printf("\nEnter transition?\n"); 59 | 60 | 61 | for(i=0;ist]==0) 99 | { 100 | c++; 101 | set[temp->st]=temp->st; 102 | } 103 | temp=temp->link; 104 | 105 | 106 | } 107 | } 108 | } 109 | printf("\n"); 110 | if(c!=0) 111 | { 112 | for(m=1;m<=nostate;m++) 113 | tmpstate.nst[m]=set[m]; 114 | 115 | insertdfastate(tmpstate); 116 | 117 | printnewstate(newstate); 118 | printf("%c\t",alphabet[k]); 119 | printnewstate(tmpstate); 120 | printf("\n"); 121 | } 122 | else 123 | { 124 | printnewstate(newstate); 125 | printf("%c\t", alphabet[k]); 126 | printf("NULL\n"); 127 | } 128 | 129 | } 130 | } 131 | printf("\nStates of DFA:\n"); 132 | for(i=0;i<=complete;i++) 133 | printnewstate(hash[i]); 134 | printf("\n Alphabets:\n"); 135 | for(i=0;ist=s; 182 | temp->link=transition[r][j]; 183 | transition[r][j]=temp; 184 | } 185 | 186 | int findalpha(char c) 187 | { 188 | int i; 189 | for(i=0;i 2 | #include 3 | 4 | #define STATES 99 5 | #define SYMBOLS 20 6 | 7 | int N_symbols; /* number of input symbols */ 8 | int N_DFA_states; /* number of DFA states */ 9 | char *DFA_finals; /* final-state string */ 10 | int DFAtab[STATES][SYMBOLS]; 11 | 12 | char StateName[STATES][STATES+1]; /* state-name table */ 13 | 14 | int N_optDFA_states; /* number of optimized DFA states */ 15 | int OptDFA[STATES][SYMBOLS]; 16 | char NEW_finals[STATES+1]; 17 | 18 | /* 19 | Print state-transition table. 20 | State names: 'A', 'B', 'C', ... 21 | */ 22 | void print_dfa_table( 23 | int tab[][SYMBOLS], /* DFA table */ 24 | int nstates, /* number of states */ 25 | int nsymbols, /* number of input symbols */ 26 | char *finals) 27 | { 28 | int i, j; 29 | 30 | puts("\nDFA: STATE TRANSITION TABLE"); 31 | 32 | /* input symbols: '0', '1', ... */ 33 | printf(" | "); 34 | for (i = 0; i < nsymbols; i++) printf(" %c ", '0'+i); 35 | 36 | printf("\n-----+--"); 37 | for (i = 0; i < nsymbols; i++) printf("-----"); 38 | printf("\n"); 39 | 40 | for (i = 0; i < nstates; i++) { 41 | printf(" %c | ", 'A'+i); /* state */ 42 | for (j = 0; j < nsymbols; j++) 43 | printf(" %c ", tab[i][j]); /* next state */ 44 | printf("\n"); 45 | } 46 | printf("Final states = %s\n", finals); 47 | } 48 | 49 | /* 50 | Initialize NFA table. 51 | */ 52 | void load_DFA_table() 53 | { 54 | 55 | DFAtab[0][0] = 'B'; DFAtab[0][1] = 'C'; 56 | DFAtab[1][0] = 'E'; DFAtab[1][1] = 'F'; 57 | DFAtab[2][0] = 'A'; DFAtab[2][1] = 'A'; 58 | DFAtab[3][0] = 'F'; DFAtab[3][1] = 'E'; 59 | DFAtab[4][0] = 'D'; DFAtab[4][1] = 'F'; 60 | DFAtab[5][0] = 'D'; DFAtab[5][1] = 'E'; 61 | 62 | DFA_finals = "EF"; 63 | N_DFA_states = 6; 64 | N_symbols = 2; 65 | } 66 | 67 | /* 68 | Get next-state string for current-state string. 69 | */ 70 | void get_next_state(char *nextstates, char *cur_states, 71 | int dfa[STATES][SYMBOLS], int symbol) 72 | { 73 | int i, ch; 74 | 75 | for (i = 0; i < strlen(cur_states); i++) 76 | *nextstates++ = dfa[cur_states[i]-'A'][symbol]; 77 | *nextstates = '\0'; 78 | } 79 | 80 | /* 81 | Get index of the equivalence states for state 'ch'. 82 | Equiv. class id's are '0', '1', '2', ... 83 | */ 84 | char equiv_class_ndx(char ch, char stnt[][STATES+1], int n) 85 | { 86 | int i; 87 | 88 | for (i = 0; i < n; i++) 89 | if (strchr(stnt[i], ch)) return i+'0'; 90 | return -1; /* next state is NOT defined */ 91 | } 92 | 93 | /* 94 | Check if all the next states belongs to same equivalence class. 95 | Return value: 96 | If next state is NOT unique, return 0. 97 | If next state is unique, return next state --> 'A/B/C/...' 98 | 's' is a '0/1' string: state-id's 99 | */ 100 | char is_one_nextstate(char *s) 101 | { 102 | char equiv_class; /* first equiv. class */ 103 | 104 | while (*s == '@') s++; 105 | equiv_class = *s++; /* index of equiv. class */ 106 | 107 | while (*s) { 108 | if (*s != '@' && *s != equiv_class) return 0; 109 | s++; 110 | } 111 | 112 | return equiv_class; /* next state: char type */ 113 | } 114 | 115 | int state_index(char *state, char stnt[][STATES+1], int n, int *pn, 116 | int cur) /* 'cur' is added only for 'printf()' */ 117 | { 118 | int i; 119 | char state_flags[STATES+1]; /* next state info. */ 120 | 121 | if (!*state) return -1; /* no next state */ 122 | 123 | for (i = 0; i < strlen(state); i++) 124 | state_flags[i] = equiv_class_ndx(state[i], stnt, n); 125 | state_flags[i] = '\0'; 126 | 127 | printf(" %d:[%s]\t--> [%s] (%s)\n", 128 | cur, stnt[cur], state, state_flags); 129 | 130 | if (i=is_one_nextstate(state_flags)) 131 | return i-'0'; /* deterministic next states */ 132 | else { 133 | strcpy(stnt[*pn], state_flags); /* state-division info */ 134 | return (*pn)++; 135 | } 136 | } 137 | 138 | /* 139 | Divide DFA states into finals and non-finals. 140 | */ 141 | int init_equiv_class(char statename[][STATES+1], int n, char *finals) 142 | { 143 | int i, j; 144 | 145 | if (strlen(finals) == n) { /* all states are final states */ 146 | strcpy(statename[0], finals); 147 | return 1; 148 | } 149 | 150 | strcpy(statename[1], finals); /* final state group */ 151 | 152 | for (i=j=0; i < n; i++) { 153 | if (i == *finals-'A') { 154 | finals++; 155 | } else statename[0][j++] = i+'A'; 156 | } 157 | statename[0][j] = '\0'; 158 | 159 | return 2; 160 | } 161 | 162 | /* 163 | Get optimized DFA 'newdfa' for equiv. class 'stnt'. 164 | */ 165 | int get_optimized_DFA(char stnt[][STATES+1], int n, 166 | int dfa[][SYMBOLS], int n_sym, int newdfa[][SYMBOLS]) 167 | { 168 | int n2=n; /* 'n' + */ 169 | int i, j; 170 | char nextstate[STATES+1]; 171 | 172 | for (i = 0; i < n; i++) { /* for each pseudo-DFA state */ 173 | for (j = 0; j < n_sym; j++) { /* for each input symbol */ 174 | get_next_state(nextstate, stnt[i], dfa, j); 175 | newdfa[i][j] = state_index(nextstate, stnt, n, &n2, i)+'A'; 176 | } 177 | } 178 | 179 | return n2; 180 | } 181 | 182 | /* 183 | char 'ch' is appended at the end of 's'. 184 | */ 185 | void chr_append(char *s, char ch) 186 | { 187 | int n=strlen(s); 188 | 189 | *(s+n) = ch; 190 | *(s+n+1) = '\0'; 191 | } 192 | 193 | void sort(char stnt[][STATES+1], int n) 194 | { 195 | int i, j; 196 | char temp[STATES+1]; 197 | 198 | for (i = 0; i < n-1; i++) 199 | for (j = i+1; j < n; j++) 200 | if (stnt[i][0] > stnt[j][0]) { 201 | strcpy(temp, stnt[i]); 202 | strcpy(stnt[i], stnt[j]); 203 | strcpy(stnt[j], temp); 204 | } 205 | } 206 | 207 | /* 208 | Divide first equivalent class into subclasses. 209 | stnt[i1] : equiv. class to be segmented 210 | stnt[i2] : equiv. vector for next state of stnt[i1] 211 | Algorithm: 212 | - stnt[i1] is splitted into 2 or more classes 's1/s2/...' 213 | - old equiv. classes are NOT changed, except stnt[i1] 214 | - stnt[i1]=s1, stnt[n]=s2, stnt[n+1]=s3, ... 215 | Return value: number of NEW equiv. classses in 'stnt'. 216 | */ 217 | int split_equiv_class(char stnt[][STATES+1], 218 | int i1, /* index of 'i1'-th equiv. class */ 219 | int i2, /* index of equiv. vector for 'i1'-th class */ 220 | int n, /* number of entries in 'stnt' */ 221 | int n_dfa) /* number of source DFA entries */ 222 | { 223 | char *old=stnt[i1], *vec=stnt[i2]; 224 | int i, n2, flag=0; 225 | char newstates[STATES][STATES+1]; /* max. 'n' subclasses */ 226 | 227 | for (i=0; i < STATES; i++) newstates[i][0] = '\0'; 228 | 229 | for (i=0; vec[i]; i++) 230 | chr_append(newstates[vec[i]-'0'], old[i]); 231 | 232 | for (i=0, n2=n; i < n_dfa; i++) { 233 | if (newstates[i][0]) { 234 | if (!flag) { /* stnt[i1] = s1 */ 235 | strcpy(stnt[i1], newstates[i]); 236 | flag = 1; /* overwrite parent class */ 237 | } else /* newstate is appended in 'stnt' */ 238 | strcpy(stnt[n2++], newstates[i]); 239 | } 240 | } 241 | 242 | sort(stnt, n2); /* sort equiv. classes */ 243 | 244 | return n2; /* number of NEW states(equiv. classes) */ 245 | } 246 | 247 | /* 248 | Equiv. classes are segmented and get NEW equiv. classes. 249 | */ 250 | int set_new_equiv_class(char stnt[][STATES+1], int n, 251 | int newdfa[][SYMBOLS], int n_sym, int n_dfa) 252 | { 253 | int i, j, k; 254 | 255 | for (i = 0; i < n; i++) { 256 | for (j = 0; j < n_sym; j++) { 257 | k = newdfa[i][j]-'A'; /* index of equiv. vector */ 258 | if (k >= n) /* equiv. class 'i' should be segmented */ 259 | return split_equiv_class(stnt, i, k, n, n_dfa); 260 | } 261 | } 262 | 263 | return n; 264 | } 265 | 266 | void print_equiv_classes(char stnt[][STATES+1], int n) 267 | { 268 | int i; 269 | 270 | printf("\nEQUIV. CLASS CANDIDATE ==>"); 271 | for (i = 0; i < n; i++) 272 | printf(" %d:[%s]", i, stnt[i]); 273 | printf("\n"); 274 | } 275 | 276 | /* 277 | State-minimization of DFA: 'dfa' --> 'newdfa' 278 | Return value: number of DFA states. 279 | */ 280 | int optimize_DFA( 281 | int dfa[][SYMBOLS], /* DFA state-transition table */ 282 | int n_dfa, /* number of DFA states */ 283 | int n_sym, /* number of input symbols */ 284 | char *finals, /* final states of DFA */ 285 | char stnt[][STATES+1], /* state name table */ 286 | int newdfa[][SYMBOLS]) /* reduced DFA table */ 287 | { 288 | char nextstate[STATES+1]; 289 | int n; /* number of new DFA states */ 290 | int n2; /* 'n' + */ 291 | 292 | n = init_equiv_class(stnt, n_dfa, finals); 293 | 294 | while (1) { 295 | print_equiv_classes(stnt, n); 296 | n2 = get_optimized_DFA(stnt, n, dfa, n_sym, newdfa); 297 | if (n != n2) 298 | n = set_new_equiv_class(stnt, n, newdfa, n_sym, n_dfa); 299 | else break; /* equiv. class segmentation ended!!! */ 300 | } 301 | 302 | return n; /* number of DFA states */ 303 | } 304 | 305 | /* 306 | Check if 't' is a subset of 's'. 307 | */ 308 | int is_subset(char *s, char *t) 309 | { 310 | int i; 311 | 312 | for (i = 0; *t; i++) 313 | if (!strchr(s, *t++)) return 0; 314 | return 1; 315 | } 316 | 317 | /* 318 | New finals states of reduced DFA. 319 | */ 320 | void get_NEW_finals( 321 | char *newfinals, /* new DFA finals */ 322 | char *oldfinals, /* source DFA finals */ 323 | char stnt[][STATES+1], /* state name table */ 324 | int n) /* number of states in 'stnt' */ 325 | { 326 | int i; 327 | 328 | for (i = 0; i < n; i++) 329 | if (is_subset(oldfinals, stnt[i])) *newfinals++ = i+'A'; 330 | *newfinals++ = '\0'; 331 | } 332 | 333 | void main() 334 | { 335 | load_DFA_table(); 336 | print_dfa_table(DFAtab, N_DFA_states, N_symbols, DFA_finals); 337 | 338 | N_optDFA_states = optimize_DFA(DFAtab, N_DFA_states, 339 | N_symbols, DFA_finals, StateName, OptDFA); 340 | get_NEW_finals(NEW_finals, DFA_finals, StateName, N_optDFA_states); 341 | 342 | print_dfa_table(OptDFA, N_optDFA_states, N_symbols, NEW_finals); 343 | } 344 | -------------------------------------------------------------------------------- /cd/p7_dfa_minimisation/11anaghasethu-p7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anaghasethu/KTU-sem7-compilerdesign-programs/abc1657a1369196c62f78ba2afb37cce07407f20/cd/p7_dfa_minimisation/11anaghasethu-p7.pdf -------------------------------------------------------------------------------- /cd/p8_operator_precedence/11anaghasethu-p8.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main(){ 4 | char stack[20],ip[20],opt[10][10][1],ter[10]; 5 | int i,j,k,n,top=0,col,row; 6 | 7 | for(i=0;i<10;i++) 8 | { 9 | stack[i]=NULL; 10 | ip[i]=NULL; 11 | for(j=0;j<10;j++) 12 | { 13 | opt[i][j][1]=NULL; 14 | } 15 | } 16 | printf("Enter the no.of terminals :\n"); 17 | scanf("%d",&n); 18 | printf("\nEnter the terminals :\n"); 19 | scanf("%s",&ter); 20 | printf("\nEnter the table values :\n"); 21 | for(i=0;i') 63 | { 64 | while(stack[top]!='<'){--top;} 65 | top=top-1; 66 | printf("Reduce"); 67 | } 68 | else 69 | { 70 | printf("\nString is not accepted"); 71 | break; 72 | } 73 | } 74 | printf("\n"); 75 | for(k=0;k<=top;k++) 76 | { 77 | printf("%c",stack[k]); 78 | } 79 | printf("\t\t\t"); 80 | for(k=i;k 2 | #include 3 | #include 4 | #include 5 | #include 6 | int n,m=0,p,i=0,j=0; 7 | char a[10][10],f[10]; 8 | void follow(char c); 9 | void first(char c); 10 | int main(){ 11 | 12 | 13 | int i,z; 14 | char c,ch; 15 | //clrscr(); 16 | printf("Enter the no of prooductions:\n"); 17 | scanf("%d",&n); 18 | printf("Enter the productions:\n"); 19 | for(i=0;i