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

CD Lab2

This C program generates three address code for various expressions. It takes user input for the type of expression - assignment, arithmetic or relational. For assignment expressions, it splits the expression into left and right sides and generates code to assign the right side value to a temporary variable and then assign it to the left side. For arithmetic expressions, it splits the expression into operands and operators, generates code to evaluate each operation and assign it to a temporary variable. For relational expressions, it generates conditional jump code to check the relation and set a flag.

Uploaded by

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

CD Lab2

This C program generates three address code for various expressions. It takes user input for the type of expression - assignment, arithmetic or relational. For assignment expressions, it splits the expression into left and right sides and generates code to assign the right side value to a temporary variable and then assign it to the left side. For arithmetic expressions, it splits the expression into operands and operators, generates code to evaluate each operation and assign it to a temporary variable. For relational expressions, it generates conditional jump code to check the relation and set a flag.

Uploaded by

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

EXPERIMENT-8(a)

OBJECTIVE:
Wite a C program for constructing of LL (1) parsing
RESOURCE:
Turbo C++
PROGRAM LOGIC:
Read the input string
Using predictive parsing table parse the given input using stack.
If stack [] matches With token nput string pop the token else shift it repeat the process until it reaches to S.

PROCEDURE:
Go to debug-> run or press CTRL + F9 to run the program.

PROGRAM
#include<stdio h>
#includecconio h>
#include<string h>
char s[20],stack[20]
void mainO

"
char m[5][613]={"tb"," "," ","tb"," "," "," ","+tb"," "," ","n", "n", "fc"," "," ","fc","
""n,"n,"1," "." "."(e)",""." "}
int size[5]|[6={2,0,0,2,0,0,0,3,0,0,1,1,2,0,0,2,0,0,0,1,3,0,1,1,1,0,0,3,0,0}

int ij.kn,strl,str2,

clrscrO;
printf("in Enter the input string: ")

scanf("%s",s);
streat(s,"S")
n=strlen(s)
stack[0]="S';
stack[1J='e,
F,

JFO,

printf("'nStack Inputin");
printf(" \n");

while(stack[J-s)&&<0]=s)

ifstack[i)=sUD

17
switch(stack[1])

case 'e: str]=0;

break;
case 'b': str1=1;

break;
case t: strl=2;,

break,

case e. strl=s,

break,
case 't: strl=4;

break

Switch(sG1)

case ': str2=0,

break,
case+str2=1

break
case * str2=2,

break,
case'(: str2=3;
break,
case ): str2=4;

break;,
case'S' str2=5;
break,

ifm[str1][str2][0]-=10')

printf"nERROR");
exit(0);

else if(m[strl ][str2][0]=n)

else if(m[strl][str2][0]==i)

18
stack[=1;
else

for(k=size[str1][str2]-1;k>=0;k-)

stack[i)=m[str1 ][str2][k];

for(k-0,k<=ikt+)
printf(" 6c",stack[k]);
printf(" ");
for(k=j.k<=n;k++)
printf(%c"s[k]);
printf(" n ");

printf("n SUCCESS");:

getch0:
INPUT&OUTPUT:
Enter the input string i*j+i

Stack INPUT

$bt ii+is
Sbef iiis
Sbei ii+is
Sbe i+is
Sbcf* i+iS
Sbct
Sbei itis
Sbe 1S
$b 15

Sbt+ 15

sbt 1S

Sbef S

bei
Sbe
Sb

Success

19
EXPERIMENT-7

OBJECTIVE:
Write a C program for implementing the fünctionalities of predictive parser for the mini language specified in
Note 1

RESOURCE:
Turbo C++

PROGRAM LOGIC:
Read the input string
By using the FIRST AND FOLLOW values.
Verify the FIRST of non terminal and insert the production in the FIRST value
f we have any @ termms in FIRST then insert the productions in FOLLOW values
Constructing the predictive parser table
PROCEDURE:
Go to debug -> run or press CTRL + F9 to run the program.

PROGRAM:

#include<stdio.h>
include<conio.h>
#inchude<string h>
har prol[7][10]={"S","A","A","B","B","C","C"}
char pror[7][10]={"A","Bb","Cd","aB","@""Ce","@");
har prod[7][10]={"S->A","A->Bb","A->Cd","B->aB","B->@""C->Ce","C>@")
char
first[7][10]=("abed", "ab","cd","'a@""@""c@""@"}
char follow[7][10]={"S","S", "S","aS","bs","eS","ds"};
char table[516][10];
umr(char c)

Switch(c)

case 'S: return 0;

case 'A retum 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 'S return 4;

13
return(2);

vOid maino

int 1j.

clrserO:

for(i-0,i5;i++)

for(j-0j<6j+)
strepy(table[G1." ");
print("nThe following is the predictive parsing table for the following grammar: \n");

for(i-0,i<7;i++)

printf"%sin" prodli|)D
printf("nPredictive parsing table isin")

ffush(stdin)
for(i-0,i<7,it+)

k=strlen(first[i])
for(j=0.j<10j++)
iffirst[0=@)

strepy(table(nunr(prol[i|[0])+1][num(first[JG)+1]prod[i)

for(i=0,i<7,i++)

ifstrlen(pror[il)=1)

ifprorfi[0]-@

=strlen(follow)
for(j-0j<kj++
strepy(table[aumr(prol[g][O)+1][mumr(follow[bD+1]prodi),
strepy(table[0O][O],"*")

strepy(tableo])"a):
strepy(table[0][2],"b");
strepy(table[0]I3]"e");
strepy(table[0]4],"d");

strepy(table| 0]I5],"5")
strepy(table[1[O],"s");
strepy(table[2]1O]."A"):

strepy(table[3][0],"B");
strepy(table[4][0],"C");
printt("r \n");

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

forý=0 j<6j++)

printf("6-10s", table[iJG):
ifj==5)

printf("

getch0

PRE LAB QUESTIONS:

1. What is
top-down parsing?
2. What are the disadvantages of brute force method?
3. What is context free grammar?
4. What 1s parse tree?
5. Whatis ambiguous grammar?
6. What are the derivation methods to generate a string tor the given grammar?
7. What is the output of parse tree?

15
INPUT & OUTPUT:
The followWing is the predictive parsing table for the tollowing grammarr

S->A
>Bb
A->Cd
B->aB
B->@
C->Ce
C->
Predictive parsing table is

S S->A S->A S->A S->A

A A->Bb A->Bb A->Cd A->Cd

BB->aB B- B-@ B->@

C C>@ C->@ C->@

16
3.Write a C program to generate three address code.

#include<stdio.h>
#include<string.h>
void pm);
void plus(O:
void div);
int i,ch.j.l.addr-100;

char ex[101, exp[10] expl[|10},.exp2[10].id1 [S].op[S].id2[|];


void main)

clrscr);
while(1)

printf"nl.assignmentin2.arithmeticn3.relational n4.Exit nEnter the choice:");


scanf("%d",&ch);
switch(ch)
case

printf("inEnter the expression with assignment operator:");


scanf("%s",exp);
-strlen(exp);
exp2[0]=\o';
F0;
while(exp[i]!==)
itt
strncat(exp2,exp.i);
strrev(exp);
exp1[0]-\O';
strncat(expl,.exp.l-i+1):
strrev(exppl)
printf"Three address code:Intemp-6sln6s=templn".expl.exp2);
break;

case 2
printf("nEnter the expression with arithmetic operator:");
scanf("6s",ex);
strcpy(exp,ex);
Fstrlen(exp);
expl[0]=\0';

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

if(explil+"||exp[il)
ifexpli+2]-="|exp[i+2]=*)
pm)
break;

else

plus)
break;

else ifexp[i--"|lexpli]-=*)

div);
break;

break;

case 3:
printf("Enter the expression with relational operator");
scanf("%sos%s",&idl,&op.&id2);
if(strcmp(op,"e")-0}|(stremp(op,">")0)|(stremp(op,"<=")=0J|(stremp(op,">=")=-0}|(st
remp(op,"-")=0)}|(strcmp(op,"!=")--0)-0)
printf("Expression is error");
else

printf"n%dtif 6s%s%s goto %d",addr,id1,op,.id2,addr+3);


addr++;
printf("n%d\t T:-0",addr);
addr++;
printf("n%d\t goto %d",addr,addr+2);
addr++;
printf("n%d\t T:-1",addr);

break;
case 4:
exit(0);

void pm)

strrev(exp);
ji-l;
strncat(exp1,exp.j);
strrev(expl);
printf("Three address code:\ntemp-%s\ntempl-%c%octempn",expl,expi+1].exp[ilD:

void div)

strncat(exp1,exp.i+2);
printf("Three address code:Intemp-6s\ntempl-temp%c%c\n",expl,exp[i+2].exp[i+3]):

void plus)

strncat(expl,exp.i+2);
printf("Three address code:ntemp-6sIntempl-temp%cocln",expl,exp[i+2].expli+3]);

Output
Output
I. assignment
2. arithmetic
3. relational
41Exit
Enter the choice:1
Enter the expression with assignment operator:
a=b
Three address code:
temp=b
a-temp

1.assignment
2.arithmetic
.relational
4.Exit
Enter the choice:2
Enter the expression with arithmetic operator:
a+b-c
Three address code:
temp-a+b
templ=temp-c

1.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:2
Enter the expression with arithmetic operator
a-b/c
Three address code:
temp=b/c
templ=a-temp

1.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:2
Enter the expression with arithmetic operator:
a"b-c
Three address code:
temp-a"b
templ=temp-c
1.assignment
2.arithmetic
3 .relational
4.Exit
Enter the choice:2
Enter the expression with arithmetic operator:a/b*c
Three address code:
temp=a/b
templ=temp"c
I.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:3
Enter the expression with relational operator
EXPERIMENT-9

OBJECTIVE:
Write a program to Design LALR Bottom up Parser.

RESOURCE:

TURBO C++

PROGRAMLOGIC:

Read the input string.

Push the input symbol with its state symbols in to the stack by referring lookaheads

We perform shift and reduce actions to parse the grammar.

Parsing is completed when we reach S symbol.

PROCEDURE:

Go to debug->in or press CTRL +F9 to run the program


PROGRAM:

/LALR PARSER
E->E+1
E->1
T->T*F
T->F
F-(E)
F-N

#include<stdio h>
#include<conio.h>
include<stdlib.h>
#include<string.h>
void push(char *,int *,char)
char stacktop(char *);
void isproduct(char,char);
int ister(char);
int isnter(char);
int isstate(char);
oid error);
void isreduce(char, char)
char pop(char *int ):
void print(char i n t " char [.int);

void rep(char [],int);

struct action

char row[6]S];

24
const struct action A[12]={

("sf","emp","emp","se","emp","emp"),
("emp","sg","emp","emp","emp","ace"),
{"emp", "re","sh", "emp","re", "re"},
("emp""re", "re","emp", "re","re"},
{"sf","emp","emp","'se","emp","emp"
"emp","rg "Tg","emp","rg", "rg",
("sf","emp","emp","se""emp","emp",
{"sf","emp", "emp", "se","emp" "emp"}
("emp","sg","emp","emp","sl","emp").
("emp","rb","sh""emp","rb","rb"},
{"emp","rb","rd","emp","rd","rd"},

("'emp","rf", "rf", "emp","rf","rf"}

struct gotol

char r[3]14

const struct gotol G[12]={

"b""e","d"
"emp","emp","emp"),
("emp", "emp","emp"},
("emp", "emp", "emp"},
"""c","d"),

("emp","emp","emp"},
("emp""""d"),
f"emp","emp","k"},
"emp" "emp","emp"},
f"emp" "emp","emp"},

char ter[6]={i,+""*"): S'};


ehar nter[3]=(E,T,F};
char
states[12]=(a,b,c;'d e','f'g,h,'m'},k,1};
char stack[100];
nt top=--1;
char temp[10];
struct grammar

25
char left,
char right[5]

const struct grammar l[6]={

{'E',"e+T"}
(E,"T
(T"T*F"),
(T,"F"),
(F/"(E)"),
(F""),

void mainO

char inp[s0] xp.di[so],y, bl=a;


int i=0j.k.ln,.m.c.len;

clrser0,
printf(" Enter the input ")

scanf("6s",inp);
len=strlen(iunp);
np[len]='$
inp[len+1]=F0;

push(stack,&top,bl);
printf("n stack ittit input")
printt(stack,& top.inp.i)

x=inp[il;
p=stacktop(stack),

1sproduct(x.P)
ifsircup(iemp,"eup"=0)
error),
ifstremp(temp, "acc"J==0)
break,
else

iftemp[0]==s)

pushstack, &top,.inp [1);


push(stack, &top,temp[1]D

26
else

f(temp[0]=)

j=isstate(temp[1]);
strepy(temp.lj-2]right);
dl[O]=rl[j-2] left,
dl[1]=\0';
n=strlen(temp);
for(k-0.k<2*nk++)

pop(stack, &top);
for(m-0;dl[m]=10"m++)
push(stack,&top, dl[m]);
top;
y=stack[1-1]
isreduce(y,dl[o);
for(m=0,temp[m]!=10';m+)
push(stack, &top,temp[m]

printt(stack,&topinp i);
}while(inpli]=0);
ifstremp(temp."ace")==0)
printf(" In accept the input ");

else

printf(" in do not accept the input ");

getchO

void push(char *s,int "sp.char item)

if("sp=100)
printf" stack is full ");

else

sp-sp+1;

27
s[sp]=item;

char stacktop(char *s)

char 1,

1ss[top]
retum i,

void isproduct(char x,char p)

inttk1;

k=ister(x)
=isstate(p),
strepy(tempA[L-1]row[k-1])
int ister(char x)

int i,

for(i=0,i<6i++)
if(x==ter[1])
return i+1
return 0,

int isnter(char x)

int i,

for(i-0i<3it++)
if(x=nter[i)
refurn i+l1

return 0,

int isstate(char p)

int 1,

for(i=0,i<12,i++)
if(p=states[i])

28
return 1+1,

returm o,

vOid error)

printf(" error in the input ");

exit(0);

void isreduce(char x,char p)

int k

k=isstate(x)
isnter(p):
strepy(temp,G[k-1]r[1-1]);

char pop(char *s,int *sp)

char item,

if( sp=-1)
printf(" stack 15 empty ");

lse

item=s["spPl
spsp-1

refun item,

void printt(char *t,int *p,char inpl]int i)

int r,

printf(" n");
for(r=0rc=*p.r+*)
rep(t.r)
printf("ittr");

fortr=implr|l=0'r++)

29
printf(%c"inp[r]);

void rep(char t[],int r)

char C,

c=t[r]
switch(c)

case 'a': printf("0");

break,
case b: printf("1");
break,
case 'c': printf("2");

break,
case 'd printt("3");

break
case 'e': printf("4");

break
case 'f pintt(">");

break,
case 'g printf("6");

break,
case h: printf("7");

break,

case 'm printt("8");

break;
case : printf("9");

break,
case k printf"10");

break,

case T' printf("1l");

break,

default printf("%c",t[r]);

break

30
INPUT & OUTPUT:

Enter the input: i*i+1

Output

Stack input
ii+1S
0is i+iS
OFS *i+is
OT2 j+1S
OT2*7 i+iS
T2*7i5 +iS
OT2*7iSF10 +15

OT2 +15
El +15
OE1+6
DE +6i5

OE1++6F3

OE1++6T9
OE1
accept the input/

31

You might also like