Professional Documents
Culture Documents
JNTUHCEH
2
OUTPUT:
-DFA To Accept Strings Containing 01-
String : 000010110
String is accepted.
JNTUHCEH
3
JNTUHCEH
4
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
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
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 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;
}
JNTUHCEH
10
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
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]);
}
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.
String : i+(i*i)
String is accepted.
JNTUHCEH
14
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;
}
JNTUHCEH
15
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;
}
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;
}
JNTUHCEH
16
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);
}}
}
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]);
}
JNTUHCEH
18
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
JNTUHCEH