EX 1: TOKEN SEP
#include <stdio.h> #include <string.h> #include <ctype.h> char *keywords[] = {"auto","double","int","struct","break","else","long","switch","case","enum","register","typedef", "char","extern","return","union","continue","for","signed","void","do","if","static","while","default","goto", "sizeof","volatile","const","float","short","unsigned"}; char *operators[] = {"+","-","","/","%","++","--","=","==","!=","<",">",">=","<=","&&","||","!","&","|","^","~", ">>","<<","+=","-=","*=","/=","{","}","[","]",")","("}; int isKeyword(char *w) { for (int i = 0; i < 32; i++) if (!strcmp(w, keywords[i])) return 1; return 0; } int isOperator(char *w) { for (int i = 0; i < 34; i++) if (!strcmp(w, operators[i])) return 1; return 0; } int main() { char str[200], tok[50]; int i = 0, j = 0; printf("Enter code: "); fgets(str, sizeof(str), stdin); while (str[i]) { if (isspace(str[i]) || str[i] == ',' || str[i] == ';') { tok[j] = '\0'; if (j) { if (isKeyword(tok)) printf("%s is a Keyword\n", tok); else { int num = 1; for (int k = 0; tok[k]; k++) if (!isdigit(tok[k])) num = 0; printf("%s is an %s\n", tok, num ? "Integer" : "Identifier"); } j = 0; } if (str[i] == ',') printf(", is a Separator\n"); if (str[i] == ';') printf("; is a Delimiter\n"); } else if (ispunct(str[i])) { tok[j] = '\0'; if (j) { if (isKeyword(tok)) printf("%s is a Keyword\n", tok); else printf("%s is an Identifier\n", tok); j = 0; } char op[3] = {str[i], str[i+1], '\0'}; if (isOperator(op)) { printf("%s is an Operator\n", op); i++; } else { op[1] = '\0'; if (isOperator(op)) printf("%s is an Operator\n", op); } } else tok[j++] = str[i]; i++; } return 0; }
EXP 2: LEX ANALYSER AND SYMBOL TABLE
#include <stdio.h> #include <string.h> #include <ctype.h> char *keys[] = {"int", "float", "char"}; struct Sym { int addr, size; char var[10], type[10]; } tab[10]; int main() { char code[100], *t; int i, a = 1000, k = 0; fgets(code, 100, stdin); t = strtok(code, " ,;=\n"); while (t) { for (i = 0; i < 3; i++) if (!strcmp(t, keys[i])) { t = strtok(NULL, " ,;=\n"); strcpy(tab[k].var, t); strcpy(tab[k].type, keys[i]); tab[k].size = (i==0)?2:(i==1)?4:1; tab[k++].addr = a; a += tab[k-1].size; break; } if (i == 3) printf("%s: %s\n", t, isdigit(t[0]) ? "Number" : "ID"); t = strtok(NULL, " ,;=\n"); } printf("\nAddr Var Type Size\n"); for (i = 0; i < k; i++) printf("%d %s %s %d\n", tab[i].addr, tab[i].var, tab[i].type, tab[i].size); }
EXP 4: E CLOSURE
#include <stdio.h> int main() { int n, k, val, end[10]; char str[30], arr[10][10], ecl[10][10]; // Input Regular Expression (Not used in logic) printf("Enter the Regular Expression: "); gets(str); // Input number of states printf("Enter the Number of States: "); scanf("%d", &n); // Input the transition matrix printf("Enter the Transitions (enter 'e' for epsilon):\n"); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { printf("From state %d to %d: ", i, j); scanf(" %c", &arr[i][j]); } // Step 1: Direct epsilon closure (self + direct 'e' transitions) for (int i = 0; i < n; i++) { k = 0; ecl[i][k++] = i + '0'; // include self for (int j = 0; j < n; j++) if (arr[i][j] == 'e') ecl[i][k++] = j + '0'; // direct epsilon transitions end[i] = k; // store count of elements in closure } // Step 2: Transitive epsilon closure (include indirect 'e' transitions) for (int i = 0; i < n; i++) for (int j = 1; j < end[i]; j++) { val = ecl[i][j] - '0'; // state number for (int m = 1; m < end[val]; m++) { int found = 0; for (int d = 0; d < end[i]; d++) if (ecl[i][d] == ecl[val][m]) found = 1; if (!found) ecl[i][end[i]++] = ecl[val][m]; } } // Output the epsilon closures printf("\nEpsilon Closures:\n"); for (int i = 0; i < n; i++) { printf("E(%d) = { ", i); for (int j = 0; j < end[i]; j++) printf("%c ", ecl[i][j]); printf("}\n"); } return 0; }
3 ADDRESS CODE
#include <stdio.h> #include <string.h> #include <stdlib.h> int addr = 100; char exp[20], id1[5], op[5], id2[5]; int main() { int ch, i, l; while (1) { printf("\n1.Assignment\n2.Arithmetic\n3.Relational\n4.Exit\nChoice: "); scanf("%d", &ch); if (ch == 4) break; if (ch == 1) { scanf("%s", exp); for (i = 0; exp[i] != '='; i++); exp[i] = '\0'; printf("temp=%s\n%s=temp\n", exp + i + 1, exp); } else if (ch == 2) { scanf("%s", exp); l = strlen(exp); for (i = 0; i < l; i++) { if (strchr("+-*/", exp[i])) { printf("temp=%c%c%c\n", exp[i - 1], exp[i], exp[i + 1]); break; } } } else if (ch == 3) { scanf("%s%s%s", id1, op, id2); printf("%d if %s%s%s goto %d\n%d T:=0\n%d goto %d\n%d T:=1\n", addr, id1, op, id2, addr+3, addr+1, addr+2, addr+4, addr+3); addr += 5; } } return 0; }
Code Generation
#include <stdio.h> #include <string.h> int main() { char icode[10][20], stk[20], op1[10]; int i = 0; int temp_reg = 0; // Input phase do { scanf("%s", icode[i]); } while (strcmp(icode[i++], "exit") != 0); printf("\nTarget code generation:\n"); i = 0; // Code generation while (strcmp(icode[i], "exit") != 0) { strcpy(stk, icode[i]); char result = stk[0]; // variable (e.g., 'a' in a=b+c) char operand1 = stk[2]; // first operand (e.g., 'b') char operator = stk[3]; // operator (e.g., '+') char operand2 = stk[4]; // second operand (e.g., 'c') // Map operator to instruction switch (operator) { case '+': strcpy(op1, "ADD"); break; case '-': strcpy(op1, "SUB"); break; case '*': strcpy(op1, "MUL"); break; case '/': strcpy(op1, "DIV"); break; default: strcpy(op1, ""); break; } // Generate code only for valid operators if (strlen(op1) > 0) { printf("\tLDAR\tR%d, %c\n", temp_reg, operand1); printf("\tLDAR\tR%d, %c\n", temp_reg + 1, operand2); printf("\t%s\tR%d, R%d\n", op1, temp_reg, temp_reg + 1); printf("\tSTAR\t%c, R%d\n", result, temp_reg); } i++; } return 0; }
FIRST AND FOLLOW
#include <stdio.h> #include <string.h> char prod[10][10], first[10], follow[10]; int n; void findFirst(char c) { for (int i = 0; i < n; i++) { if (prod[i][0] == c) { if (!isupper(prod[i][3])) printf("%c ", prod[i][3]); else findFirst(prod[i][3]); } } } void findFollow(char c) { if (c == prod[0][0]) printf("$ "); for (int i = 0; i < n; i++) { for (int j = 3; prod[i][j]; j++) { if (prod[i][j] == c) { if (prod[i][j + 1] && !isupper(prod[i][j + 1])) printf("%c ", prod[i][j + 1]); else if (prod[i][j + 1] == '\0' && prod[i][0] != c) findFollow(prod[i][0]); } } } } int main() { printf("No. of productions: "); scanf("%d", &n); getchar(); for (int i = 0; i < n; i++) gets(prod[i]); char ch; printf("Enter Non-Terminal for FIRST: "); scanf(" %c", &ch); printf("FIRST(%c): ", ch); findFirst(ch); printf("\nEnter Non-Terminal for FOLLOW: "); scanf(" %c", &ch); printf("FOLLOW(%c): ", ch); findFollow(ch); return 0; }
OPTIMIZATION
#include <stdio.h> #include <string.h> struct statement { char left[10], right[10]; int useful; }; int main() { struct statement code[20]; int n; printf("Enter number of statements: "); scanf("%d", &n); // Input for (int i = 0; i < n; i++) { printf("Left: "); scanf("%s", code[i].left); printf("Right: "); scanf("%s", code[i].right); code[i].useful = 0; } // Dead Code Elimination for (int i = n - 1; i >= 0; i--) { if (i == n - 1) { code[i].useful = 1; continue; } for (int j = i + 1; j < n; j++) { if (strchr(code[j].right, code[i].left[0])) { code[i].useful = 1; break; } } } // Common Subexpression Elimination for (int i = 0; i < n; i++) { if (!code[i].useful) continue; for (int j = i + 1; j < n; j++) { if (!code[j].useful) continue; if (strcmp(code[i].right, code[j].right) == 0) strcpy(code[j].right, code[i].left); } } // Optimized Output printf("\nOptimized Code:\n"); for (int i = 0; i < n; i++) if (code[i].useful) printf("%s = %s\n", code[i].left, code[i].right); return 0; }
LEX (tokens.l)
%{ #include <stdio.h> %} %% "int" { printf("Keyword: int\n"); } "float" { printf("Keyword: float\n"); } "return" { printf("Keyword: return\n"); } [0-9]+\.[0-9]+ { printf("Float constant: %s\n", yytext); } [0-9]+ { printf("Integer constant: %s\n", yytext); } [a-zA-Z_][a-zA-Z0-9_]* { printf("Identifier: %s\n", yytext); } "=" { printf("Operator: =\n"); } "+" { printf("Operator: +\n"); } "{" { printf("Symbol: {\n"); } "}" { printf("Symbol: }\n"); } ";" { printf("Symbol: ;\n"); } [ \t\n]+ { /* ignore whitespace */ } . { printf("Unknown character: %s\n", yytext); } %% int main() { yylex(); return 0; } int yywrap() { return 1; }
YACC (YOCC)
%{ #include "expr.tab.h" #include <stdlib.h> %} %% [0-9]+ { yylval = atoi(yytext); return NUMBER; } [*/] { return yytext[0]; } \n { return EOL; } [ \t]+ ; // Ignore whitespace %% %{ #include <stdio.h> #include <stdlib.h> %} %token NUMBER EOL %% input: expr EOL { printf("Result = %d\n", $1); }; expr: expr '*' expr { $$ = $1 * $3; } | expr '/' expr { if ($3 == 0) { fprintf(stderr, "Div by 0\n"); exit(1); } $$ = $1 / $3; } | NUMBER { $$ = $1; }; %% int main() { printf("Enter expression:\n"); return yyparse(); } int yyerror(char *s) { fprintf(stderr, "Syntax Error: %s\n", s); return 1; }
Developed and Maintained by Anonymous...