You are on page 1of 20

1

1. Design a DFA to accept all strings containing a substring(01).


PROGRAM:
#include<stdio.h>
#include<string.h>
void main() {
char s[100]={'\0'};
printf("-DFA To Accept Strings Containing 01-\n\n");
printf("String : ");
scanf("%s",s);
int l=strlen(s),i;
for(i=0;i<l;i++) {
if(s[i]=='0' && s[i+1]=='1') break;
}
(i!=l)?printf("\nString is accepted."):printf("String is not
accepted.");
}

JNTUHCEH
2

OUTPUT:
-DFA To Accept Strings Containing 01-

String : 000010110

String is accepted.

JNTUHCEH
3

2. Design a lexical analyzer in lex.


PROGRAM:
* program name is lexp.l */
%{
/* program to recognize a c program */
int COMMENT=0;
%}
identifier [a-zA-Z][a-zA-Z0-9]*
%%
#.* { printf("\n%s is a PREPROCESSOR DIRECTIVE",yytext);}
int |
float |
char |
double |
while |
for |
do |
if |
break |
continue |
void |
switch |
case |
long |
struct |
const |
typedef |
return |
else |
goto {printf("\n\t%s is a KEYWORD",yytext);}
"/*" {COMMENT = 1;}
/*{printf("\n\n\t%s is a COMMENT\n",yytext);}*/
"*/" {COMMENT = 0;}
/* printf("\n\n\t%s is a COMMENT\n",yytext);}*/
{identifier}\( {if(!COMMENT)printf("\n\nFUNCTION\n\t%s",yytext);}
\{ {if(!COMMENT) printf("\n BLOCK BEGINS");}
\} {if(!COMMENT) printf("\n BLOCK ENDS");}
{identifier}(\[[0-9]*\])? {if(!COMMENT) printf("\n %s
IDENTIFIER",yytext);}
\".*\" {if(!COMMENT) printf("\n\t%s is a STRING",yytext);}
[0-9]+ {if(!COMMENT) printf("\n\t%s is a NUMBER",yytext);}
\)(\;)? {if(!COMMENT) printf("\n\t");ECHO;printf("\n");}
\( ECHO;
= {if(!COMMENT)printf("\n\t%s is an ASSIGNMENT OPERATOR",yytext);}
\<= |
\>= |
\< |
== |
\> {if(!COMMENT) printf("\n\t%s is a RELATIONAL OPERATOR",yytext);}
%%

JNTUHCEH
4

int main(int argc,char **argv)


{
if (argc > 1)
{
FILE *file;
file = fopen(argv[1],"r");
if(!file)
{
printf("could not open %s \n",argv[1]);
exit(0);
}
yyin = file;
}
yylex();
printf("\n\n");
return 0;
}
int yywrap()
{
return 0;
}

JNTUHCEH
5

INPUT:
$vi var.c
#include<stdio.h>
main()
{
int a,b;
}

OUTPUT:
$lex lex.l
$cc lex.yy.c
$./a.out var.c
#include<stdio.h> is a PREPROCESSOR DIRECTIVE
FUNCTION
main (

)
BLOCK BEGINS
int is a KEYWORD

a IDENTIFIER

b IDENTIFIER
BLOCK ENDS

JNTUHCEH
6

3. Implement RD Parser for grammar


E->TE'
E'->+TE'/e
T->FT'
T'->*FT'/e
F->(E)/id
PROGRAM:
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
char s[30]={'\0'};
int c=0;
bool in=true;
void E();
void F() {
if(s[c]=='(') {
c++;
E();
if(s[c]==')') c++;
else in=false;
}
else if(s[c]=='i' && s[c+1]=='d') c+=2;
else in=false;
}
void T_() {
if(s[c]=='*') {
c++;
F();
T_();
}
}
void T() {
F();
T_();
}
void E_() {
if(s[c]=='+') {
c++;
T();
E_();
}
}
void E() {
T();
E_();
}
void main() {
printf("-RD Parser-\n\tE -> TE_\n\tE_ -> +TE_ / e\n\tT -> FT_\n\t");
printf("T_ -> *FT_ / e\n\tF -> (E) / id\n\nString : ");
scanf("%s",s);

JNTUHCEH
7

E();
(in)?printf("Given string is accepted."):printf("Given string is not
accepted.");
}

JNTUHCEH
8

OUTPUT:
-RD Parser-
E -> TE_
E_ -> +TE_ / e
T -> FT_
T_ -> *FT_ / e
F -> (E) / id

String : id+id*id
Given string is accepted.

JNTUHCEH
9

4. Implement LL(1) parser.


PROGRAM:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char nt[6]="EGTUF", t[7]="+*()ie", p[5][10]={"TG", "+TG/e", "FU", "*FU/e",


"(E)/i"};
char fi[6][6]={'\0'}, fo[6][6]={'\0'}, a[15][7]={'\0'}, *u,
ss[7]={'\0'};// ta consists of i from a[i].
int ntn=5, tn=6, c=0, pf, ta[6][7];// pf : print follow().

int isTerminal(char c) {
int x;
for(x=0; x<tn && t[x]!=c; x++);
return (x<tn)?x:-1;
}

int isNonTerminal(char c) {
int x;
for(x=0; x<ntn && nt[x]!=c; x++);
return (x<ntn)?x:-1;
}

int anotherProductionPresent(int i, int j) {


int x;
for(x=j; x<strlen(p[i]) && p[i][x]!='/'; x++);
return (x<strlen(p[i]))?x:-1;
}

void copyToFrom(char* to, int* a, char* from) {


int x;
for(x=0; x<strlen(from); x++) {
if(strchr(to, from[x])==NULL) to[(*a)++]=from[x];
}
}

int contains_e(char* s) {
int x;
for(x=0; x<strlen(s) && s[x]!='e'; x++);
if(x==strlen(s)) return 0;
for( ; x<strlen(s)-1; x++) s[x]=s[x+1];
s[x]='\0';
return 1;
}

void firstOf(int i, int j, int a) {// p[i][j] and fi[i][a].


int x;
if(isTerminal(p[i][j])!=-1) fi[i][a++]=p[i][j];

JNTUHCEH
10

else if((x=isNonTerminal(p[i][j]))!=-1) copyToFrom(fi[i], &a,


fi[x]);
if((x=anotherProductionPresent(i, j+1))!=-1) firstOf(i, x+1, a);
}

void followOf(int i, int a) {// nt[i] and fo[i][a].


int x, k, l;
for(k=0; k<ntn; k++) {
for(l=0; l<strlen(p[k]); l++) {
if(p[k][l]==nt[i]) {
if(l+1<strlen(p[k]) && p[k][l+1]!='/') {
if(isTerminal(p[k][l+1])!=-1) fo[i][a++]=p[k][l+1];
else if((x=isNonTerminal(p[k][l+1]))!=-1) {
copyToFrom(fo[i], &a, fi[x]);
if(contains_e(fo[i])) {
a--;
if(strlen(fo[x])==0) followOf(x, 0);
copyToFrom(fo[i], &a, fo[x]);
}}}
else {
if(i!=k) {
if(fo[k][0]=='\0') followOf(k, 0);
copyToFrom(fo[i], &a, fo[k]);
}}}}}
}

void parsingTable() {
int i, j, k;
t[tn-1]='$';
for(i=0; i<tn; i++) printf("\t%-10c", t[i]);
for(i=0, pf=0; i<ntn; i++, pf=0) {
printf("\n%c ", nt[i]);
if(strchr(fi[i], 'e')!=NULL) pf=1;// If e is in first(i).
for(j=0; j<tn; j++) {
ta[i][j]=-1;
if(strchr(fi[i], t[j])!=NULL) {// If terminal(j) is in
first(i).
if((k=anotherProductionPresent(i, 0))!=-1) {
if(p[i][0]==t[j]) {// If terminal(j) is in first
production.
strcpy(a[c], p[i]);
a[c][k]='\0';
printf("\t%c->%-7s", nt[i], a[c++]);
}
else if(p[i][k+1]==t[j]) {// Is in second
production.
u=p[i]+(k+1);
printf("\t%c->%-7s", nt[i], strcpy(a[c++], u));
}}

JNTUHCEH
11

else printf("\t%c->%-7s", nt[i], strcpy(a[c++],


p[i]));// Only single production.
ta[i][j]=c-1;
}
else if(pf==1 && strchr(fo[i], t[j])!=NULL) {// If
terminal(j) is in follow(i).
printf("\t%c->%-7s", nt[i], strcpy(a[c++], "e"));
ta[i][j]=c-1;
}
else printf("\t%-10s", " ");
}}
}

char* getReverse(int i) {
if(a[i][0]=='e') return "\0";
return strrev(strcpy(ss, a[i]));
}

void isOfGrammar() {
char s[100]={'\0'};// Stack.
int top=0, un=strlen(u), i, j, k;
u[un++]=s[top++]='$';
for(k=0, i=0; k<un; k++) {// ta[i][j] and u[k].
j=isTerminal(u[k]);
if(j==-1 || ta[i][j]==-1) break;// Invalid string or input
element.
strcat(s, getReverse(ta[i][j]));
top=strlen(s);
while((i=isNonTerminal(s[top-1]))!=-1) {
s[--top]='\0';
strcat(s, getReverse(ta[i][j]));
top=strlen(s);
}
if(isTerminal(s[top-1])!=-1) s[--top]='\0';
else break;// Invalid input element.
if((i=isNonTerminal(s[top-1]))!=-1) s[--top]='\0';
else break;
}
printf("String %s accepted.\n", (strlen(s)==0 && k==un-
1)?"is":"isn't");
}

void main() {
printf("---- LL(1) Parser ----\n\n");
int i, j, k=1;
printf("FIRST() :\n");
for(i=ntn-1; i>=0; i--) {
firstOf(i, 0, 0);
printf("\t%c : %s\n", nt[i], fi[i]);
}

JNTUHCEH
12

printf("\nFOLLOW() :\n");
fo[0][0]='$';
for(i=0; i<ntn; i++) {
followOf(i, (i==0)?1:0);
printf("\t%c : %s\n", nt[i], fo[i]);
}

printf("\nParsing table :\nNT\t Input Symbol\n ");


parsingTable();

while(k) {
printf("\n\nString : ");
scanf("%s", u);
isOfGrammar();
printf("\nDo you want to exit? Yes(0), No(1).\t>");
scanf("%d", &k);
}
}

JNTUHCEH
13

OUTPUT:
---- LL(1) Parser ----

FIRST() :
F : (i
U : *e
T : (i
G : +e
E : (i

FOLLOW() :
E : $)
G : $)
T : +$)
U : +$)
F : *+$)

Parsing table :
NT Input Symbol
+ * ( ) i
$
E E->TG E-
>TG
G G->+TG G->e
G->e
T T->FU T-
>FU
U U->e U->*FU U->e
U->e
F F->(E) F-
>i

String : (i+i)
String is accepted.

Do you want to exit? Yes(0), No(1). >1

String : i+(i*i)
String is accepted.

Do you want to exit? Yes(0), No(1). >i++i

String : String isn't accepted.

Do you want to exit? Yes(0), No(1). >0

JNTUHCEH
14

5. Implement SLR(1) Parser.


PROGRAM:
#include<stdio.h>
#include<string.h>
typedef struct {
int n, f;// Number of items in an item set, from I[f].
char on, ph[10], pb[10][5];// I[f] on (char)on : I[i], Production head
and body.
}SOI;// Set of items.
SOI I[15]={0, -1, '\0', "\0", "\0"};

typedef struct { char s[6]; }String;// For ACTION.

char nt[]={"ASSLLR"}, t[]={"=*i$"}, p[][5]={"S", "L=R", "R", "*R", "i",


"L"}, te[5]={'\0'}, rnt[]={"SLR"};
char fo[][3]={"$", "$", "$", "$=", "$=", "$="}, is[20]={'\0'};// is :
Input String.
int ntn=6, tn=4, In=0, rntn=3;

int isNT(char c) {// Checks whether c is a non terminal.


int a;
for(a=0; a<ntn && nt[a]!=c; a++);
return a<ntn?a:-1;
}

int isT(char c) {// Checks whether c is a terminal.


int a;
for(a=0; a<tn && t[a]!=c; a++);
return a<tn?a:-1;
}

int isProduction(char* s) {
int a;
for(a=0; a<ntn && strcmp(s, strcat(strcpy(te, p[a]), "."))!=0; a++);
return a<ntn?a:-1;
}

int contains(char* s, int* n) {// If s contains in I[In].pb.


int a;
for(a=0; a<*n && strcmp(I[In].pb[a], s); a++);
return a<*n?1:0;
}

int dotAt(int in, int i) {// .


int a, b=strlen(I[in].pb[i]);
for(a=0; a<b && I[in].pb[i][a]!='.'; a++);
return a;
}

JNTUHCEH
15

void swap(char* s, int x, int y) {


char te=s[x];
s[x]=s[y];
s[y]=te;
}

int if2ItemSetsEqual(int in) {// 1 for equal and 0 for not equal.
int a, b;
for(a=0; a<in; a++) {
for(b=0; b<I[a].n && strcmp(I[a].pb[b], I[in].pb[b])==0; b++);
if(b==I[a].n) return 1;
}
return 0;
}

void emptyItemSet(int in) {


int a;
for(a=0; a<I[in].n; a++) {
I[in].ph[a]='\0';
strcpy(I[in].pb[a], "\0");
}
I[in].n=0;
}

int findItemSet(int in, char c) {// Finds the item set which is derived
from I[in] on c.
int a;
for(a=0; a<In && !(I[a].f==in && I[a].on==c); a++);
if(a==In) for(a=in; a<In && I[a].on!=c; a++);
if(a==In) for(a=0; a<In && I[a].on!=c; a++);
return a;
}

int getReducedNT(int l) {
int a;
for(a=0; a<rntn && rnt[a]!=nt[l]; a++);
return a;
}

void closure(char c, int* n) {


int x, y;
for(x=0; x<ntn; x++) {
if(nt[x]==c) {
if(!contains(strcat(strcpy(te, "."), p[x]), n)) {
strcat(strcpy(I[In].pb[++(*n)], "."), p[x]);
I[In].ph[*n]=nt[x];
if((y=isNT(I[In].pb[*n][1]))!=-1) closure(nt[y], n);
}}}
}

JNTUHCEH
16

void setOfItems(int r, int c, char h, char* b) {// Row, column where .


should be inserted, head and body.
int n=I[In].n, a;
if(c==0) strcat(strcpy(I[In].pb[n], "."), b);// In case of I[0].
else swap(strcpy(I[In].pb[n], b), c-1, c);
I[In].ph[n]=h;
if((a=isNT(I[In].pb[n][c+1]))!=-1) closure(nt[a], &n);// If non
terminal next to . .
I[In].n=n+1;
}

void goTo(int in, char c) {// I[in] and c is the element next to . .
int a, b;
for(a=0; a<I[in].n; a++) {
b=dotAt(in, a);
if(b+1<strlen(I[in].pb[a]) && I[in].pb[a][b+1]==c) {
setOfItems(a, b+1, I[in].ph[a], I[in].pb[a]);
I[In].f=in;
I[In].on=I[in].pb[a][b+1];
}}
}

void reduce(String* aci, int i, int l) {// ith row of parsing table and
lth follow.
int a, b;
char *s, c[2]={'\0'};
for(a=0; a<tn; a++) {
if((s=strchr(fo[l], t[a]))!=NULL) {
b=t-s;
c[0]=48+l;
strcat(strcat(aci[a].s, "r\0"), c);
}}
}

void stack(String ac[In][tn], int gt[In][rntn]) {


int a, st[20], top=0, b, isAccepted=0, i, j, k;
for(a=0, st[top]=0; a<strlen(is); a++) {
printf("\n%c , b: %d, ", is[a], b=isT(is[a]));
if((b=isT(is[a]))==-1) break;// String not of this grammar.
if(ac[st[top]][b].s[0]=='s') {
st[++top]=ac[st[top]][b].s[1];
}
else if(ac[st[top]][b].s[0]=='r') {
j=ac[st[top]][b].s[1];
for(i=a-1, k=strlen(p[j])-1; i>=0 && is[a]==p[j][k]; i--, k--
);
if(k==0) {
is[++i]=nt[j];
if(gt[st[top-1]][getReducedNT(j)]==-1) top--;
else st[top]=gt[st[top-1]][getReducedNT(j)];

JNTUHCEH
17

a--;
}
else --top;
}
else if(ac[st[top]][b].s[0]=='a') isAccepted=1;
else break;
for(b=0; b<=top; b++) printf("%d ", st[b]);
printf("-- %s", is);
}
if(a==strlen(is) && st[top]==1) printf("String is accepted.\n");
else printf("String isn't accepted.\n");
}

void main() {
printf("---- SLR Parser ----\n\n");
printf("Set of items :\n");
setOfItems(0, 0, nt[0], p[0]);// I[0].
int i, j, k, l, m;
In++;
for(j=0; j<In; j++) {// I[j].
for(i=0; i<I[j].n; i++) {// Each item in I[j].
k=dotAt(j, i);
if(k+1<strlen(I[j].pb[i])) {
goTo(j, I[j].pb[i][k+1]);
k=if2ItemSetsEqual(In);
if(k==1) emptyItemSet(In);
else In++;
}}
}
for(j=0; j<In; j++) {
if(j!=0) printf("\nI[%d] : I[%d] on %c\n", j, I[j].f, I[j].on);
else printf("\nI[%d] :\n", j);
for(i=0; i<I[j].n; i++) printf("\t%c -> %s\n", I[j].ph[i],
I[j].pb[i]);
}

int gt[In][rntn];// GOTO.


for(i=0; i<In; i++) {
for(j=0; j<rntn; j++) gt[i][j]=-1;
}

String ac[In][tn];// ACTION.


for(i=0; i<In; i++) {
for(j=0; j<tn; j++) strcpy(ac[i][j].s, "\0");
}

strcpy(ac[1][tn-1].s, "acc");// PARSING TABLE.


printf("\nParsing Table :\n \t%-28s\tGOTO\n \t", "ACTION");
for(i=0; i<tn; i++) printf("%-5c\t", t[i]);
for(i=0; i<rntn; i++) printf("%-5c\t", rnt[i]);

JNTUHCEH
18

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


printf("\n%-2d\t", i);
for(j=0; j<I[i].n; j++) {
k=dotAt(i, j);
if(k+1<strlen(I[i].pb[j]) && (l=isT(I[i].pb[j][k+1]))!=-1)
{// Shift.
m=findItemSet(i, t[l]);
ac[i][l].s[0]='s';
ac[i][l].s[1]=m+'0';
ac[i][l].s[2]='\0';
}
else if(k+1<strlen(I[i].pb[j]) && (l=isNT(I[i].pb[j][k+1]))!=-
1) {// Goto.
m=findItemSet(i, nt[l]);
gt[i][getReducedNT(l)]=m;
}
else if(k+1==strlen(I[i].pb[j]) && i!=1) {// Reduce.
l=isProduction(I[i].pb[j]);
reduce(ac[i], i, l);
}
}
for(j=0; j<tn; j++) printf("%-5s\t",
(ac[i][j].s[0]!='\0')?ac[i][j].s:" ");
for(j=0; j<rntn; j++) printf("%-5c\t", (gt[i][j]!=-
1)?(gt[i][j]+'0'):' ');
}

printf("\n\nString (End it with $): ");


scanf("%s", is);
stack(ac, gt);
}

JNTUHCEH
19

OUTPUT:
---- SLR Parser ----

Set of items :

I[0] :
A -> .S
S -> .L=R
L -> .*R
L -> .i
S -> .R
R -> .L

I[1] : I[0] on S
A -> S.

I[2] : I[0] on L
S -> L.=R
R -> L.

I[3] : I[0] on *
L -> *.R
R -> .L
L -> .*R
L -> .i

I[4] : I[0] on i
L -> i.

I[5] : I[0] on R
S -> R.

I[6] : I[2] on =
S -> L=.R
R -> .L
L -> .*R
L -> .i

I[7] : I[3] on R
L -> *R.

I[8] : I[3] on L
R -> L.

I[9] : I[6] on R
S -> L=R.

Parsing Table :

JNTUHCEH
20

ACTION GOTO
= * i $ S L R
0 s3 s4 1 2 5
1 acc
2 s6r5 r5
3 s3 s4 8 7
4 r4 r4
5 r2
6 s3 s4 8 9
7 r3 r3
8 r5 r5
9 r1

String (End it with $): i


String is accepted.

JNTUHCEH

You might also like