PCDLABBBBB
PCDLABBBBB
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int ag = 0, z = 1;
void main() {
char a[50], b[50];
int ti = 0;
int i, j, k, count;
/*
d=(a-b)+(a+c)+b*c
*/
-----------------------------------------------------------------------------
Shift Reduce Parser
#include<stdio.h>
#include<string.h>
struct stack {
char s[20];
int top;
};
int main() {
int n, i, j = 0, k, h;
char lp[10];
char ip[10];
char rp[10][10];
st.top = 0;
printf("\nEnter the number of productions: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("\nEnter the left side of production %d: ", i + 1);
scanf(" %c", &lp[i]);
printf("\nEnter the right side of production %d: ", i + 1);
scanf("%s", rp[i]);
}
printf("============================================================\n");
printf("STACK\t\tINPUT\t\tOUTPUT\n");
printf("============================================================\n");
strcat(ip, "$");
push('$');
printf("$\t\t%s\n", ip);
return 0;
}
/*
Enter the number of productions: 3
-------------------------------------------------------------------------------
Code Optimizer
#include <stdio.h>
#include <string.h>
struct op {
char l; // Left operand
char r[20]; // Right operand
} op[10], pr[10]; // Arrays to hold operations and optimized results
void main() {
int a, i, k, j, n, z = 0, m, q;
char *p, *l, *tem, temp, t;
char nu[] = "\0"; // Null string for empty initialization
// Input operations
for (i = 0; i < n; i++) {
printf("\nLeft: ");
scanf(" %c", &op[i].l); // Read a single character for left operand
printf("Right: ");
scanf("%s", op[i].r); // Read the right operand as a string
}
printf("\nIntermediate code:\n");
for (i = 0; i < n; i++) {
printf("%c = %s\n", op[i].l, op[i].r); // Print intermediate code
}
// Final optimization
for (i = 0; i < z; i++) {
for (j = i + 1; j < z; j++) {
q = strcmp(pr[i].r, pr[j].r);
if ((pr[i].l == pr[j].l) && !q) {
pr[i].l = '\0'; // Mark for elimination
strcpy(pr[i].r, nu); // Clear the right operand
}
}
}
printf("\nOptimized code:\n");
for (i = 0; i < z; i++) {
if (pr[i].l != '\0') {
printf("%c = %s\n", pr[i].l, pr[i].r); // Print the final optimized
code
}
}
}
Left: a
Right: 10
Left: b
Right: 20
Left: c
Right: a+b
Left: d
Right: a+b
Left: e
Right: a+d*/
-----------------------------------------------------------------------------------
Symbol table
#include <stdio.h>
#include <string.h>
#include <ctype.h>
struct symtab {
int lineno;
char var[25], dt[25], val[10];
} sa[20];
int main() {
int i = 0, line = 0, max;
char input[100];
printf("\n\nSYMBOL TABLE MANAGEMENT\n\n");
printf("Enter variable declarations ");
fgets(input, sizeof(input), stdin);
parseInput(input, &line, &i);
max = i;
printf("\nVariable\tDatatype\tLine.no.\tValue\n");
for (i = 1; i <= max; i++) {
printf("%s\t\t%s\t\t%d\t\t%s\n", sa[i].var, sa[i].dt, sa[i].lineno,
sa[i].val);
}
return 0;
}
-----------------------------------------------------------------------------------
Lexical Analyzer
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
right++;
left = right;
} else if ((isDelimiter(input[right]) && left != right) || (right == len &&
left != right)) {
char* subStr = getSubstring(input, left, right - 1);
if (isKeyword(subStr))
printf("Token: Keyword, Value: %s\n", subStr);
else if (isInteger(subStr))
printf("Token: Integer, Value: %s\n", subStr);
else if (isValidIdentifier(subStr) && !isDelimiter(input[right - 1]))
printf("Token: Identifier, Value: %s\n", subStr);
else if (!isValidIdentifier(subStr) && !isDelimiter(input[right - 1]))
printf("Token: Unidentified, Value: %s\n", subStr);
left = right;
}
}
return 0;
}
// Main function
int main() {
char lex_input[MAX_LENGTH];
lex_input[strcspn(lex_input, "\n")] = 0;
return 0;
}
/*a+b+c+d>/*/
-----------------------------------------------------------------------------------
--
Backend. c
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int ag = 0, z = 1;
void main() {
char a[50], id[50], mov[] = "MOVF", mul[] = "MULF", div[] = "DIVF", add[] =
"ADDF", sub[] = "SUBF";
int i = 0, j = 0, len = 0, s = 0, e = 0, r = 1;
/*
id1=id2*id3+id4
*/
-----------------------------------------------------------------------------------
-