Posts

CS6612 COMPILER LABORATORY SYLLABUS

LIST OF EXPERIMENTS: 1. Implementation of Symbol Table 2. Develop a lexical analyzer to recognize a few patterns in C. (Ex. identifiers, constants, comments, operators etc.) 3. Implementation of Lexical Analyzer using  Lex Tool 4. Generate YACC specification for a few syntactic categories. a) Program to recognize a valid arithmetic expression that uses operator +, - , * and /. b) Program to recognize a valid variable which starts with a letter followed by any  number of letters or digits. d)Implementation of Calculator using LEX and YACC 5. Convert the BNF rules into Yacc form and write code to generate Abstract Syntax Tree. 6. Implement type checking 7. Implement control flow analysis and Data flow Analysis 8. Implement any one storage allocation strategies(Heap,Stack,Static) 9. Construction of DAG 10. Implement the back end of the compiler which takes the three address code and produces the 8086 assembly language instructions that can be assembled and run us...

CONSTANT FOLDING

PROGRAM CODE : // Code Optmization for constant folding #include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> struct ConstFold { char New_Str[10]; char str[10]; } Opt_Data[20]; void ReadInput(char Buffer[],FILE *Out_file); int Gen_Token(char str[],char Tokens[][10]); int New_Index=0; int main() { FILE *In_file,*Out_file; char Buffer[100],ch; int i=0; In_file=fopen("code.txt","r"); Out_file=fopen("output.txt","w"); while(1) { ch= fgetc(In_file); i=0; while(1) { if(ch=='\n') break; Buffer[i++] = ch; ch = fgetc(In_file); if(ch==EOF) break; } if(ch==EOF) break; Buffer[i]='\0'; ReadInput(Buffer,Out_file); } return 0; fclose(In_file); fclose(Out_file); } void ReadInput(char Buffer[],FILE *Out_file) { char temp[100],Token[10][10]; int n,i,j,flag=0; ...

IMPLEMENT DATAFLOW AND CONTROL FLOW ANALYSIS

Image
PROGRAM CODE: // Data Flow Analysis implementation #include<stdio.h> #include<string.h> #include<ctype.h> void input(); void output(); void change(int p,int q,char *res); void constant(); void expression(); struct expr { char op[2],op1[5],op2[5],res[5]; int flag; }arr[10]; int n; int main() { int ch=0; input(); constant(); expression(); output(); } void input() { int i; printf("\n\nEnter the maximum number of expressions:"); scanf("%d",&n); printf("\nEnter the input : \n"); for(i=0;i<n;i++) { scanf("%s",arr[i].op); scanf("%s",arr[i].op1); scanf("%s",arr[i].op2); scanf("%s",arr[i].res); arr[i].flag=0; } } void constant() { int i; int op1,op2,res; char op,res1[5]; for(i=0;i<n;i++) { if(isdigit(arr[i].op1[0]) && isdigit(arr[i].op2[0])) { op...

CONSTRUCTION OF DAG

Image
PROGRAM CODE: // Construction of DAG #include<stdio.h> #include<stdlib.h> #include<time.h> #define MIN_PER_RANK 1 #define MAX_PER_RANK 5 #define MIN_RANKS 3 #define MAX_RANKS 5 #define PERCENT 30 void main() { int i,j,k,nodes=0; srand(time(NULL)); int ranks=MIN_RANKS+(rand()%(MAX_RANKS-MIN_RANKS+1)); printf("DIRECTED ACYCLIC GRAPH\n"); for(i=1;i<ranks;i++) { int new_nodes=MIN_PER_RANK+(rand()%(MAX_PER_RANK-MIN_PER_RANK+1)); for(j=0;j<nodes;j++) for(k=0;k<new_nodes;k++) if((rand()%100)<PERCENT) printf("%d->%d;\n",j,k+nodes); nodes+=new_nodes; } } OUTPUT:

CODE OPTIMIZATION TECHNIQUE

Image
AIM:          To write a program for implementation of Code Optimization Technique. ALGORITHM:   Step1: Generate the program for factorial program using for and do-while loop to specify optimization technique.   Step2: In for loop variable initialization is activated first and the condition is checked next. If the condition is true the corresponding statements are executed and specified increment / decrement operation is performed.   Step3:   The for loop operation is activated till the condition failure.   Step4:   In do-while loop the variable is initialized and the statements are executed then the condition checking and increment / decrement operation is performed.   Step5:   When comparing both for and do-while loop for optimization dowhile is best because first the statement execution is done then only the condition is checked. So, during the statement execution itself we can ...

IMPLEMENT ANY ONE STORAGE ALLOCATION STRATEGIES

Image
AIM:     To implement Stack storage allocation strategies using C program. ALGORITHM: Step1: Initially check whether the stack is empty Step2: Insert an element into the stack using push operation Step3: Insert more elements onto the stack until stack becomes full Step4: Delete an element from the stack using pop operation Step5: Display the elements in the stack   Step6: Top the stack element will be displayed PROGRAM CODE: // implementation of heap allocation storage strategies// #include<stdio.h> #include<stdlib.h> #define TRUE 1 #define FALSE 0 typedef struct Heap { int data; struct Heap *next; } node; node *create(); void main() { int choice,val; char ans; node *head; void display(node *); node *search(node *,int); node *insert(node *); void dele(node **); head=NULL; do { printf("\nprogram to ...