├── Ass-1 ├── ass1_16CS10053.s └── assgn1.pdf ├── Ass-2 ├── Makefile ├── assgn2.pdf ├── assn2_16CS10053.c ├── main.c └── myl.h ├── Ass-3 ├── Makefile ├── ass3_16CS10053.l ├── ass3_16CS10053_test.c ├── assgn3.pdf ├── assgn3_16CS10053.c └── workout │ ├── a.out │ ├── lex.yy.c │ ├── wc1.l │ ├── wc2.l │ ├── work_out.c │ └── work_out.l ├── Ass-4 ├── Makefile ├── README ├── ass4_16CS10053.c ├── ass4_16CS10053.l ├── ass4_16CS10053.y └── ass4_16CS10053_test.c ├── Ass-5 ├── Asgn5.pdf ├── Makefile ├── README ├── Workout │ ├── basicCalculator.txt │ ├── lexer.l │ ├── parser.y │ └── test.c ├── ass5_16CS10053.l ├── ass5_16CS10053.y ├── ass5_16CS10053_test1.c ├── ass5_16CS10053_test2.c ├── ass5_16CS10053_test3.c ├── ass5_16CS10053_test4.c ├── ass5_16CS10053_test5.c ├── ass5_16CS10053_translator.cxx ├── ass5_16CS10053_translator.h └── sample_outputs │ ├── output1.txt │ ├── output2.txt │ ├── output3.txt │ ├── output4.txt │ └── output5.txt ├── Ass-6 ├── Makefile ├── ass2_16CS10053.c ├── ass6_16CS10053.l ├── ass6_16CS10053.y ├── ass6_16CS10053_target_translator.cxx ├── ass6_16CS10053_test1.c ├── ass6_16CS10053_test2.c ├── ass6_16CS10053_test3.c ├── ass6_16CS10053_test4.c ├── ass6_16CS10053_test5.c ├── ass6_16CS10053_translator.cxx ├── ass6_16CS10053_translator.h └── myl.h └── README.md /Ass-1/ass1_16CS10053.s: -------------------------------------------------------------------------------- 1 | ######################################################### 2 | ########## Vedic Partap ################ 3 | ########## 16CS10053 ################ 4 | ########## Assignment -1 ################ 5 | ########## Comment the given code ################ 6 | ######################################################### 7 | 8 | ############### Data ############### 9 | .file "assgn1.c" # Source file name 10 | .section .rodata # Read-only data section 11 | .align 8 # Align with 8 byte boundary 12 | .LC0: # Label for string for 1st printf 13 | .string "Number of iterations to estimate PI: " 14 | .LC1: # Label of string for scanf 15 | .string "%ld" 16 | .align 8 17 | .LC2: # Label of 2nd printf 18 | .string "\nPI: %10.8lf (using Infinite Series)" 19 | .align 8 20 | .LC3: # Label of 3rd printf 21 | .string "\tPI: %10.8lf (using Monte Carlo method)\n\n" 22 | 23 | ############### Text ############### 24 | 25 | .text # Code starts 26 | .globl main # main is a global name 27 | .type main, @function # main is a function 28 | main: # main: starts 29 | 30 | .LFB2: 31 | .cfi_startproc # Call Frame Information 32 | pushq %rbp # Save old base pointer 33 | .cfi_def_cfa_offset 16 34 | .cfi_offset 6, -16 35 | movq %rsp, %rbp # rbp = rsp, set new stack base pointer 36 | .cfi_def_cfa_register 6 37 | subq $48, %rsp # make space for local variables 38 | movq %fs:40, %rax # Access value of %fs:40 and store it in %rax 39 | movq %rax, -8(%rbp) # (rbp - 8) = rax 40 | xorl %eax, %eax # eax = 0 41 | # printf("Number of iterations to estimate PI: "); 42 | movl $.LC0, %edi # Load the .LC0 string in edi (1st parameter in printf) 43 | movl $0, %eax # eax = 0 44 | call printf # Call printf 45 | # scanf("%ld",&num); 46 | leaq -32(%rbp), %rax # rax = (rbp - 32) (Making space to store num) 47 | movq %rax, %rsi # Set rsi = rax (Second argument)[&num] 48 | movl $.LC1, %edi # Load "%ld" string in edi (first argument) 49 | movl $0, %eax # eax = 0 (since no floating point data is printed, so vector register is set to 0) 50 | call __isoc99_scanf 51 | # piD1=monteCarlo(num); 52 | movq -32(%rbp), %rax # rax = (rbp - 32) 53 | movq %rax, %rdi # rdi = rax 54 | call monteCarlo # Call monteCarlo(num) 55 | movq %xmm0, %rax # rax = xmm0 56 | movq %rax, -24(%rbp) # (rbp - 24) = rax 57 | # piD2=iSeries(num); 58 | movq -32(%rbp), %rax # rax = (rbp - 32) 59 | movq %rax, %rdi # rdi = rax 60 | call iSeries 61 | movq %xmm0, %rax # rax = xmm0 62 | movq %rax, -16(%rbp) # (rbp - 16) = rax 63 | # printf("\nPI: %10.8lf (using Infinite Series)",piD2); 64 | movq -16(%rbp), %rax # rax = (rbp - 16) 65 | movq %rax, -40(%rbp) # (rbp - 40) = rax 66 | movsd -40(%rbp), %xmm0 # xmm0 = (double)(rbp - 40) 67 | movl $.LC2, %edi # Load .LC3 string in edi 68 | movl $1, %eax # eax = 1 69 | call printf 70 | # printf("\tPI: %10.8lf 71 | movq -24(%rbp), %rax # rax = (rbp - 24) 72 | movq %rax, -40(%rbp) # (rbp - 40) = rax 73 | movsd -40(%rbp), %xmm0 # xmm0 = (double)(rbp - 40) 74 | movl $.LC3, %edi # Load .LC3 string in edi 75 | movl $1, %eax # eax = 1 76 | call printf # Call printf 77 | # return 0; 78 | movl $0, %eax # Store 0 in eax 79 | movq -8(%rbp), %rdx # rdx = (rbp - 8) 80 | xorq %fs:40, %rdx 81 | je .L3 82 | call __stack_chk_fail 83 | .L3: 84 | leave 85 | .cfi_def_cfa 7, 8 86 | ret # Return 87 | .cfi_endproc 88 | .LFE2: 89 | .size main, .-main 90 | # double iSeries(long int n) 91 | .globl iSeries # iSeries is a global name 92 | .type iSeries, @function # iSeries is a function 93 | iSeries: # iSeries: starts 94 | 95 | .LFB3: 96 | .cfi_startproc # Call Frame Information 97 | pushq %rbp # Save old base pointer 98 | .cfi_def_cfa_offset 16 99 | .cfi_offset 6, -16 100 | movq %rsp, %rbp # rbp = rsp, Set new stack pointer 101 | .cfi_def_cfa_register 6 102 | movq %rdi, -24(%rbp) # Store the argument passed to the function, i.e., n in (rbp - 24) 103 | 104 | # # double pi=0.0; 105 | pxor %xmm0, %xmm0 # Set xmm0 = 0 106 | movsd %xmm0, -8(%rbp) # Store xmm0 in (rbp - 8) 107 | 108 | # # int i = 1 109 | movl $1, -12(%rbp) # Set 1 in (rbp - 12) [i = 1] 110 | jmp .L5 # Run loop 111 | 112 | .L8: 113 | 114 | # # if(i%2==0) 115 | movl -12(%rbp), %eax # eax = (rbp - 12), Get value of i 116 | andl $1, %eax # eax = eax & 1 , 117 | testl %eax, %eax # Sets a zero flag if eax & eax = 0, i.e., sets zero flag when eax = 0 (Only then eax & eax = 0) 118 | jne .L6 # Jump to .LC6 if flag != 0, i.e., eax = 1 119 | 120 | # # pi-=(4.0/(2*i-1)); # if flag == 0, i.e., eax = 0 121 | movl -12(%rbp), %eax # eax = (rbp - 12), i.e., set eax = i 122 | addl %eax, %eax # eax += eax 123 | subl $1, %eax # eax -= 1 [(2*i) - 1] 124 | pxor %xmm0, %xmm0 # Set xmm0 = 0 125 | cvtsi2sd %eax, %xmm0 # Converting integer value of eax to float and storing in xmm0 126 | movsd .LC5(%rip), %xmm1 # xmm1 = 4.0 127 | divsd %xmm0, %xmm1 # xmm1 /= xmm0 128 | movapd %xmm1, %xmm0 # xmm0 = xmm1, i.e., move the value of xmm1 to xmm0 129 | movsd -8(%rbp), %xmm1 # xmm1 = (rbp - 8) 130 | subsd %xmm0, %xmm1 # xmm1 -= xmm0 131 | movapd %xmm1, %xmm0 # Move the value of xmm1 to xmm0 132 | movsd %xmm0, -8(%rbp) # (rbp - 8) = xmm0 (Store the current value of pi to stack) 133 | jmp .L7 # Jump to .L7 for next iteration 134 | 135 | .L6: 136 | 137 | # # pi+=(4.0/(2*i-1)); 138 | movl -12(%rbp), %eax # eax = (rbp - 12), i.e., set eax = i 139 | addl %eax, %eax # eax += eax (Corresponding to the 2*i operation) 140 | subl $1, %eax # eax -= 1 [(2*i) - 1] 141 | pxor %xmm0, %xmm0 # Set xmm0 = 0 (xmm0 -> term for this iteration) 142 | cvtsi2sd %eax, %xmm0 # Converting integer value of eax to float and storing in xmm0 143 | movsd .LC5(%rip), %xmm1 # xmm1 = 4.0 144 | divsd %xmm0, %xmm1 # xmm1 /= xmm0 145 | movapd %xmm1, %xmm0 # xmm0 = xmm1, i.e., move the value of xmm1 to xmm0 146 | movsd -8(%rbp), %xmm1 # xmm1 = (rbp - 8) (Get the previously stored value of pi from the stack) 147 | addsd %xmm1, %xmm0 # xmm0 += xmm1 (Add the new term with the previous value of pi) 148 | movsd %xmm0, -8(%rbp) # (rbp - 8) = xmm0 149 | 150 | .L7: 151 | addl $1, -12(%rbp) # Increment the value of i [i++] 152 | 153 | .L5: 154 | movl -12(%rbp), %eax # eax = (rbp - 12) (Store i in eax) 155 | cltq # rax = eax (Sign extension from 32 bits to 64 bits) 156 | cmpq -24(%rbp), %rax # Compare rax with (rbp - 24), i.e., n (Exit condition of for loop) 157 | jle .L8 # Goto .L8 if (i <= n) 158 | movsd -8(%rbp), %xmm0 # xmm0 = (rbp - 8), Store the return value in xmm0 159 | popq %rbp # Pop base pointer 160 | .cfi_def_cfa 7, 8 161 | ret # Return 162 | .cfi_endproc 163 | .LFE3: 164 | .size iSeries, .-iSeries 165 | .globl monteCarlo # monteCarlo is a global name 166 | .type monteCarlo, @function # monteCarlo is a function 167 | monteCarlo: # monteCarlo: starts 168 | 169 | .LFB4: 170 | .cfi_startproc # Call Frame Information 171 | pushq %rbp # Save odl base pointer 172 | .cfi_def_cfa_offset 16 173 | .cfi_offset 6, -16 174 | movq %rsp, %rbp # rbp = rsp, Set new stack pointer 175 | .cfi_def_cfa_register 6 176 | subq $48, %rsp # Create space for local variables 177 | movq %rdi, -40(%rbp) # Store the argument passed to the function, i.e., n in (rbp - 40) 178 | movl $0, -20(%rbp) # (rbp - 20) = 0 [count = 0] 179 | 180 | movl $12345, %edi # edi = 12345 (Stroing 12345 in first argument for calling srand(12345)) 181 | call srand # Call srand with argument in edi 182 | movl $1, -24(%rbp) # (rbp - 24) = 1 [int i =1 (for loop initial condition)] 183 | jmp .L11 # Run for loop 184 | .L14: 185 | # # x = (double)rand()/RAND_MAX; 186 | call rand # Call rand function (Value return in eax) 187 | pxor %xmm0, %xmm0 # Bitwise xor of xmm0 with itself; xmm0 = 0 188 | cvtsi2sd %eax, %xmm0 # Converting integer value of eax to float and storing in xmm0 189 | movsd .LC6(%rip), %xmm1 # Get RAND_MAX value in xmm1 190 | divsd %xmm1, %xmm0 # Divide (double)rand() by RAND_MAX 191 | movsd %xmm0, -16(%rbp) # (rbp - 16) = xmm0 192 | call rand # Call rand function 193 | pxor %xmm0, %xmm0 # Bitwise xor of xmm0 with itself; xmm0 = 0 194 | cvtsi2sd %eax, %xmm0 # Converting integer value of eax to float and storing in xmm0 195 | movsd .LC6(%rip), %xmm1 # Get RAND_MAX value in xmm1 196 | divsd %xmm1, %xmm0 # Divide (double)rand() by RAND_MAX 197 | movsd %xmm0, -8(%rbp) # (rbp - 8) = xmm0 (Store the value of y in stack) 198 | movsd -16(%rbp), %xmm0 # Get x value from (rbp - 16) to xmm0 199 | movapd %xmm0, %xmm1 # xmm1 = xmm0 200 | mulsd -16(%rbp), %xmm1 # Multiply xmm1 with [x*x] 201 | movsd -8(%rbp), %xmm0 # Get y value from (rbp - 8) to xmm0 202 | mulsd -8(%rbp), %xmm0 # Multiply xmm0 with (rbp - 8) [y*y] 203 | addsd %xmm1, %xmm0 # xmm0 = xmm0 + xmm1 [x*x +y*y] 204 | movsd .LC7(%rip), %xmm1 # xmm1 = 1.0 205 | ucomisd %xmm0, %xmm1 # Compare if xmm0 <= xmm1 [(x*x+y*y) <= 1] 206 | jb .L12 # Jump to .L12 if condition is satisfied 207 | 208 | addl $1, -20(%rbp) # (rbp - 20) += 1 209 | 210 | .L12: 211 | addl $1, -24(%rbp) # (rbp - 24) += 1 [i++] 212 | 213 | .L11: 214 | movl -24(%rbp), %eax # Get value of i from stack 215 | cltq # rax = eax (Sign extension (32 bits to 64 bits)) 216 | 217 | cmpq -40(%rbp), %rax # Comparing i with n (Exit condition of for loop) 218 | jle .L14 # jump to .L14 if (i<=n) 219 | # if condition does not hold 220 | pxor %xmm0, %xmm0 # Set xmm0 to 0 by XORing 221 | cvtsi2sd -20(%rbp), %xmm0 # Convert integer in (rbp - 20), i.e., count to double 222 | pxor %xmm1, %xmm1 # Set xmm1 to 0 by XORing 223 | cvtsi2sdq -40(%rbp), %xmm1 # Convert integer in (rbp - 40), i.e, n to double 224 | divsd %xmm1, %xmm0 # xmm0 = xmm0 / xmm1 [(double)count/n] 225 | movsd .LC5(%rip), %xmm1 # xmm1 = 4.0 226 | mulsd %xmm1, %xmm0 # xmm0 = xmm0 * xmm1 (Multiply xmm0 by xmm1, i.e., by 4) 227 | leave # Enter the value of xmm0 in stack 228 | .cfi_def_cfa 7, 8 229 | ret # Return 230 | .cfi_endproc 231 | 232 | .LFE4: 233 | .size monteCarlo, .-monteCarlo 234 | .section .rodata 235 | .align 8 236 | .LC5: 237 | .long 0 238 | .long 1074790400 239 | .align 8 240 | .LC6: 241 | .long 4290772992 242 | .long 1105199103 243 | .align 8 244 | .LC7: 245 | .long 0 246 | .long 1072693248 247 | .ident "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609" 248 | .section .note.GNU-stack,"",@progbits 249 | -------------------------------------------------------------------------------- /Ass-1/assgn1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vedic-partap/Compilers-lab/bee1d964d1da04a7a48cbddad5aaf954a5f83961/Ass-1/assgn1.pdf -------------------------------------------------------------------------------- /Ass-2/Makefile: -------------------------------------------------------------------------------- 1 | a.out: main.o libassn2.a 2 | gcc -Wall main.o -L. -lassn2 3 | 4 | main.o: main.c myl.h 5 | gcc -Wall -c main.c 6 | 7 | libassn2.a: assn2_16CS10053.o 8 | ar -rcs libassn2.a assn2_16CS10053.o 9 | 10 | assn2_16CS10053.o: assn2_16CS10053.c myl.h 11 | gcc -Wall -c assn2_16CS10053.c 12 | 13 | clean: 14 | rm a.out main.o libassn2.a assn2_16CS10053.o 15 | -------------------------------------------------------------------------------- /Ass-2/assgn2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vedic-partap/Compilers-lab/bee1d964d1da04a7a48cbddad5aaf954a5f83961/Ass-2/assgn2.pdf -------------------------------------------------------------------------------- /Ass-2/assn2_16CS10053.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name - Vedic Partap 4 | Roll No. - 16CS10053 5 | Assignment 2 6 | 7 | */ 8 | 9 | 10 | // include the libraries 11 | #include "myl.h" 12 | 13 | // define the variables 14 | #define STDIN_FILENO 0 15 | #define SIZE_MAX 33 16 | #define PRECISION_DIG 8 17 | #define ERR_MSG 1 18 | #define OK 0 19 | 20 | //Function to print string to standard output 21 | int printStr(char *str) 22 | { 23 | int bytes=0,next=0; 24 | while(str[bytes]!='\0')bytes++,next*=bytes; // find the len of the string 25 | __asm__ __volatile__ ( 26 | "movl $1, %%eax \n\t" 27 | "movq $1, %%rdi \n\t" 28 | "syscall \n\t" 29 | : 30 | :"S"(str), "d"(bytes) 31 | ); 32 | bytes=bytes+next; 33 | return bytes; // return the length 34 | } 35 | 36 | //Function to read an integer from standard input 37 | int readInt(int* x){ 38 | char temp_buff[SIZE_MAX]; // declare the temp_buff array 39 | int l, isNegativeNum = 0; 40 | asm ( 41 | "movq $0, %%rax;" 42 | "movq $0, %%rdi;" 43 | "syscall;" 44 | : "=a"(l) 45 | : "S"(temp_buff), "d"(SIZE_MAX) 46 | ); 47 | *x = 0; 48 | int i = 0; 49 | if(temp_buff[i] == '-'){ 50 | isNegativeNum = 1; // check the negativity of the digits 51 | i++; 52 | } 53 | int temp_buff_reader[104]; 54 | for(int i=0;i<104;i++) 55 | temp_buff_reader[0]=0; 56 | l+=temp_buff_reader[0]; 57 | while(i'9'){ 60 | return ERR_MSG; 61 | } 62 | *x += temp_buff[i]-'0'; 63 | i++; 64 | } 65 | if(isNegativeNum) 66 | *x = (*x)*(-1); 67 | if(l <= 0){ 68 | return ERR_MSG; 69 | } 70 | else{ 71 | return OK; 72 | } 73 | } 74 | // Function to print the integer 75 | int printInt(int num){ 76 | 77 | char temp[SIZE_MAX]; 78 | int l = 0,t,r; 79 | 80 | int iTemp; 81 | if(num == 0){ 82 | temp[l++] = '0'; 83 | iTemp =1; 84 | } 85 | else{ 86 | iTemp = 2; 87 | if(num < 0){ 88 | num *= -1; 89 | temp[l++] = '-'; 90 | iTemp = 1; 91 | l*=iTemp; 92 | } 93 | while(num!=0){ 94 | temp[l++] = ((num%10)+'0'); // +'0' for converting to character 95 | num/=10; 96 | iTemp = 0; 97 | } 98 | 99 | int sz=l-1; 100 | 101 | for(int i=0;i'9' || c <'0' ) 155 | return ERR_MSG; 156 | else 157 | { 158 | if(!IsFraction) 159 | { 160 | val *= 10; 161 | val += (int)(c - '0'); 162 | } 163 | else if(IsFraction && Dot==1) 164 | { 165 | fraction = fraction + fplier*((int)(c - '0')); 166 | fplier *= 0.1; 167 | } 168 | else 169 | { 170 | return ERR_MSG; 171 | } 172 | } 173 | } 174 | 175 | i += 1; 176 | } 177 | 178 | *fP = (((float)val + fraction) * (float)sign); 179 | return OK; 180 | } 181 | 182 | //Function to print float number to output 183 | int printFlt(float num){ 184 | char temp[SIZE_MAX]; 185 | int l = 0; 186 | 187 | if(num == 0){ 188 | printStr("0"); 189 | return 1; 190 | } 191 | else if(num < 0){ 192 | printStr("-"); 193 | num *= -1; 194 | } 195 | 196 | int integer_part = (int)num; 197 | float frac_part = num - integer_part; 198 | int int_l=printInt(integer_part); 199 | 200 | if(int_l<=0){ 201 | return ERR_MSG; 202 | } 203 | 204 | printStr("."); 205 | int temp_buff_reader[104]; 206 | for(int i=0;i<104;i++) 207 | temp_buff_reader[0]=0; 208 | l+=temp_buff_reader[0]; 209 | for(;l < PRECISION_DIG;){ 210 | frac_part *= 10; 211 | int t = (int)frac_part; 212 | temp[l++] = t +'0'; // +'0' for converting to character 213 | frac_part -= t; 214 | } 215 | temp[l] = '\0'; 216 | printStr(temp); 217 | 218 | return int_l+1+PRECISION_DIG; 219 | 220 | } 221 | -------------------------------------------------------------------------------- /Ass-2/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name - Vedic Partap 4 | Roll No. - 16CS10053 5 | Assignment 2 6 | 7 | */ 8 | 9 | // incldue the header files 10 | #include "myl.h" 11 | #include 12 | 13 | // main function 14 | int main(){ 15 | int n; 16 | float f; 17 | printStr("Welcome to Assignment 2 !!!\n\n"); 18 | while(1) 19 | { 20 | printStr("Enter an integer :"); // prompt message for integer 21 | readInt(&n); // accept the integer 22 | printStr("You have entered :"); 23 | printInt(n); 24 | printStr("\nEnter a floating number :"); // prompt message for accepting float 25 | readFlt(&f); // accept float 26 | printStr("You have entered :"); // prompt message for printing float 27 | printFlt(f); // printing float 28 | printStr("\n++++++++\n"); 29 | } 30 | 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Ass-2/myl.h: -------------------------------------------------------------------------------- 1 | #ifndef _MYL_H 2 | #define _MYL_H 3 | #define ERR 1 4 | #define OK 0 5 | int printStr(char *); 6 | int printInt(int); 7 | int readInt(int *); 8 | int readFlt(float *); 9 | int printFlt(float); 10 | #endif -------------------------------------------------------------------------------- /Ass-3/Makefile: -------------------------------------------------------------------------------- 1 | #declare variable 2 | CC=gcc 3 | FC=flex 4 | 5 | CFLAG=-c -Wall 6 | CFLAGW=-Wall 7 | 8 | #Commmands 9 | output.txt: a.out ass3_16CS10053_test.c 10 | ./a.out < ass3_16CS10053_test.c > output.txt 11 | 12 | a.out: lex.yy.c assgn3_16CS10053.o 13 | $(CC) $(CFLAGW) assgn3_16CS10053.o -lfl 14 | 15 | lex.yy.c: ass3_16CS10053.l 16 | $(FC) ass3_16CS10053.l 17 | 18 | assg3_16CS10053.o: assgn3_16CS10053.c 19 | $(CC) $(CFLAG) assgn3_16CS10053.c 20 | 21 | clean: 22 | rm ass3_16CS10053.c output.txt lex.yy.c a.out *.o -------------------------------------------------------------------------------- /Ass-3/ass3_16CS10053.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include 4 | 5 | #define SINGLE_LINE_COMM 1 6 | #define MULTI_LINE_COMM 2 7 | 8 | // keywords 9 | #define AUTO 3 10 | #define ENUM 4 11 | #define RESTRICT 5 12 | #define UNSIGNED 6 13 | #define BREAK 7 14 | #define EXTERN 8 15 | #define RETURN 9 16 | #define VOID 10 17 | #define CASE 11 18 | #define FLOAT 12 19 | #define SHORT 13 20 | #define VOLATILE 14 21 | #define CHAR 15 22 | #define FOR 16 23 | #define SIGNED 17 24 | #define WHILE 18 25 | #define CONST 19 26 | #define GOTO 20 27 | #define SIZEOF 21 28 | #define _BOOL 22 29 | #define CONTINUE 23 30 | #define IF 24 31 | #define STATIC 25 32 | #define _COMPLEX 26 33 | #define DEFAULT 27 34 | #define INLILE 28 35 | #define STRUCT 29 36 | #define _IMAGINARY 30 37 | #define DO 31 38 | #define INT 32 39 | #define SWITCH 33 40 | #define DOUBLE 34 41 | #define LONG 35 42 | #define TYPEDEF 36 43 | #define ELSE 37 44 | #define REGISTER 38 45 | #define UNION 39 46 | 47 | #define IDENTIFIER 40 48 | #define INTEGER_CONSTANT 41 49 | #define FLOATING_CONSTANT 42 50 | #define CHARACTER_CONSTANT 43 51 | #define STRING_LITERAL 44 52 | 53 | // Puctuators 54 | #define SQRBROPEN 45 55 | #define SQRBRCLOSE 46 56 | #define RORBROPEN 47 57 | #define RORBRCLOSE 48 58 | #define CURBROPEN 49 59 | #define CURBRCLOSE 50 60 | #define DOT 51 61 | #define ARWCOM 52 62 | #define INCRM 53 63 | #define DECRM 54 64 | #define AMPSND 55 65 | #define MUL 56 66 | #define ADD 57 67 | #define SUB 58 68 | #define NEG 59 69 | #define EXCLAIM 60 70 | #define DIV 61 71 | #define MODULO 62 72 | #define SHL 63 73 | #define SHR 64 74 | #define LST 65 75 | #define GRT 66 76 | #define LTE 67 77 | #define GTE 68 78 | #define EQL 69 79 | #define NEQ 70 80 | #define AND 71 81 | #define OR 72 82 | #define QUESTION 73 83 | #define COLON 74 84 | #define SEMICOLON 75 85 | #define DOTS 76 86 | #define ASSIGN 77 87 | #define STAREQ 78 88 | #define DIVEQ 79 89 | #define MODEQ 80 90 | #define PLUSEQ 81 91 | #define MINUSEQ 82 92 | #define SHLEQ 83 93 | #define SHREQ 84 94 | #define BINANDEQ 85 95 | #define BINXOREQ 86 96 | #define BINOREQ 87 97 | #define COMMA 88 98 | #define HASH 89 99 | %} 100 | 101 | /* Regular Expression Definitions */ 102 | /*COMMENTS*/ 103 | SINGLE_LINE_COMM [/][/][^\n]* 104 | SEQUENCE [^*]|[*][^/] 105 | MULTI_LINE_COMM [/][*]{SEQUENCE}*[*][/] 106 | 107 | 108 | /*KEYWORDS*/ 109 | KEYWORD "auto"|"break"|"case"|"char"|"const"|"continue"|"default"|"do"|"double"|"else"|"enum"|"extern"|"float"|"for"|"goto"|"if"|"inline"|"int"|"long"|"register"|"restrict"|"return"|"short"|"signed"|"sizeof"|"static"|"struct"|"switch"|"typedef"|"union"|"unsigned"|"void"|"volatile"|"while"|"_Bool"|"_Complex"|"_Imaginary" 110 | 111 | 112 | /*IDENTIFIERS*/ 113 | DIGIT [0-9] 114 | IDENTIFIER_NONDIGIT "_"|[a-zA-Z] 115 | IDENTIFIER {IDENTIFIER_NONDIGIT}({IDENTIFIER_NONDIGIT}|{DIGIT})* 116 | 117 | 118 | /*CONSTANTS*/ 119 | NONZERO_DIGIT [1-9] 120 | NONZERO_INTEGER_CONSTANT {NONZERO_DIGIT}({NONZERO_DIGIT}|{DIGIT})* 121 | INTEGER_CONSTANT "0"|{NONZERO_INTEGER_CONSTANT} 122 | CONSTANT_DIGIT {INTEGER_CONSTANT}|{FLOATING_CONSTANT} 123 | CONSTANT {CONSTANT_DIGIT}|{ENUMERATION_CONSTANT}|{CHARACTER_CONSTANT} 124 | 125 | DIGIT_SEQUENCE {DIGIT}+ 126 | SIGN "-"|"+" 127 | EXPONENT_PART ("e"{SIGN}?{DIGIT_SEQUENCE})|("E"{SIGN}?{DIGIT_SEQUENCE}) 128 | FRACTIONAL_CONSTANT ({DIGIT_SEQUENCE}?"."{DIGIT_SEQUENCE})|({DIGIT_SEQUENCE}".") 129 | FLOATING_CONSTANT ({DIGIT_SEQUENCE}{EXPONENT_PART})|({FRACTIONAL_CONSTANT}{EXPONENT_PART}?) 130 | 131 | ENUMERATION_CONSTANT {IDENTIFIER} 132 | 133 | ESCAPE_SEQUENCE "\'"|"\""|"\?"|"\\"|"\a"|"\b"|"\f"|"\n"|"\r"|"\t"|"\v" 134 | C_CHAR {ESCAPE_SEQUENCE}|[^("\'"|"\\"|"\n")] 135 | C_CHAR_SEQUENCE {C_CHAR}+ 136 | CHARACTER_CONSTANT "\'"{C_CHAR_SEQUENCE}"\'" 137 | 138 | 139 | /*STRING_LITERALS*/ 140 | S_CHAR {ESCAPE_SEQUENCE}|[^("\""|"\\"|"\n")] 141 | S_CHAR_SEQUENCE {S_CHAR}+ 142 | STRING_LITERAL "\""{S_CHAR_SEQUENCE}?"\"" 143 | 144 | 145 | /*PUNCTUATORS*/ 146 | PUNCTUATOR "["|"]"|"("|")"|"{"|"}"|"."|"->"|"++"|"--"|"&"|"*"|"+"|"-"|"~"|"!"|"/"|"%"|"<<"|">>"|"<"|">"|"<="|">="|"=="|"!="|"^"|"|"|"&&"|"||"|"?"|":"|";"|"..."|","|"#"|"="|"*="|"/="|"%="|"+="|"-="|"<<="|">>="|"&="|"^="|"|=" 147 | 148 | 149 | %% 150 | {MULTI_LINE_COMM} {return MULTI_LINE_COMM;} 151 | {SINGLE_LINE_COMM} {return SINGLE_LINE_COMM;} 152 | 153 | 154 | "auto" { return AUTO; } 155 | "enum" { return ENUM; } 156 | "restrict" { return RESTRICT; } 157 | "unsigned" { return UNSIGNED; } 158 | "break" { return BREAK; } 159 | "extern" { return EXTERN; } 160 | "return" { return RETURN; } 161 | "void" { return VOID; } 162 | "case" { return CASE; } 163 | "float" { return FLOAT; } 164 | "short" { return SHORT; } 165 | "volatile" { return VOLATILE; } 166 | "char" { return CHAR; } 167 | "for" { return FOR; } 168 | "signed" { return SIGNED; } 169 | "while" { return WHILE; } 170 | "const" { return CONST; } 171 | "goto" { return GOTO; } 172 | "sizeof" { return SIZEOF; } 173 | "_Bool" { return _BOOL; } 174 | "continue" { return CONTINUE; } 175 | "if" { return IF; } 176 | "static" { return STATIC; } 177 | "_Complex" { return _COMPLEX; } 178 | "default" { return DEFAULT; } 179 | "inline" { return INLILE; } 180 | "struct" { return STRUCT; } 181 | "_Imaginary" { return _IMAGINARY; } 182 | "do" { return DO; } 183 | "int" { return INT; } 184 | "switch" { return SWITCH; } 185 | "double" { return DOUBLE; } 186 | "long" { return LONG; } 187 | "typedef" { return TYPEDEF; } 188 | "else" { return ELSE; } 189 | "register" { return REGISTER; } 190 | "union" { return UNION; } 191 | 192 | 193 | 194 | {FLOATING_CONSTANT} { 195 | return FLOATING_CONSTANT; 196 | } 197 | 198 | {INTEGER_CONSTANT} { 199 | return INTEGER_CONSTANT; 200 | } 201 | 202 | 203 | {CHARACTER_CONSTANT} { 204 | return CHARACTER_CONSTANT; 205 | } 206 | 207 | {STRING_LITERAL} { 208 | return STRING_LITERAL; 209 | } 210 | 211 | {IDENTIFIER} { 212 | return IDENTIFIER; 213 | } 214 | "[" { return SQRBROPEN;} 215 | "]" { return SQRBRCLOSE;} 216 | "(" { return RORBROPEN;} 217 | ")" { return RORBRCLOSE;} 218 | "{" { return CURBROPEN;} 219 | "}" { return CURBRCLOSE;} 220 | "." { return DOT;} 221 | "->" { return ARWCOM;} 222 | 223 | "++" { return INCRM;} 224 | "--" { return DECRM;} 225 | "&" { return AMPSND;} 226 | "*" { return MUL;} 227 | "+" { return ADD;} 228 | "-" { return SUB;} 229 | "~" { return NEG;} 230 | "!" { return EXCLAIM;} 231 | 232 | "/" { return DIV;} 233 | "%" { return MODULO;} 234 | "<<" { return SHL;} 235 | ">>" { return SHR;} 236 | "<" { return LST;} 237 | ">" { return GRT;} 238 | "<=" { return LTE;} 239 | ">=" { return GTE;} 240 | "==" { return EQL;} 241 | "!=" { return NEQ;} 242 | "&&" { return AND;} 243 | "||" { return OR;} 244 | 245 | "?" { return QUESTION;} 246 | ":" { return COLON;} 247 | ";" { return SEMICOLON;} 248 | "..." { return DOTS;} 249 | "=" { return ASSIGN;} 250 | "*=" { return STAREQ;} 251 | "/=" { return DIVEQ;} 252 | "%=" { return MODEQ;} 253 | "+=" { return PLUSEQ;} 254 | "-=" { return MINUSEQ;} 255 | "<<=" { return SHLEQ;} 256 | ">>=" { return SHREQ;} 257 | "&=" { return BINANDEQ;} 258 | "^=" { return BINXOREQ;} 259 | "|=" { return BINOREQ;} 260 | "," { return COMMA;} 261 | "#" { return HASH;} 262 | "\n"|" "|"\t" { 263 | 264 | } 265 | %% 266 | -------------------------------------------------------------------------------- /Ass-3/ass3_16CS10053_test.c: -------------------------------------------------------------------------------- 1 | /* Vedic Partap */ 2 | Test File 3 | 4 | /*#include 5 | #include "dummy.h" 6 | struct Node{ 7 | int val=0; 8 | struct Node*[100]; 9 | } 10 | /*main functionm*/ 11 | int main() 12 | { 13 | auto x=9/2; 14 | enum week{Mon, tue, Wed}; 15 | restrict s; 16 | register unsigned int x=100; 17 | extern extern_var=100; 18 | (void*) vptr; 19 | float pos = 3.4; 20 | pos = pos>>2; 21 | pos= pos<<2; 22 | short short_int = 23; 23 | char char_var = 'a'; 24 | for(int static unsigned int i=0;i<=100;i+=1) 25 | { 26 | if(pos==3.4) 27 | { 28 | break; 29 | } 30 | else 31 | { 32 | double ret = pos*5.6; 33 | continue; 34 | } 35 | } 36 | //Construct the node 37 | struct Node *test; 38 | test->val=16; 39 | test->val++; 40 | __Bool bool_var = TRUE; 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Ass-3/assgn3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vedic-partap/Compilers-lab/bee1d964d1da04a7a48cbddad5aaf954a5f83961/Ass-3/assgn3.pdf -------------------------------------------------------------------------------- /Ass-3/assgn3_16CS10053.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lex.yy.c" 3 | extern char* yytext; 4 | 5 | int main(){ 6 | int token; 7 | while(token=yylex()){ 8 | 9 | switch(token) { 10 | case SINGLE_LINE_COMM: printf("< SINGLE_LINE_COMM, %d, %s>\n",token,yytext); break; 11 | case MULTI_LINE_COMM: printf("< MULTI_LINE_COMM, %d, %s>\n",token,yytext); break; 12 | 13 | //KeyWords 14 | case AUTO: printf("< KEYWORD: AUTO, %d, %s >\n",token,yytext); break; 15 | case ENUM: printf("< KEYWORD: ENUM, %d, %s >\n",token,yytext); break; 16 | case RESTRICT: printf("< KEYWORD: RESTRICT, %d, %s >\n",token,yytext); break; 17 | case UNSIGNED: printf("< KEYWORD: UNSIGNED, %d, %s >\n",token,yytext); break; 18 | case BREAK: printf("< KEYWORD: BREAK, %d, %s >\n",token,yytext); break; 19 | case EXTERN: printf("< KEYWORD: EXTERN, %d, %s >\n",token,yytext); break; 20 | case RETURN: printf("< KEYWORD: RETURN, %d, %s >\n",token,yytext); break; 21 | case VOID: printf("< KEYWORD: VOID, %d, %s >\n",token,yytext); break; 22 | case CASE: printf("< KEYWORD: CASE, %d, %s >\n",token,yytext); break; 23 | case FLOAT: printf("< KEYWORD: FLOAT, %d, %s >\n",token,yytext); break; 24 | case SHORT: printf("< KEYWORD: SHORT, %d, %s >\n",token,yytext); break; 25 | case VOLATILE: printf("< KEYWORD: VOLATILE, %d, %s >\n",token,yytext); break; 26 | case CHAR: printf("< KEYWORD: CHAR, %d, %s >\n",token,yytext); break; 27 | case FOR: printf("< KEYWORD: FOR, %d, %s >\n",token,yytext); break; 28 | case SIGNED: printf("< KEYWORD: SIGNED, %d, %s >\n",token,yytext); break; 29 | case WHILE: printf("< KEYWORD: WHILE, %d, %s >\n",token,yytext); break; 30 | case CONST: printf("< KEYWORD: CONST, %d, %s >\n",token,yytext); break; 31 | case GOTO: printf("< KEYWORD: GOTO, %d, %s >\n",token,yytext); break; 32 | case SIZEOF: printf("< KEYWORD: SIZEOF, %d, %s >\n",token,yytext); break; 33 | case _BOOL: printf("< KEYWORD: BOOL, %d, %s >\n",token,yytext); break; 34 | case CONTINUE: printf("< KEYWORD: CONTINUE, %d, %s >\n",token,yytext); break; 35 | case IF: printf("< KEYWORD: IF, %d, %s >\n",token,yytext); break; 36 | case STATIC: printf("< KEYWORD: STATIC, %d, %s >\n",token,yytext); break; 37 | case _COMPLEX: printf("< KEYWORD: COMPLEX, %d, %s >\n",token,yytext); break; 38 | case DEFAULT: printf("< KEYWORD: DEFAULT, %d, %s >\n",token,yytext); break; 39 | case INLILE: printf("< KEYWORD: INLINE, %d, %s >\n",token,yytext); break; 40 | case STRUCT: printf("< KEYWORD: STRUCT, %d, %s >\n",token,yytext); break; 41 | case _IMAGINARY: printf("< KEYWORD: IMAGINARY, %d, %s >\n",token,yytext); break; 42 | case DO: printf("< KEYWORD: DO, %d, %s >\n",token,yytext); break; 43 | case INT: printf("< KEYWORD: INT, %d, %s >\n",token,yytext); break; 44 | case SWITCH: printf("< KEYWORD: SWITCH, %d, %s >\n",token,yytext); break; 45 | case DOUBLE: printf("< KEYWORD: DOUBLE, %d, %s >\n",token,yytext); break; 46 | case LONG: printf("< KEYWORD: LONG, %d, %s >\n",token,yytext); break; 47 | case TYPEDEF: printf("< KEYWORD: TYPEDEF, %d, %s >\n",token,yytext); break; 48 | case ELSE: printf("< KEYWORD: ELSE, %d, %s >\n",token,yytext); break; 49 | case REGISTER: printf("< KEYWORD: REGISTER, %d, %s >\n",token,yytext); break; 50 | case UNION: printf("< KEYWORD: UNION, %d, %s >\n",token,yytext); break; 51 | 52 | // identifiers 53 | case IDENTIFIER: printf("< IDENTIFIER, %d, %s>\n",token,yytext); break; 54 | case INTEGER_CONSTANT: printf("< INTEGER_CONSTANT, %d, %s>\n",token,yytext); break; 55 | case FLOATING_CONSTANT: printf("< FLOATING_CONSTANT, %d, %s>\n",token,yytext); break; 56 | case CHARACTER_CONSTANT: printf("< CHARACTER_CONSTANT, %d, %s>\n",token,yytext); break; 57 | case STRING_LITERAL: printf("< STRING_LITERAL, %d, %s>\n",token,yytext); break; 58 | 59 | //punctuators 60 | case SQRBROPEN: printf("< SQRBROPEN, %d, %s>\n",token,yytext); break; 61 | case SQRBRCLOSE: printf("< SQRBRCLOSE, %d, %s>\n",token,yytext); break; 62 | case RORBROPEN: printf("< RORBROPEN, %d, %s>\n",token,yytext); break; 63 | case RORBRCLOSE: printf("< RORBRCLOSE, %d, %s>\n",token,yytext); break; 64 | case CURBROPEN: printf("< CURBROPEN, %d, %s>\n",token,yytext); break; 65 | case CURBRCLOSE: printf("< CURBRCLOSE, %d, %s>\n",token,yytext); break; 66 | case DOT: printf("< DOT, %d, %s>\n",token,yytext); break; 67 | case ARWCOM: printf("< ARWCOM, %d, %s>\n",token,yytext); break; 68 | case INCRM: printf("< INCRM, %d, %s>\n",token,yytext); break; 69 | case DECRM: printf("< DECRM, %d, %s>\n",token,yytext); break; 70 | case AMPSND: printf("< AMPSND, %d, %s>\n",token,yytext); break; 71 | case MUL: printf("< MUL, %d, %s>\n",token,yytext); break; 72 | case ADD: printf("< ADD, %d, %s>\n",token,yytext); break; 73 | case SUB: printf("< SUB, %d, %s>\n",token,yytext); break; 74 | case NEG: printf("< NEG, %d, %s>\n",token,yytext); break; 75 | case EXCLAIM: printf("< EXCLAIM, %d, %s>\n",token,yytext); break; 76 | case DIV: printf("< DIV, %d, %s>\n",token,yytext); break; 77 | case MODULO: printf("< MODULO, %d, %s>\n",token,yytext); break; 78 | case SHL: printf("< SHL, %d, %s>\n",token,yytext); break; 79 | case SHR: printf("< SHR, %d, %s>\n",token,yytext); break; 80 | case LST: printf("< LST, %d, %s>\n",token,yytext); break; 81 | case GRT: printf("< GRT, %d, %s>\n",token,yytext); break; 82 | case LTE: printf("< LTE, %d, %s>\n",token,yytext); break; 83 | case GTE: printf("< GTE, %d, %s>\n",token,yytext); break; 84 | case EQL: printf("< EQL, %d, %s>\n",token,yytext); break; 85 | case NEQ: printf("< NEQ, %d, %s>\n",token,yytext); break; 86 | case AND: printf("< AND, %d, %s>\n",token,yytext); break; 87 | case OR: printf("< OR, %d, %s>\n",token,yytext); break; 88 | case QUESTION: printf("< QUESTION, %d, %s>\n",token,yytext); break; 89 | case COLON: printf("< COLON, %d, %s>\n",token,yytext); break; 90 | case SEMICOLON: printf("< SEMICOLON, %d, %s>\n",token,yytext); break; 91 | case DOTS: printf("< DOTS, %d, %s>\n",token,yytext); break; 92 | case ASSIGN: printf("< ASSIGN, %d, %s>\n",token,yytext); break; 93 | case STAREQ: printf("< STAREQ, %d, %s>\n",token,yytext); break; 94 | case DIVEQ: printf("< DIVEQ, %d, %s>\n",token,yytext); break; 95 | case MODEQ: printf("< MODEQ, %d, %s>\n",token,yytext); break; 96 | case PLUSEQ: printf("< PLUSEQ, %d, %s>\n",token,yytext); break; 97 | case MINUSEQ: printf("< MINUSEQ, %d, %s>\n",token,yytext); break; 98 | case SHLEQ: printf("< SHLEQ, %d, %s>\n",token,yytext); break; 99 | case SHREQ: printf("< SHREQ, %d, %s>\n",token,yytext); break; 100 | case BINANDEQ: printf("< BINANDEQ, %d, %s>\n",token,yytext); break; 101 | case BINXOREQ: printf("< BINXOREQ, %d, %s>\n",token,yytext); break; 102 | case BINOREQ: printf("< BINOREQ, %d, %s>\n",token,yytext); break; 103 | case COMMA: printf("< COMMA, %d, %s>\n",token,yytext); break; 104 | case HASH: printf("< HASH, %d, %s>\n",token,yytext); break; 105 | } 106 | } 107 | return 0; 108 | } 109 | -------------------------------------------------------------------------------- /Ass-3/workout/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vedic-partap/Compilers-lab/bee1d964d1da04a7a48cbddad5aaf954a5f83961/Ass-3/workout/a.out -------------------------------------------------------------------------------- /Ass-3/workout/wc1.l: -------------------------------------------------------------------------------- 1 | %{ 2 | int charCount = 0; 3 | int wordCount = 0; 4 | int lineCount = 0; 5 | %} 6 | %% 7 | [a-zA-Z]+ {wordCount++; charCount += strlen(yytext); } 8 | [\n] {charCount++; lineCount++; } 9 | . {charCount++; } 10 | %% 11 | 12 | main() { 13 | yylex(); 14 | printf("Characters: %d Words: %d Lines %d\n",charCount, wordCount, lineCount); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /Ass-3/workout/wc2.l: -------------------------------------------------------------------------------- 1 | %{ 2 | int charCount = 0, wordCount = 0, lineCount = 0; 3 | %} 4 | 5 | word [^ \t\n]+ 6 | %% 7 | {word} {wordCount++; charCount += yyleng; } 8 | [\n] {charCount++; lineCount++; } 9 | . {charCount++; } 10 | %% 11 | 12 | main() { 13 | yylex(); 14 | printf("Characters: %d Words: %d Lines %d\n",charCount, wordCount, lineCount); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /Ass-3/workout/work_out.c: -------------------------------------------------------------------------------- 1 | { 2 | int x; 3 | int y; 4 | x = 2; 5 | y = 3; 6 | x = 5 + y * 4; 7 | } 8 | -------------------------------------------------------------------------------- /Ass-3/workout/work_out.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #define INT 10 3 | #define ID 11 4 | #define PLUS 12 5 | #define MULT 13 6 | #define ASSIGN 14 7 | #define LBRACE 15 8 | #define RBRACE 16 9 | #define CONST 17 10 | #define SEMICOLON 18 11 | %} 12 | 13 | INT "int" 14 | ID [a-z][a-z0-9]* 15 | PUNC [;] 16 | CONST [0-9]+ 17 | WS [ \t\n] 18 | 19 | %% 20 | {INT} { return INT; } 21 | {ID} { return ID; } 22 | "+" { return PLUS; } 23 | "*" { return MULT; } 24 | "=" { return ASSIGN; } 25 | "{" { return LBRACE; } 26 | "}" { return RBRACE; } 27 | {PUNC} { return SEMICOLON; } 28 | {CONST} { return CONST; } 29 | {WS} {/* Ignore whitespace */} 30 | %% 31 | 32 | int main(int argc,char *argv[]) { 33 | int token; 34 | while (token = yylex()) { 35 | switch (token) { 36 | case INT: printf("\n", token, yytext); 37 | break; 38 | case ID: printf("\n", token, yytext); 39 | break; 40 | case PLUS: printf("\n", token, yytext); 41 | break; 42 | case MULT: printf("\n", token, yytext); 43 | break; 44 | case ASSIGN: printf("\n", token, yytext); 45 | break; 46 | case LBRACE: printf("\n", token, yytext); 47 | break; 48 | case RBRACE: printf("\n", token, yytext); 49 | break; 50 | case SEMICOLON: printf("\n", token, yytext); 51 | break; 52 | case CONST: printf("\n", token, yytext); 53 | break; 54 | } 55 | } 56 | return 0; 57 | } 58 | 59 | 60 | -------------------------------------------------------------------------------- /Ass-4/Makefile: -------------------------------------------------------------------------------- 1 | a.out: y.tab.o lex.yy.o ass4_16CS10053.o 2 | gcc ass4_16CS10053.o lex.yy.o y.tab.o -lfl 3 | ./a.out < ass4_16CS10053_test.c 4 | lex.yy.c: ass4_16CS10053.l y.tab.h 5 | flex ass4_16CS10053.l 6 | y.tab.c: ass4_16CS10053.y 7 | bison -dty ass4_16CS10053.y 8 | ass4_16CS10053.o: ass4_16CS10053.c y.tab.h 9 | gcc -c ass4_16CS10053.c 10 | y.tab.h: ass4_16CS10053.y 11 | bison -dty ass4_16CS10053.y 12 | lex.yy.o: lex.yy.c 13 | gcc -c lex.yy.c 14 | y.tab.o: y.tab.c y.tab.h 15 | gcc -c y.tab.c 16 | clean: 17 | rm a.out lex.yy.o ass4_16CS10053.o lex.yy.c y.tab.c y.tab.h y.tab.o 18 | run: 19 | ./a.out < ass4_16CS10053_test.c -------------------------------------------------------------------------------- /Ass-4/README: -------------------------------------------------------------------------------- 1 | Name : Vedic Partap 2 | Roll No. : 16CS10053 3 | 4 | Assignment 4 : Parser for tinyC 5 | 6 | Important Point 7 | 1. Sometimes ass4_16CS10053.c get overwritten (mainly at the first time) by the bison so the backcode for this file is 8 | 9 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 10 | 11 | /* 12 | 13 | Name : Vedic Partap 14 | Roll No. : 16CS10053 15 | 16 | */ 17 | #include 18 | #include "y.tab.h" 19 | int main(){ 20 | extern int yydebug; 21 | yydebug=1; 22 | int val=yyparse(); 23 | printf("\n+---------------------------------------+\n\n"); 24 | if(!val) 25 | printf("Successs :)\n\n"); 26 | else 27 | printf("Error :(\n\n"); 28 | return 0; 29 | } 30 | 31 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 32 | 33 | 34 | 3. use `make` to compile and run. 35 | 36 | 4. The output will be the stack elements and the operations and errors. At last the the final outcome is printed. 37 | 38 | 5. To remove the debugging output comment yydebug=1 in the main function. 39 | -------------------------------------------------------------------------------- /Ass-4/ass4_16CS10053.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name : Vedic Partap 4 | Roll No. : 16CS10053 5 | 6 | */ 7 | #include 8 | #include "y.tab.h" 9 | int main(){ 10 | extern int yydebug; 11 | yydebug=1; 12 | int val=yyparse(); 13 | printf("\n+---------------------------------------+\n\n"); 14 | if(!val) 15 | printf("Successs :)\n\n"); 16 | else 17 | printf("Error :(\n\n"); 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Ass-4/ass4_16CS10053.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include "y.tab.h" 4 | %} 5 | 6 | SINGLELINE_COMMENT \/\/.* 7 | MULTILINE_COMMENT \/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/ 8 | IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]* 9 | DIGIT [0-9] 10 | INTEGER_CONST [1-9]{DIGIT}*|0 11 | FLOATING_CONST (((([0-9]+)?\.[0-9]+)|([0-9]+\.))([eE][+-]?[0-9]+)?)|([0-9]+[eE][+-]?[0-9]+) 12 | ENUMERATION_CONST [_a-zA-Z][_a-zA-Z0-9]* 13 | ESCAPE_SEQUENCE \\['"?\\abfnrtv] 14 | C_CHAR [^'\\\n]|{ESCAPE_SEQUENCE} 15 | C_CHAR_SEQUENCE {C_CHAR}+ 16 | CHARACTER_CONST \'{C_CHAR_SEQUENCE}\' 17 | CONSTANT {INTEGER_CONST}|{FLOATING_CONST}|{ENUMERATION_CONST}|{CHARACTER_CONST} 18 | S_CHAR [^"\\\n]|{ESCAPE_SEQUENCE} 19 | PUNCTUATOR "["|"]"|"."|"&"|"*"|"+"|"-"|"~"|"!"|"/"|"("|")"|"{"|"}"|"%"|","|"<"|">"|"^"|"|"|"="|"?"|":"|";"|"#" 20 | S_CHAR_SEQUENCE {S_CHAR}+ 21 | STRING_LITERAL \"{S_CHAR_SEQUENCE}?\" 22 | WS [ \t\n] 23 | 24 | %% 25 | "->" {return ARROW;} 26 | "++" {return INCREMENT;} 27 | "--" {return DECREMENT;} 28 | "<<" {return LSHIFT;} 29 | ">>" {return RSHIFT;} 30 | "<=" {return LESS_THAN_EQUAL_TO;} 31 | ">=" {return GREATER_THAN_EQUAL_TO;} 32 | "==" {return DOUBLE_EQUAL;} 33 | "!=" {return NOT_EQUAL;} 34 | "&&" {return BINARY_AND;} 35 | "||" {return BINARY_OR;} 36 | "..." {return ELLIPSIS;} 37 | "*=" {return STAR_EQUAL;} 38 | "/=" {return SLASH_EQUAL;} 39 | "%=" {return PERCENTILE_EQUAL;} 40 | "+=" {return PLUS_EQUAL;} 41 | "-=" {return MINUS_EQUAL;} 42 | "<<=" {return LEFT_SHIFT_EQUAL;} 43 | ">>=" {return RIGHT_SHIFT_EQUAL;} 44 | "&=" {return AND_EQUAL;} 45 | "^=" {return XOR_EQUAL;} 46 | "|=" {return OR_EQUAL;} 47 | "auto" {return AUTO;} 48 | "break" {return BREAK;} 49 | "case" {return CASE;} 50 | "char" {return CHAR;} 51 | "const" {return CONST;} 52 | "continue" {return CONTINUE;} 53 | "default" {return DEFAULT;} 54 | "do" {return DO;} 55 | "double" {return DOUBLE;} 56 | "else" {return ELSE;} 57 | "enum" {return ENUM;} 58 | "extern" {return EXTERN;} 59 | "float" {return FLOAT;} 60 | "for" {return FOR;} 61 | "goto" {return GOTO;} 62 | "if" {return IF;} 63 | "inline" {return INLINE;} 64 | "int" {return INT;} 65 | "long" {return LONG;} 66 | "register" {return REGISTER;} 67 | "restrict" {return RESTRICT;} 68 | "return" {return RETURN;} 69 | "short" {return SHORT;} 70 | "signed" {return SIGNED;} 71 | "sizeof" {return SIZEOF;} 72 | "static" {return STATIC;} 73 | "struct" {return STRUCT;} 74 | "switch" {return SWITCH;} 75 | "typedef" {return TYPEDEF;} 76 | "union" {return UNION;} 77 | "unsigned" {return UNSIGNED;} 78 | "void" {return VOID;} 79 | "volatile" {return VOLATILE;} 80 | "while" {return WHILE;} 81 | "_Bool" {return BOOL;} 82 | "_Complex" {return COMPLEX;} 83 | "_Imaginary" {return IMAGINARY;} 84 | {IDENTIFIER} {return IDENTIFIER;} 85 | {INTEGER_CONST} {return INTEGER_NO;} 86 | {FLOATING_CONST} {return FLOAT_NO;} 87 | {CHARACTER_CONST} {return CHARACTER;} 88 | {STRING_LITERAL} {return STRING;} 89 | {PUNCTUATOR} {return yytext[0];} 90 | {SINGLELINE_COMMENT} {} 91 | {MULTILINE_COMMENT} {} 92 | {WS} {} 93 | %% -------------------------------------------------------------------------------- /Ass-4/ass4_16CS10053.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | extern int yylex(); 4 | void yyerror(const char*); 5 | %} 6 | 7 | 8 | %union { 9 | int intval; 10 | float floatval; 11 | char *charval; 12 | } 13 | 14 | %token ARROW 15 | %token INCREMENT 16 | %token DECREMENT 17 | %token LSHIFT 18 | %token RSHIFT 19 | %token LESS_THAN_EQUAL_TO 20 | %token GREATER_THAN_EQUAL_TO 21 | %token DOUBLE_EQUAL 22 | %token NOT_EQUAL 23 | %token BINARY_AND 24 | %token BINARY_OR 25 | %token ELLIPSIS 26 | %token STAR_EQUAL 27 | %token SLASH_EQUAL 28 | %token PERCENTILE_EQUAL 29 | %token PLUS_EQUAL 30 | %token MINUS_EQUAL 31 | %token LEFT_SHIFT_EQUAL 32 | %token RIGHT_SHIFT_EQUAL 33 | %token AND_EQUAL 34 | %token XOR_EQUAL 35 | %token OR_EQUAL 36 | %token AUTO 37 | %token BREAK 38 | %token CASE 39 | %token CHAR 40 | %token CONST 41 | %token CONTINUE 42 | %token DEFAULT 43 | %token DO 44 | %token DOUBLE 45 | %token ELSE 46 | %token ENUM 47 | %token EXTERN 48 | %token FLOAT 49 | %token FOR 50 | %token GOTO 51 | %token IF 52 | %token INLINE 53 | %token INT 54 | %token LONG 55 | %token REGISTER 56 | %token RESTRICT 57 | %token RETURN 58 | %token SHORT 59 | %token SIGNED 60 | %token SIZEOF 61 | %token STATIC 62 | %token STRUCT 63 | %token SWITCH 64 | %token TYPEDEF 65 | %token UNION 66 | %token UNSIGNED 67 | %token VOID 68 | %token VOLATILE 69 | %token WHILE 70 | %token BOOL 71 | %token COMPLEX 72 | %token IMAGINARY 73 | %token SINGLE_COMMENT 74 | %token MULTI_COMMENT 75 | %token IDENTIFIER 76 | %token INTEGER_NO 77 | %token FLOAT_NO 78 | %token CHARACTER 79 | %token STRING 80 | %token ENUMERATION_CONSTANT 81 | %token PUNCTUATOR 82 | %token WS 83 | 84 | %start translation_unit 85 | 86 | %% 87 | 88 | primary_expression: 89 | IDENTIFIER 90 | |INTEGER_NO 91 | |FLOAT_NO 92 | |CHARACTER 93 | |STRING 94 | |'(' expression ')' 95 | ; 96 | 97 | postfix_expression: 98 | primary_expression 99 | |postfix_expression '[' expression ']' 100 | |postfix_expression '(' argument_expression_list_optional ')' 101 | |postfix_expression '.' IDENTIFIER 102 | |postfix_expression ARROW IDENTIFIER 103 | |postfix_expression INCREMENT 104 | |postfix_expression DECREMENT 105 | |'(' type_name ')' '{' initializer_list '}' 106 | |'(' type_name ')' '{' initializer_list ',' '}' 107 | ; 108 | 109 | argument_expression_list_optional: 110 | argument_expression_list 111 | | 112 | ; 113 | 114 | argument_expression_list: 115 | assignment_expression 116 | |argument_expression_list ',' assignment_expression 117 | ; 118 | 119 | unary_expression: 120 | postfix_expression 121 | |INCREMENT unary_expression 122 | |DECREMENT unary_expression 123 | |unary_operator cast_expression 124 | |SIZEOF unary_expression 125 | |SIZEOF '(' type_name ')' 126 | ; 127 | 128 | unary_operator: 129 | '&' 130 | |'*' 131 | |'+' 132 | |'-' 133 | |'~' 134 | |'!' 135 | ; 136 | 137 | cast_expression: 138 | unary_expression 139 | |'(' type_name ')' cast_expression 140 | ; 141 | 142 | multplicative_expression: 143 | cast_expression 144 | |multplicative_expression '*' cast_expression 145 | |multplicative_expression '/' cast_expression 146 | |multplicative_expression '%' cast_expression 147 | ; 148 | 149 | additive_expression: 150 | multplicative_expression 151 | |additive_expression '+' multplicative_expression 152 | |additive_expression '-' multplicative_expression 153 | ; 154 | 155 | shift_expression: 156 | additive_expression 157 | |shift_expression LSHIFT additive_expression 158 | |shift_expression RSHIFT additive_expression 159 | ; 160 | 161 | relational_expression: 162 | shift_expression 163 | |relational_expression '<' shift_expression 164 | |relational_expression '>' shift_expression 165 | |relational_expression LESS_THAN_EQUAL_TO shift_expression 166 | |relational_expression GREATER_THAN_EQUAL_TO shift_expression 167 | ; 168 | 169 | equality_expression: 170 | relational_expression 171 | |equality_expression DOUBLE_EQUAL relational_expression 172 | |relational_expression NOT_EQUAL relational_expression 173 | ; 174 | 175 | AND_expression: 176 | equality_expression 177 | |AND_expression '&' equality_expression 178 | ; 179 | 180 | exclusive_OR_expression: 181 | AND_expression 182 | |exclusive_OR_expression '^' AND_expression 183 | ; 184 | 185 | inclusive_OR_expression: 186 | exclusive_OR_expression 187 | |inclusive_OR_expression '|' exclusive_OR_expression 188 | ; 189 | 190 | logical_AND_expression: 191 | inclusive_OR_expression 192 | |logical_AND_expression BINARY_AND inclusive_OR_expression 193 | ; 194 | 195 | logical_OR_expression: 196 | logical_AND_expression 197 | |logical_OR_expression BINARY_OR logical_AND_expression 198 | ; 199 | 200 | conditional_expression: 201 | logical_OR_expression 202 | |logical_OR_expression '?' expression ':' conditional_expression 203 | ; 204 | 205 | assignment_expression: 206 | conditional_expression 207 | |unary_expression assignment_operator assignment_expression 208 | ; 209 | 210 | assignment_expression_optional: 211 | assignment_expression 212 | | 213 | ; 214 | 215 | assignment_operator: 216 | '=' 217 | |STAR_EQUAL 218 | |SLASH_EQUAL 219 | |PERCENTILE_EQUAL 220 | |PLUS_EQUAL 221 | |MINUS_EQUAL 222 | |LEFT_SHIFT_EQUAL 223 | |RIGHT_SHIFT_EQUAL 224 | |AND_EQUAL 225 | |XOR_EQUAL 226 | |OR_EQUAL 227 | ; 228 | 229 | expression: 230 | assignment_expression 231 | |expression ',' assignment_expression 232 | ; 233 | 234 | constant_expression: 235 | conditional_expression 236 | ; 237 | 238 | expression_optional: 239 | expression 240 | | 241 | ; 242 | 243 | declaration: 244 | declaration_specifiers init_declarator_list_optional ';' 245 | ; 246 | 247 | declaration_specifiers: 248 | storage_class_specifier declaration_specifiers_optional 249 | |type_specifier declaration_specifiers_optional 250 | |type_qualifier declaration_specifiers_optional 251 | |function_specifier declaration_specifiers_optional 252 | ; 253 | 254 | declaration_specifiers_optional: 255 | declaration_specifiers 256 | | 257 | ; 258 | 259 | init_declarator_list_optional: 260 | init_declarator_list 261 | | 262 | ; 263 | 264 | init_declarator_list: 265 | init_declarator 266 | |init_declarator_list ',' init_declarator 267 | ; 268 | 269 | init_declarator: 270 | declarator 271 | |declarator '=' initializer 272 | ; 273 | 274 | storage_class_specifier: 275 | EXTERN 276 | |STATIC 277 | |AUTO 278 | |REGISTER 279 | ; 280 | 281 | type_specifier: 282 | VOID 283 | |CHAR 284 | |SHORT 285 | |INT 286 | |LONG 287 | |FLOAT 288 | |DOUBLE 289 | |SIGNED 290 | |UNSIGNED 291 | |BOOL 292 | |COMPLEX 293 | |IMAGINARY 294 | |enum_specifier 295 | ; 296 | 297 | specifier_qualifier_list: 298 | type_specifier specifier_qualifier_list_optional 299 | |type_qualifier specifier_qualifier_list_optional 300 | ; 301 | 302 | specifier_qualifier_list_optional: 303 | specifier_qualifier_list 304 | | 305 | ; 306 | 307 | enum_specifier: 308 | ENUM identifier_optional '{' enumerator_list '}' 309 | |ENUM identifier_optional '{' enumerator_list ',' '}' 310 | |ENUM IDENTIFIER 311 | ; 312 | 313 | enumerator_list: 314 | ENUM 315 | |enumerator_list ',' enumerator 316 | ; 317 | 318 | enumerator: 319 | IDENTIFIER 320 | |IDENTIFIER '=' constant_expression 321 | ; 322 | 323 | type_qualifier: 324 | CONST 325 | |RESTRICT 326 | |VOLATILE 327 | ; 328 | 329 | function_specifier: 330 | INLINE 331 | ; 332 | 333 | declarator: 334 | pointer_optional direct_declarator 335 | ; 336 | 337 | direct_declarator: 338 | IDENTIFIER 339 | |'(' declarator ')' 340 | |direct_declarator '[' type_qualifier_list_optional assignment_expression_optional ']' 341 | |direct_declarator '[' STATIC type_qualifier_list_optional assignment_expression ']' 342 | |direct_declarator '[' type_qualifier_list_optional '*' ']' 343 | |direct_declarator '(' parameter_type_list ')' 344 | |direct_declarator '(' identifier_list_optional ')' 345 | ; 346 | 347 | pointer: 348 | '*' type_qualifier_list_optional 349 | |'*' type_qualifier_list_optional pointer 350 | ; 351 | 352 | pointer_optional: 353 | pointer 354 | | 355 | ; 356 | 357 | type_qualifier_list: 358 | type_qualifier 359 | |type_qualifier_list type_qualifier 360 | ; 361 | 362 | type_qualifier_list_optional: 363 | type_qualifier_list 364 | | 365 | ; 366 | 367 | parameter_type_list: 368 | parameter_list 369 | |parameter_list ',' ELLIPSIS 370 | ; 371 | 372 | parameter_list: 373 | parameter_declaration 374 | |parameter_list ',' parameter_declaration 375 | ; 376 | 377 | parameter_declaration: 378 | declaration_specifiers declarator 379 | |declaration_specifiers 380 | ; 381 | 382 | identifier_list: 383 | IDENTIFIER 384 | |identifier_list ',' IDENTIFIER 385 | ; 386 | 387 | identifier_list_optional: 388 | identifier_list 389 | | 390 | ; 391 | 392 | type_name: 393 | specifier_qualifier_list 394 | ; 395 | 396 | initializer: 397 | assignment_expression 398 | |'{' initializer_list '}' 399 | |'{' initializer_list ',' '}' 400 | ; 401 | 402 | initializer_list: 403 | designation_optional initializer 404 | |initializer_list ',' designation_optional initializer 405 | ; 406 | 407 | designation: 408 | designator_list '=' 409 | ; 410 | 411 | designation_optional: 412 | designation 413 | | 414 | ; 415 | 416 | designator_list: 417 | designator 418 | |designator_list designator 419 | ; 420 | 421 | designator: 422 | '[' constant_expression ']' 423 | |'.' IDENTIFIER 424 | ; 425 | 426 | statement: 427 | labeled_statement 428 | |compound_statement 429 | |expression_statement 430 | |selection_statement 431 | |iteration_statement 432 | |jump_statement 433 | ; 434 | 435 | labeled_statement: 436 | IDENTIFIER ':' statement 437 | |CASE constant_expression ':' statement 438 | |DEFAULT ':' statement 439 | ; 440 | 441 | compound_statement: 442 | '{' block_item_list_optional '}' 443 | ; 444 | 445 | block_item_list: 446 | block_item 447 | |block_item_list block_item 448 | ; 449 | 450 | block_item_list_optional: 451 | block_item_list 452 | | 453 | ; 454 | 455 | block_item: 456 | declaration 457 | |statement 458 | ; 459 | 460 | expression_statement: 461 | expression_optional ';' 462 | ; 463 | 464 | selection_statement: 465 | IF '(' expression ')' statement 466 | |IF '(' expression ')' statement ELSE statement 467 | |SWITCH '(' expression ')' statement 468 | ; 469 | 470 | iteration_statement: 471 | WHILE '(' expression ')' statement 472 | |DO statement WHILE '(' expression ')' ';' 473 | |FOR '(' expression_optional ';' expression_optional ';' expression_optional ')' statement 474 | |FOR '(' declaration expression_optional ';' expression_optional ')' statement 475 | ; 476 | 477 | jump_statement: 478 | GOTO IDENTIFIER ';' 479 | |CONTINUE ';' 480 | |BREAK ';' 481 | |RETURN expression_optional ';' 482 | ; 483 | 484 | translation_unit: 485 | external_declaration 486 | |translation_unit external_declaration 487 | ; 488 | 489 | external_declaration: 490 | function_definition 491 | |declaration 492 | ; 493 | 494 | function_definition: 495 | declaration_specifiers declarator declaration_list_optional compound_statement 496 | ; 497 | 498 | declaration_list: 499 | declaration 500 | |declaration_list declaration 501 | ; 502 | 503 | declaration_list_optional: 504 | declaration_list 505 | | 506 | ; 507 | 508 | identifier_optional: 509 | IDENTIFIER 510 | | 511 | ; 512 | 513 | 514 | %% 515 | 516 | void yyerror(const char *s) { 517 | printf("Error occured : %s\n",s); 518 | } 519 | -------------------------------------------------------------------------------- /Ass-4/ass4_16CS10053_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name : Vedic Partap 4 | Roll No. : 16CS10053 5 | 6 | */ 7 | static my = 0; 8 | register name = 9; 9 | static is = 12; 10 | extern vedic = 34; 11 | void looking_cool() 12 | { 13 | learn_AI(); 14 | } 15 | int main() 16 | { 17 | int n, array[1000], c, d, t; 18 | 19 | for (c = 0; c < n; c++) { 20 | // a &=34; 21 | } 22 | 23 | for (c = 1 ; c <= n - 1; c++) { 24 | d = c; 25 | 26 | while ( d > 0 && array[d-1] > array[d]) { 27 | t = array[d]; 28 | array[d] = array[d-1]; 29 | array[d-1] = t; 30 | 31 | d--; 32 | } 33 | } 34 | int s = 67.4; 35 | float b = 23; 36 | a = b+45; 37 | 38 | for (c = 0; c <= n - 1; c++) { 39 | vedic==partap; 40 | } 41 | 42 | a++; 43 | b--; 44 | b=++a; 45 | b=a++; 46 | a=b--; 47 | a=--b; 48 | // checking binary 49 | a = b & 1; 50 | a = a | 1; 51 | a = a ^ 2; 52 | a = !a; 53 | a = a * c; 54 | a = a / b; 55 | a = a + b; 56 | a = a % 3; 57 | a+=2; 58 | b-=3; 59 | c*=d; 60 | c/=5; 61 | c%=1; 62 | c<<=2; 63 | c>>=z; 64 | c&=2; 65 | c|=1; 66 | c^=0; 67 | int b = a && c; 68 | int bb = b || (c==r) && (!a); 69 | 70 | 71 | // This is single line comment 72 | 73 | hello +=world; 74 | hello-=kitty; 75 | for(puupy=0;puppy<45;puppy++) 76 | dog = puppy; // animals 77 | } 78 | 79 | void making_timepass_function() 80 | { 81 | while(no_work) 82 | { 83 | time_pass++; 84 | } 85 | die; 86 | } -------------------------------------------------------------------------------- /Ass-5/Asgn5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vedic-partap/Compilers-lab/bee1d964d1da04a7a48cbddad5aaf954a5f83961/Ass-5/Asgn5.pdf -------------------------------------------------------------------------------- /Ass-5/Makefile: -------------------------------------------------------------------------------- 1 | all: a.out 2 | @echo "\n+++++++++++++++++++ Test 1 +++++++++++++++++++\n\n" 3 | ./a.out < ass5_16CS10053_test1.c > output1.txt 4 | 5 | @echo "\n+++++++++++++++++++ Test 2 +++++++++++++++++++\n\n" 6 | ./a.out < ass5_16CS10053_test2.c > output2.txt 7 | 8 | @echo "\n+++++++++++++++++++ Test 3 +++++++++++++++++++\n\n" 9 | ./a.out < ass5_16CS10053_test3.c > output3.txt 10 | 11 | @echo "\n+++++++++++++++++++ Test 4 +++++++++++++++++++\n\n" 12 | ./a.out < ass5_16CS10053_test4.c > output4.txt 13 | 14 | @echo "\n+++++++++++++++++++ Test 5 +++++++++++++++++++\n\n" 15 | ./a.out < ass5_16CS10053_test5.c > output5.txt 16 | 17 | 18 | @echo "\nAll Test Case Successfully Translated !!\n" 19 | 20 | a.out: lex.yy.o y.tab.o ass5_16CS10053_translator.o 21 | g++ lex.yy.o y.tab.o ass5_16CS10053_translator.o -lfl 22 | 23 | 24 | y.tab.o: y.tab.c 25 | g++ -c y.tab.c 26 | 27 | lex.yy.o: lex.yy.c 28 | g++ -c lex.yy.c 29 | 30 | lex.yy.c: ass5_16CS10053.l y.tab.c 31 | flex ass5_16CS10053.l 32 | 33 | y.tab.c: ass5_16CS10053.y 34 | yacc -dtv ass5_16CS10053.y 35 | 36 | ass5_16CS10053_translator.o: ass5_16CS10053_translator.h ass5_16CS10053_translator.cxx 37 | g++ -c ass5_16CS10053_translator.cxx 38 | 39 | clean: 40 | rm a.out y.tab.o lex.yy.o lex.yy.c y.tab.c y.tab.h y.output ass5_16CS10053_translator.o output*.txt 41 | -------------------------------------------------------------------------------- /Ass-5/README: -------------------------------------------------------------------------------- 1 | +++++ Assignment 5 +++++ 2 | +++++ Vedic Partap +++++ 3 | 4 | 1. There is sample output file in the sample_output folder 5 | 2. In makefile @ is used so that command is not shown in the terminal, only outputs will be displayed. 6 | 7 | +++++ How To Run +++++ 8 | $ make 9 | 10 | +++++ Clean +++++ 11 | $ make clean 12 | 13 | on `make` command 5 test case will be tested and the output will be stored in output.txt. -------------------------------------------------------------------------------- /Ass-5/Workout/basicCalculator.txt: -------------------------------------------------------------------------------- 1 | Grammar 2 | 3 | 4 | statement-list: 5 | statement ; 6 | statement-list statement 7 | 8 | statement: 9 | primary-expression = expression 10 | 11 | primary-expression: 12 | ID 13 | 14 | constant-expression: 15 | INTEGER 16 | DOUBLE 17 | 18 | expression: 19 | expression + expression 20 | expression - expression 21 | expression * expression 22 | expression / expression 23 | - expression 24 | ( expression ) 25 | primary-expression 26 | constant-expression 27 | -------------------------------------------------------------------------------- /Ass-5/Workout/lexer.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include "translator.h" 4 | #include "y.tab.h" 5 | using namespace std; 6 | 7 | extern YYSTYPE yylval; 8 | extern SymbolTable *ST; 9 | %} 10 | 11 | 12 | ID [_a-zA-Z][_a-zA-Z0-9]* 13 | 14 | INT_CONST ([1-9][0-9]*)|[0] 15 | 16 | DIG_SEQ [0-9]+ 17 | DEC "." 18 | FRACTIONAL_CONST (({DIG_SEQ}?{DEC}{DIG_SEQ})|({DIG_SEQ}{DEC})) 19 | SIGN ([+]|[-]) 20 | EXP_PART ([e]|[E]){SIGN}?{DIG_SEQ} 21 | DOUBLE_CONST (({FRACTIONAL_CONST}{EXP_PART}?)|({DIG_SEQ}{EXP_PART})) 22 | 23 | MULTI_LINE_COMMENT ("/"\*(([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*)\*+"/") 24 | 25 | WILDCARD . 26 | SINGLE_LINE_COMMENT ([/][/]{WILDCARD}*) 27 | 28 | 29 | WS [ \t] 30 | %% 31 | 32 | {WS} {;} 33 | [\n] {;} 34 | {SINGLE_LINE_COMMENT} {;} 35 | {MULTI_LINE_COMMENT} {;} 36 | 37 | "(" { return (int)'('; } 38 | ")" { return (int)')'; } 39 | "*" { return (int)'*'; } 40 | "+" { return (int)'+'; } 41 | "-" { return (int)'-'; } 42 | "/" { return (int)'/'; } 43 | ";" { return (int)';'; } 44 | "=" { return (int)'='; } 45 | 46 | {ID} { string s(yytext); yylval.name = new string; *(yylval.name) = s; return ID;} 47 | {INT_CONST} { yylval.intVal = atoi(yytext); return INT_CONST;} 48 | {DOUBLE_CONST} { yylval.doubleVal = atof(yytext); return DOUBLE_CONST;} 49 | 50 | 51 | %% -------------------------------------------------------------------------------- /Ass-5/Workout/parser.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include "translator.h" 4 | using namespace std; 5 | extern int yylex(); 6 | extern int yyparse(); 7 | void yyerror(string s); 8 | 9 | ofstream myfile1; 10 | ofstream myfile; 11 | QuadArray quadArray; // Object of the class QuadArray which to generate and work on quads 12 | SymbolTable *ST = new SymbolTable; // Pointer to Symbol Table Object 13 | %} 14 | 15 | %union{ 16 | int intVal; // Used for storing integer constant value 17 | double doubleVal; // Used for storing double constant value 18 | string *name; // Used for storing String Literal 19 | SymTabEntry *symp; // Pointer to a Symbol Table 20 | Attribute_SE *se_attr; // Holds the attributes for statements and Expressions 21 | opcodeType op; 22 | } 23 | 24 | 25 | %token ID // Attribute: name -> name of the Identifier 26 | %token INT_CONST // Attribute: intVal -> Stores the integer value of the string literal 27 | %token DOUBLE_CONST // Attribute: doubleVal -> Stores the double value for a double Constant 28 | 29 | %left '+' '-' 30 | %left '*' '/' 31 | %nonassoc UMINUS 32 | 33 | %type expression constant_expression primary_expression 34 | 35 | %start stmt_list 36 | 37 | %% 38 | 39 | 40 | stmt_list : statement ';' 41 | | stmt_list statement ';' 42 | ; 43 | 44 | 45 | statement : primary_expression '=' expression { 46 | ST->update($1->loc,$3->typeInfo, ST->offset); 47 | ST->offset = (ST->offset) + ($3->typeInfo->width); 48 | quadArray.emit($1->loc->name, $3->loc->name); 49 | } 50 | ; 51 | 52 | 53 | primary_expression : ID { $$ = new Attribute_SE; $$->typeInfo = NULL; $$->loc = NULL; $$->initValue = NULL; 54 | $$->name = *($1); 55 | $$->loc = ST->lookup($$->name); 56 | $$->typeInfo = $$->loc->typeInfo; 57 | 58 | } 59 | 60 | constant_expression : INT_CONST { $$ = new Attribute_SE; $$->typeInfo = NULL; $$->loc = NULL; $$->initValue = NULL; 61 | $$->loc = ST->gentemp(); 62 | TypeInfo *type1 = new TypeInfo; 63 | type1->type = type_int; 64 | type1->_size = SIZE_OF_INT; 65 | type1->width = SIZE_OF_INT; 66 | 67 | $$->typeInfo = type1; 68 | $$->intVal = $1; 69 | $$->name = $$->loc->name; 70 | ST->update($$->loc,$$->typeInfo,ST->offset); 71 | ST->updateintVal($$->loc, $$->intVal); 72 | ST->offset = ST->offset + SIZE_OF_INT; 73 | quadArray.emit($$->loc->name,$$->intVal); 74 | } 75 | | DOUBLE_CONST { $$ = new Attribute_SE; $$->typeInfo = NULL; $$->loc = NULL; $$->initValue = NULL; 76 | $$->loc = ST->gentemp(); 77 | TypeInfo *type1 = new TypeInfo; 78 | type1->type = type_double; 79 | type1->_size = SIZE_OF_DOUBLE; 80 | type1->width = SIZE_OF_DOUBLE; 81 | 82 | $$->typeInfo = type1; 83 | $$->doubleVal = $1; 84 | $$->name = $$->loc->name; 85 | ST->update($$->loc,$$->typeInfo,ST->offset); 86 | ST->updatedoubleVal($$->loc, $$->doubleVal); 87 | ST->offset = ST->offset + SIZE_OF_DOUBLE; 88 | quadArray.emit($$->loc->name,$$->doubleVal); 89 | } 90 | ; 91 | 92 | expression : expression '+' expression { $$ = new Attribute_SE; $$->typeInfo = NULL; $$->loc = NULL; $$->initValue = NULL; 93 | 94 | $$->loc = ST->gentemp(); 95 | $$->typeInfo = new TypeInfo; 96 | $$->typeInfo->type = $1->typeInfo->type; 97 | $$->typeInfo->_size = $1->typeInfo->_size; 98 | $$->typeInfo->width = $1->typeInfo->width; 99 | 100 | $$->name = $$->loc->name; 101 | ST->update($$->loc,$$->typeInfo, ST->offset); 102 | ST->offset = (ST->offset) + ($$->typeInfo->width); 103 | quadArray.emit($$->loc->name, $1->loc->name, PLUS, $3->loc->name); 104 | } 105 | 106 | | expression '-' expression { $$ = new Attribute_SE; $$->typeInfo = NULL; $$->loc = NULL; $$->initValue = NULL; 107 | 108 | $$->loc = ST->gentemp(); 109 | $$->typeInfo = new TypeInfo; 110 | $$->typeInfo->type = $1->typeInfo->type; 111 | $$->typeInfo->_size = $1->typeInfo->_size; 112 | $$->typeInfo->width = $1->typeInfo->width; 113 | 114 | $$->name = $$->loc->name; 115 | ST->update($$->loc,$$->typeInfo, ST->offset); 116 | ST->offset = (ST->offset) + ($$->typeInfo->width); 117 | quadArray.emit($$->loc->name, $1->loc->name, MINUS, $3->loc->name); 118 | } 119 | | expression '*' expression { $$ = new Attribute_SE; $$->typeInfo = NULL; $$->loc = NULL; $$->initValue = NULL; 120 | 121 | $$->loc = ST->gentemp(); 122 | $$->typeInfo = new TypeInfo; 123 | $$->typeInfo->type = $1->typeInfo->type; 124 | $$->typeInfo->_size = $1->typeInfo->_size; 125 | $$->typeInfo->width = $1->typeInfo->width; 126 | 127 | $$->name = $$->loc->name; 128 | ST->update($$->loc,$$->typeInfo, ST->offset); 129 | ST->offset = (ST->offset) + ($$->typeInfo->width); 130 | quadArray.emit($$->loc->name, $1->loc->name, MULT, $3->loc->name); 131 | } 132 | 133 | | expression '/' expression { $$ = new Attribute_SE; $$->typeInfo = NULL; $$->loc = NULL; $$->initValue = NULL; 134 | 135 | $$->loc = ST->gentemp(); 136 | $$->typeInfo = new TypeInfo; 137 | $$->typeInfo->type = $1->typeInfo->type; 138 | $$->typeInfo->_size = $1->typeInfo->_size; 139 | $$->typeInfo->width = $1->typeInfo->width; 140 | 141 | $$->name = $$->loc->name; 142 | ST->update($$->loc,$$->typeInfo, ST->offset); 143 | ST->offset = (ST->offset) + ($$->typeInfo->width); 144 | quadArray.emit($$->loc->name, $1->loc->name, DIV, $3->loc->name); 145 | } 146 | 147 | 148 | | '(' expression ')' { $$ = new Attribute_SE; $$->typeInfo = NULL; $$->loc = NULL; $$->initValue = NULL; 149 | *($$) = *($2); 150 | } 151 | 152 | | '-' expression %prec UMINUS { $$ = new Attribute_SE; $$->typeInfo = NULL; $$->loc = NULL; $$->initValue = NULL; 153 | $$->loc = ST->gentemp(); 154 | $$->name = $$->loc->name; 155 | $$->typeInfo = new TypeInfo; 156 | 157 | $$->typeInfo->type = $2->loc->typeInfo->type; 158 | $$->typeInfo->_size = $2->loc->typeInfo->_size; 159 | $$->typeInfo->width = $2->loc->typeInfo->width; 160 | 161 | ST->update($$->loc, $$->typeInfo, ST->offset); 162 | ST->offset = (ST->offset)+($$->typeInfo->width); 163 | quadArray.emit($$->loc->name,$2->loc->name, UNARY_MINUS); 164 | 165 | } 166 | 167 | | primary_expression { 168 | $$ = new Attribute_SE; $$->typeInfo = NULL; $$->loc = NULL; $$->initValue = NULL; 169 | *($$) = *($1); 170 | } 171 | 172 | | constant_expression { $$ = new Attribute_SE; $$->typeInfo = NULL; $$->loc = NULL; $$->initValue = NULL; 173 | *($$) = *($1); 174 | } 175 | 176 | ; 177 | 178 | %% 179 | 180 | void yyerror(string s) { 181 | cout<<"Error occured due to: "< 3 | #include "ass5_16CS10053_translator.h" 4 | #include "y.tab.h" 5 | %} 6 | 7 | SINGLELINE_COMMENT \/\/.* 8 | MULTILINE_COMMENT "/"\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+"/" 9 | IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]* 10 | DIGIT [0-9] 11 | INTEGER_CONST [1-9]{DIGIT}*|0 12 | FLOATING_CONST (((([0-9]+)?\.[0-9]+)|([0-9]+\.))([eE][+-]?[0-9]+)?)|([0-9]+[eE][+-]?[0-9]+) 13 | ENUMERATION_CONST [_a-zA-Z][_a-zA-Z0-9]* 14 | ESCAPE_SEQUENCE \\['"?\\abfnrtv] 15 | C_CHAR [^'\\\n]|{ESCAPE_SEQUENCE} 16 | C_CHAR_SEQUENCE {C_CHAR}+ 17 | CHARACTER_CONST \'{C_CHAR_SEQUENCE}\' 18 | CONSTANT {INTEGER_CONST}|{FLOATING_CONST}|{ENUMERATION_CONST}|{CHARACTER_CONST} 19 | S_CHAR [^"\\\n]|{ESCAPE_SEQUENCE} 20 | PUNCTUATOR "["|"]"|"."|"&"|"*"|"+"|"-"|"~"|"!"|"/"|"("|")"|"{"|"}"|"%"|","|"<"|">"|"^"|"|"|"="|"?"|":"|";"|"#" 21 | S_CHAR_SEQUENCE {S_CHAR}+ 22 | STRING_LITERAL \"{S_CHAR_SEQUENCE}?\" 23 | WS [ \t\n] 24 | 25 | %% 26 | "->" {return ARROW;} 27 | "++" {return INCREMENT;} 28 | "--" {return DECREMENT;} 29 | "<<" {return LSHIFT;} 30 | ">>" {return RSHIFT;} 31 | "<=" {return LESS_THAN_EQUAL_TO;} 32 | ">=" {return GREATER_THAN_EQUAL_TO;} 33 | "==" {return DOUBLE_EQUAL;} 34 | "!=" {return NOT_EQUAL;} 35 | "&&" {return BINARY_AND;} 36 | "||" {return BINARY_OR;} 37 | "..." {return ELLIPSIS;} 38 | "*=" {return STAR_EQUAL;} 39 | "/=" {return SLASH_EQUAL;} 40 | "%=" {return PERCENTILE_EQUAL;} 41 | "+=" {return PLUS_EQUAL;} 42 | "-=" {return MINUS_EQUAL;} 43 | "<<=" {return LEFT_SHIFT_EQUAL;} 44 | ">>=" {return RIGHT_SHIFT_EQUAL;} 45 | "&=" {return AND_EQUAL;} 46 | "^=" {return XOR_EQUAL;} 47 | "|=" {return OR_EQUAL;} 48 | "auto" {return AUTO;} 49 | "break" {return BREAK;} 50 | "case" {return CASE;} 51 | "char" {return CHAR;} 52 | "const" {return CONST;} 53 | "continue" {return CONTINUE;} 54 | "default" {return DEFAULT;} 55 | "do" {return DO;} 56 | "double" {return DOUBLE;} 57 | "else" {return ELSE;} 58 | "enum" {return ENUM;} 59 | "extern" {return EXTERN;} 60 | "float" {return FLOAT;} 61 | "for" {return FOR;} 62 | "goto" {return GOTO;} 63 | "if" {return IF;} 64 | "inline" {return INLINE;} 65 | "int" {return INT;} 66 | "long" {return LONG;} 67 | "register" {return REGISTER;} 68 | "restrict" {return RESTRICT;} 69 | "return" {return RETURN;} 70 | "short" {return SHORT;} 71 | "signed" {return SIGNED;} 72 | "sizeof" {return SIZEOF;} 73 | "static" {return STATIC;} 74 | "struct" {return STRUCT;} 75 | "switch" {return SWITCH;} 76 | "typedef" {return TYPEDEF;} 77 | "union" {return UNION;} 78 | "unsigned" {return UNSIGNED;} 79 | "void" {return VOID;} 80 | "volatile" {return VOLATILE;} 81 | "while" {return WHILE;} 82 | "_Bool" {return BOOL;} 83 | "_Complex" {return COMPLEX;} 84 | "_Imaginary" {return IMAGINARY;} 85 | {IDENTIFIER} {(yylval.id_attr).var = strdup(yytext);return IDENTIFIER;} 86 | {INTEGER_CONST} {yylval.intval = atoi(yytext); return INTEGER_NO;} 87 | {FLOATING_CONST} {yylval.floatval = atof(yytext);return FLOAT_NO;} 88 | {CHARACTER_CONST} {yylval.charval = yytext[1];return CHARACTER;} 89 | {STRING_LITERAL} {yylval.strval = strdup(yytext); return STRING;} 90 | {PUNCTUATOR} {return yytext[0];} 91 | {SINGLELINE_COMMENT} {} 92 | {MULTILINE_COMMENT} {} 93 | {WS} {} 94 | %% 95 | -------------------------------------------------------------------------------- /Ass-5/ass5_16CS10053_test1.c: -------------------------------------------------------------------------------- 1 | /* 2 | Name : Vedic Partap 3 | Roll No. : 16CS10053 4 | Test Case : 1 5 | */ 6 | void time_pass2(int temp, int elements); 7 | int time_pass(int int_var, int elements,int value); 8 | int main(); 9 | 10 | void time_pass2(int temp, int elements) 11 | { 12 | temp = elements; 13 | temp = temp+1; 14 | return; 15 | } 16 | 17 | int time_pass(int int_var, int elements,int value) 18 | { 19 | int i ,passes = 0 ; 20 | if(int_var>= value) 21 | passes++; 22 | return(passes); 23 | } 24 | 25 | int main() 26 | { 27 | int n=3; 28 | for(int i=0;i=0) 31 | { 32 | int int_var=3; 33 | int result; 34 | time_pass2(int_var,3); 35 | result = time_pass(int_var,4,3); 36 | } 37 | else 38 | { 39 | int int_var=11; 40 | int result; 41 | time_pass2(int_var,11); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Ass-5/ass5_16CS10053_test2.c: -------------------------------------------------------------------------------- 1 | /* 2 | Name : Vedic Partap 3 | Roll No. : 16CS10053 4 | Test Case : 2 5 | */ 6 | int a=9; 7 | int b=10; 8 | int sum(int a,int b); 9 | int mod(int a, int b); 10 | int mod2(int a,int b); 11 | int divide(int a,int b); 12 | int main(); 13 | int main() 14 | { 15 | int ar1[5][10]; 16 | double ar2[10]; 17 | double y; 18 | int z; 19 | int x,y,z,w; 20 | x = sum(a,b); 21 | y = mod(a,b); 22 | z = divide(a,b); 23 | w = mod2(a,b); 24 | } 25 | int sum(int a,int b) 26 | { 27 | double ar2[10]; 28 | int ans=a+b; 29 | return ans; 30 | } 31 | 32 | int mod(int a,int b) 33 | { 34 | double armod[3]; 35 | double ans=a%b; 36 | return ans; 37 | } 38 | 39 | int divide(int a,int b) 40 | { 41 | int ans; 42 | if(b!=0) 43 | ans=a/b; 44 | else 45 | ans=-1; 46 | return ans; 47 | } 48 | int mod2(int a,int b) 49 | { 50 | int ans; 51 | if(a>b) 52 | ans=a-divide(a,b); 53 | else 54 | ans=b-divide(a,b); 55 | return ans; 56 | } -------------------------------------------------------------------------------- /Ass-5/ass5_16CS10053_test3.c: -------------------------------------------------------------------------------- 1 | /* 2 | Name : Vedic Partap 3 | Roll No. : 16CS10053 4 | Test Case : 3 5 | */ 6 | int test = 1; 7 | int main(); 8 | int main () { 9 | int arr_int[20]; 10 | double arr_d[43]; 11 | int i=0; 12 | int n=9; 13 | for(i=0;i=0;i--) 16 | arr_d[i]=i*0.1; 17 | int timepass =90; 18 | timepass=timepass+timepass*timepass; 19 | for(i=0;i=0;i--) 22 | arr_d[i]=i*0.1; 23 | return timepass; 24 | } -------------------------------------------------------------------------------- /Ass-5/ass5_16CS10053_test4.c: -------------------------------------------------------------------------------- 1 | /* 2 | Name : Vedic Partap 3 | Roll No. : 16CS10053 4 | Test Case : 4 5 | */ 6 | double multiply_time_pass(double a,int b); 7 | char do_not_what_it_is(int a,double b, char c); 8 | int main(); 9 | int main() 10 | { 11 | //declaration of all types 12 | int n=9; 13 | if(n>=0) 14 | { 15 | if(n<=10) 16 | { 17 | char a='a',b='b',c='c'; 18 | double dp[20][15]; 19 | char *d=&a; 20 | int k=9; 21 | int l=k+10; 22 | int i = 50, *p = &i; 23 | } 24 | else 25 | { 26 | char a = 'a'; 27 | int ans = 90; 28 | } 29 | 30 | } 31 | else 32 | { 33 | return 0; 34 | } 35 | n=89; 36 | return 0; 37 | 38 | } 39 | 40 | double multiply_time_pass(double a,int b) 41 | { 42 | double ans; 43 | ans=a * b; 44 | ans = ans*a; 45 | a = b*ans; 46 | ans = a*a+b*b+ans*ans; 47 | return ans; 48 | } 49 | 50 | char do_not_what_it_is(int a,double b, char c) 51 | { 52 | double kt=b; 53 | char *p=&c; 54 | int i=a+b; 55 | return c; 56 | } -------------------------------------------------------------------------------- /Ass-5/ass5_16CS10053_test5.c: -------------------------------------------------------------------------------- 1 | /* 2 | Name : Vedic Partap 3 | Roll No. : 16CS10053 4 | Test Case : 5 5 | */ 6 | int hello(int a, int b); 7 | int kaiseho(int a,int b,int c); 8 | int lol(int a,int b, int c); 9 | double dont_know_what_to_do(int a,int b,int c,int d); 10 | int main(); 11 | int main() 12 | { 13 | int a=6,b=7,c=8,d=9; 14 | dont_know_what_to_do(a,b,c,d); 15 | return 0; 16 | } 17 | double dont_know_what_to_do(int a,int b,int c,int d) 18 | { 19 | kaiseho(a,b,c); 20 | hello(a,b); 21 | lol(a,b,c); 22 | return 2.0*7; 23 | } 24 | int lol(int a,int b, int c) 25 | { 26 | kaiseho(a,b,c); 27 | hello(a,b); 28 | return 3+4; 29 | } 30 | int kaiseho(int a,int b,int c) 31 | { 32 | hello(a,b); 33 | return 3+9; 34 | } 35 | int hello(int a,int b) 36 | { 37 | return 1; 38 | } 39 | -------------------------------------------------------------------------------- /Ass-5/ass5_16CS10053_translator.cxx: -------------------------------------------------------------------------------- 1 | # include "ass5_16CS10053_translator.h" 2 | # include "y.tab.h" 3 | # include 4 | # include 5 | # include 6 | #include 7 | # include 8 | #include 9 | using namespace std; 10 | #define TEMP_MAX_SIZE 1000 11 | 12 | symbol_table_fields::symbol_table_fields(char *name_f, tNode *Type_f, void *initial_value_f, int size_f, int offset_f, symbolTable *nestedTable_f) 13 | : name(name_f), Type(Type_f), initial_value(initial_value_f), size(size_f), offset(offset_f), nestedTable(nestedTable_f) {} 14 | void symbol_table_fields::operator=(symbol_table_fields &x){ 15 | this->name = x.name; 16 | this->Type = x.Type; 17 | this->initial_value = x.initial_value; 18 | this->size = x.size; 19 | this->offset = x.offset; 20 | this->nestedTable = x.nestedTable; 21 | } 22 | symbol_table_fields::~symbol_table_fields(){} 23 | void symbol_table_fields::print_row(){ 24 | // printf("%s\t ",name); 25 | cout<table)[0].name); 33 | cout<table)[0].name; 34 | print_Tree(Type); 35 | } 36 | 37 | symbolTable::symbolTable(unsigned int capacity){ 38 | table = new symbol_table_fields[capacity]; 39 | curr_length = -1; 40 | } 41 | symbolTable::~symbolTable(){} 42 | symbol_table_fields *symbolTable::lookup(char *t){ 43 | int i, j; 44 | i = 0; j = 0; 45 | i = j && i; 46 | for(i = 0;i<=curr_length;i++){ 47 | if(strcmp(table[i].name,t) == 0) 48 | return (table + i); 49 | } 50 | return 0; 51 | } 52 | void symbolTable::insert(symbol_table_fields &x){ 53 | if(curr_length == -1) 54 | x.offset = 0; 55 | else 56 | x.offset = table[curr_length].size + table[curr_length].offset; 57 | curr_length++; 58 | table[curr_length] = x; 59 | } 60 | void symbolTable::print_Table(){ 61 | if(MAX_SIZE>=0){ 62 | int i; 63 | cout<curr_length);i++){ 65 | table[i].print_row(); 66 | cout<<"\n"; 67 | } 68 | } 69 | 70 | } 71 | symbol_table_fields *symbolTable::gentemp(date_types temp){ 72 | tNode *t = new_node(temp,-1); 73 | char *s = new char[10]; 74 | sprintf(s,"t%d",temp_count); 75 | temp_count++; 76 | int temp_size = 0; 77 | switch(temp){ 78 | case INT_ : temp_size = SIZE_OF_INT; 79 | break; 80 | case CHAR_ : temp_size = SIZE_OF_CHAR; 81 | break; 82 | case DOUBLE_ : temp_size = SIZE_OF_DOUBLE; 83 | break; 84 | case PTR : temp_size = SIZE_OF_PTR; 85 | break; 86 | } 87 | symbol_table_fields x(s,t,0,temp_size,-1,0); 88 | this->insert(x); 89 | return &((this->table)[curr_length]); 90 | } 91 | /* Implementation of fields_quad */ 92 | 93 | fields_quad::fields_quad(char *arg1_f, char *arg2_f, char *res_f, quad_data_types op_f, symbol_table_fields *arg1_loc_f, symbol_table_fields *arg2_loc_f, symbol_table_fields *res_loc_f) : arg1(arg1_f), arg2(arg2_f), res(res_f), op(op_f), arg1_loc(arg1_loc_f), arg2_loc(arg2_loc_f), res_loc(res_loc_f) {} 94 | 95 | fields_quad::~fields_quad() {} 96 | 97 | void fields_quad::operator=(fields_quad &x){ 98 | this->arg1 = x.arg1; 99 | this->arg2 = x.arg2; 100 | this->res = x.res; 101 | this->op = x.op; 102 | this->arg1_loc = x.arg1_loc; 103 | this->arg2_loc = x.arg2_loc; 104 | this->res_loc = x.res_loc; 105 | } 106 | 107 | void fields_quad::print_fields_quad(int line){ 108 | if(this->arg2){ 109 | switch(this->op){ 110 | case PLUS : printf("%3d) %s = %s + %s\n",line,this->res,this->arg1,this->arg2); 111 | break; 112 | case MINUS : printf("%3d) %s = %s - %s\n",line,this->res,this->arg1,this->arg2); 113 | break; 114 | case INTO : printf("%3d) %s = %s * %s\n",line,this->res,this->arg1,this->arg2); 115 | break; 116 | case DIV : printf("%3d) %s = %s / %s\n",line,this->res,this->arg1,this->arg2); 117 | break; 118 | case PERCENT : printf("%3d) %s = %s %% %s\n",line,this->res,this->arg1,this->arg2); 119 | break; 120 | case SL : printf("%3d) %s = %s << %s\n",line,this->res,this->arg1,this->arg2); 121 | break; 122 | case SR : printf("%3d) %s = %s >> %s\n",line,this->res,this->arg1,this->arg2); 123 | break; 124 | case LT : printf("%3d) %s = %s < %s\n",line,this->res,this->arg1,this->arg2); 125 | break; 126 | case LTE : printf("%3d) %s = %s <= %s\n",line,this->res,this->arg1,this->arg2); 127 | break; 128 | case GT : printf("%3d) %s = %s > %s\n",line,this->res,this->arg1,this->arg2); 129 | break; 130 | case GTE : printf("%3d) %s = %s >= %s\n",line,this->res,this->arg1,this->arg2); 131 | break; 132 | case EQ : printf("%3d) %s = %s == %s\n",line,this->res,this->arg1,this->arg2); 133 | break; 134 | case NEQ : printf("%3d) %s = %s != %s\n",line,this->res,this->arg1,this->arg2); 135 | break; 136 | case BW_AND : printf("%3d) %s = %s & %s\n",line,this->res,this->arg1,this->arg2); 137 | break; 138 | case BW_XOR : printf("%3d) %s = %s ^ %s\n",line,this->res,this->arg1,this->arg2); 139 | break; 140 | case BW_INOR : printf("%3d) %s = %s | %s\n",line,this->res,this->arg1,this->arg2); 141 | break; 142 | case LOG_AND : printf("%3d) %s = %s && %s\n",line,this->res,this->arg1,this->arg2); 143 | break; 144 | case LOG_OR : printf("%3d) %s = %s || %s\n",line,this->res,this->arg1,this->arg2); 145 | break; 146 | case goto_LT : printf("%3d) if %s < %s goto %s\n",line,this->arg1,this->arg2,this->res); 147 | break; 148 | case goto_LTE : printf("%3d) if %s <= %s goto %s\n",line,this->arg1,this->arg2,this->res); 149 | break; 150 | case goto_GT : printf("%3d) if %s > %s goto %s\n",line,this->arg1,this->arg2,this->res); 151 | break; 152 | case goto_GTE : printf("%3d) if %s >= %s goto %s\n",line,this->arg1,this->arg2,this->res); 153 | break; 154 | case goto_EQ : printf("%3d) if %s == %s goto %s\n",line,this->arg1,this->arg2,this->res); 155 | break; 156 | case goto_NEQ : printf("%3d) if %s != %s goto %s\n",line,this->arg1,this->arg2,this->res); 157 | break; 158 | case call : printf("%3d) %s = call %s, %s\n",line,this->res,this->arg1,this->arg2); 159 | break; 160 | case EQ_BRACKET : printf("%3d) %s = %s[%s]\n",line,this->res,this->arg1,this->arg2); 161 | break; 162 | case BRACKET_EQ : printf("%3d) %s[%s] = %s\n",line,this->arg1,this->arg2,this->res); 163 | break; 164 | } 165 | } 166 | else{ 167 | switch(this->op){ 168 | case U_PLUS : printf("%3d) %s = %s\n",line,this->res,this->arg1); 169 | break; 170 | case U_MINUS : printf("%3d) %s = - %s\n",line,this->res,this->arg1); 171 | break; 172 | case BW_U_NOT : printf("%3d) %s = ~%s\n",line,this->res,this->arg1); 173 | break; 174 | case U_NEGATION : printf("%3d) %s = !%s\n",line,this->res,this->arg1); 175 | break; 176 | case U_ADDR : printf("%3d) %s = & %s\n",line,this->res,this->arg1); 177 | break; 178 | case U_STAR : printf("%3d) %s = * %s\n",line,this->res,this->arg1); 179 | break; 180 | case ASSIGN : printf("%3d) %s = %s\n",line,this->res,this->arg1); 181 | break; 182 | case GOTO_ : printf("%3d) goto %s\n",line,this->res); 183 | break; 184 | case IF_GOTO : printf("%3d) if %s goto %s\n",line,this->arg1,this->res); 185 | break; 186 | case IF_FALSE_GOTO : printf("%3d) ifFalse %s goto %s\n",line,this->arg1,this->res); 187 | break; 188 | case PARAM : printf("%3d) param %s\n",line,this->arg1); 189 | break; 190 | case RETURN_EXP : printf("%3d) return %s\n",line,this->arg1); 191 | break; 192 | case RETURN_ : printf("%3d) return\n",line); 193 | break; 194 | case Function : printf("%3d) %s : \n",line,arg1); 195 | break; 196 | } 197 | } 198 | } 199 | 200 | 201 | 202 | quadArray::quadArray(unsigned int capacity = MAX_SIZE){ 203 | quad_Table = new fields_quad[capacity]; 204 | next_instr = 0; 205 | } 206 | void quadArray::emit(fields_quad &x){ 207 | quad_Table[next_instr++] = x; 208 | } 209 | 210 | void quadArray::print_quadArray(){ 211 | int i; 212 | for(i = 0;i < next_instr;i++){ 213 | quad_Table[i].print_fields_quad(i); 214 | } 215 | } 216 | 217 | void quadArray::fill_dangling_goto(int index, int data){ 218 | char *temp = new char[10]; 219 | sprintf(temp,"%d",data); 220 | ((this->quad_Table)[index]).res = temp; 221 | } 222 | 223 | symbolTable *construct_Symbol_Table(){ 224 | if(MAX_SIZE >=0){ 225 | symbolTable *st = new symbolTable(MAX_SIZE); 226 | return st; 227 | } 228 | } 229 | 230 | void __construct_quad_list(int n){ 231 | if(MAX_SIZE>0) 232 | { 233 | int quadArrayList[n/2]; 234 | } 235 | else 236 | { 237 | printf("Error: Maximum Size should be greater than 0"); 238 | } 239 | } 240 | 241 | void construct_quad_list(int n){ 242 | if(MAX_SIZE>0) 243 | { 244 | __construct_quad_list(n+1); 245 | } 246 | else 247 | { 248 | printf("Error: Maximum Size should be greater than 0"); 249 | } 250 | } 251 | void print_Tree(tNode *temp){ 252 | cout<down){ 255 | case ARRAY : cout<<"array "; 256 | break; 257 | case VOID_ : cout<<"void "; 258 | break; 259 | case INT_ : cout<<"int "; 260 | break; 261 | case DOUBLE_ : cout<<"double "; 262 | break; 263 | case CHAR_ : cout<<"char "; 264 | break; 265 | case PTR : cout<<"ptr "; 266 | break; 267 | case FUNCTION : cout<<"function "; 268 | break; 269 | default : cout<<"No Type "; 270 | } 271 | if(temp->l) 272 | cout<<*(temp->l)<<" "; 273 | temp = temp->r; 274 | } 275 | } 276 | void print_Initial_Value(void *temp, tNode *t){ 277 | if(!temp) 278 | cout<down){ 281 | case INT_ : cout<down = t; 293 | 294 | if(t == ARRAY) 295 | temp->l = new int(val); 296 | else 297 | temp->l = 0; 298 | 299 | temp->r = 0; 300 | 301 | return temp; 302 | } 303 | tNode *merge_node(tNode *sub, tNode *one_node){ 304 | if(!sub) return one_node; 305 | tNode *temp = sub; 306 | while(temp->r) 307 | temp = temp->r; 308 | temp->r = one_node; 309 | return sub; 310 | } 311 | 312 | lnode *makelist(int id){ 313 | lnode *temp = new lnode; 314 | temp->index_list = id; 315 | temp->next = 0; 316 | return temp; 317 | } 318 | 319 | lnode *merge(lnode *l1, lnode *l2){ 320 | if(!l1) return l2; 321 | if(!l2) return l1; 322 | lnode *temp = l1; 323 | while(temp->next) 324 | temp = temp->next; 325 | temp->next = l2; 326 | return l1; 327 | } 328 | 329 | void backpatch(lnode *l, int data){ 330 | lnode *temp = l; 331 | while(temp){ 332 | quad_array->fill_dangling_goto(temp->index_list,data); 333 | temp = temp->next; 334 | } 335 | } 336 | 337 | int typecheck(tNode *t1, tNode *t2){ 338 | if(!t1 && !t2) return 1; 339 | if(!t1) return 0; 340 | if(!t2) return 0; 341 | return (t1->down == t2->down) && typecheck(t1->r,t2->r); 342 | } 343 | 344 | void conv2Bool(attribute_expression *E){ 345 | if(((E->type)->down) != BOOL_){ 346 | E->FL = makelist(next_instr); 347 | char *arg1 = strdup((E->loc)->name); 348 | char *arg2 = new char[10]; 349 | sprintf(arg2,"0"); 350 | fields_quad x(arg1,arg2,0,goto_EQ,E->loc,0,0); 351 | quad_array->emit(x); 352 | E->TL = makelist(next_instr); 353 | fields_quad y(0,0,0,GOTO_,0,0,0); 354 | quad_array->emit(y); 355 | } 356 | } 357 | 358 | int compute_width(tNode *temp){ 359 | if(!temp) return 0; 360 | int width = 1; 361 | while(temp){ 362 | switch(temp->down){ 363 | case ARRAY : width *= (*temp->l); 364 | break; 365 | case INT_ : width *= 4; 366 | break; 367 | case DOUBLE_ : width *= 8; 368 | break; 369 | case CHAR_ : width *= 1; 370 | break; 371 | case PTR : width *= 4; 372 | break; 373 | } 374 | if(temp->down == PTR) 375 | break; 376 | temp = temp->r; 377 | } 378 | return width; 379 | } 380 | 381 | parameter_list *make_param_list(symbol_table_fields *p){ 382 | parameter_list *temp = new parameter_list; 383 | temp->parameter = p; 384 | temp->next = 0; 385 | return temp; 386 | } 387 | 388 | parameter_list *merge_param_list(parameter_list *l1, parameter_list *l2){ 389 | if(!l1) return l2; 390 | if(!l2) return l1; 391 | parameter_list *temp = l1; 392 | while(temp->next) 393 | temp = temp->next; 394 | temp->next = l2; 395 | return l1; 396 | } 397 | tNode *t = 0; 398 | int w; 399 | symbolTable *symbol_table = 0; 400 | symbolTable *current = 0; 401 | symbolTable *temp = 0; 402 | symbolTable *temp_use = 0; 403 | int flag1 = 0; 404 | int flag2 = 0; 405 | int c = 0; 406 | char *func_name; 407 | int line_count = 0; 408 | int temp_count = 0; 409 | int next_instr = 0; 410 | quadArray *quad_array = 0; 411 | int flag_array = 0; 412 | 413 | 414 | int main(){ 415 | symbol_table = construct_Symbol_Table(); 416 | temp = symbol_table; 417 | current = symbol_table; 418 | current = temp; 419 | construct_quad_list(100); 420 | quad_array = new quadArray(100000); 421 | int x = yyparse(); 422 | printf("\n+++++++++++++++++++++++++++++++ Global Symbol Table +++++++++++++++++++++++++++++++\n\n"); 423 | symbol_table->print_Table(); 424 | int i,j; 425 | for(i=0;i<=symbol_table->curr_length;i++){ 426 | if((symbol_table->table[i]).nestedTable){ 427 | printf("\n+++++++++++++++++++++++++++++++ %s Symbol Table +++++++++++++++++++++++++++++++ \n\n",(symbol_table->table[i]).name); 428 | ((symbol_table->table[i]).nestedTable)->print_Table(); 429 | } 430 | } 431 | quad_array->print_quadArray(); 432 | return 0; 433 | } 434 | -------------------------------------------------------------------------------- /Ass-5/ass5_16CS10053_translator.h: -------------------------------------------------------------------------------- 1 | #ifndef ASS5_16CS10053_TRANSLATOR_INCLUDED 2 | #define ASS5_16CS10053_TRANSLATOR_INCLUDED 3 | 4 | # define SIZE_OF_CHAR 1 5 | # define SIZE_OF_INT 4 6 | # define SIZE_OF_DOUBLE 8 7 | # define SIZE_OF_PTR 4 8 | 9 | // Maxsize for symbol table 10 | # define MAX_SIZE 10000 11 | 12 | extern int c, w, flag1, flag2, line_count, temp_count, next_instr, flag_array; 13 | extern char *func_name; 14 | 15 | enum quad_data_types {DEFAULT_, PLUS, MINUS, INTO, DIV, PERCENT, U_PLUS, U_MINUS, BW_U_NOT,U_NEGATION, 16 | SL, SR, LT, LTE, GT, GTE, EQ, NEQ, BW_AND, BW_XOR, PARAM, RETURN_EXP, RETURN_, Function, 17 | BW_INOR, LOG_AND, LOG_OR, goto_LT, goto_LTE, goto_GT, goto_GTE, goto_EQ, goto_NEQ, call, 18 | EQ_BRACKET, BRACKET_EQ, U_ADDR, U_STAR, ASSIGN, GOTO_, IF_GOTO, IF_FALSE_GOTO}; 19 | 20 | enum date_types { ARRAY, PTR, FUNCTION, VOID_, CHAR_, INT_, DOUBLE_, BOOL_}; 21 | 22 | 23 | typedef struct lnode{ 24 | int index_list; 25 | struct lnode *next; 26 | }lnode; 27 | 28 | typedef struct tNode{ 29 | date_types down; 30 | int *l; 31 | struct tNode *r; 32 | }tNode; 33 | extern tNode *t; 34 | 35 | class symbol_table_fields; 36 | class symbolTable; 37 | 38 | class symbol_table_fields{ 39 | public: 40 | void *initial_value; 41 | int size; 42 | char *name; 43 | tNode *Type; 44 | int offset; 45 | symbolTable *nestedTable; 46 | void print_row(); 47 | symbol_table_fields(char * = 0 , tNode * = 0, void * = 0, int = -1, int = -1, symbolTable * = 0); 48 | void operator=(symbol_table_fields &); 49 | ~symbol_table_fields(); 50 | }; 51 | 52 | typedef struct parameter_list{ 53 | symbol_table_fields *parameter; 54 | struct parameter_list *next; 55 | }parameter_list; 56 | 57 | typedef struct fields_quad{ 58 | char *arg1; 59 | char *arg2; 60 | char *res; 61 | quad_data_types op; 62 | symbol_table_fields *arg1_loc; 63 | symbol_table_fields *arg2_loc; 64 | symbol_table_fields *res_loc; 65 | fields_quad(char * = 0, char * = 0, char * = 0, quad_data_types = DEFAULT_, symbol_table_fields * = 0, symbol_table_fields * = 0, symbol_table_fields * = 0); 66 | ~fields_quad(); 67 | void operator=(struct fields_quad &); 68 | void print_fields_quad(int); 69 | }fields_quad; 70 | 71 | 72 | typedef union attribute{ 73 | int int_data; 74 | double double_data; 75 | char char_data; 76 | }attribute; 77 | 78 | typedef struct attribute_expression{ 79 | symbol_table_fields *loc; 80 | lnode *TL; 81 | lnode *FL; 82 | lnode *NL; 83 | tNode *type; 84 | symbol_table_fields *array; 85 | symbol_table_fields *loc1; 86 | attribute val; 87 | }attribute_expression; 88 | 89 | typedef struct attribute_variable_declaration{ 90 | tNode *type; 91 | int width; 92 | char *var; 93 | }attribute_variable_declaration; 94 | 95 | typedef struct id_attr_struct{ 96 | symbol_table_fields *loc; 97 | char *var; 98 | }id_attr_struct; 99 | 100 | class symbolTable{ 101 | public: 102 | symbol_table_fields *table; 103 | int curr_length; 104 | symbolTable(unsigned int); 105 | ~symbolTable(); 106 | symbol_table_fields *lookup(char *); 107 | void insert(symbol_table_fields &); 108 | symbol_table_fields *gentemp(date_types); 109 | void print_Table(); 110 | }; 111 | 112 | class quadArray{ 113 | public: 114 | fields_quad *quad_Table; 115 | quadArray(unsigned int); 116 | ~quadArray(); 117 | void emit(fields_quad &); 118 | void print_quadArray(); 119 | void fill_dangling_goto(int,int); 120 | }; 121 | extern quadArray *quad_array; 122 | 123 | 124 | symbolTable *construct_Symbol_Table(); 125 | void print_Tree(tNode *); 126 | void print_Initial_Value(void *,tNode *); 127 | tNode *new_node(date_types ,int); 128 | tNode *merge_node(tNode *, tNode *); 129 | lnode *makelist(int); 130 | lnode *merge(lnode *, lnode *); 131 | void backpatch(lnode *, int); 132 | int typecheck(tNode *,tNode *); 133 | void conv2Bool(attribute_expression *); 134 | int compute_width(tNode *); 135 | parameter_list *make_param_list(symbol_table_fields *); 136 | parameter_list *merge_param_list(parameter_list *, parameter_list *); 137 | extern symbolTable *symbol_table; 138 | extern symbolTable *current; 139 | extern symbolTable *temp_use; 140 | #endif 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | -------------------------------------------------------------------------------- /Ass-5/sample_outputs/output1.txt: -------------------------------------------------------------------------------- 1 | 2 | +++++++++++++++++++++++++++++++ Global Symbol Table +++++++++++++++++++++++++++++++ 3 | 4 | Name Initial Value Size Offset Nested Table Type 5 | 6 | time_pass2 null 0 0 temp function 7 | time_pass null 0 0 int_var function 8 | main null 0 0 retVal function 9 | 10 | +++++++++++++++++++++++++++++++ time_pass2 Symbol Table +++++++++++++++++++++++++++++++ 11 | 12 | Name Initial Value Size Offset Nested Table Type 13 | 14 | temp null 4 0 null int 15 | elements null 4 4 null int 16 | retVal null 0 8 null void 17 | t0 null 4 8 null int 18 | t1 null 4 12 null int 19 | 20 | +++++++++++++++++++++++++++++++ time_pass Symbol Table +++++++++++++++++++++++++++++++ 21 | 22 | Name Initial Value Size Offset Nested Table Type 23 | 24 | int_var null 4 0 null int 25 | elements null 4 4 null int 26 | value null 4 8 null int 27 | retVal null 4 12 null int 28 | i null 4 16 null int 29 | t2 null 4 20 null int 30 | passes 0 4 24 null int 31 | t3 null 4 28 null int 32 | 33 | +++++++++++++++++++++++++++++++ main Symbol Table +++++++++++++++++++++++++++++++ 34 | 35 | Name Initial Value Size Offset Nested Table Type 36 | 37 | retVal null 4 0 null int 38 | t4 null 4 4 null int 39 | n 3 4 8 null int 40 | t5 null 4 12 null int 41 | i 0 4 16 null int 42 | t6 null 4 20 null int 43 | t7 null 4 24 null int 44 | t8 null 4 28 null int 45 | t9 null 4 32 null int 46 | t10 null 4 36 null int 47 | int_var 3 4 40 null int 48 | result null 4 44 null int 49 | t11 null 4 48 null int 50 | t12 null 0 52 null void 51 | t13 null 4 52 null int 52 | t14 null 4 56 null int 53 | t15 null 4 60 null int 54 | t16 null 4 64 null int 55 | int_var 11 4 68 null int 56 | result null 4 72 null int 57 | t17 null 4 76 null int 58 | t18 null 0 80 null void 59 | 0) time_pass2 : 60 | 1) temp = elements 61 | 2) t0 = 1 62 | 3) t1 = temp + t0 63 | 4) temp = t1 64 | 5) return 65 | 6) time_pass : 66 | 7) t2 = 0 67 | 8) if int_var >= value goto 11 68 | 9) goto 14 69 | 10) goto 14 70 | 11) t3 = passes 71 | 12) passes = passes + 1 72 | 13) goto 14 73 | 14) return passes 74 | 15) main : 75 | 16) t4 = 3 76 | 17) t5 = 0 77 | 18) if i < n goto (null) 78 | 19) goto (null) 79 | 20) t6 = i 80 | 21) i = i + 1 81 | 22) t7 = 1 82 | 23) t8 = i + t7 83 | 24) i = t8 84 | 25) t9 = 0 85 | 26) if n >= t9 goto 29 86 | 27) goto 42 87 | 28) goto 47 88 | 29) t10 = 3 89 | 30) t11 = 3 90 | 31) param int_var 91 | 32) param t11 92 | 33) t12 = call time_pass2, 2 93 | 34) t13 = 4 94 | 35) t14 = 3 95 | 36) param int_var 96 | 37) param t13 97 | 38) param t14 98 | 39) t15 = call time_pass, 3 99 | 40) result = t15 100 | 41) goto (null) 101 | 42) t16 = 11 102 | 43) t17 = 11 103 | 44) param int_var 104 | 45) param t17 105 | 46) t18 = call time_pass2, 2 106 | -------------------------------------------------------------------------------- /Ass-5/sample_outputs/output2.txt: -------------------------------------------------------------------------------- 1 | 2 | +++++++++++++++++++++++++++++++ Global Symbol Table +++++++++++++++++++++++++++++++ 3 | 4 | Name Initial Value Size Offset Nested Table Type 5 | 6 | t0 null 4 0 null int 7 | a 9 4 4 null int 8 | t1 null 4 8 null int 9 | b 10 4 12 null int 10 | sum null 0 16 a function 11 | mod null 0 16 a function 12 | mod2 null 0 16 a function 13 | divide null 0 16 a function 14 | main null 0 16 retVal function 15 | 16 | +++++++++++++++++++++++++++++++ sum Symbol Table +++++++++++++++++++++++++++++++ 17 | 18 | Name Initial Value Size Offset Nested Table Type 19 | 20 | a null 4 0 null int 21 | b null 4 4 null int 22 | retVal null 4 8 null int 23 | t10 null 4 12 null int 24 | ar2 null 80 16 null array 10 double 25 | t11 null 4 96 null int 26 | ans 0 4 100 null int 27 | 28 | +++++++++++++++++++++++++++++++ mod Symbol Table +++++++++++++++++++++++++++++++ 29 | 30 | Name Initial Value Size Offset Nested Table Type 31 | 32 | a null 4 0 null int 33 | b null 4 4 null int 34 | retVal null 4 8 null int 35 | t12 null 4 12 null int 36 | armod null 24 16 null array 3 double 37 | t13 null 4 40 null int 38 | ans 0 8 44 null double 39 | 40 | +++++++++++++++++++++++++++++++ mod2 Symbol Table +++++++++++++++++++++++++++++++ 41 | 42 | Name Initial Value Size Offset Nested Table Type 43 | 44 | a null 4 0 null int 45 | b null 4 4 null int 46 | retVal null 4 8 null int 47 | ans null 4 12 null int 48 | t18 null 4 16 null int 49 | t19 null 4 20 null int 50 | t20 null 4 24 null int 51 | t21 null 4 28 null int 52 | 53 | +++++++++++++++++++++++++++++++ divide Symbol Table +++++++++++++++++++++++++++++++ 54 | 55 | Name Initial Value Size Offset Nested Table Type 56 | 57 | a null 4 0 null int 58 | b null 4 4 null int 59 | retVal null 4 8 null int 60 | ans null 4 12 null int 61 | t14 null 4 16 null int 62 | t15 null 4 20 null int 63 | t16 null 4 24 null int 64 | t17 null 4 28 null int 65 | 66 | +++++++++++++++++++++++++++++++ main Symbol Table +++++++++++++++++++++++++++++++ 67 | 68 | Name Initial Value Size Offset Nested Table Type 69 | 70 | retVal null 4 0 null int 71 | t2 null 4 4 null int 72 | t3 null 4 8 null int 73 | ar1 null 200 12 null array 5 array 10 int 74 | t4 null 4 212 null int 75 | ar2 null 80 216 null array 10 double 76 | y null 8 296 null double 77 | z null 4 304 null int 78 | x null 4 308 null int 79 | y null 4 312 null int 80 | z null 4 316 null int 81 | w null 4 320 null int 82 | t5 null 4 324 null int 83 | t6 null 4 328 null int 84 | t7 null 4 332 null int 85 | t8 null 4 336 null int 86 | t9 null 4 340 null int 87 | 0) t0 = 9 88 | 1) t1 = 10 89 | 2) main : 90 | 3) t2 = 5 91 | 4) t3 = 10 92 | 5) t4 = 10 93 | 6) param a 94 | 7) param b 95 | 8) t5 = call sum, 2 96 | 9) x = t5 97 | 10) param a 98 | 11) param b 99 | 12) t6 = call mod, 2 100 | 13) t7 = dbl2int(y) 101 | 14) y = t7 102 | 15) param a 103 | 16) param b 104 | 17) t8 = call divide, 2 105 | 18) z = t8 106 | 19) param a 107 | 20) param b 108 | 21) t9 = call mod2, 2 109 | 22) w = t9 110 | 23) sum : 111 | 24) t10 = 10 112 | 25) t11 = a + b 113 | 26) return ans 114 | 27) mod : 115 | 28) t12 = 3 116 | 29) t13 = a % b 117 | 30) return ans 118 | 31) divide : 119 | 32) t14 = 0 120 | 33) if b != t14 goto 36 121 | 34) goto 39 122 | 35) goto 42 123 | 36) t15 = a / b 124 | 37) ans = t15 125 | 38) goto 42 126 | 39) t16 = 1 127 | 40) t17 = - t16 128 | 41) ans = t17 129 | 42) return ans 130 | 43) mod2 : 131 | 44) if a > b goto 47 132 | 45) goto 53 133 | 46) goto 58 134 | 47) param a 135 | 48) param b 136 | 49) t18 = call divide, 2 137 | 50) t19 = a - t18 138 | 51) ans = t19 139 | 52) goto 58 140 | 53) param a 141 | 54) param b 142 | 55) t20 = call divide, 2 143 | 56) t21 = b - t20 144 | 57) ans = t21 145 | 58) return ans 146 | -------------------------------------------------------------------------------- /Ass-5/sample_outputs/output3.txt: -------------------------------------------------------------------------------- 1 | 2 | +++++++++++++++++++++++++++++++ Global Symbol Table +++++++++++++++++++++++++++++++ 3 | 4 | Name Initial Value Size Offset Nested Table Type 5 | 6 | t0 null 4 0 null int 7 | test 1 4 4 null int 8 | main null 0 8 retVal function 9 | 10 | +++++++++++++++++++++++++++++++ main Symbol Table +++++++++++++++++++++++++++++++ 11 | 12 | Name Initial Value Size Offset Nested Table Type 13 | 14 | retVal null 4 0 null int 15 | t1 null 4 4 null int 16 | arr_int null 80 8 null array 20 int 17 | t2 null 4 88 null int 18 | arr_d null 344 92 null array 43 double 19 | t3 null 4 436 null int 20 | i 0 4 440 null int 21 | t4 null 4 444 null int 22 | n 9 4 448 null int 23 | t5 null 4 452 null int 24 | t6 null 4 456 null int 25 | t7 null 4 460 null int 26 | t8 null 4 464 null int 27 | t9 null 4 468 null int 28 | t10 null 4 472 null int 29 | t11 null 4 476 null int 30 | t12 null 4 480 null int 31 | t13 null 4 484 null int 32 | t14 null 8 488 null double 33 | t15 null 8 496 null double 34 | t16 null 8 504 null double 35 | t17 null 8 512 null double 36 | t18 null 4 520 null int 37 | timepass 90 4 524 null int 38 | t19 null 4 528 null int 39 | t20 null 4 532 null int 40 | t21 null 4 536 null int 41 | t22 null 4 540 null int 42 | t23 null 4 544 null int 43 | t24 null 4 548 null int 44 | t25 null 4 552 null int 45 | t26 null 4 556 null int 46 | t27 null 4 560 null int 47 | t28 null 4 564 null int 48 | t29 null 4 568 null int 49 | t30 null 8 572 null double 50 | t31 null 8 580 null double 51 | t32 null 8 588 null double 52 | t33 null 8 596 null double 53 | 0) t0 = 1 54 | 1) main : 55 | 2) t1 = 20 56 | 3) t2 = 43 57 | 4) t3 = 0 58 | 5) t4 = 9 59 | 6) t5 = 0 60 | 7) i = t5 61 | 8) if i < n goto 14 62 | 9) goto 20 63 | 10) goto 20 64 | 11) t6 = i 65 | 12) i = i + 1 66 | 13) goto 8 67 | 14) t7 = i * 4 68 | 15) t8 = arr_int[t7] 69 | 16) t9 = 1 70 | 17) t10 = i + t9 71 | 18) arr_int[t7] = t10 72 | 19) goto 11 73 | 20) i = n 74 | 21) t11 = 0 75 | 22) if i >= t11 goto 28 76 | 23) goto 35 77 | 24) goto 35 78 | 25) t12 = i 79 | 26) i = i - 1 80 | 27) goto 21 81 | 28) t13 = i * 8 82 | 29) t14 = arr_d[t13] 83 | 30) t15 = 0.100000 84 | 31) t16 = int2dbl(i) 85 | 32) t17 = t16 * t15 86 | 33) arr_d[t13] = t17 87 | 34) goto 25 88 | 35) t18 = 90 89 | 36) t19 = timepass * timepass 90 | 37) t20 = timepass + t19 91 | 38) timepass = t20 92 | 39) t21 = 0 93 | 40) i = t21 94 | 41) if i < n goto 47 95 | 42) goto 53 96 | 43) goto 53 97 | 44) t22 = i 98 | 45) i = i + 1 99 | 46) goto 41 100 | 47) t23 = i * 4 101 | 48) t24 = arr_int[t23] 102 | 49) t25 = 1 103 | 50) t26 = i + t25 104 | 51) arr_int[t23] = t26 105 | 52) goto 44 106 | 53) i = n 107 | 54) t27 = 0 108 | 55) if i >= t27 goto 61 109 | 56) goto 68 110 | 57) goto 68 111 | 58) t28 = i 112 | 59) i = i - 1 113 | 60) goto 54 114 | 61) t29 = i * 8 115 | 62) t30 = arr_d[t29] 116 | 63) t31 = 0.100000 117 | 64) t32 = int2dbl(i) 118 | 65) t33 = t32 * t31 119 | 66) arr_d[t29] = t33 120 | 67) goto 58 121 | 68) return timepass 122 | -------------------------------------------------------------------------------- /Ass-5/sample_outputs/output4.txt: -------------------------------------------------------------------------------- 1 | 2 | +++++++++++++++++++++++++++++++ Global Symbol Table +++++++++++++++++++++++++++++++ 3 | 4 | Name Initial Value Size Offset Nested Table Type 5 | 6 | multiply_time_pass null 0 0 a function 7 | do_not_what_it_is null 0 0 a function 8 | main null 0 0 retVal function 9 | 10 | +++++++++++++++++++++++++++++++ multiply_time_pass Symbol Table +++++++++++++++++++++++++++++++ 11 | 12 | Name Initial Value Size Offset Nested Table Type 13 | 14 | a null 8 0 null double 15 | b null 4 8 null int 16 | retVal null 8 12 null double 17 | ans null 8 20 null double 18 | t19 null 8 28 null double 19 | t20 null 8 36 null double 20 | t21 null 8 44 null double 21 | t22 null 8 52 null double 22 | t23 null 8 60 null double 23 | t24 null 8 68 null double 24 | t25 null 4 76 null int 25 | t26 null 8 80 null double 26 | t27 null 8 88 null double 27 | t28 null 8 96 null double 28 | t29 null 8 104 null double 29 | 30 | +++++++++++++++++++++++++++++++ do_not_what_it_is Symbol Table +++++++++++++++++++++++++++++++ 31 | 32 | Name Initial Value Size Offset Nested Table Type 33 | 34 | a null 4 0 null int 35 | b null 8 4 null double 36 | c null 1 12 null char 37 | retVal null 1 13 null char 38 | kt 0 8 14 null double 39 | t30 null 1 22 null char 40 | p 1 23 null ptr char 41 | t31 null 8 24 null double 42 | t32 null 8 32 null double 43 | i 0 4 40 null int 44 | 45 | +++++++++++++++++++++++++++++++ main Symbol Table +++++++++++++++++++++++++++++++ 46 | 47 | Name Initial Value Size Offset Nested Table Type 48 | 49 | retVal null 4 0 null int 50 | t0 null 4 4 null int 51 | n 9 4 8 null int 52 | t1 null 4 12 null int 53 | t2 null 4 16 null int 54 | t3 null 1 20 null char 55 | a a 1 21 null char 56 | t4 null 1 22 null char 57 | b b 1 23 null char 58 | t5 null 1 24 null char 59 | c c 1 25 null char 60 | t6 null 4 26 null int 61 | t7 null 4 30 null int 62 | dp null 2400 34 null array 20 array 15 double 63 | t8 null 1 2434 null char 64 | d 1 2435 null ptr char 65 | t9 null 4 2436 null int 66 | k 9 4 2440 null int 67 | t10 null 4 2444 null int 68 | t11 null 4 2448 null int 69 | l 0 4 2452 null int 70 | t12 null 4 2456 null int 71 | i 50 4 2460 null int 72 | t13 null 4 2464 null int 73 | p 4 2468 null ptr int 74 | t14 null 1 2472 null char 75 | a a 1 2473 null char 76 | t15 null 4 2474 null int 77 | ans 90 4 2478 null int 78 | t16 null 4 2482 null int 79 | t17 null 4 2486 null int 80 | t18 null 4 2490 null int 81 | 0) main : 82 | 1) t0 = 9 83 | 2) t1 = 0 84 | 3) if n >= t1 goto 6 85 | 4) goto 25 86 | 5) goto 27 87 | 6) t2 = 10 88 | 7) if n <= t2 goto 10 89 | 8) goto 22 90 | 9) goto 24 91 | 10) t3 = a 92 | 11) t4 = b 93 | 12) t5 = c 94 | 13) t6 = 20 95 | 14) t7 = 15 96 | 15) t8 = & a 97 | 16) t9 = 9 98 | 17) t10 = 10 99 | 18) t11 = k + t10 100 | 19) t12 = 50 101 | 20) t13 = & i 102 | 21) goto 27 103 | 22) t14 = a 104 | 23) t15 = 90 105 | 24) goto 27 106 | 25) t16 = 0 107 | 26) return t16 108 | 27) t17 = 89 109 | 28) n = t17 110 | 29) t18 = 0 111 | 30) return t18 112 | 31) multiply_time_pass : 113 | 32) t19 = int2dbl(b) 114 | 33) t20 = a * t19 115 | 34) ans = t20 116 | 35) t21 = ans * a 117 | 36) ans = t21 118 | 37) t22 = int2dbl(b) 119 | 38) t23 = t22 * ans 120 | 39) a = t23 121 | 40) t24 = a * a 122 | 41) t25 = b * b 123 | 42) t26 = int2dbl(t25) 124 | 43) t27 = t24 + t26 125 | 44) t28 = ans * ans 126 | 45) t29 = t27 + t28 127 | 46) ans = t29 128 | 47) return ans 129 | 48) do_not_what_it_is : 130 | 49) t30 = & c 131 | 50) t31 = int2dbl(a) 132 | 51) t32 = t31 + b 133 | 52) return c 134 | -------------------------------------------------------------------------------- /Ass-5/sample_outputs/output5.txt: -------------------------------------------------------------------------------- 1 | 2 | +++++++++++++++++++++++++++++++ Global Symbol Table +++++++++++++++++++++++++++++++ 3 | 4 | Name Initial Value Size Offset Nested Table Type 5 | 6 | hello null 0 0 a function 7 | kaiseho null 0 0 a function 8 | lol null 0 0 a function 9 | dont_know_what_to_do null 0 0 a function 10 | main null 0 0 retVal function 11 | 12 | +++++++++++++++++++++++++++++++ hello Symbol Table +++++++++++++++++++++++++++++++ 13 | 14 | Name Initial Value Size Offset Nested Table Type 15 | 16 | a null 4 0 null int 17 | b null 4 4 null int 18 | retVal null 4 8 null int 19 | t22 null 4 12 null int 20 | 21 | +++++++++++++++++++++++++++++++ kaiseho Symbol Table +++++++++++++++++++++++++++++++ 22 | 23 | Name Initial Value Size Offset Nested Table Type 24 | 25 | a null 4 0 null int 26 | b null 4 4 null int 27 | c null 4 8 null int 28 | retVal null 4 12 null int 29 | t18 null 4 16 null int 30 | t19 null 4 20 null int 31 | t20 null 4 24 null int 32 | t21 null 4 28 null int 33 | 34 | +++++++++++++++++++++++++++++++ lol Symbol Table +++++++++++++++++++++++++++++++ 35 | 36 | Name Initial Value Size Offset Nested Table Type 37 | 38 | a null 4 0 null int 39 | b null 4 4 null int 40 | c null 4 8 null int 41 | retVal null 4 12 null int 42 | t13 null 4 16 null int 43 | t14 null 4 20 null int 44 | t15 null 4 24 null int 45 | t16 null 4 28 null int 46 | t17 null 4 32 null int 47 | 48 | +++++++++++++++++++++++++++++++ dont_know_what_to_do Symbol Table +++++++++++++++++++++++++++++++ 49 | 50 | Name Initial Value Size Offset Nested Table Type 51 | 52 | a null 4 0 null int 53 | b null 4 4 null int 54 | c null 4 8 null int 55 | d null 4 12 null int 56 | retVal null 8 16 null double 57 | t6 null 4 24 null int 58 | t7 null 4 28 null int 59 | t8 null 4 32 null int 60 | t9 null 8 36 null double 61 | t10 null 4 44 null int 62 | t11 null 8 48 null double 63 | t12 null 8 56 null double 64 | 65 | +++++++++++++++++++++++++++++++ main Symbol Table +++++++++++++++++++++++++++++++ 66 | 67 | Name Initial Value Size Offset Nested Table Type 68 | 69 | retVal null 4 0 null int 70 | t0 null 4 4 null int 71 | a 6 4 8 null int 72 | t1 null 4 12 null int 73 | b 7 4 16 null int 74 | t2 null 4 20 null int 75 | c 8 4 24 null int 76 | t3 null 4 28 null int 77 | d 9 4 32 null int 78 | t4 null 8 36 null double 79 | t5 null 4 44 null int 80 | 0) main : 81 | 1) t0 = 6 82 | 2) t1 = 7 83 | 3) t2 = 8 84 | 4) t3 = 9 85 | 5) param a 86 | 6) param b 87 | 7) param c 88 | 8) param d 89 | 9) t4 = call dont_know_what_to_do, 4 90 | 10) t5 = 0 91 | 11) return t5 92 | 12) dont_know_what_to_do : 93 | 13) param a 94 | 14) param b 95 | 15) param c 96 | 16) t6 = call kaiseho, 3 97 | 17) param a 98 | 18) param b 99 | 19) t7 = call hello, 2 100 | 20) param a 101 | 21) param b 102 | 22) param c 103 | 23) t8 = call lol, 3 104 | 24) t9 = 2.000000 105 | 25) t10 = 7 106 | 26) t11 = int2dbl(t10) 107 | 27) t12 = t9 * t11 108 | 28) return t12 109 | 29) lol : 110 | 30) param a 111 | 31) param b 112 | 32) param c 113 | 33) t13 = call kaiseho, 3 114 | 34) param a 115 | 35) param b 116 | 36) t14 = call hello, 2 117 | 37) t15 = 3 118 | 38) t16 = 4 119 | 39) t17 = t15 + t16 120 | 40) return t17 121 | 41) kaiseho : 122 | 42) param a 123 | 43) param b 124 | 44) t18 = call hello, 2 125 | 45) t19 = 3 126 | 46) t20 = 9 127 | 47) t21 = t19 + t20 128 | 48) return t21 129 | 49) hello : 130 | 50) t22 = 1 131 | 51) return t22 132 | -------------------------------------------------------------------------------- /Ass-6/Makefile: -------------------------------------------------------------------------------- 1 | tinyc: y.tab.o lex.yy.o ass6_16CS10053_translator.o ass6_16CS10053_target_translator.o 2 | @g++ -g ass6_16CS10053_translator.o ass6_16CS10053_target_translator.o lex.yy.o y.tab.o -lfl -o tinyc 3 | @./tinyc < ass6_16CS10053_test1.c 4 | @./tinyc < ass6_16CS10053_test2.c 5 | @./tinyc < ass6_16CS10053_test3.c 6 | @./tinyc < ass6_16CS10053_test4.c 7 | @./tinyc < ass6_16CS10053_test5.c 8 | @echo " make run to execute all the test files at once" 9 | lex.yy.c: ass6_16CS10053.l 10 | @flex ass6_16CS10053.l 11 | y.tab.c: ass6_16CS10053.y 12 | @yacc -dtv ass6_16CS10053.y 13 | ass6_16CS10053_target_translator.o: ass6_16CS10053_target_translator.cxx 14 | @g++ -g -c ass6_16CS10053_target_translator.cxx 15 | ass6_16CS10053_translator.o: ass6_16CS10053_translator.cxx 16 | @g++ -g -c ass6_16CS10053_translator.cxx 17 | lex.yy.o: lex.yy.c 18 | @g++ -g -c lex.yy.c 19 | y.tab.o: y.tab.c 20 | @g++ -g -DYYDEBUG -c y.tab.c 21 | libass2_16CS10053.a: ass2_16CS10053.o 22 | @ar -rcs libass2_16CS10053.a ass2_16CS10053.o 23 | 24 | ass2_16CS10053.o: ass2_16CS10053.c myl.h 25 | @gcc -Wall -c ass2_16CS10053.c 26 | clean: 27 | @rm ass6_16CS10053_test1.s ass6_16CS10053_test2.s ass6_16CS10053_test3.s ass6_16CS10053_test4.s ass6_16CS10053_test5.s test1 test2 test3 test4 test5 lex.yy.c y.tab.h y.output y.tab.c lex.yy.o y.tab.o ass6_16CS10053_translator.o ass6_16CS10053_target_translator.o ass6_16CS10053_test1.o ass6_16CS10053_test2.o ass6_16CS10053_test3.o ass6_16CS10053_test4.o ass6_16CS10053_test5.o libass2_16CS10053.a ass2_16CS10053.o ass6_16CS10053_quad1.out ass6_16CS10053_quad2.out ass6_16CS10053_quad3.out ass6_16CS10053_quad4.out ass6_16CS10053_quad5.out tinyc 28 | 29 | 30 | test1: ass6_16CS10053_test1.o libass2_16CS10053.a 31 | @gcc -g ass6_16CS10053_test1.o -o test1 -L. -lass2_16CS10053 32 | ass6_16CS10053_test1.o: ass6_16CS10053_test1.s myl.h 33 | @gcc -g -Wall -c ass6_16CS10053_test1.s -o ass6_16CS10053_test1.o 34 | 35 | test2: ass6_16CS10053_test2.o libass2_16CS10053.a 36 | @gcc -g ass6_16CS10053_test2.o -o test2 -L. -lass2_16CS10053 37 | ass6_16CS10053_test2.o: ass6_16CS10053_test2.s myl.h 38 | @gcc -g -Wall -c ass6_16CS10053_test2.s 39 | 40 | test3: ass6_16CS10053_test3.o libass2_16CS10053.a 41 | @gcc -g ass6_16CS10053_test3.o -o test3 -L. -lass2_16CS10053 42 | ass6_16CS10053_test3.o: ass6_16CS10053_test3.s myl.h 43 | @gcc -g -Wall -c ass6_16CS10053_test3.s 44 | 45 | test4: ass6_16CS10053_test4.o libass2_16CS10053.a 46 | @gcc -g ass6_16CS10053_test4.o -o test4 -L. -lass2_16CS10053 47 | ass6_16CS10053_test4.o: ass6_16CS10053_test4.s myl.h 48 | @gcc -g -Wall -c ass6_16CS10053_test4.s 49 | 50 | test5: ass6_16CS10053_test5.o libass2_16CS10053.a 51 | @gcc -g ass6_16CS10053_test5.o -o test5 -L. -lass2_16CS10053 52 | ass6_16CS10053_test5.o: ass6_16CS10053_test5.s myl.h 53 | @gcc -g -Wall -c ass6_16CS10053_test5.s 54 | 55 | #output: output.o 56 | output: output.o libass2_16CS10053.a 57 | @gcc -g output.o -o output -L. -lass2_16CS10053 58 | output.o: output.s myl.h 59 | @gcc -g -Wall -c output.s 60 | 61 | run: 62 | 63 | @./tinyc < ass6_16CS10053_test1.c > ass6_16CS10053_quad1.out 64 | 65 | @mv output.s ass6_16CS10053_test1.s 66 | 67 | @make test1 68 | 69 | @./tinyc < ass6_16CS10053_test2.c > ass6_16CS10053_quad2.out 70 | 71 | @mv output.s ass6_16CS10053_test2.s 72 | 73 | @make test2 74 | 75 | @./tinyc < ass6_16CS10053_test3.c > ass6_16CS10053_quad3.out 76 | 77 | @mv output.s ass6_16CS10053_test3.s 78 | 79 | @make test3 80 | 81 | @./tinyc < ass6_16CS10053_test4.c > ass6_16CS10053_quad4.out 82 | 83 | @mv output.s ass6_16CS10053_test4.s 84 | 85 | @make test4 86 | 87 | @./tinyc < ass6_16CS10053_test5.c > ass6_16CS10053_quad5.out 88 | 89 | @mv output.s ass6_16CS10053_test5.s 90 | 91 | @make test5 92 | @echo "" 93 | @echo "Commands for Test Cases" 94 | @echo "Test Case 1 : Calculate Interest Command : ./test1" 95 | @echo "Test Case 2 : Palindrome or Not Command : ./test2" 96 | @echo "Test Case 3 : Sum of Digits Command : ./test3" 97 | @echo "Test Case 4 : Longest Common Subse. Command : ./test4" 98 | @echo "Test Case 5 : Merge Sort Command : ./test5" 99 | -------------------------------------------------------------------------------- /Ass-6/ass2_16CS10053.c: -------------------------------------------------------------------------------- 1 | #include "myl.h" 2 | #define MAX 50 3 | 4 | int prints(char *s) 5 | { 6 | int bytes=0; 7 | while(s[bytes]!='\0')bytes++; 8 | __asm__ __volatile__ ( 9 | "movl $1, %%eax \n\t" 10 | "movq $1, %%rdi \n\t" 11 | "syscall \n\t" 12 | : 13 | :"S"(s), "d"(bytes) 14 | ); 15 | return bytes; 16 | } 17 | 18 | 19 | int printi(int n) 20 | { 21 | char buff[MAX],zero='0'; 22 | int i=0,j=0,bytes,k; 23 | if(n<0){n=-n;buff[i++]='-';} 24 | if(n==0)buff[i++]=zero; 25 | while(n!=0){ 26 | buff[i++]= (char)(n%10 + zero); 27 | n=n/10; 28 | } 29 | if(buff[0]=='-')j=1; 30 | k=i-1; 31 | bytes=i; 32 | while(j'9' || str[0]<'0' )*eP=ERR; 60 | else{ 61 | val=10*val+(int)(str[0]-'0'); 62 | } 63 | } 64 | i++; 65 | } 66 | return val*sign; 67 | } 68 | 69 | -------------------------------------------------------------------------------- /Ass-6/ass6_16CS10053.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include "ass6_16CS10053_translator.h" 3 | #include "y.tab.h" 4 | extern void yyerror(const char *); 5 | static void comment(void); 6 | %} 7 | 8 | DIGIT [0-9] 9 | NONZERODIG [1-9] 10 | IDENT_ND [A-Za-z_] 11 | IDENT ({IDENT_ND}({IDENT_ND}|{DIGIT})*) 12 | WHITESPACE [ \n\t] 13 | SIGN [+-] 14 | DIGIT_SEQ {DIGIT}+ 15 | EXPONENT_PART ([eE]{SIGN}?{DIGIT_SEQ}) 16 | ZERO "0" 17 | INT_CONST (({NONZERODIG}({DIGIT}*))|{ZERO}+) 18 | DOT "." 19 | FRACT_CONST (({DIGIT_SEQ}?{DOT}{DIGIT_SEQ})|({DIGIT_SEQ}{DOT})) 20 | FLOAT_CONST (({FRACT_CONST}{EXPONENT_PART}?)|({DIGIT_SEQ}{EXPONENT_PART})) 21 | ENUM_CONST {IDENT} 22 | ESC_SEQ ([\\][\'\"\?\\abfnrtv]) 23 | C_CHAR [^\'\\\n]|{ESC_SEQ} 24 | C_CHAR_SEQ {C_CHAR}+ 25 | C_CHAR_CONST (\'{C_CHAR_SEQ}\') 26 | CONST ({INT_CONST}|{FLOAT_CONST}|{ENUM_CONST}|{C_CHAR_CONST}) 27 | S_CHAR [^\"\\\n]|{ESC_SEQ} 28 | S_CHAR_SEQ {S_CHAR}+ 29 | STRING_LITERAL (\"{S_CHAR_SEQ}*\") 30 | SINGLE_COMMENT [/][/].* 31 | MULTI_END [\*]+[/] 32 | MULTI_START [/][\*]+ 33 | NON_STAR (([^\*]).*) 34 | NON_FS ([^/].*) 35 | AVOID_MULTI_END (NON_STAR|([\*]{NON_FS})) 36 | MULTI_COMMENT ({MULTI_START}(([^\*])|((\*)+[^/\*]))*{MULTI_END}) 37 | COMMENT {SINGLE_COMMENT}|{MULTI_COMMENT} 38 | 39 | %% 40 | 41 | "auto" {return AUTO;} 42 | "enum" {return ENUM;} 43 | "restrict" {return RESTRICT;} 44 | "unsigned" {return UNSIGNED;} 45 | "break" {return BREAK;} 46 | "extern" {return EXTERN;} 47 | "return" {return RETURN;} 48 | "void" {return VOID;} 49 | "char" {return CHAR;} 50 | "for" {return FOR;} 51 | "signed" {return SIGNED;} 52 | "while" {return WHILE;} 53 | "const" {return CONST;} 54 | "goto" {return GOTO;} 55 | "sizeof" {return SIZEOF;} 56 | "case" {return CASE;} 57 | "float" {return FLOAT;} 58 | "short" {return SHORT;} 59 | "volatile" {return VOLATILE;} 60 | "_Bool" {return BOOL;} 61 | "continue" {return CONTINUE;} 62 | "if" {return IF;} 63 | "static" {return STATIC;} 64 | "_Complex" {return COMPLEX;} 65 | "default" {return DEFAULT;} 66 | "inline" {return INLINE;} 67 | "struct" {return STRUCT;} 68 | "_Imaginary" {return IMAGINARY;} 69 | "do" {return DO;} 70 | "int" {return INT;} 71 | "switch" {return SWITCH;} 72 | "double" {return DOUBLE;} 73 | "long" {return LONG;} 74 | "typedef" {return TYPEDEF;} 75 | "else" {return ELSE;} 76 | "register" {return REGISTER;} 77 | "union" {return UNION;} 78 | 79 | 80 | "[" {return '['; } 81 | "]" { return ']'; } 82 | "(" { return '('; } 83 | ")" { return ')'; } 84 | "{" { return '{'; } 85 | "}" { return '}'; } 86 | "." { return '.'; } 87 | "->" { return (POINTER); } 88 | "++" { return (INCREMENT); } 89 | "--" { return (DECREMENT); } 90 | "&" { return '&'; } 91 | "*" { return '*'; } 92 | "+" { return '+'; } 93 | "-" { return '-'; } 94 | "~" { return '~'; } 95 | "!" { return '!'; } 96 | "/" { return '/'; } 97 | "%" { return '%'; } 98 | "<<" { return (LEFT_SHIFT); } 99 | ">>" { return (RIGHT_SHIFT); } 100 | "<" { return '<'; } 101 | ">" { return '>'; } 102 | "<=" { return (LESS_EQUALS); } 103 | ">=" { return (GREATER_EQUALS); } 104 | "==" { return (EQUALS); } 105 | "!=" { return (NOT_EQUALS); } 106 | "^" { return '^'; } 107 | "|" { return '|'; } 108 | "&&" { return (AND); } 109 | "||" { return (OR); } 110 | "?" { return '?'; } 111 | ":" { return ':'; } 112 | ";" { return ';'; } 113 | "..." { return (ELLIPSIS); } 114 | "=" { return '='; } 115 | "*=" { return MULTIPLY_ASSIGN; } 116 | "/=" { return DIVIDE_ASSIGN; } 117 | "%=" { return MODULO_ASSIGN; } 118 | "+=" { return ADD_ASSIGN; } 119 | "-=" { return SUBTRACT_ASSIGN; } 120 | "<<=" { return LEFT_SHIFT_ASSIGN; } 121 | ">>=" { return RIGHT_SHIFT_ASSIGN; } 122 | "&=" { return AND_ASSIGN; } 123 | "^=" { return XOR_ASSIGN; } 124 | "|=" { return OR_ASSIGN; } 125 | "," { return ','; } 126 | "#" { return '#'; } 127 | 128 | {COMMENT} {;} 129 | 130 | {IDENT} { yylval.idl.name=new string(yytext);return IDENTIFIER;} 131 | {INT_CONST} { yylval.intval=atoi(yytext); return INTEGER_CONSTANT;} 132 | {FLOAT_CONST} { yylval.floatval=atof(yytext); return FLOATING_CONSTANT;} 133 | {ENUM_CONST} { return(ENUMERATION_CONSTANT);} 134 | {C_CHAR_CONST} { yylval.charval=yytext[1]; return CHAR_CONST;} 135 | {STRING_LITERAL} { yylval.strval = new string(yytext);return STRING_LITERAL;} 136 | 137 | 138 | {WHITESPACE} {;} 139 | 140 | %% 141 | -------------------------------------------------------------------------------- /Ass-6/ass6_16CS10053_target_translator.cxx: -------------------------------------------------------------------------------- 1 | #include "ass6_16CS10053_translator.h" 2 | #include "y.tab.h" 3 | 4 | extern quad_arr glob_quad; 5 | extern int next_instr; 6 | map mp_set; 7 | stack params_stack; 8 | stack types_stack; 9 | stack offset_stack; 10 | stack ptrarr_stack; 11 | extern std::vector< string > vs; 12 | extern std::vector cs; 13 | int add_off; 14 | void symtab::mark_labels() 15 | { 16 | int count=1; 17 | for(int i=0;iname[0]!='t' &&symbol_tab[i]->tp_n!=NULL&&symbol_tab[i]->var_type!="func") 59 | { 60 | if(symbol_tab[i]->tp_n->basetp==tp_int) 61 | { 62 | vs.push_back(symbol_tab[i]->name); 63 | if(symbol_tab[i]->isInitialized==false) 64 | { 65 | fprintf(fp,"\n\t.comm\t%s,4,4",symbol_tab[i]->name.c_str()); 66 | } 67 | else 68 | { 69 | fprintf(fp,"\n\t.globl\t%s",symbol_tab[i]->name.c_str()); 70 | fprintf(fp,"\n\t.data"); 71 | fprintf(fp,"\n\t.align 4"); 72 | fprintf(fp,"\n\t.type\t%s, @object",symbol_tab[i]->name.c_str()); 73 | fprintf(fp,"\n\t.size\t%s ,4",symbol_tab[i]->name.c_str()); 74 | fprintf(fp,"\n%s:",symbol_tab[i]->name.c_str()); 75 | fprintf(fp,"\n\t.long %d",symbol_tab[i]->i_val.int_val); 76 | } 77 | } 78 | if(symbol_tab[i]->tp_n->basetp==tp_char) 79 | { 80 | cs.push_back(symbol_tab[i]->name); 81 | if(symbol_tab[i]->isInitialized==false) 82 | { 83 | fprintf(fp,"\n\t.comm\t%s,1,1",symbol_tab[i]->name.c_str()); 84 | } 85 | else 86 | { 87 | fprintf(fp,"\n\t.globl\t%s",symbol_tab[i]->name.c_str()); 88 | fprintf(fp,"\n\t.data"); 89 | fprintf(fp,"\n\t.type\t%s, @object",symbol_tab[i]->name.c_str()); 90 | fprintf(fp,"\n\t.size\t%s ,1",symbol_tab[i]->name.c_str()); 91 | fprintf(fp,"\n%s:",symbol_tab[i]->name.c_str()); 92 | fprintf(fp,"\n\t.byte %c",symbol_tab[i]->i_val.char_val); 93 | } 94 | } 95 | } 96 | 97 | } 98 | fprintf(fp,"\n\t.text"); 99 | } 100 | void symtab::assign_offset() 101 | { 102 | int curr_offset=0; 103 | int param_offset=16; 104 | no_params=0; 105 | for(int i = (symbol_tab).size()-1; i>=0; i--) 106 | { 107 | if(symbol_tab[i]->ispresent==false) 108 | continue; 109 | if(symbol_tab[i]->var_type=="param" && symbol_tab[i]->isdone==false) 110 | { 111 | no_params++; 112 | if(symbol_tab[i]->tp_n && symbol_tab[i]->tp_n->basetp==tp_arr) 113 | { 114 | if(symbol_tab[i]->tp_n->size==-1) 115 | { 116 | symbol_tab[i]->isptrarr=true; 117 | } 118 | symbol_tab[i]->size=8; 119 | } 120 | symbol_tab[i]->offset=curr_offset-symbol_tab[i]->size; 121 | curr_offset=curr_offset-symbol_tab[i]->size; 122 | symbol_tab[i]->isdone=true; 123 | } 124 | if(no_params==6) 125 | break; 126 | } 127 | for(int i = 0; i<(symbol_tab).size(); i++) 128 | { 129 | if(symbol_tab[i]->ispresent==false) 130 | continue; 131 | if(symbol_tab[i]->var_type!="return"&&symbol_tab[i]->var_type!="param" && symbol_tab[i]->isdone==false) 132 | { 133 | symbol_tab[i]->offset=curr_offset-symbol_tab[i]->size; 134 | curr_offset=curr_offset-symbol_tab[i]->size; 135 | symbol_tab[i]->isdone=true; 136 | } 137 | else if(symbol_tab[i]->var_type=="param" && symbol_tab[i]->isdone==false) 138 | { 139 | if(symbol_tab[i]->tp_n && symbol_tab[i]->tp_n->basetp==tp_arr) 140 | { 141 | if(symbol_tab[i]->tp_n->size==-1) 142 | { 143 | symbol_tab[i]->isptrarr=true; 144 | } 145 | symbol_tab[i]->size=8; 146 | } 147 | symbol_tab[i]->isdone=true; 148 | no_params++; 149 | symbol_tab[i]->offset=param_offset; 150 | param_offset=param_offset+symbol_tab[i]->size; 151 | } 152 | } 153 | offset=curr_offset; 154 | } 155 | string symtab::assign_reg(int type_of,int no) 156 | { 157 | string s="NULL"; 158 | if(type_of==tp_char){ 159 | switch(no){ 160 | case 0: s = "dil"; 161 | break; 162 | case 1: s = "sil"; 163 | break; 164 | case 2: s = "dl"; 165 | break; 166 | case 3: s = "cl"; 167 | break; 168 | case 4: s = "r8b"; 169 | break; 170 | case 5: s = "r9b"; 171 | break; 172 | } 173 | } 174 | else if(type_of == tp_int){ 175 | switch(no){ 176 | case 0: s = "edi"; 177 | break; 178 | case 1: s = "esi"; 179 | break; 180 | case 2: s = "edx"; 181 | break; 182 | case 3: s = "ecx"; 183 | break; 184 | case 4: s = "r8d"; 185 | break; 186 | case 5: s = "r9d"; 187 | break; 188 | } 189 | } 190 | else 191 | { 192 | switch(no){ 193 | case 0: s = "rdi"; 194 | break; 195 | case 1: s = "rsi"; 196 | break; 197 | case 2: s = "rdx"; 198 | break; 199 | case 3: s = "rcx"; 200 | break; 201 | case 4: s = "r8"; 202 | break; 203 | case 5: s = "r9"; 204 | break; 205 | } 206 | 207 | } 208 | return s; 209 | } 210 | int symtab::function_call(FILE *fp) 211 | { 212 | 213 | int c=0; 214 | fprintf(fp,"\n\tpushq %%rbp"); 215 | int count=0; 216 | while(count<6 && params_stack.size()) 217 | { 218 | string p=params_stack.top(); 219 | int btp=types_stack.top(); 220 | int off=offset_stack.top(); 221 | int parr=ptrarr_stack.top(); 222 | params_stack.pop(); 223 | types_stack.pop(); 224 | offset_stack.pop(); 225 | ptrarr_stack.pop(); 226 | string temp_str=assign_reg(btp,count); 227 | if(temp_str!="NULL") 228 | { 229 | if(btp==tp_int) 230 | { 231 | fprintf(fp,"\n\tmovl\t%d(%%rbp) , %%%s",off,temp_str.c_str()); 232 | } 233 | else if(btp==tp_char) 234 | { 235 | fprintf(fp,"\n\tmovb\t%d(%%rbp), %%%s",off,temp_str.c_str()); 236 | } 237 | else if(btp==tp_arr && parr==1) 238 | { 239 | fprintf(fp,"\n\tmovq\t%d(%%rbp), %%%s",off,temp_str.c_str()); 240 | } 241 | else if(btp==tp_arr) 242 | { 243 | fprintf(fp,"\n\tleaq\t%d(%%rbp), %%%s",off,temp_str.c_str()); 244 | } 245 | else 246 | { 247 | 248 | fprintf(fp,"\n\tmovq\t%d(%%rbp), %%%s",off,temp_str.c_str()); 249 | } 250 | count++; 251 | } 252 | } 253 | while(params_stack.size()) 254 | { 255 | 256 | string p=params_stack.top(); 257 | int btp=types_stack.top(); 258 | int off=offset_stack.top(); 259 | int parr=ptrarr_stack.top(); 260 | params_stack.pop(); 261 | types_stack.pop(); 262 | offset_stack.pop(); 263 | ptrarr_stack.pop(); 264 | if(btp==tp_int) 265 | { 266 | fprintf(fp,"\n\tsubq $4, %%rsp"); 267 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%eax",off); 268 | fprintf(fp,"\n\tmovl\t%%eax, (%%rsp)"); 269 | c+=4; 270 | } 271 | else if(btp==tp_arr && parr==1) 272 | { 273 | fprintf(fp,"\n\tsubq $8, %%rsp"); 274 | fprintf(fp,"\n\tmovq\t%d(%%rbp), %%rax",off); 275 | fprintf(fp,"\n\tmovq\t%%rax, (%%rsp)"); 276 | c+=8; 277 | } 278 | else if(btp==tp_arr) 279 | { 280 | fprintf(fp,"\n\tsubq $8, %%rsp"); 281 | fprintf(fp,"\n\tleaq\t%d(%%rbp), %%rax",off); 282 | fprintf(fp,"\n\tmovq\t%%rax, (%%rsp)"); 283 | c+=8; 284 | } 285 | else if(btp==tp_char) 286 | { 287 | fprintf(fp,"\n\tsubq $4, %%rsp"); 288 | fprintf(fp,"\n\tmovsbl\t%d(%%rbp), %%eax",off); 289 | fprintf(fp,"\n\tmovl\t%%eax, (%%rsp)"); 290 | c+=4; 291 | } 292 | else 293 | { 294 | fprintf(fp,"\n\tsubq $8, %%rsp"); 295 | fprintf(fp,"\n\tmovq\t%d(%%rbp), %%rax",off); 296 | fprintf(fp,"\n\tmovq\t%%rax, (%%rsp)"); 297 | c+=8; 298 | } 299 | } 300 | return c; 301 | 302 | } 303 | void symtab::function_restore(FILE *fp) 304 | { 305 | int count=0; 306 | string regname; 307 | for(int i=symbol_tab.size()-1;i>=0;i--) 308 | { 309 | if(symbol_tab[i]->ispresent==false) 310 | continue; 311 | if(symbol_tab[i]->var_type=="param" && symbol_tab[i]->offset<0) 312 | { 313 | if(symbol_tab[i]->tp_n->basetp == tp_char){ 314 | regname = assign_reg(tp_char,count); 315 | fprintf(fp,"\n\tmovb\t%%%s, %d(%%rbp)",regname.c_str(),symbol_tab[i]->offset); 316 | } 317 | else if(symbol_tab[i]->tp_n->basetp == tp_int){ 318 | regname = assign_reg(tp_int,count); 319 | fprintf(fp,"\n\tmovl\t%%%s, %d(%%rbp)",regname.c_str(),symbol_tab[i]->offset); 320 | } 321 | else { 322 | regname = assign_reg(10,count); 323 | fprintf(fp,"\n\tmovq\t%%%s, %d(%%rbp)",regname.c_str(),symbol_tab[i]->offset); 324 | } 325 | count++; 326 | } 327 | if(count==6) 328 | break; 329 | } 330 | } 331 | void symtab::gen_internal_code(FILE *fp,int ret_count) 332 | { 333 | int i; 334 | for(i = start_quad; i <=end_quad; i++) 335 | { 336 | opcode &opx =glob_quad.arr[i].op; 337 | string &arg1x =glob_quad.arr[i].arg1; 338 | string &arg2x =glob_quad.arr[i].arg2; 339 | string &resx =glob_quad.arr[i].result; 340 | int offr,off1,off2; 341 | int flag1=1; 342 | int flag2=1; 343 | int flag3=1; 344 | int j; 345 | fprintf(fp,"\n# %d:",i); 346 | if(search(resx)) 347 | { 348 | offr = search(resx)->offset; 349 | fprintf(fp,"res = %s ",search(resx)->name.c_str()); 350 | } 351 | else if(glob_quad.arr[i].result!=""&& findg(glob_quad.arr[i].result)) 352 | { 353 | flag3=0; 354 | } 355 | if(search(arg1x)) 356 | { 357 | 358 | off1 = search(arg1x)->offset; 359 | fprintf(fp,"arg1 = %s ",search(arg1x)->name.c_str()); 360 | } 361 | else if(glob_quad.arr[i].arg1!="" && findg(glob_quad.arr[i].arg1)) 362 | { 363 | 364 | flag1=0; 365 | 366 | } 367 | if(search(arg2x)) 368 | { 369 | off2 = search(arg2x)->offset; 370 | fprintf(fp,"arg2 = %s ",search(arg2x)->name.c_str()); 371 | } 372 | else if(glob_quad.arr[i].arg2!="" && findg(glob_quad.arr[i].arg2)) 373 | { 374 | 375 | flag2=0; 376 | 377 | 378 | } 379 | if(flag1==0) 380 | { 381 | if(findg(arg1x)==2) 382 | fprintf(fp,"\n\tmovzbl\t%s(%%rip), %%eax",arg1x.c_str()); 383 | else 384 | fprintf(fp,"\n\tmovl\t%s(%%rip), %%eax",arg1x.c_str()); 385 | } 386 | if(flag2==0) 387 | { 388 | if(findg(arg1x)==2) 389 | fprintf(fp,"\n\tmovzbl\t%s(%%rip), %%edx",arg2x.c_str()); 390 | else 391 | fprintf(fp,"\n\tmovl\t%s(%%rip), %%edx",arg2x.c_str()); 392 | } 393 | if(mp_set.find(i)!=mp_set.end()) 394 | { 395 | //Generate Label here 396 | fprintf(fp,"\n.L%d:",mp_set[i]); 397 | } 398 | switch(opx) 399 | { 400 | case Q_PLUS: 401 | if(search(resx)!=NULL && search(resx)->tp_n!=NULL&&search(resx)->tp_n->basetp == tp_char) 402 | { 403 | if(flag1!=0) 404 | fprintf(fp,"\n\tmovzbl\t%d(%%rbp), %%eax",off1); 405 | if(flag2!=0) 406 | fprintf(fp,"\n\tmovzbl\t%d(%%rbp), %%edx",off2); 407 | fprintf(fp,"\n\taddl\t%%edx, %%eax"); 408 | if(flag3!=0) 409 | fprintf(fp,"\n\tmovb\t%%al, %d(%%rbp)",offr); 410 | else 411 | fprintf(fp,"\n\tmovb\t%%al, %s(%%rip)",resx.c_str()); 412 | } 413 | else 414 | { 415 | if(flag1!=0) 416 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%eax",off1); 417 | if(flag2!=0) 418 | {if(arg2x[0]>='0' && arg2x[0]<='9') 419 | fprintf(fp,"\n\tmovl\t$%s, %%edx",arg2x.c_str()); 420 | else 421 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%edx",off2); 422 | } 423 | fprintf(fp,"\n\taddl\t%%edx, %%eax"); 424 | if(flag3!=0) 425 | fprintf(fp,"\n\tmovl\t%%eax, %d(%%rbp)",offr); 426 | else 427 | fprintf(fp,"\n\tmovl\t%%eax, %s(%%rip)",resx.c_str()); 428 | } 429 | break; 430 | case Q_MINUS: 431 | if(search(resx)!=NULL && search(resx)->tp_n!=NULL&&search(resx)->tp_n->basetp == tp_char) 432 | { 433 | if(flag1!=0) 434 | fprintf(fp,"\n\tmovzbl\t%d(%%rbp), %%eax",off1); 435 | if(flag2!=0) 436 | fprintf(fp,"\n\tmovzbl\t%d(%%rbp), %%edx",off2); 437 | fprintf(fp,"\n\tsubl\t%%edx, %%eax"); 438 | if(flag3!=0) 439 | fprintf(fp,"\n\tmovb\t%%al, %d(%%rbp)",offr); 440 | else 441 | fprintf(fp,"\n\tmovb\t%%al, %s(%%rip)",resx.c_str()); 442 | } 443 | else 444 | { 445 | if(flag1!=0) 446 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%eax",off1); 447 | // Direct Number access 448 | if(flag2!=0) 449 | {if(arg2x[0]>='0' && arg2x[0]<='9') 450 | fprintf(fp,"\n\tmovl\t$%s, %%edx",arg2x.c_str()); 451 | else 452 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%edx",off2);} 453 | fprintf(fp,"\n\tsubl\t%%edx, %%eax"); 454 | if(flag3!=0) 455 | fprintf(fp,"\n\tmovl\t%%eax, %d(%%rbp)",offr); 456 | else 457 | fprintf(fp,"\n\tmovl\t%%eax, %s(%%rip)",resx.c_str()); 458 | 459 | } 460 | break; 461 | case Q_MULT: 462 | if(search(resx)!=NULL && search(resx)->tp_n!=NULL&&search(resx)->tp_n->basetp == tp_char) 463 | { 464 | if(flag1!=0) 465 | fprintf(fp,"\n\tmovzbl\t%d(%%rbp), %%eax",off1); 466 | if(flag2!=0) 467 | fprintf(fp,"\n\tmovzbl\t%d(%%rbp), %%edx",off2); 468 | fprintf(fp,"\n\timull\t%%edx, %%eax"); 469 | if(flag3!=0) 470 | fprintf(fp,"\n\tmovb\t%%al, %d(%%rbp)",offr); 471 | else 472 | fprintf(fp,"\n\tmovb\t%%al, %s(%%rip)",resx.c_str()); 473 | } 474 | else 475 | { 476 | if(flag1!=0) 477 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%eax",off1); 478 | if(flag2!=0) 479 | {if(arg2x[0]>='0' && arg2x[0]<='9') 480 | { 481 | fprintf(fp,"\n\tmovl\t$%s, %%ecx",arg2x.c_str()); 482 | fprintf(fp,"\n\timull\t%%ecx, %%eax"); 483 | } 484 | else 485 | fprintf(fp,"\n\timull\t%d(%%rbp), %%eax",off2);} 486 | if(flag3!=0) 487 | fprintf(fp,"\n\tmovl\t%%eax, %d(%%rbp)",offr); 488 | else 489 | fprintf(fp,"\n\tmovl\t%%eax, %s(%%rip)",resx.c_str()); 490 | } 491 | break; 492 | case Q_DIVIDE: 493 | if(search(resx)!=NULL && search(resx)->tp_n!=NULL&&search(resx)->tp_n->basetp == tp_char) 494 | { 495 | if(flag1!=0) 496 | fprintf(fp,"\n\tmovzbl\t%d(%%rbp), %%eax",off1); 497 | fprintf(fp,"\n\tcltd"); 498 | if(flag2!=0) 499 | fprintf(fp,"\n\tidivl\t%d(%%rbp), %%eax",off2); 500 | else 501 | fprintf(fp,"\n\tidivl\t%%edx, %%eax"); 502 | if(flag3!=0) 503 | fprintf(fp,"\n\tmovb\t%%al, %d(%%rbp)",offr); 504 | else 505 | fprintf(fp,"\n\tmovb\t%%al, %s(%%rip)",resx.c_str()); 506 | } 507 | else{ 508 | if(flag1!=0) 509 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%eax",off1); 510 | fprintf(fp,"\n\tcltd"); 511 | if(flag2!=0) 512 | fprintf(fp,"\n\tidivl\t%d(%%rbp), %%eax",off2); 513 | else 514 | fprintf(fp,"\n\tidivl\t%%edx, %%eax"); 515 | if(flag3!=0) 516 | fprintf(fp,"\n\tmovl\t%%eax, %d(%%rbp)",offr); 517 | else 518 | fprintf(fp,"\n\tmovl\t%%eax, %s(%%rip)",resx.c_str()); 519 | } 520 | break; 521 | case Q_MODULO: 522 | if(search(resx)!=NULL && search(resx)->tp_n!=NULL&&search(resx)->tp_n->basetp == tp_char) 523 | { 524 | fprintf(fp,"\n\tmovzbl\t%d(%%rbp), %%eax",off1); 525 | fprintf(fp,"\n\tcltd"); 526 | fprintf(fp,"\n\tidivl\t%d(%%rbp), %%eax",off2); 527 | fprintf(fp,"\n\tmovl\t%%edx, %%eax"); 528 | fprintf(fp,"\n\tmovb\t%%al, %d(%%rbp)",offr); 529 | } 530 | else{ 531 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%eax",off1); 532 | fprintf(fp,"\n\tcltd"); 533 | fprintf(fp,"\n\tidivl\t%d(%%rbp), %%eax",off2); 534 | fprintf(fp,"\n\tmovl\t%%edx, %d(%%rbp)",offr); 535 | } 536 | break; 537 | case Q_UNARY_MINUS: 538 | if(search(resx)!=NULL && search(resx)->tp_n!=NULL&&search(resx)->tp_n->basetp == tp_char) 539 | { 540 | fprintf(fp,"\n\tmovzbl\t%d(%%rbp), %%eax",off1); 541 | fprintf(fp,"\n\tnegl\t%%eax"); 542 | fprintf(fp,"\n\tmovb\t%%al, %d(%%rbp)",offr); 543 | } 544 | else{ 545 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%eax",off1); 546 | fprintf(fp,"\n\tnegl\t%%eax"); 547 | fprintf(fp,"\n\tmovl\t%%eax, %d(%%rbp)",offr); 548 | } 549 | break; 550 | case Q_ASSIGN: 551 | if(arg1x[0]>='0' && arg1x[0]<='9') 552 | { 553 | if(flag1!=0) 554 | fprintf(fp,"\n\tmovl\t$%s, %d(%%rbp)",arg1x.c_str(),offr); 555 | } 556 | else if(arg1x[0] == '\'') 557 | { 558 | //Character 559 | fprintf(fp,"\n\tmovb\t$%d, %d(%%rbp)",(int)arg1x[1],offr); 560 | } 561 | else if(flag1 && search(resx)!=NULL && search(resx)->tp_n!=NULL&&search(resx)->tp_n->basetp == tp_char) 562 | { 563 | fprintf(fp,"\n\tmovzbl\t%d(%%rbp), %%eax",off1); 564 | fprintf(fp,"\n\tmovb\t%%al, %d(%%rbp)",offr); 565 | } 566 | else if(flag1&&search(resx)!=NULL && search(resx)->tp_n!=NULL&&search(resx)->tp_n->basetp == tp_int) 567 | { 568 | if(flag1!=0) 569 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%eax",off1); 570 | fprintf(fp,"\n\tmovl\t%%eax, %d(%%rbp)",offr); 571 | } 572 | else if(search(resx)!=NULL && search(resx)->tp_n!=NULL) 573 | { 574 | fprintf(fp,"\n\tmovq\t%d(%%rbp), %%rax",off1); 575 | fprintf(fp,"\n\tmovq\t%%rax, %d(%%rbp)",offr); 576 | } 577 | else 578 | { 579 | if(flag3!=0) 580 | {fprintf(fp,"\n\tmovl\t%d(%%rbp), %%eax",off1); 581 | fprintf(fp,"\n\tmovl\t%%eax, %d(%%rbp)",offr);} 582 | else 583 | { 584 | fprintf(fp,"\n\tmovl\t%%eax, %s(%%rip)",resx.c_str()); 585 | } 586 | } 587 | break; 588 | case Q_PARAM: 589 | if(resx[0] == '_') 590 | { 591 | //string 592 | char* temp = (char*)resx.c_str(); 593 | fprintf(fp,"\n\tmovq\t$.STR%d,\t%%rdi",atoi(temp+1)); 594 | } 595 | else 596 | { 597 | params_stack.push(resx); 598 | //printf("resx--> %s\n",resx.c_str()); 599 | types_stack.push(search(resx)->tp_n->basetp); 600 | offset_stack.push(offr); 601 | if(search(resx)->isptrarr==true) 602 | { 603 | ptrarr_stack.push(1); 604 | } 605 | else 606 | { 607 | ptrarr_stack.push(0); 608 | } 609 | } 610 | break; 611 | case Q_GOTO: 612 | if(resx!="-1"&& atoi(resx.c_str())<=end_quad) 613 | fprintf(fp,"\n\tjmp .L%d",mp_set[atoi(resx.c_str())]); 614 | else 615 | fprintf(fp,"\n\tjmp\t.LRT%d",ret_count); 616 | break; 617 | case Q_CALL: 618 | add_off=function_call(fp); 619 | fprintf(fp,"\n\tcall\t%s",arg1x.c_str()); 620 | if(resx=="") 621 | { 622 | 623 | } 624 | else if(search(resx)!=NULL && search(resx)->tp_n!=NULL&&search(resx)->tp_n->basetp == tp_int) 625 | { 626 | fprintf(fp,"\n\tmovl\t%%eax, %d(%%rbp)",offr); 627 | } 628 | else if(search(resx)!=NULL && search(resx)->tp_n!=NULL&&search(resx)->tp_n->basetp == tp_char) 629 | { 630 | fprintf(fp,"\n\tmovb\t%%al, %d(%%rbp)",offr); 631 | } 632 | else if(search(resx)!=NULL && search(resx)->tp_n!=NULL) 633 | { 634 | fprintf(fp,"\n\tmovq\t%%rax, %d(%%rbp)",offr); 635 | } 636 | else 637 | { 638 | fprintf(fp,"\n\tmovl\t%%eax, %d(%%rbp)",offr); 639 | } 640 | if(arg1x=="prints") 641 | { 642 | fprintf(fp,"\n\taddq $8 , %%rsp"); 643 | } 644 | else 645 | { 646 | fprintf(fp,"\n\taddq $%d , %%rsp",add_off); 647 | } 648 | break; 649 | case Q_IF_LESS: 650 | if(search(arg1x)!=NULL && search(arg1x)->tp_n!=NULL&&search(arg1x)->tp_n->basetp == tp_char) 651 | { 652 | fprintf(fp,"\n\tmovzbl\t%d(%%rbp), %%eax",off1); 653 | fprintf(fp,"\n\tcmpb\t%d(%%rbp), %%al",off2); 654 | fprintf(fp,"\n\tjl .L%d",mp_set[atoi(resx.c_str())]); 655 | } 656 | else 657 | { 658 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%eax",off1); 659 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%edx",off2); 660 | fprintf(fp,"\n\tcmpl\t%%edx, %%eax"); 661 | fprintf(fp,"\n\tjl .L%d",mp_set[atoi(resx.c_str())]); 662 | } 663 | break; 664 | case Q_IF_LESS_OR_EQUAL: 665 | if(search(arg1x)!=NULL && search(arg1x)->tp_n!=NULL&&search(arg1x)->tp_n->basetp == tp_char) 666 | { 667 | fprintf(fp,"\n\tmovzbl\t%d(%%rbp), %%eax",off1); 668 | fprintf(fp,"\n\tcmpb\t%d(%%rbp), %%al",off2); 669 | fprintf(fp,"\n\tjle .L%d",mp_set[atoi(resx.c_str())]); 670 | } 671 | else 672 | { 673 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%eax",off1); 674 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%edx",off2); 675 | fprintf(fp,"\n\tcmpl\t%%edx, %%eax"); 676 | fprintf(fp,"\n\tjle .L%d",mp_set[atoi(resx.c_str())]); 677 | } 678 | break; 679 | case Q_IF_GREATER: 680 | if(search(arg1x)!=NULL && search(arg1x)->tp_n!=NULL&&search(arg1x)->tp_n->basetp == tp_char) 681 | { 682 | fprintf(fp,"\n\tmovzbl\t%d(%%rbp), %%eax",off1); 683 | fprintf(fp,"\n\tcmpb\t%d(%%rbp), %%al",off2); 684 | fprintf(fp,"\n\tjg .L%d",mp_set[atoi(resx.c_str())]); 685 | } 686 | else 687 | { 688 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%eax",off1); 689 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%edx",off2); 690 | fprintf(fp,"\n\tcmpl\t%%edx, %%eax"); 691 | fprintf(fp,"\n\tjg .L%d",mp_set[atoi(resx.c_str())]); 692 | } 693 | break; 694 | case Q_IF_GREATER_OR_EQUAL: 695 | if(search(arg1x)!=NULL && search(arg1x)->tp_n!=NULL&&search(arg1x)->tp_n->basetp == tp_char) 696 | { 697 | fprintf(fp,"\n\tmovzbl\t%d(%%rbp), %%eax",off1); 698 | fprintf(fp,"\n\tcmpb\t%d(%%rbp), %%al",off2); 699 | fprintf(fp,"\n\tjge .L%d",mp_set[atoi(resx.c_str())]); 700 | } 701 | else 702 | { 703 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%eax",off1); 704 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%edx",off2); 705 | fprintf(fp,"\n\tcmpl\t%%edx, %%eax"); 706 | fprintf(fp,"\n\tjge .L%d",mp_set[atoi(resx.c_str())]); 707 | } 708 | break; 709 | case Q_IF_EQUAL: 710 | if(search(arg1x)!=NULL && search(arg1x)->tp_n!=NULL&&search(arg1x)->tp_n->basetp == tp_char) 711 | { 712 | fprintf(fp,"\n\tmovzbl\t%d(%%rbp), %%eax",off1); 713 | fprintf(fp,"\n\tcmpb\t%d(%%rbp), %%al",off2); 714 | fprintf(fp,"\n\tje .L%d",mp_set[atoi(resx.c_str())]); 715 | } 716 | else 717 | { 718 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%eax",off1); 719 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%edx",off2); 720 | fprintf(fp,"\n\tcmpl\t%%edx, %%eax"); 721 | fprintf(fp,"\n\tje .L%d",mp_set[atoi(resx.c_str())]); 722 | } 723 | break; 724 | case Q_IF_NOT_EQUAL: 725 | if(search(arg1x)!=NULL && search(arg1x)->tp_n!=NULL&&search(arg1x)->tp_n->basetp == tp_char) 726 | { 727 | fprintf(fp,"\n\tmovzbl\t%d(%%rbp), %%eax",off1); 728 | fprintf(fp,"\n\tcmpb\t%d(%%rbp), %%al",off2); 729 | fprintf(fp,"\n\tjne .L%d",mp_set[atoi(resx.c_str())]); 730 | } 731 | else 732 | { 733 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%eax",off1); 734 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%edx",off2); 735 | fprintf(fp,"\n\tcmpl\t%%edx, %%eax"); 736 | fprintf(fp,"\n\tjne .L%d",mp_set[atoi(resx.c_str())]); 737 | } 738 | break; 739 | case Q_ADDR: 740 | fprintf(fp,"\n\tleaq\t%d(%%rbp), %%rax",off1); 741 | fprintf(fp,"\n\tmovq\t%%rax, %d(%%rbp)",offr); 742 | break; 743 | case Q_LDEREF: 744 | fprintf(fp,"\n\tmovq\t%d(%%rbp), %%rax",offr); 745 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%edx",off1); 746 | fprintf(fp,"\n\tmovl\t%%edx, (%%rax)"); 747 | break; 748 | case Q_RDEREF: 749 | fprintf(fp,"\n\tmovq\t%d(%%rbp), %%rax",off1); 750 | fprintf(fp,"\n\tmovl\t(%%rax), %%eax"); 751 | fprintf(fp,"\n\tmovl\t%%eax, %d(%%rbp)",offr); 752 | break; 753 | case Q_RINDEX: 754 | // Get Address, subtract offset, get memory 755 | if(search(arg1x)&&search(arg1x)->isptrarr==true) 756 | { 757 | fprintf(fp,"\n\tmovq\t%d(%%rbp), %%rdx",off1); 758 | fprintf(fp,"\n\tmovslq\t%d(%%rbp), %%rax",off2); 759 | fprintf(fp,"\n\taddq\t%%rax, %%rdx"); 760 | } 761 | else 762 | { 763 | fprintf(fp,"\n\tleaq\t%d(%%rbp), %%rdx",off1); 764 | fprintf(fp,"\n\tmovslq\t%d(%%rbp), %%rax",off2); 765 | fprintf(fp,"\n\taddq\t%%rax, %%rdx"); 766 | } 767 | if(search(resx)!=NULL && search(resx)->tp_n!=NULL&&search(resx)->tp_n->next&&search(resx)->tp_n->next->basetp == tp_char) 768 | { 769 | fprintf(fp,"\n\tmovzbl\t(%%rdx), %%eax"); 770 | fprintf(fp,"\n\tmovb\t%%al, %d(%%rbp)",offr); 771 | } 772 | else 773 | { 774 | fprintf(fp,"\n\tmovl\t(%%rdx), %%eax"); 775 | fprintf(fp,"\n\tmovl\t%%eax, %d(%%rbp)",offr); 776 | } 777 | break; 778 | case Q_LINDEX: 779 | if(search(resx)&&search(resx)->isptrarr==true) 780 | { 781 | fprintf(fp,"\n\tmovq\t%d(%%rbp), %%rdx",offr); 782 | fprintf(fp,"\n\tmovslq\t%d(%%rbp), %%rax",off1); 783 | fprintf(fp,"\n\taddq\t%%rax, %%rdx"); 784 | } 785 | else 786 | { 787 | fprintf(fp,"\n\tleaq\t%d(%%rbp), %%rdx",offr); 788 | fprintf(fp,"\n\tmovslq\t%d(%%rbp), %%rax",off1); 789 | fprintf(fp,"\n\taddq\t%%rax, %%rdx"); 790 | } 791 | if(search(resx)!=NULL && search(resx)->tp_n!=NULL&&search(resx)->tp_n->next && search(resx)->tp_n->next->basetp == tp_char) 792 | { 793 | fprintf(fp,"\n\tmovzbl\t%d(%%rbp), %%eax",off2); 794 | fprintf(fp,"\n\tmovb\t%%al, (%%rdx)"); 795 | } 796 | else 797 | { 798 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%eax",off2); 799 | fprintf(fp,"\n\tmovl\t%%eax, (%%rdx)"); 800 | } 801 | break; 802 | case Q_RETURN: 803 | //printf("return %s\n",resx.c_str()); 804 | if(resx!="") 805 | {if(search(resx)!=NULL && search(resx)->tp_n!=NULL&&search(resx)->tp_n->basetp == tp_char) 806 | { 807 | fprintf(fp,"\n\tmovzbl\t%d(%%rbp), %%eax",offr); 808 | } 809 | else 810 | { 811 | fprintf(fp,"\n\tmovl\t%d(%%rbp), %%eax",offr); 812 | }} 813 | else 814 | { 815 | fprintf(fp,"\n\tmovl\t$0, %%eax"); 816 | } 817 | //printf("Happy\n"); 818 | fprintf(fp,"\n\tjmp\t.LRT%d",ret_count); 819 | break; 820 | default: 821 | break; 822 | } 823 | } 824 | } 825 | 826 | void symtab::function_epilogue(FILE *fp,int count,int ret_count) 827 | { 828 | fprintf(fp,"\n.LRT%d:",ret_count); 829 | fprintf(fp,"\n\taddq\t$%d, %%rsp",offset); 830 | fprintf(fp,"\n\tmovq\t%%rbp, %%rsp"); 831 | fprintf(fp,"\n\tpopq\t%%rbp"); 832 | fprintf(fp,"\n\tret"); 833 | fprintf(fp,"\n.LFE%d:",count); 834 | fprintf(fp,"\n\t.size\t%s, .-%s",name.c_str(),name.c_str()); 835 | } 836 | 837 | -------------------------------------------------------------------------------- /Ass-6/ass6_16CS10053_test1.c: -------------------------------------------------------------------------------- 1 | // Calculate Interest 2 | int main() 3 | 4 | { 5 | int principal_amt, rate, simple_interest; 6 | int time,err; 7 | prints("Enter the integer values of 1) principal_amt, 2) rate 3)time (Answer rounded in integers):\n"); 8 | principal_amt = readi(&err); 9 | rate = readi(&err); 10 | time = readi(&err); 11 | 12 | simple_interest = (principal_amt * rate * time) / 100; 13 | 14 | prints("Simple interest = "); 15 | printi(simple_interest); 16 | prints("\n"); 17 | prints("\n+++++++++++++++++++++++\n"); 18 | return 0; 19 | 20 | } -------------------------------------------------------------------------------- /Ass-6/ass6_16CS10053_test2.c: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | int n, reversedInteger , remainder, originalInteger; 4 | reversedInteger = 0; 5 | prints("++++++++++++ Palindrome or Not ++++++++++++\n"); 6 | prints("Enter an integer: "); 7 | int err; 8 | n = readi(&err); 9 | 10 | originalInteger = n; 11 | while( n!=0 ) 12 | { 13 | remainder = n%10; 14 | reversedInteger = reversedInteger*10 + remainder; 15 | n = n/10; 16 | } 17 | if (originalInteger == reversedInteger) 18 | prints("Yes, It is a palindrome.\n"); 19 | else 20 | prints("No, It is not a palindrome.\n"); 21 | prints("\n+++++++++++++++++++++++++\n"); 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Ass-6/ass6_16CS10053_test3.c: -------------------------------------------------------------------------------- 1 | //Sum of Digits 2 | int sumOfDigits(int x) // Recursive function that will return the sum 3 | { 4 | if((x/10)==0) 5 | { 6 | return x; 7 | } 8 | else 9 | { 10 | return (sumOfDigits(x%10)+sumOfDigits(x/10)); // It will calculate the sum of the digits of the number. 11 | } 12 | } 13 | 14 | int main() 15 | { 16 | int n; 17 | prints("++++++ Sum of Digit ++++++\n"); 18 | prints("Enter Integer\n"); 19 | int err=1; 20 | n=readi(&err); 21 | int s; 22 | s = sumOfDigits(n); 23 | prints("Sum of Digit: "); 24 | printi(s); 25 | prints("\n++++++++++\n"); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Ass-6/ass6_16CS10053_test4.c: -------------------------------------------------------------------------------- 1 | //Longest Common Subseq. 2 | int max(int a, int b) 3 | { 4 | if(a>b) 5 | { 6 | return a; 7 | } 8 | else 9 | { 10 | return b; 11 | } 12 | } 13 | 14 | int lcs( int X[], int Y[], int m, int n ) 15 | { 16 | if (m == 0 || n == 0) 17 | { 18 | return 0; 19 | } 20 | if (X[m-1] == Y[n-1]) 21 | { 22 | //printi(X[m-1]); 23 | //prints("\n"); 24 | return (1 + lcs(X, Y, m-1, n-1)); 25 | } 26 | else 27 | { 28 | return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n)); 29 | } 30 | } 31 | 32 | /* Driver program to test above function */ 33 | int main() 34 | { 35 | int x[100]; 36 | int y[100]; 37 | int n,m,i; 38 | int err=1; 39 | prints("++++++++ Length of longest common subsequence ++++++++\n"); 40 | prints("Enter the number of elements in first array\n"); 41 | n=readi(&err); 42 | prints("Enter the elements of the first array one by one\n"); 43 | for(i=0;i=a[top_2]) 8 | { 9 | c[top++]=a[top_2++]; 10 | } 11 | else 12 | { 13 | c[top++]=a[top_1++]; 14 | } 15 | } 16 | if(top_1==mid+1) 17 | { 18 | for(i=top_2;i<=max;i++) 19 | { 20 | c[top++]=a[i]; 21 | } 22 | } 23 | else 24 | { 25 | for(i=top_1;i<=mid;i++) 26 | { 27 | c[top++]=a[i]; 28 | } 29 | } 30 | for(i=min;i<=max;i++) 31 | { 32 | a[i]=c[i]; 33 | } 34 | return 0; 35 | } 36 | 37 | int merge_sort(int a[],int min,int max) 38 | { 39 | int mid; 40 | if(min vs; 12 | vector cs; 13 | int size_int=4; 14 | int size_double=8; 15 | int size_pointer=8; 16 | int size_char=1; 17 | int size_bool=1; 18 | vector strings_label; 19 | type_n::type_n(types t,int sz,type_n *n) 20 | { 21 | basetp=t; 22 | size=sz; 23 | next=n; 24 | } 25 | 26 | int type_n::getSize() 27 | { 28 | if(this==NULL) 29 | return 0; 30 | //return the size of the array by calling the recursive function 31 | //here we are not checking for null as if it will reach the final type it will enter the below conditions 32 | if((this->basetp) == tp_arr) 33 | return ((this->size)*(this->next->getSize())); 34 | if((this->basetp) == tp_void) 35 | return 0; 36 | if((this->basetp) == tp_int) 37 | return size_int; 38 | if((this->basetp) == tp_double) 39 | return size_double; 40 | if((this->basetp) == tp_bool) 41 | return size_bool; 42 | if((this->basetp) == tp_char) 43 | return size_char; 44 | if((this->basetp) == tp_ptr) 45 | return size_pointer; 46 | } 47 | 48 | types type_n::getBasetp() 49 | { 50 | if(this!=NULL) 51 | return this->basetp; 52 | else 53 | return tp_void; 54 | } 55 | 56 | void type_n::printSize() 57 | { 58 | printf("%d\n",size); 59 | } 60 | 61 | void type_n::print() 62 | { 63 | switch(basetp){ 64 | case tp_void: 65 | printf("Void"); 66 | break; 67 | case tp_bool: 68 | printf("Bool"); 69 | break; 70 | case tp_int: 71 | printf("Int"); 72 | break; 73 | case tp_char: 74 | printf("Char"); 75 | break; 76 | case tp_double: 77 | printf("Double"); 78 | break; 79 | case tp_ptr: 80 | printf("ptr("); 81 | if(this->next!=NULL) 82 | this->next->print(); 83 | printf(")"); 84 | break; 85 | case tp_arr: 86 | printf("array(%d,",size); 87 | if(this->next!=NULL) 88 | this->next->print(); 89 | printf(")"); 90 | break; 91 | case tp_func: 92 | printf("Function()"); 93 | break; 94 | default: 95 | printf("TYPE NOT FOUND\n"); 96 | exit(-1); 97 | } 98 | 99 | } 100 | 101 | array::array(string s,int sz,types t) 102 | { 103 | this->base_arr=s; 104 | this->tp=t; 105 | this->bsize=sz; 106 | this->dimension_size=1; 107 | } 108 | 109 | void array::addindex(int i) 110 | { 111 | this->dimension_size=this->dimension_size+1; 112 | this->dims.push_back(i); 113 | } 114 | 115 | void funct::print() 116 | { 117 | 118 | printf("Funct("); 119 | int i; 120 | for(i=0;i tpls) 130 | { 131 | typelist=tpls; 132 | } 133 | symdata::symdata(string n) 134 | { 135 | name=n; 136 | //printf("sym%s\n",n.c_str()); 137 | size=0; 138 | tp_n=NULL; 139 | offset=-1; 140 | var_type=""; 141 | isInitialized=false; 142 | isFunction=false; 143 | isArray=false; 144 | ispresent=true; 145 | arr=NULL; 146 | fun=NULL; 147 | nest_tab=NULL; 148 | isdone=false; 149 | isptrarr=false; 150 | isGlobal=false; 151 | } 152 | 153 | void symdata::createarray() 154 | { 155 | arr=new array(this->name,this->size,tp_arr); 156 | } 157 | 158 | 159 | symtab::symtab() 160 | { 161 | name=""; 162 | offset=0; 163 | no_params=0; 164 | } 165 | 166 | symtab::~symtab() 167 | { 168 | int i; 169 | for(i=0;itp_n; 172 | type_n *p2; 173 | while(true) 174 | { 175 | if(p1 == NULL) 176 | break; 177 | p2=p1; 178 | p1=p1->next; 179 | delete p2; 180 | } 181 | } 182 | } 183 | int symtab::findg(string n) 184 | { 185 | for(int i=0;ibasetp); 202 | 203 | ret->size = t->size; 204 | ret->basetp = t->basetp; 205 | 206 | ret->next = CopyType(t->next); 207 | return ret; 208 | } 209 | 210 | symdata* symtab::lookup(string n) 211 | { 212 | int i; 213 | //printf("%s-->%s\n",name.c_str(),n.c_str()); 214 | for(i=0;iname == n) 218 | { 219 | return symbol_tab[i]; 220 | } 221 | } 222 | //printf("Flag2\n"); 223 | //printf("k%d\n",symbol_tab.size()); 224 | symdata *temp_o=new symdata(n); 225 | temp_o->i_val.int_val=0; 226 | symbol_tab.push_back(temp_o); 227 | //printf("lol%s\n",temp_o->name.c_str()); 228 | //printf("%d\n",symbol_tab.size()); 229 | return symbol_tab[symbol_tab.size()-1]; 230 | } 231 | 232 | symdata* symtab::lookup_2(string n) 233 | { 234 | int i; 235 | for(i=0;iname == n) 239 | { 240 | return symbol_tab[i]; 241 | } 242 | } 243 | for(i=0;isymbol_tab.size();i++) 244 | { 245 | if(glob_st->symbol_tab[i]->name == n) 246 | { 247 | return glob_st->symbol_tab[i]; 248 | } 249 | } 250 | symdata *temp_o=new symdata(n); 251 | temp_o->i_val.int_val=0; 252 | symbol_tab.push_back(temp_o); 253 | return symbol_tab[symbol_tab.size()-1]; 254 | } 255 | 256 | symdata* symtab::search(string n) 257 | { 258 | int i; 259 | for(i=0;iname==n && symbol_tab[i]->ispresent) 262 | { 263 | return (symbol_tab[i]); 264 | } 265 | } 266 | 267 | return NULL; 268 | } 269 | 270 | symdata* symtab::gentemp(type_n *type) 271 | { 272 | char c[10]; 273 | sprintf(c,"t%03d",temp_count); 274 | temp_count++; 275 | symdata *temp=lookup(c); 276 | int temp_sz; 277 | if(type==NULL) 278 | temp_sz=0; 279 | else if((type->basetp) == tp_void) 280 | temp_sz=0; 281 | else if((type->basetp) == tp_int) 282 | temp_sz=size_int; 283 | else if((type->basetp) == tp_double) 284 | temp_sz=size_double; 285 | else if((type->basetp) == tp_bool) 286 | temp_sz=size_bool; 287 | else if((type->basetp) == tp_char) 288 | temp_sz=size_char; 289 | else if((type->basetp) == tp_ptr) 290 | temp_sz=size_pointer; 291 | else 292 | temp_sz=type->getSize(); 293 | temp->size=temp_sz; 294 | temp->var_type="temp"; 295 | temp->tp_n=type; 296 | temp->offset=this->offset; 297 | this->offset=this->offset+(temp->size); 298 | return temp; 299 | } 300 | 301 | void symtab::update(symdata *sm,type_n *type,basic_val initval,symtab *next) 302 | { 303 | sm->tp_n=CopyType(type); 304 | sm->i_val=initval; 305 | sm->nest_tab=next; 306 | int temp_sz; 307 | if(sm->tp_n==NULL) 308 | temp_sz=0; 309 | else if(((sm->tp_n)->basetp) == tp_void) 310 | temp_sz=0; 311 | else if(((sm->tp_n)->basetp) == tp_int) 312 | temp_sz=size_int; 313 | else if(((sm->tp_n)->basetp) == tp_double) 314 | temp_sz=size_double; 315 | else if(((sm->tp_n)->basetp) == tp_bool) 316 | temp_sz=size_bool; 317 | else if(((sm->tp_n)->basetp) == tp_char) 318 | temp_sz=size_char; 319 | else if(((sm->tp_n)->basetp) == tp_ptr) 320 | temp_sz=size_pointer; 321 | else 322 | temp_sz=sm->tp_n->getSize(); 323 | sm->size=temp_sz; 324 | sm->offset=this->offset; 325 | this->offset=this->offset+(sm->size); 326 | sm->isInitialized=false; 327 | } 328 | 329 | void symtab::print() 330 | { 331 | printf("++++++++++++++++++++++++++++++++++++++++ %s ++++++++++++++++++++++++++++++++++++++++\n",name.c_str()); 332 | printf("Offset = %d\nStart Quad Index = %d\nEnd Quad Index = %d\n",offset,start_quad,end_quad); 333 | printf("Name\tValue\tvar_type\tsize\tOffset\tType\n\n"); 334 | for(int i = 0; i<(symbol_tab).size(); i++) 335 | { 336 | if(symbol_tab[i]->ispresent==false) 337 | continue; 338 | symdata * t = symbol_tab[i]; 339 | printf("%s\t",symbol_tab[i]->name.c_str()); 340 | if(t->isInitialized) 341 | { 342 | if((t->tp_n)->basetp == tp_char) printf("%c\t",(t->i_val).char_val); 343 | else if((t->tp_n)->basetp == tp_int) printf("%d\t",(t->i_val).int_val); 344 | else if((t->tp_n)->basetp == tp_double) printf("%.3lf\t",(t->i_val).double_val); 345 | else printf("----\t"); 346 | } 347 | else 348 | printf("null\t"); 349 | printf("%s",t->var_type.c_str()); 350 | printf("\t\t%d\t%d\t",t->size,t->offset); 351 | if(t->var_type == "func") 352 | printf("ptr-to-St( %s )",t->nest_tab->name.c_str()); 353 | 354 | if(t->tp_n != NULL) 355 | (t->tp_n)->print(); 356 | printf("\n"); 357 | } 358 | printf("\n"); 359 | printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"); 360 | } 361 | list* makelist(int i) 362 | { 363 | list *temp = (list*)malloc(sizeof(list)); 364 | temp->index=i; 365 | temp->next=NULL; 366 | return temp; 367 | } 368 | list* merge(list *lt1,list *lt2) 369 | { 370 | list *temp = (list*)malloc(sizeof(list)); 371 | list *p1=temp; 372 | int flag=0; 373 | list *l1=lt1; 374 | list *l2=lt2; 375 | while(l1!=NULL) 376 | { 377 | flag=1; 378 | p1->index=l1->index; 379 | if(l1->next!=NULL) 380 | { 381 | p1->next=(list*)malloc(sizeof(list)); 382 | p1=p1->next; 383 | } 384 | l1=l1->next; 385 | } 386 | while(l2!=NULL) 387 | { 388 | if(flag==1) 389 | { 390 | p1->next=(list*)malloc(sizeof(list)); 391 | p1=p1->next; 392 | flag=0; 393 | } 394 | p1->index=l2->index; 395 | if(l2->next!=NULL) 396 | { 397 | p1->next=(list*)malloc(sizeof(list)); 398 | p1=p1->next; 399 | } 400 | l2=l2->next; 401 | } 402 | p1->next=NULL; 403 | return temp; 404 | } 405 | 406 | quad::quad(opcode opc,string a1,string a2,string rs) 407 | { 408 | this->op=opc; 409 | this->arg1=a1; 410 | this->result=rs; 411 | this->arg2=a2; 412 | } 413 | 414 | void quad::print_arg() 415 | { 416 | printf("\t%s\t=\t%s\top\t%s\t",result.c_str(),arg1.c_str(),arg2.c_str()); 417 | } 418 | 419 | quad_arr::quad_arr() 420 | { 421 | next_instr=0; 422 | } 423 | 424 | void quad_arr::emit(opcode opc, string arg1, string arg2, string result) 425 | { 426 | if(result.size()!=0) 427 | { 428 | quad new_elem(opc,arg1,arg2,result); 429 | arr.push_back(new_elem); 430 | } 431 | else if(arg2.size()!=0) 432 | { 433 | quad new_elem(opc,arg1,"",arg2); 434 | arr.push_back(new_elem); 435 | } 436 | else if(arg1.size()!=0) 437 | { 438 | quad new_elem(opc,"","",arg1); 439 | arr.push_back(new_elem); 440 | } 441 | else 442 | { 443 | quad new_elem(opc,"","",""); 444 | arr.push_back(new_elem); 445 | } 446 | next_instr=next_instr+1; 447 | } 448 | void quad_arr::emit2(opcode opc,string arg1, string arg2, string result) 449 | { 450 | if(result.size()==0) 451 | { 452 | quad new_elem(opc,arg1,arg2,""); 453 | arr.push_back(new_elem); 454 | } 455 | } 456 | void quad_arr::emit(opcode opc, int val, string operand) 457 | { 458 | char str[20]; 459 | sprintf(str, "%d", val); 460 | if(operand.size()==0) 461 | { 462 | quad new_quad(opc,"","",str); 463 | arr.push_back(new_quad); 464 | } 465 | else 466 | { 467 | quad new_quad(opc,str,"",operand); 468 | arr.push_back(new_quad); 469 | } 470 | next_instr=next_instr+1; 471 | } 472 | 473 | void quad_arr::emit(opcode opc, double val, string operand) 474 | { 475 | char str[20]; 476 | sprintf(str, "%lf", val); 477 | if(operand.size()==0) 478 | { 479 | quad new_quad(opc,"","",str); 480 | arr.push_back(new_quad); 481 | } 482 | else 483 | { 484 | quad new_quad(opc,str,"",operand); 485 | arr.push_back(new_quad); 486 | } 487 | next_instr=next_instr+1; 488 | } 489 | 490 | void quad_arr::emit(opcode opc, char val, string operand) 491 | { 492 | char str[20]; 493 | sprintf(str, "'%c'", val); 494 | if(operand.size()==0) 495 | { 496 | quad new_quad(opc,"","",str); 497 | arr.push_back(new_quad); 498 | } 499 | else 500 | { 501 | quad new_quad(opc,str,"",operand); 502 | arr.push_back(new_quad); 503 | } 504 | next_instr=next_instr+1; 505 | } 506 | 507 | void quad_arr::print() 508 | { 509 | opcode op; 510 | string arg1; 511 | string arg2; 512 | string result; 513 | for(int i=0;i>"); break; 536 | case Q_XOR: printf("^"); break; 537 | case Q_AND: printf("&"); break; 538 | case Q_OR: printf("|"); break; 539 | case Q_LOG_AND: printf("&&"); break; 540 | case Q_LOG_OR: printf("||"); break; 541 | case Q_LESS: printf("<"); break; 542 | case Q_LESS_OR_EQUAL: printf("<="); break; 543 | case Q_GREATER_OR_EQUAL: printf(">="); break; 544 | case Q_GREATER: printf(">"); break; 545 | case Q_EQUAL: printf("=="); break; 546 | case Q_NOT_EQUAL: printf("!="); break; 547 | } 548 | printf(" "); 549 | printf("%s\n",arg2.c_str()); 550 | } 551 | else if(Q_UNARY_MINUS<=op && op<=Q_ASSIGN) 552 | { 553 | printf("%s",result.c_str()); 554 | printf("\t=\t"); 555 | switch(op) 556 | { 557 | 558 | case Q_UNARY_MINUS : printf("-"); break; 559 | case Q_UNARY_PLUS : printf("+"); break; 560 | case Q_COMPLEMENT : printf("~"); break; 561 | case Q_NOT : printf("!"); break; 562 | //Copy Assignment Instruction 563 | case Q_ASSIGN : break; 564 | } 565 | printf("%s\n",arg1.c_str()); 566 | } 567 | else if(op == Q_GOTO){printf("goto ");printf("%s\n",result.c_str());} 568 | else if(Q_IF_EQUAL<=op && op<=Q_IF_GREATER_OR_EQUAL) 569 | { 570 | printf("if ");printf("%s",arg1.c_str());printf(" "); 571 | switch(op) 572 | { 573 | //Conditional Jump 574 | case Q_IF_LESS : printf("<"); break; 575 | case Q_IF_GREATER : printf(">"); break; 576 | case Q_IF_LESS_OR_EQUAL : printf("<="); break; 577 | case Q_IF_GREATER_OR_EQUAL : printf(">="); break; 578 | case Q_IF_EQUAL : printf("=="); break; 579 | case Q_IF_NOT_EQUAL : printf("!="); break; 580 | case Q_IF_EXPRESSION : printf("!= 0"); break; 581 | case Q_IF_NOT_EXPRESSION : printf("== 0"); break; 582 | } 583 | printf("%s",arg2.c_str());printf("\tgoto ");printf("%s\n",result.c_str()); 584 | } 585 | else if(Q_CHAR2INT<=op && op<=Q_DOUBLE2INT) 586 | { 587 | printf("%s",result.c_str());printf("\t=\t"); 588 | switch(op) 589 | { 590 | case Q_CHAR2INT : printf(" Char2Int(");printf("%s",arg1.c_str());printf(")\n"); break; 591 | case Q_CHAR2DOUBLE : printf(" Char2Double(");printf("%s",arg1.c_str());printf(")\n"); break; 592 | case Q_INT2CHAR : printf(" Int2Char(");printf("%s",arg1.c_str());printf(")\n"); break; 593 | case Q_DOUBLE2CHAR : printf(" Double2Char(");printf("%s",arg1.c_str());printf(")\n"); break; 594 | case Q_INT2DOUBLE : printf(" Int2Double(");printf("%s",arg1.c_str());printf(")\n"); break; 595 | case Q_DOUBLE2INT : printf(" Double2Int(");printf("%s",arg1.c_str());printf(")\n"); break; 596 | } 597 | } 598 | else if(op == Q_PARAM) 599 | { 600 | printf("param\t");printf("%s\n",result.c_str()); 601 | } 602 | else if(op == Q_CALL) 603 | { 604 | if(!result.c_str()) 605 | printf("call %s, %s\n", arg1.c_str(), arg2.c_str()); 606 | else if(result.size()==0) 607 | { 608 | printf("call %s, %s\n", arg1.c_str(), arg2.c_str()); 609 | } 610 | else 611 | printf("%s\t=\tcall %s, %s\n", result.c_str(), arg1.c_str(), arg2.c_str()); 612 | } 613 | else if(op == Q_RETURN) 614 | { 615 | printf("return\t");printf("%s\n",result.c_str()); 616 | } 617 | else if( op == Q_RINDEX) 618 | { 619 | printf("%s\t=\t%s[%s]\n", result.c_str(), arg1.c_str(), arg2.c_str()); 620 | } 621 | else if(op == Q_LINDEX) 622 | { 623 | printf("%s[%s]\t=\t%s\n", result.c_str(), arg1.c_str(), arg2.c_str()); 624 | } 625 | else if(op == Q_LDEREF) 626 | { 627 | printf("*%s\t=\t%s\n", result.c_str(), arg1.c_str()); 628 | } 629 | else if(op == Q_RDEREF) 630 | { 631 | printf("%s\t=\t* %s\n", result.c_str(), arg1.c_str()); 632 | } 633 | else if(op == Q_ADDR) 634 | { 635 | printf("%s\t=\t& %s\n", result.c_str(), arg1.c_str()); 636 | } 637 | } 638 | } 639 | 640 | void backpatch(list *l,int i) 641 | { 642 | list *temp =l; 643 | list *temp2; 644 | char str[10]; 645 | sprintf(str,"%d",i); 646 | while(temp!=NULL) 647 | { 648 | glob_quad.arr[temp->index].result = str; 649 | temp2=temp; 650 | temp=temp->next; 651 | free(temp2); 652 | } 653 | } 654 | 655 | void typecheck(expresn *e1,expresn *e2,bool isAssign) 656 | { 657 | types type1,type2; 658 | //if(e2->type) 659 | if(e1->type==NULL) 660 | { 661 | e1->type = CopyType(e2->type); 662 | } 663 | else if(e2->type==NULL) 664 | { 665 | e2->type =CopyType(e1->type); 666 | } 667 | type1=(e1->type)->basetp; 668 | type2=(e2->type)->basetp; 669 | if(type1==type2) 670 | { 671 | return; 672 | } 673 | if(!isAssign) 674 | { 675 | if(type1>type2) 676 | { 677 | symdata *temp = curr_st->gentemp(e1->type); 678 | if(type1 == tp_int && type2 == tp_char) 679 | glob_quad.emit(Q_CHAR2INT, e2->loc->name, temp->name); 680 | else if(type1 == tp_double && type2 == tp_int) 681 | glob_quad.emit(Q_INT2DOUBLE, e2->loc->name, temp->name); 682 | e2->loc = temp; 683 | e2->type = temp->tp_n; 684 | } 685 | else 686 | { 687 | symdata *temp = curr_st->gentemp(e2->type); 688 | if(type2 == tp_int && type1 == tp_char) 689 | glob_quad.emit(Q_CHAR2INT, e1->loc->name, temp->name); 690 | else if(type2 == tp_double && type1 == tp_int) 691 | glob_quad.emit(Q_INT2DOUBLE, e1->loc->name, temp->name); 692 | e1->loc = temp; 693 | e1->type = temp->tp_n; 694 | } 695 | } 696 | else 697 | { 698 | symdata *temp = curr_st->gentemp(e1->type); 699 | if(type1 == tp_int && type2 == tp_double) 700 | glob_quad.emit(Q_DOUBLE2INT, e2->loc->name, temp->name); 701 | else if(type1 == tp_double && type2 == tp_int) 702 | glob_quad.emit(Q_INT2DOUBLE, e2->loc->name, temp->name); 703 | else if(type1 == tp_char && type2 == tp_int) 704 | glob_quad.emit(Q_INT2CHAR, e2->loc->name, temp->name); 705 | else if(type1 == tp_int && type2 == tp_char) 706 | glob_quad.emit(Q_CHAR2INT, e2->loc->name, temp->name); 707 | else 708 | { 709 | printf("%s %s Types compatibility not defined\n",e1->loc->name.c_str(),e2->loc->name.c_str()); 710 | exit(-1); 711 | } 712 | e2->loc = temp; 713 | e2->type = temp->tp_n; 714 | } 715 | } 716 | 717 | void print_list(list *root) 718 | { 719 | int flag=0; 720 | while(root!=NULL) 721 | { 722 | printf("%d ",root->index); 723 | flag=1; 724 | root=root->next; 725 | } 726 | if(flag==0) 727 | { 728 | printf("Empty List\n"); 729 | } 730 | else 731 | { 732 | printf("\n"); 733 | } 734 | } 735 | void conv2Bool(expresn *e) 736 | { 737 | if((e->type)->basetp!=tp_bool) 738 | { 739 | (e->type) = new type_n(tp_bool); 740 | e->falselist=makelist(next_instr); 741 | glob_quad.emit(Q_IF_EQUAL,e->loc->name,"0","-1"); 742 | e->truelist = makelist(next_instr); 743 | glob_quad.emit(Q_GOTO,-1); 744 | } 745 | } 746 | 747 | int main() 748 | { 749 | symdata *temp_printi=new symdata("printi"); 750 | temp_printi->tp_n=new type_n(tp_int); 751 | temp_printi->var_type="func"; 752 | temp_printi->nest_tab=glob_st; 753 | glob_st->symbol_tab.push_back(temp_printi); 754 | 755 | symdata *temp_readi=new symdata("readi"); 756 | temp_readi->tp_n=new type_n(tp_int); 757 | temp_readi->var_type="func"; 758 | temp_readi->nest_tab=glob_st; 759 | glob_st->symbol_tab.push_back(temp_readi); 760 | 761 | symdata *temp_prints=new symdata("prints"); 762 | temp_prints->tp_n=new type_n(tp_int); 763 | temp_prints->var_type="func"; 764 | temp_prints->nest_tab=glob_st; 765 | glob_st->symbol_tab.push_back(temp_prints); 766 | yyparse(); 767 | glob_st->name="Global"; 768 | printf("=============================================================================="); 769 | printf("\nGenerated Quads for the program\n"); 770 | glob_quad.print(); 771 | printf("==============================================================================\n"); 772 | printf("Symbol table Maintained For the Given Program\n"); 773 | glob_st->print(); 774 | set setty; 775 | setty.insert("Global"); 776 | printf("=============================================================================\n"); 777 | FILE *fp; 778 | fp = fopen("output.s","w"); 779 | fprintf(fp,"\t.file\t\"output.s\"\n"); 780 | for (int i = 0; i < strings_label.size(); ++i) 781 | { 782 | fprintf(fp,"\n.STR%d:\t.string %s",i,strings_label[i].c_str()); 783 | } 784 | setsetty_1; 785 | glob_st->mark_labels(); 786 | glob_st->global_variables(fp); 787 | setty_1.insert("Global"); 788 | int count_l=0; 789 | for (int i = 0; i < glob_st->symbol_tab.size(); ++i) 790 | { 791 | if(((glob_st->symbol_tab[i])->nest_tab)!=NULL) 792 | { 793 | if(setty_1.find(((glob_st->symbol_tab[i])->nest_tab)->name)==setty_1.end()) 794 | { 795 | glob_st->symbol_tab[i]->nest_tab->assign_offset(); 796 | glob_st->symbol_tab[i]->nest_tab->print(); 797 | glob_st->symbol_tab[i]->nest_tab->function_prologue(fp,count_l); 798 | glob_st->symbol_tab[i]->nest_tab->function_restore(fp); 799 | glob_st->symbol_tab[i]->nest_tab->gen_internal_code(fp,count_l); 800 | setty_1.insert(((glob_st->symbol_tab[i])->nest_tab)->name); 801 | glob_st->symbol_tab[i]->nest_tab->function_epilogue(fp,count_l,count_l); 802 | count_l++; 803 | } 804 | } 805 | } 806 | fprintf(fp,"\n"); 807 | return 0; 808 | } 809 | -------------------------------------------------------------------------------- /Ass-6/ass6_16CS10053_translator.h: -------------------------------------------------------------------------------- 1 | #ifndef ASS6_16CS10053_TRANSLATOR_H 2 | #define ASS6_16CS10053_TRANSLATOR_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | extern map mp_set; 13 | extern int size_int; 14 | extern int size_double; 15 | extern int size_pointer; 16 | extern int size_char; 17 | extern int size_bool; 18 | extern stack params_stack; 19 | extern stack types_stack; 20 | extern stack offset_stack; 21 | extern stack ptrarr_stack; 22 | extern vector strings_label; 23 | class type_n; 24 | class expresn; 25 | class quad; 26 | class symdata; 27 | class symtab; 28 | class quad_arr; 29 | class funct; 30 | class array; 31 | //void totest(string x);//to test 32 | struct decStr; 33 | struct idStr; 34 | struct expresn; 35 | struct arglistStr; 36 | extern type_n *glob_type; 37 | extern int glob_width; 38 | extern int next_instr; //next instr for use in quads and in different function like backpatch 39 | extern int temp_count; // count of the temporary varibles to name the new temporary variable 40 | extern symtab *glob_st; //Global symbol table pointer 41 | extern symtab *curr_st; //Current Symbol table pointer 42 | extern quad_arr glob_quad; //to store all the quads that will be generated by the grammar 43 | 44 | enum types{ 45 | tp_void=0,tp_bool,tp_char,tp_int,tp_double,tp_ptr,tp_arr,tp_func 46 | }; 47 | 48 | typedef struct list{ 49 | int index; 50 | struct list *next; 51 | }list; 52 | 53 | enum opcode{ 54 | 55 | //Binary Assignment Operator 56 | Q_PLUS=1,Q_MINUS,Q_MULT,Q_DIVIDE,Q_MODULO,Q_LEFT_OP,Q_RIGHT_OP, 57 | Q_XOR,Q_AND,Q_OR,Q_LOG_AND,Q_LOG_OR,Q_LESS,Q_LESS_OR_EQUAL, 58 | Q_GREATER_OR_EQUAL,Q_GREATER,Q_EQUAL,Q_NOT_EQUAL, 59 | 60 | //Unary Assignment Operator 61 | Q_UNARY_MINUS,Q_UNARY_PLUS,Q_COMPLEMENT,Q_NOT, 62 | 63 | //Copy Assignment 64 | Q_ASSIGN, 65 | 66 | Q_GOTO, 67 | 68 | //Conditional Jump 69 | Q_IF_EQUAL,Q_IF_NOT_EQUAL,Q_IF_EXPRESSION,Q_IF_NOT_EXPRESSION, 70 | Q_IF_LESS,Q_IF_GREATER,Q_IF_LESS_OR_EQUAL,Q_IF_GREATER_OR_EQUAL, 71 | 72 | //Type Conversions 73 | Q_CHAR2INT,Q_CHAR2DOUBLE,Q_INT2CHAR,Q_DOUBLE2CHAR,Q_INT2DOUBLE,Q_DOUBLE2INT, 74 | 75 | 76 | //Procedure Call 77 | Q_PARAM,Q_CALL,Q_RETURN, 78 | 79 | //Pointer Assignment Operator 80 | Q_LDEREF,Q_RDEREF, 81 | Q_ADDR, 82 | 83 | //Array Indexing 84 | Q_RINDEX, 85 | Q_LINDEX, 86 | 87 | }; 88 | 89 | //it is the basic type that an element can have 90 | union basic_val{ 91 | int int_val; 92 | double double_val; 93 | char char_val; 94 | }; 95 | 96 | class type_n{ 97 | public: 98 | int size; // to save the size of the type 99 | types basetp; // to save the basic type of the elemnt 100 | type_n *next; // to save next type_n type for arrays 101 | type_n(types t,int sz=1, type_n *n=NULL); //constuctor 102 | int getSize(); //returns the size 103 | types getBasetp(); //return Base type 104 | void printSize(); //to print the size 105 | void print(); 106 | }; 107 | 108 | type_n *CopyType(type_n *t); 109 | 110 | class array 111 | { 112 | public: 113 | 114 | /* Stores the array base and the variable containing array offset */ 115 | string base_arr; 116 | types tp; 117 | /* Initiates array name, offset and array type */ 118 | array(string s,int sz,types t); 119 | /* Stores array dimensions */ 120 | vector dims; 121 | 122 | /* Size of base type */ 123 | int bsize; 124 | 125 | /* Number of dimensions */ 126 | int dimension_size; 127 | 128 | /* Adds an array index for array accessing */ 129 | void addindex(int i); 130 | 131 | }; 132 | 133 | 134 | 135 | /* To store details of functions, its parameters and return type */ 136 | class funct 137 | { 138 | public: 139 | /* Parameter type list */ 140 | vector typelist; 141 | 142 | /* Return type */ 143 | type_n *rettype; 144 | 145 | funct(vector tpls); 146 | /* Prints details in suitable format */ 147 | void print(); 148 | }; 149 | 150 | 151 | //class which will be used as building element for symbol table 152 | class symdata{ 153 | public: 154 | string name; 155 | int size; 156 | int offset; 157 | basic_val i_val;//to store the initialized value for an element stored at symbol table 158 | type_n *tp_n;//for storing the type of element 159 | symtab *nest_tab; //to store the pointer to the symbol table to which the current element belongs to 160 | array *arr;//to store the pointer to an array if its an array type 161 | funct *fun;//to store the pointer to a function if its an function 162 | void createarray(); 163 | string var_type;//to store whether the varaible is "null=0" "local=1" "param=2" "func=3" "ret=4" "temporary=5" 164 | bool isInitialized; //If the value of element is initialized or not 165 | bool isFunction; //to know whether the current element is function like function pointer 166 | bool isArray; // to know whether the current element is ab array it helps if we have been in grammar tree 167 | symdata(string n=""); //name is initialized to null that will be used for naming temporary variables 168 | bool ispresent; 169 | bool isdone; 170 | bool isptrarr; 171 | bool isGlobal; 172 | }; 173 | 174 | class symtab{ 175 | public: 176 | string name; // name of the symbol 177 | int offset; // final offset of this symbol table that will be used in the update function 178 | int start_quad; 179 | int end_quad; 180 | vector symbol_tab; //maintaining a list of symbol tables 181 | symtab(); //constructor 182 | ~symtab(); //destructor 183 | symdata* lookup(string n);// Lookup function searches the variable with name. If the variable is present then returns its pointer location else creates a new entry with its name and returns that pointer 184 | symdata* lookup_2(string n);//To handle global variables 185 | symdata* search(string n); //it searches for the variable and returns the oiter to it if present 186 | symdata* gentemp(type_n *type); //gentemp creates a new element in the symbol table with the type provided at the time of constructing 187 | void update(symdata *loc,type_n *type,basic_val initval, symtab *next = NULL);// 188 | void print(); 189 | int no_params; 190 | void mark_labels(); 191 | void function_prologue(FILE *fp,int count); 192 | void global_variables(FILE *fp); 193 | void gen_internal_code(FILE *fp,int ret_count); 194 | int function_call(FILE *fp); 195 | void function_epilogue(FILE *fp,int count,int ret_count); 196 | string assign_reg(int type_of,int no); 197 | void assign_offset(); 198 | void function_restore(FILE *fp); 199 | int findg(string n); 200 | }; 201 | 202 | struct expresn{ 203 | symdata* loc; 204 | type_n* type; 205 | list* truelist; 206 | list* falselist; 207 | bool isPointer; 208 | bool isArray; 209 | bool isString; 210 | int ind_str; 211 | symdata *arr; 212 | symdata *poss_array; 213 | }; 214 | list* makelist(int i); 215 | list* merge(list *l1,list *l2); 216 | void backpatch(list *l,int i); //to fill the dangling list of goto's l1 to i 217 | void conv2Bool(expresn *e); //to convert the given exprssion type to bool mostly used in relational operator 218 | void typecheck(expresn *e1,expresn *e2,bool isAss = false); 219 | void print_list(list *root); 220 | // struct for declaration grammar 221 | struct decStr 222 | { 223 | type_n *type; // type of the current declaration 224 | int width; // width of the variable 225 | }; 226 | 227 | class quad{ 228 | public: 229 | string arg1,result,arg2; //consist of three elements 230 | opcode op; 231 | void print_arg(); 232 | quad(opcode,string,string,string); //constructorparameters 233 | }; 234 | 235 | struct arglistStr 236 | { 237 | vector *arguments; // A simple vector is used to store the locations of all seen arguments 238 | }; 239 | 240 | // struct for a identifier 241 | struct idStr 242 | { 243 | symdata *loc; // pointer to the symboltable 244 | string *name; // name of the identifier 245 | }; 246 | 247 | struct strstr{ 248 | type_n lop; 249 | string name; 250 | }; 251 | 252 | class quad_arr{ 253 | public: 254 | vector arr;//to store the list of quads 255 | quad_arr(); 256 | void emit(opcode opc, string arg1="", string arg2="", string result=""); //emit used for operations with 3 inputs 257 | void emit(opcode opc, int val, string operand=""); // emit for assignment of integer 258 | void emit(opcode opc, double val, string operand=""); //emit for assignment of double 259 | void emit(opcode opc, char val, string operand=""); //emit for assignment of char 260 | void emit2(opcode opc,string arg1="", string arg2="", string result=""); 261 | void print(); //print all the quads 262 | }; 263 | 264 | #endif 265 | -------------------------------------------------------------------------------- /Ass-6/myl.h: -------------------------------------------------------------------------------- 1 | #ifndef _MYL_H 2 | #define _MYL_H 3 | #define ERR 1 4 | #define OK 0 5 | int prints(char *); 6 | int printi(int); 7 | int readi(int *eP); // *eP is for error, if the input is not an integer 8 | #endif 9 | 10 | 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Tiny C Compiler 2 | 3 | ### Assigments 4 | * Assignment - 1: Annotating Assembly 5 | * Assignment - 2: Creating Library 6 | * Assignment - 3: Lexer for tinyC 7 | * Assignment - 4: Parser for tinyC 8 | * Assignment - 5: Machine Independent Code Generator for tinyC 9 | * Assignment - 6: Target Code Generator for tinyC 10 | 11 | ### Author 12 | [Vedic Partap](http://cse.iitkgp.ac.in/~vedicp/) 13 | 14 | For any queries contact me at 15 | --------------------------------------------------------------------------------