You are on page 1of 46

1)Write a C Program to implement a lexical

analyzer for given language and the lexical


analyzer should ignore redundant spaces,
tabs and new lines. It should also ignore
comments.
Code :-
Lexical Analyzer code in c:
#include<string.h>
#include<ctype.h>
#include<stdio.h>
void keyword(char str[10])
{
if(strcmp("for",str)==0||strcmp("while",str)==0||strcmp("do",str)=
=0||strcmp("int",str)==0||strcmp("float",str)==0||strcmp("char",s
tr)==0||strcmp("double",str)==0||strcmp("static",str)==0||strcmp
("switch",str)==0||strcmp("case",str)==0)
printf("\n%s is a keyword",str);
else
printf("\n%s is an identifier",str);
}
main()
{
FILE *f1,*f2,*f3;
char c,str[10],st1[10];
int num[100],lineno=0,tokenvalue=0,i=0,j=0,k=0;
printf("\nEnter the c program");/*gets(st1);*/
f1=fopen("input","w"); while((c=getchar())!
=EOF)
putc(c,f1);
fclose(f1);
f1=fopen("input","r");
f2=fopen("identifier","w");
f3=fopen("specialchar","w");
while((c=getc(f1))!=EOF) {
if(isdigit(c))
{
tokenvalue=c-'0';
c=getc(f1);
while(isdigit(c)) {
tokenvalue*=10+c-'0';
c=getc(f1);
}
num[i++]=tokenvalue;
ungetc(c,f1);
}
else
if(isalpha(c))
{
putc(c,f2);
c=getc(f1);
while(isdigit(c)||isalpha(c)||c=='_'||c=='$')
{
putc(c,f2);
c=getc(f1);
}
putc(' ',f2);
ungetc(c,f1);
}
else
if(c==' '||c=='\t')
printf(" ");
else
if(c=='\n')
lineno++;
else
putc(c,f3);
}
fclose(f2);
fclose(f3);
fclose(f1);
printf("\nThe no's in the program are");
for(j=0;j<i;j++)
printf("%d",num[j]); printf("\
n"); f2=fopen("identifier","r");
k=0;
printf("The keywords and identifiersare:");
while((c=getc(f2))!=EOF) {
if(c!=' ') str[k+
+]=c; else
{
str[k]='\0';
keyword(str);
k=0;
}
}
fclose(f2);
f3=fopen("specialchar","r");
printf("\nSpecial characters are");
while((c=getc(f3))!=EOF)
printf("%c",c);
printf("\n");
fclose(f3);
printf("Total no. of lines are:%d",lineno);
}

OUTPUT :-

OUTPUT:
2)Write a C program to recognize strings
under 'a*', 'a*b+', 'abb'?
CODE :-
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char s[20],c;
int state=0,i=0;
printf("\n Enter a
string:"); gets(s);
while(s[i]!='\0')
{
switch(state)
{
case 0: c=s[i++];
if(c=='a')
state=1;
else
if(c=='b')
state=2;
else
state=6;
break;
case 1: c=s[i++];
if(c=='a')
state=3;
else if(c=='b')
state=4;
else
state=6;
break;
case 2: c=s[i++];
if(c=='a')
state=6;
else
if(c=='b')
state=2;
else
state=6;
break;
case 3: c=s[i++];
if(c=='a')
state=3;
else
if(c=='b')
state=2;
else
state=6;
break;
case 4: c=s[i++];
if(c=='a')
state=6;
else
if(c=='b')
state=5;
else
state=6;
break;
case 5: c=s[i++];
if(c=='a')
state=6;
else
if(c=='b')
state=2;
else
state=6;
break;
case 6: printf("\n %s is not
recognised.",s); exit(0);
}
}
if(state==1)
printf("\n %s is accepted under rule 'a'",s);
else if((state==2)||(state==4))
printf("\n %s is accepted under rule
'a*b+'",s); else if(state==5)
printf("\n %s is accepted under rule 'abb'",s);
getch();
}

OUTPUT :-

OUTPUT FOR ACCEPTANCE CASE :-


3) Write a “C” code for Recursive desent parser.

4) Write a “C” code for three address code.


Recurssive desent parser in C:
#include<stdio.h>

#include<string.h>

void E(), E1(), T(), T1(), F();

int ip = 0;

static char s[10];

int main() {

char k;

int l;

ip = 0;

printf("enter the input");

scanf("%s", s);

printf("the string is :%s",


s);

E();

if (s[ip] == '$')

printf("\n string is accepted the length of string is %d", strlen(s) -

1); else

printf("\n string not accepted\n");

return 0;

void E() {

T();

E1();

return;

void E1() {

if (s[ip] == '+')
{ ip++;

T();

E1();

return;

void T() {

F();

T1();

return;

void T1() {

if (s[ip] == '*')

{ ip++;

F();

T1();

return;

void F() {

if (s[ip] == '(')

{ ip++;

E();

if (s[ip] ==

')') ip++;

} else

if (s[ip] ==

'i') ip++;

else

printf("\n id expected");

return;

}
Execution of code:

Output:
Code for Three address code:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

struct three {

char data[10], temp[7];

s[30];

void main() {

char d1[7], d2[7] =

"t"; int i = 0, j = 1,

len = 0; FILE * f1, *

f2;

clrscr();

f1 = fopen("sum.txt", "r");

f2 = fopen("out.txt", "w");

while (fscanf(f1, "%s", s[len].data) !=

EOF) len++;

itoa(j, d1, 7);

strcat(d2, d1);

strcpy(s[j].temp, d2);

strcpy(d1, "");

strcpy(d2, "t");

if (!strcmp(s[3].data, "+")) {

fprintf(f2, "%s=%s+%s", s[j].temp, s[i + 2].data, s[i + 4].data);

j++;
} else if (!strcmp(s[3].data, "-")) {

fprintf(f2, "%s=%s-%s", s[j].temp, s[i + 2].data, s[i + 4].data);

j++;

for (i = 4; i < len - 2; i +=

2) { itoa(j, d1, 7);

strcat(d2, d1);

strcpy(s[j].temp, d2);

if (!strcmp(s[i + 1].data, "+"))

fprintf(f2, "\n%s=%s+%s", s[j].temp, s[j - 1].temp, s[i + 2].data);

else if (!strcmp(s[i + 1].data, "-"))

fprintf(f2, "\n%s=%s-%s", s[j].temp, s[j - 1].temp, s[i + 2].data);

strcpy(d1, "");

strcpy(d2, "t");

j++;

fprintf(f2, "\n%s=%s", s[0].data, s[j -

1].temp); fclose(f1);

fclose(f2);

getch();

Execution of code in online compiler:


OUTPUT:
5) LR PARSER :-
#include<stdio.h>

#include<iostream>

using namespace std;

char stack[30];

int top=-1;

void push(char c)

top++;

stack[top]=c;

char pop()

char c;

if(top!=-1)

c=stack[top];

top--;

return c;

return'x';

void printstat()

int i; printf("\n\t\t\

t $");

for(i=0;i<=top;i++)

printf("%c",stack[i]);

int main(int argc, char *argv[])

{
int i,j,k,l;

char s1[20],s2[20],ch1,ch2,ch3;

printf("\n\n\t\t LR PARSING"); printf("\

n\t\t ENTER THE EXPRESSION");

scanf("%s",s1);

l=strlen(s1);

j=0;

printf("\n\t\t $");

for(i=0;i<l;i++)

if(s1[i]=='i' && s1[i+1]=='d')

s1[i]=' ';

s1[i+1]='E';

printstat(); printf("id");

push('E');

printstat();

else if(s1[i]=='+'||s1[i]=='-'||s1[i]=='*' ||s1[i]=='/' ||s1[i]=='d')

push(s1[i]);

printstat();

printstat();

l=strlen(s2);

while(l)

ch1=pop();

if(ch1=='x')

{
printf("\n\t\t\t $");

break;

if(ch1=='+'||ch1=='/'||ch1=='*'||ch1=='-')

ch3=pop(); if(ch3!

='E')

printf("errror");

exit(0);

else

push('E');

printstat();

ch2=ch1;

Sy

stem("PAUSE");

6) IDENTIFICATION OF IDENTIFIERS :-
/ C++ implementation of the approach

#include <bits/stdc++.h>

using namespace std;

// Function that returns true if str

// is a valid identifier

bool isValid(string str, int n)

// If first character is invalid

if (!((str[0] >= 'a' && str[0] <= 'z')

|| (str[0] >= 'A' && str[0] <= 'Z')

|| str[0] == '_'))

return false;

// Traverse the string for the rest of the

characters for (int i = 1; i < str.length(); i++) {

if (!((str[i] >= 'a' && str[i] <= 'z')

|| (str[i] >= 'A' && str[i] <= 'Z')

|| (str[i] >= '0' && str[i] <= '9')

|| str[i] == '_'))

return false;

// String is a valid

identifier return true;

// Driver code

int main()
{

string str = "Abc";

int n = str.length();

if (isValid(str, n))

cout << "Valid";

else

cout << "Invalid";

return 0;

7) IDENTIFICATION OF OPERATOR :-
#include <stdio.h>

#include <stdbool.h>

#include <string.h>

// Function to check if a character is an

operator bool isOperator(char ch) {

char operators[] = {'+', '-', '*', '/', '%'};

int numOperators = sizeof(operators) / sizeof(operators[0]);

for (int i = 0; i < numOperators; i+

+) { if (ch == operators[i]) {

return true;

return false;

}
int main() {

char expression[100];

printf("Enter an expression: ");

fgets(expression, sizeof(expression), stdin);

printf("Operators in the expression:

"); for (int i = 0; i <

strlen(expression); i++) {

if (isOperator(expression[i])) {

printf("%c ", expression[i]);

printf("\n");

return 0;

8) Write a “C” code for code optimization.

9) Write a “C” code for code generation.


CODE OPTIMIZATION IN C:
#include<stdio.h>

#include<string.h>

struct op

{ char l;

char r[20];

} op[10], pr[10];

int main() {

int a, i, k, j, n, z = 0, m,

q; char *p, *l;

char temp,

t; char

*tem;

printf("Enter the Number of Values:");

scanf("%d", &n);

for (i = 0; i < n; i++)

{ printf("left: ");

scanf("

%c",&op[i].l);

printf("\tright: ");

scanf("%s", op[i].r);

}
printf("Intermediate Code\n");

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

printf("%c = %s\n", op[i].l, op[i].r);

for (i = 0; i < n - 1; i++)

{ temp = op[i].l;

for (j = 0; j < n; j++) {

p = strchr(op[j].r, temp);

if (p != NULL) {

pr[z].l = op[i].l;

strcpy(pr[z].r, op[i].r);

z++;

pr[z].l = op[n - 1].l;

strcpy(pr[z].r, op[n - 1].r); z+

+;

printf("\nAfter Dead Code Elimination\n");

for (k = 0; k < z; k++) {

printf("%c = %s\n", pr[k].l, pr[k].r);

for (m = 0; m < z; m+

+) { tem = pr[m].r;

for (j = m + 1; j < z; j+

+) { p = strstr(tem,

pr[j].r); if (p != NULL) {

t = pr[j].l;
pr[j].l = pr[m].l;

for (i = 0; i < z; i+

+) { l =

strchr(pr[i].r, t); if

(l != NULL) {

a = l - pr[i].r;

printf("pos: %d\n", a);

pr[i].r[a] = pr[m].l;

printf("Eliminate Common Expression\n");

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

printf("%c = %s\n", pr[i].l, pr[i].r);

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 == 0)) {

pr[i].l = '\0';

strcpy(pr[i].r, "");

printf("Optimized Code\n");

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

if (pr[i].l != '\0') {
printf("%c = %s\n", pr[i].l, pr[i].r);

return 0;

Output :-
Code for Code generation:-
#include <stdio.h>

char stk[100];

int stktop = -1;

int cnt = 0;

void push(char pchar)

{ stk[++stktop] =

pchar;

char pop() {

return stk[stktop--];

char checkoperation(char char1) {

char oper;

if (char1 ==

'+') oper =

'A';

else if (char1

=='-') oper = 'S';

else if (char1 == '*')

oper = 'M';

else if (char1 == '/')

oper = 'D';

else if (char1 == '@')


oper = 'N';

return oper;

int checknstore(char check)

{ int ret;

if (check != '+' && check != '-' && check != '*' && check != '/' && check != '@') { push(+

+cnt);

if (stktop > 0)

printf("ST $%d\n",cnt);

ret = 1;

} else

ret =

0;

return ret;

int main(int argc, char * argv[]) {

char msg[100], op1, op2,

operation; int i, val;

while (scanf("%s", msg) != EOF)

{ cnt = 0;

stktop = -1;

for (i = 0; msg[i] != '\0'; i++) {

if ((msg[i] >= 'A' && msg[i] <= 'Z') || (msg[i] >= 'a' && msg[i] <= 'z'))

printf("(A + B) - C\n");

push(msg[i]);

else {

op1 = pop();

op2 = pop();
operation = checkoperation(msg[i]); val

= checknstore(msg[i + 1]);

while (val == 0)

{ op1 = pop();

cnt--;

operation = checkoperation(msg[++i]); if

(operation == 'S' && stktop >= -1)

{ operation = 'A';

//printf("%c %c\n", operation, op1); val

= checknstore(msg[i + 1]);

return 0;

Input :-

Output :-
1) Write a C/C++ program for construction of
predictive parsing table
Code :-
#include<stdio.h>

#include<iostream>
#include<string.h>
using namespace std;
char prol[7][10]={"S","A","A","B","B","C","C"};
char pror[7][10]={"A","Bb","Cd","aB","@","Cc","@"};
char prod[7][10]={"S->A","A->Bb","A->Cd","B->aB","B-
>@","C->Cc","C->@"}; char
first[7][10]={"abcd","ab","cd","a@","@","c@","@"}; char
follow[7][10]={"$","$","$","a$","b$","c$","d$"};
char table[5][6][10];
int numr(char c)
{
switch(c)
{
case 'S': return
0; case 'A':
return 1; case
'B': return 2;
case 'C': return
3; case 'a':
return 0; case
'b': return 1;
case 'c': return
2; case 'd':
return 3;
case '$': return 4;
}
return(2);
}
int main(int argc, char *argv[])
{
int i,j,k; for(i=0;i<5;i+
+) for(j=0;j<6;j++)
strcpy(table[i][j]," ");
printf("\nThe following is the predictive parsing table for
the following grammar:\n");
for(i=0;i<7;i++)
printf("%s\n",prod[i]);
printf("\nPredictive parsing table is\n");
fflush(stdin);
for(i=0;i<7;i++)
{
k=strlen(first[i]);
for(j=0;j<10;j++)
if(first[i][j]!='@')
strcpy(table[numr(prol[i][0])+1][numr(first[i][j])+1],prod[i]);
}
for(i=0;i<7;i++)
{
if(strlen(pror[i])==1)
{
if(pror[i][0]=='@')
{
k=strlen(follow[i]);
for(j=0;j<k;j++)

strcpy(table[numr(prol[i][0])+1][numr(follow[i][j])+1],prod[i])
;
}
}
}
strcpy(table[0][0]," ");
strcpy(table[0][1],"a");
strcpy(table[0][2],"b");
strcpy(table[0][3],"c");
strcpy(table[0][4],"d");
strcpy(table[0][5],"$");
strcpy(table[1][0],"S");
strcpy(table[2][0],"A");
strcpy(table[3][0],"B");
strcpy(table[4][0],"C");
printf("\n \n");
for(i=0;i<5;i++)
for(j=0;j<6;j++)
{
printf("%-10s",table[i][j]);
if(j==5)
printf("\n \n");
}
system("PAUSE"); // statement in Bloodshed dev c++
IDE requirement
}
Code Execution and Output :-
10 ) Write a C/C++ program to construct DFA from
NFA

Code :-
#include <iostream>
#include<stdio.h>
#include<ctype.h>
#include<process.h>
using namespace std;
typedef struct
{
int num[10],top;
}
stack;
stack s;
int mark[16][31],e_close[16][31],n,st=0;
char data[15][15];
void push(int a)
{
s.num[s.top]=a;
s.top=s.top+1;
}
int pop()
{
int a;
if(s.top==0)
return(-1);
s.top=s.top-1;
a=s.num[s.top];
return(a);
}
void epi_close(int s1,int s2,int c)
{
int i,k,f;
for(i=1;i<=n;i++)
{
if(data[s2][i]=='e')
{
f=0;
for(k=1;k<=c;k++)
if(e_close[s1][k]==i)
f=1;
if(f==0)
{ c+
+;
e_close[s1][c]=i;
push(i);
}
}
}
while(s.top!=0) epi_close(s1,pop(),c);
}
int move(int sta,char c)
{
int i; for(i=1;i<=n;i+
+)
{
if(data[sta][i]==c)
return(i);
}
return(0);
}
void e_union(int m,int n)
{
int i=0,j,t; for(j=1;mark[m]
[i]!=-1;j++)
{
while((mark[m][i]!=e_close[n][j])&&(mark[m][i]!=-1))
i++;
if(mark[m][i]==-1)
mark[m][i]=e_close[n][j];
}
}
int main(int argc, char *argv[])
{
int i,j,k,Lo,m,p,q,t,f;
printf("\n enter the NFA state table
entries:"); scanf("%d",&n);
printf("\n");
for(i=0;i<=n;i++)
printf("%d",i);
printf("\n");
for(i=0;i<=n;i++)
printf("-----");
printf("\n");
for(i=1;i<=n;i++)
{
printf("%d|",i);
fflush(stdin);
for(j=1;j<=n;j++)
scanf("%c",&data[i][j]);
}
for(i=1;i<=15;i++)
for(j=1;j<=30;j++)
{
e_close[i][j]=-1;
mark[i][j]=-1;
}
for(i=1;i<=n;i++)
{
e_close[i][1]=i;
s.top=0;
epi_close(i,i,1);
}
for(i=1;i<=n;i++)
{
for(j=1;e_close[i][j]!=-1;j++)
for(k=2;e_close[i][k]!=-1;k++)
if(e_close[i][k-1]>e_close[i][k])
{
t=e_close[i][k-1]; e_close[i]
[k-1]=e_close[i][k];
e_close[i][k]=t;
}
}
printf("\n the epsilon closures are:"); for(i=1;i<=n;i+
+)
{
printf("\n E(%d)={",i);
for(j=1;e_close[i][j]!=-1;j++)
printf("%d",e_close[i][j]);
printf("}");
}
j=1;
while(e_close[1][j]!=-1)
{
mark[1][j]=e_close[1][j];
j++;
}
st=1;
printf("\n DFA Table is:");
printf("\n a b ");
printf("\n ");
for(i=1;i<=st;i++)
{
printf("\n{");
for(j=1;mark[i][j]!=-1;j++)
printf("%d",mark[i][j]);
printf("}");
while(j<7)
{
printf(" ");
j++;
} for(Lo=1;Lo<=2;Lo+
+)
{
for(j=1;mark[i][j]!=-1;j++)
{
if(Lo==1)
t=move(mark[i][j],'a');
if(Lo==2)
t=move(mark[i][j],'b');
if(t!=0)
e_union(st+1,t);
}
for(p=1;mark[st+1][p]!=-1;p++)
for(q=2;mark[st+1][q]!=-1;q++)
{
if(mark[st+1][q-1]>mark[st+1][q])
{
t=mark[st+1][q]; mark[st+1]
[q]=mark[st+1][q-1];
mark[st+1][q-1]=t;
}
}
f=1;
for(p=1;p<=st;p++)
{ j=
1;
while((mark[st+1][j]==mark[p][j])&&(mark[st+1][j]!=-1))
j++;
if(mark[st+1][j]==-1 && mark[p][j]==-1)
f=0;
}
if(mark[st+1][1]==-1)
f=0;
printf("\t{");
for(j=1;mark[st+1][j]!=-1;j++)
{
printf("%d",mark[st+1][j]);
}
printf("}\t");
if(Lo==1)
printf(" ");
if(f==1) st+
+; if(f==0)
{
for(p=1;p<=30;p++)
mark[st+1][p]=-1;
}
}
}
system("PAUSE");
return EXIT_SUCCESS;
}

Code Execution and Output :-

You might also like