0% found this document useful (0 votes)
7 views

Compiler Design Practical File

Uploaded by

abhit8938
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Compiler Design Practical File

Uploaded by

abhit8938
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

INDEX

Sr. No. Name of the Practical Date Sign.


1 Program to check a string under a
given grammar.
2 Program to check for keywords in a
given string
3 Program to check for identifiers in a
given string
4 Program to check for constants in a
given string
5 Program to check for relational
operators in a given string
6 Program for Lexical Analyzer

7 Implementation of Stack using C


8 Implementation of Shift-Reduce
parsing using C

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>

int isValidString(char *s) {


for (int i = 0; s[i] != '\0'; i++) {
if (!isalpha(s[i])) {
return 0;
}
}
return 1;
}
int main() {
char string[] = "exampleString";
if (isValidString(string)) {
prinK("The string is valid under the given grammar.\n");
} else {
prinK("The string is not valid under the given grammar.\n");
}
return 0;
}

Output:-

The string is valid under the given grammar.


2. Program to check for keywords in a given string.

#include <stdio.h>
#include <string.h>

int isKeyword(char *word) {


char *keywords[] = {"int", "return", "if", "else", "while", "for", "char"};
for (int i = 0; i < 7; i++) {
if (strcmp(word, keywords[i]) == 0) {
return 1;
}
}
return 0;
}

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 isIdenQfier(char *s) {


if (!isalpha(s[0]) && s[0] != '_') {
return 0;
}
for (int i = 1; s[i] != '\0'; i++) {
if (!isalnum(s[i]) && s[i] != '_') {
return 0;
}
}
return 1;
}

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:-

variable1 is a valid idenQfier


4. Program to check for constants in a given string.

#include <stdio.h>
#include <ctype.h>

int isConstant(char *s) {


int dotCount = 0;
for (int i = 0; s[i] != '\0'; i++) {
if (!isdigit(s[i]) && s[i] != '.') {
return 0;
}
if (s[i] == '.') {
dotCount++;
}
}
return dotCount <= 1;
}

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>

void findRelaQonalOperators(char *s) {


char *operators[] = {"==", "!=", "<=", ">=", "<", ">"};
for (int i = 0; i < 6; i++) {
if (strstr(s, operators[i]) != NULL) {
prinK("Found relaQonal operator: %s\n", operators[i]);
}
}
}

int main() {
char expression[] = "a == b and a != c";
findRelaQonalOperators(expression);
return 0;
}

Output:-

Found relaQonal operator: ==


Found relaQonal operator: !=
6. Program for Lexical Analyzer.

#include <stdio.h>
#include <ctype.h>
#include <string.h>

typedef enum { NUMBER, IDENTIFIER, OPERATOR, UNKNOWN } TokenType;

typedef struct {
TokenType type;
char value[100];
} Token;

void lexicalAnalyzer(char *code) {


char *operators = "+-*/=<>!";
Token tokens[100];
int tokenCount = 0;
int i = 0;

while (code[i] != '\0') {


if (isdigit(code[i])) {
int j = 0;
while (isdigit(code[i]) || code[i] == '.') {
tokens[tokenCount].value[j++] = code[i++];
}
tokens[tokenCount].value[j] = '\0';
tokens[tokenCount].type = NUMBER;
tokenCount++;
} else if (isalpha(code[i]) || code[i] == '_') {
int j = 0;
while (isalnum(code[i]) || code[i] == '_') {
tokens[tokenCount].value[j++] = code[i++];
}
tokens[tokenCount].value[j] = '\0';
tokens[tokenCount].type = IDENTIFIER;
tokenCount++;
} else if (strchr(operators, code[i])) {
int j = 0;
while (strchr(operators, code[i])) {
tokens[tokenCount].value[j++] = code[i++];
}
tokens[tokenCount].value[j] = '\0';
tokens[tokenCount].type = OPERATOR;
tokenCount++;
} else {
i++;
}
}

for (i = 0; i < tokenCount; i++) {


prinK("Token: %s, Type: %d\n", tokens[i].value, tokens[i].type);
}
}

int main() {
char code[] = "int x = 10; if (x >= 10) return x;";
lexicalAnalyzer(code);
return 0;
}

Output:-

Token: int, Type: 1


Token: x, Type: 1
Token: =, Type: 2
Token: 10, Type: 0
Token: if, Type: 1
Token: (, Type: 3
Token: x, Type: 1
Token: >=, Type: 2
Token: 10, Type: 0
Token: ), Type: 3
Token: return, Type: 1
Token: x, Type: 1
Token: ;, Type: 3
7. Implementation of Stack using C.

#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;

// Indirect Triple structure


typedef struct {
int index;
Triple triple;
} IndirectTriple;

void printQuadruples(Quadruple quads[], int n) {


prinK("Quadruples:\n");
for (int i = 0; i < n; i++) {
prinK("%s = %s %s %s\n", quads[i].result, quads[i].arg1, quads[i].op,
quads[i].arg2);
}
}

void printTriples(Triple triples[], int n) {


prinK("Triples:\n");
for (int i = 0; i < n; i++) {
prinK("(%d) %s %s %s\n", i, triples[i].op, triples[i].arg1, triples[i].arg2);
}
}

void printIndirectTriples(IndirectTriple indirectTriples[], int n) {


prinK("Indirect Triples:\n");
for (int i = 0; i < n; i++) {
prinK("[%d] (%d) %s %s %s\n", i, indirectTriples[i].index,
indirectTriples[i].triple.op, indirectTriples[i].triple.arg1, indirectTriples[i].triple.arg2);
}
}

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)

You might also like