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

Compiler Design

The document discusses writing C programs to solve arithmetic expressions, convert statements into tokens, implement a symbol table, and determine first and follow sets from production rules. It provides code snippets for each task and sample outputs.

Uploaded by

Dwaipayan Nandi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Compiler Design

The document discusses writing C programs to solve arithmetic expressions, convert statements into tokens, implement a symbol table, and determine first and follow sets from production rules. It provides code snippets for each task and sample outputs.

Uploaded by

Dwaipayan Nandi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Assignment 1: Write C program to solve an arithmetic

expression using +, -, *, / operators.

Code
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
struct Stack {
int top;
int array[100];
};
struct Stack* createStack() {
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
stack->top = -1;
return stack;
};
int isEmpty(struct Stack* stack) {
return stack->top == -1;
}
int peek(struct Stack* stack) {
return stack->array[stack->top];
}
int pop(struct Stack* stack) {
if (!isEmpty(stack))
return stack->array[stack->top--];
return '$';
}
void push(struct Stack* stack, int op) {
stack->array[++stack->top] = op;
}
int isOperand(char ch) {
return isdigit(ch);
}
int priority(char ch) {
switch (ch) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
}
return -1;
}
int applyOp(int a, int b, char op){
switch(op){
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
}
}
int evaluate(char* tokens){
int i;
struct Stack* values = createStack();
struct Stack* ops = createStack();
for(i = 0; i < strlen(tokens); i++){
if(tokens[i] == ' ')
continue;
else if(isdigit(tokens[i])){
int val = 0;
while(i < strlen(tokens) && isdigit(tokens[i])){
val = (val*10) + (tokens[i]-'0');
i++;
}
push(values, val);
i--;
}
else{
while(!isEmpty(ops) && priority(peek(ops)) >=
priority(tokens[i])){
int val2 = pop(values);
int val1 = pop(values);
char op = pop(ops);
push(values, applyOp(val1, val2, op));
}
push(ops, tokens[i]);
}
}
while(!isEmpty(ops)){
int val2 = pop(values);
int val1 = pop(values);
char op = pop(ops);
push(values, applyOp(val1, val2, op));
}
return pop(values);
}
int main() {
char exp[100];
printf("Enter an expression: ");
fgets(exp, 100, stdin);
exp[strcspn(exp, "\n")] = 0;
printf("Answer of the arithmatic expression is %d\n", evaluate(exp));
return 0;
}

Output
Assignment 2

a. Write a program in C to convert declarative statements into a sequence of


tokens

Code

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
int main() {
char *keywords[] = {"int", "bool", "double", "char"};
char *operators[] = {"+", "*", "/", "-", "=", "^"};
char str[100];
printf("Enter the expression:");
fgets(str, 100, stdin);
char token[100];
for (int i = 0; i < sizeof(str) / sizeof(str[0]); i++) {
if (!isspace(str[i]) && str[i] != ';') {
char temp[2] = {str[i], '\0'};
strncat(token, temp, sizeof(temp) - 1);
}
else{
if (isspace(token[0]) || token[0] == '\0') {
break;
}
bool isFound = false;
for(int i = 0; i < sizeof(keywords)/sizeof(keywords[0]); i++){
if(token && strcmp(keywords[i], token) == 0){
printf("%s -> Keyword\n", token);
isFound = true;
}
}
for(int i = 0; i < sizeof(operators)/sizeof(operators[0]); i++){
if(token && strcmp(operators[i], token) == 0){
printf("%s -> Operator\n", token);
isFound = true;
}
}
if(token && isalpha(token[0]) && !isFound){
printf("%s -> Variable\n", token);
isFound = true;
}
if(token && !isFound && token[0] == '\''){
printf("%s -> character\n", token);
isFound = true;
}
if(token && !isFound) {
printf("%s -> Value\n", token);
isFound = true;
}
token[0] = '\0';
}
}
return 0;
}

Output
b. Write a program in C to convert control flow statements into a sequence of
tokens

Code

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

int main() {
char *keywords[] = {"int", "bool", "double", "char", "for", "while",
"if","else", "do"};
char *operators[] = {"+", "*", "/", "-", "=", "^", "<", ">", "==",
">=","<="};
char str[100];
printf("Enter the expression:");
fgets(str, 100, stdin);
char token[100];
for (int i = 0; i < sizeof(str) / sizeof(str[0]); i++) {
if (!isspace(str[i]) && str[i] != ';' && str[i] != '(' && str[i] !=')'
&& str[i] != '{' && str[i] != '}') {
char temp[2] = {str[i], '\0'};
strncat(token, temp, sizeof(temp) - 1);
}
else{
if (isspace(token[0]) || token[0] == '\0') {
break;
}
bool isFound = false;
for(int i = 0; i < sizeof(keywords)/sizeof(keywords[0]); i++){
if(token && strcmp(keywords[i], token) == 0){
printf("%s -> Keyword\n", token);
isFound = true;
}
}
for(int i = 0; i < sizeof(operators)/sizeof(operators[0]); i++){
if(token && strcmp(operators[i], token) == 0){
printf("%s -> Operator\n", token);
isFound = true;
}
}
if(token && isalpha(token[0]) && !isFound){
printf("%s -> Variable\n", token);
isFound = true;
}
if(token && !isFound && token[0] == '\''){
printf("%s -> character\n", token);
isFound = true;
}
if(token && !isFound) {
printf("%s -> Value\n", token);
isFound = true;
}
token[0] = '\0';
}
}
return 0;
}

Output
c. Write a program in C to convert I/O statements into a sequence of tokens

Code

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
int main() {
char *keywords[] = {"int", "bool", "double", "char", "for", "while","if",
"else", "do", "printf", "scanf"};
char *operators[] = {"+", "*", "/", "-", "=", "^", "<", ">", "==",">=",
"<="};
char str[100];
printf("Enter the expression:");
fgets(str, 100, stdin);
char token[100];
for (int i = 0; i < sizeof(str) / sizeof(str[0]); i++) {
if (str[i] != ';' && str[i] != '(' && str[i] != ')' && str[i] !='{' &&
str[i] != '}' && str[i] != ',') {
char temp[2] = {str[i], '\0'};
strncat(token, temp, sizeof(temp) - 1);
}
else{
if (token[0] == '\0') {
break;
}
bool isFound = false;
for(int i = 0; i < sizeof(keywords)/sizeof(keywords[0]); i++){
if(token && strcmp(keywords[i], token) == 0){
printf("%s -> Keyword\n", token);
isFound = true;
}
}
for(int i = 0; i < sizeof(operators)/sizeof(operators[0]);i++){
if(token && strcmp(operators[i], token) == 0){
printf("%s -> Operator\n", token);
isFound = true;
}
}
if(token && token[0] == '&' && !isFound){
printf("%s -> Variable\n", token);
isFound = true;
}
if(token && !isFound && token[0] == '\"'){
printf("%s -> string format\n", token);
isFound = true;
}
if(token && !isFound && token[0] == '\''){
printf("%s -> character\n", token);
isFound = true;
}
if(token && !isFound) {
printf("%s -> Value\n", token);
isFound = true;
}
token[0] = '\0';
}
}
return 0;
}

Output
Assignment 4: Write a program in C to implement a
Symbol Table to work with Declaration Statements and
Arithmetic Expression.

Code

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
void main() {
int x=0, n, i=0,j=0;
void *mypointer,*Symbol_address[5];
char ch,First_Search,Symbol_Array2[15],Symbol_Array3[15],c;
printf("Input the expression ending with $:");
while((c=getchar())!='$') {
Symbol_Array2[i]=c;
i++;
}
n=i-1;
printf("Given Expression:");
i=0;
while(i<=n) {
printf("%c",Symbol_Array2[i]);
i++;
}
printf("\n Symbol Table display\n");
printf("Symbol \t addr \t type");
while(j<=n) {
c=Symbol_Array2[j];
if(isalpha(toascii(c))) {
mypointer=malloc(c);
Symbol_address[x]=mypointer;
Symbol_Array3[x]=c;
printf("\n%c \t %d \t identifier\n",c,mypointer);
x++;
j++;
}
else {
ch=c;
if(ch=='+'||ch=='-'||ch=='*'||ch=='='||ch=='/') {
mypointer=malloc(ch);
Symbol_address[x]=mypointer;
Symbol_Array3[x]=ch;
printf("\n %c \t %d \t operator\n",ch,mypointer);
x++;
j++;
}
}
}
}

Output
Assignment 6: Write a program in C to determine First
and follow sets from a set of production rules.

Code

#include <ctype.h>
#include <stdio.h>
#include <string.h>
void grammarfollow(char, int, int);
void follow(char c);
void find_first(char, int, int);
int count, n = 0;
char final_first[10][100];
char final_follow[10][100];
int m = 0;
char production[10][10];
char f[10], first[10];
int k;
char ck;
int e;
int main(int argc, char **argv) {
int jm = 0;
int km = 0;
int i, choice;
char c, ch;
printf("Enter the number of production rules: ");
scanf("%d", &count);
printf("Enter the production rules:\n");
for (i = 0; i < count; i++) {
printf("Rule %d: ", i + 1);
scanf("%s", production[i]);
}
int ff;
char done[count];
int ptr = -1;
for (k = 0; k < count; k++) {
for (ff = 0; ff < 100; ff++) {
final_first[k][ff] = '!';
}
}
int point1 = 0, point2, xxx;
for (k = 0; k < count; k++) {
c = production[k][0];
point2 = 0;
xxx = 0;
for (ff = 0; ff <= ptr; ff++)
if (c == done[ff])
xxx = 1;
if (xxx == 1)
continue;
find_first(c, 0, 0);
ptr += 1;
done[ptr] = c;
printf("\n First(%c) = { ", c);
final_first[point1][point2++] = c;
for (i = 0 + jm; i < n; i++) {
int fs = 0, chk = 0;
for (fs = 0; fs < point2; fs++) {
if (first[i] == final_first[point1][fs]) {
chk = 1;
break;
}
}
if (chk == 0) {
printf("%c, ", first[i]);
final_first[point1][point2++] = first[i];
}
}
printf("}\n");
jm = n;
point1++;
}
printf("\n");
printf("x=x=x=x=x=x=x=x=x=x=x=x=x=x=x=x=x=x=x=x=x=x\n\n");
char donee[count];
ptr = -1;
for (k = 0; k < count; k++) {
for (ff = 0; ff < 100; ff++) {
final_follow[k][ff] = '!';
}
}
point1 = 0;
int land = 0;
for (e = 0; e < count; e++) {
ck = production[e][0];
point2 = 0;
xxx = 0;
for (ff = 0; ff <= ptr; ff++)
if (ck == donee[ff])
xxx = 1;
if (xxx == 1)
continue;
land += 1;
follow(ck);
ptr += 1;
done[ptr] = ck;
printf(" Follow(%c) = { ", ck);
final_follow[point1][point2++] = ck;
for (i = 0 + km; i < m; i++) {
int fs = 0, chk = 0;
for (fs = 0; fs < point2; fs++) {
if (f[i] == final_follow[point1][fs]) {
chk = 1;
break;
}
}
if (chk == 0) {
printf("%c, ", f[i]);
final_follow[point1][point2++] = f[i];
}
}
printf(" }\n\n");
km = m;
point1++;
}
}
void follow(char c) {
int i, j;
if (production[0][0] == c) {
f[m++] = '$';
}
for (i = 0; i < 10; i++) {
for (j = 2; j < 10; j++) {
if (production[i][j] == c) {
if (production[i][j + 1] != '\0') {
grammarfollow(production[i][j + 1], i, (j + 2));
}
if (production[i][j + 1] == '\0' && c != production[i][0]) {
follow(production[i][0]);
}
}
}
}
}
void find_first(char c, int q1, int q2) {
int j;
if (!(isupper(c))) {
first[n++] = c;
}
for (j = 0; j < count; j++) {
if (production[j][0] == c) {
if (production[j][2] == '#') {
if (production[q1][q2] == '\0')
first[n++] = '#';
else if (production[q1][q2] != '\0' && (q1 != 0 || q2 != 0)) {
find_first(production[q1][q2], q1, (q2 + 1));
}
else
first[n++] = '#';
}
else if (!isupper(production[j][2])) {
first[n++] = production[j][2];
}
else {
find_first(production[j][2], j, 3);
}
}
}
}
void grammarfollow(char c, int c1, int c2) {
int k;
if (!(isupper(c)))
f[m++] = c;
else {
int i = 0, j = 1;
for (i = 0; i < count; i++) {
if (final_first[i][0] == c)
break;
}
while (final_first[i][j] != '!') {
if (final_first[i][j] != '#') {
f[m++] = final_first[i][j];
}
else {
if (production[c1][c2] == '\0') {
follow(production[c1][0]);
}
else {
grammarfollow(production[c1][c2], c1, c2 + 1);
}
}
j++;
}
}}
Output

You might also like