Compiler Design Practical File
Compiler Design Practical File
9 Implementation of Three
address code using
Quadruples, Triples, Indirect
Triples.
1. Program to check a string under given grammar.
#include <stdio.h>
#include <ctype.h>
Output:-
#include <stdio.h>
#include <string.h>
int main() {
char string[] = "int x = 10; return x;";
char *token = strtok(string, " ;");
while (token != NULL) {
if (isKeyword(token)) {
printf("%s is a keyword\n", token);
}
token = strtok(NULL, " ;");
}
return 0;
}
Output:-
int is a keyword
return is a keyword
3. Program to check for identifiers in a given string.
#include <stdio.h>
#include <ctype.h>
int main() {
char idenQfier[] = "variable1";
if (isIdenQfier(idenQfier)) {
prinK("%s is a valid idenQfier\n", idenQfier);
} else {
prinK("%s is not a valid idenQfier\n", idenQfier);
}
return 0;
}
Output:-
#include <stdio.h>
#include <ctype.h>
int main() {
char constant[] = "42.0";
if (isConstant(constant)) {
prinK("%s is a constant\n", constant);
} else {
prinK("%s is not a constant\n", constant);
}
return 0;
}
Output:-
42.0 is a constant
5. Program to check for relational operators in a given string
#include <stdio.h>
#include <string.h>
int main() {
char expression[] = "a == b and a != c";
findRelaQonalOperators(expression);
return 0;
}
Output:-
#include <stdio.h>
#include <ctype.h>
#include <string.h>
typedef struct {
TokenType type;
char value[100];
} Token;
int main() {
char code[] = "int x = 10; if (x >= 10) return x;";
lexicalAnalyzer(code);
return 0;
}
Output:-
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int stack[MAX], top = -1;
void push(int item) {
if (top >= MAX - 1) {
prinK("Stack overflow\n");
} else {
stack[++top] = item;
prinK("Pushed %d\n", item);
}}
int pop() {
if (top < 0) {
prinK("Stack underflow\n");
return -1;
} else {
return stack[top--];
}}
void display() {
if (top < 0) {
prinK("Stack is empty\n");
} else {
prinK("Stack: ");
for (int i = 0; i <= top; i++) {
prinK("%d ", stack[i]);
}
prinK("\n");
}}
int main() {
push(10);
push(20);
display();
prinK("Popped %d\n", pop());
display();
return 0;
}
Output:-
Pushed 10
Pushed 20
Stack: 10 20
Popped 20
Stack: 10
8. Implementation of Shift-Reduce parsing using C.
#include <stdio.h>
#include <string.h>
#define MAX 100
char stack[MAX][MAX];
int top = -1;
void push(char *item) {
strcpy(stack[++top], item);
}
char *pop() {
return stack[top--];
}
int isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/';
}
void shisReduceParser(char *input) {
char *token = strtok(input, " ");
while (token != NULL) {
push(token);
prinK("Shis: %s\n", token);
while (top > 1 && isOperator(stack[top][0])) {
char operator[2];
strcpy(operator, pop());
char operand2[100];
strcpy(operand2, pop());
char operand1[100];
strcpy(operand1, pop());
prinK("Reduce: %s %s %s\n", operand1, operator, operand2);
}
token = strtok(NULL, " ");
}
}
int main() {
char expression[] = "id1 + id2 * id3";
shisReduceParser(expression);
Output:-
Shis: id1
Shis: +
Shis: id2
Shis: *
Shis: id3
Reduce: id2 * id3
Reduce: id1 + id2*id3
9. Implementation of Three address code using Quadruples , Triples,
Indirect Triples.
#include <stdio.h>
#include <string.h>
// Quadruple structure
typedef struct {
char op[3];
char arg1[10];
char arg2[10];
char result[10];
} Quadruple;
// Triple structure
typedef struct {
char op[3];
char arg1[10];
char arg2[10];
} Triple;
int main() {
// Quadruples
Quadruple quads[3] = {
{"+", "a", "b", "t1"},
{"*", "t1", "c", "t2"},
{"=", "t2", "", "d"}
};
// Triples
Triple triples[3] = {
{"+", "a", "b"},
{"*", "(0)", "c"},
{"=", "(1)", ""}
};
// Indirect Triples
IndirectTriple indirectTriples[3] = {
{0, {"+", "a", "b"}},
{1, {"*", "(0)", "c"}},
{2, {"=", "(1)", ""}}
};
// Print the representaQons
printQuadruples(quads, 3);
printTriples(triples, 3);
printIndirectTriples(indirectTriples, 3);
return 0;
}
Output:-
Quadruples:
t1 = a + b
t2 = t1 * c
d = t2
Triples:
(0) + a b
(1) * (0) c
(2) = (1)
Indirect Triples:
[0] (0) + a b
[1] (1) * (0) c
[2] (2) = (1)