Sie sind auf Seite 1von 20

Compiler Design

Aim: Program for 3-address code.


#include<conio.h>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
void main()
{
char s[20],st[20];
int t=0,i,num[20],top=0;
clrscr();
printf(“\n Enter postfix notation”);
scanf(“%s”,s);
for(i=0;i<strlen(s);i++)
if isalnum(s[i]);
st[top++]=s[i];
else
{
printf(”\n t %d=”,t);
if((str[top-2]!=’t’)&&(top>=2))
printf(“%c”,st[top-2]);
else if(st[top-2]==’t’)
printf(“%c %d”,st[top-2],num[top-2]);
printf(“%c”,s[i]);
if(st[top-1]==’t’)
printf(“%c %d”,st[top-1],num[top-1]);
else
printf(“%c”,st[top-1]);
if(top>=2)
top=top-2;
else
top=top-1;
st[top]=’t’;
num[top++]=t++;
}
}
Output:
Input : Enter postfix notation;
ab+cd+*
Output: T0=a+b
T1=c+d
T2=T0*T1

Write a program for Brute Force Technique

Aim: Program to implement Brute Force Tchnique

SA|AB
A ab|adb
B cd

Source Code:

#include<stdio.h>
#include<string.h>
void S();
void A();
void AB();
char str[10];
int i=0,count=0;
int main()
{
printf(“Enter the string:”);
scanf(“%s”,str);
S();
if(count<0)
{
Printf(“\nString can’t be delivered from given
grammar”);
}
}

void S()
{
A();
if(count==0)
AB();
}
void A()
{
if(str[i]==’a’&&str[i+1]==’b’&&str[i+2]==’$’)
{
printf(“ \n Parsing is successful for ab”);
count++;
return ;
}
if(str[i]==’a’&&str[i+1]==’b’&&str[i+3]==’d’&&str[i
+4]==’$’)
{
printf(“\n Parsing is successful for abd”);
count++;
return ;
}
}
Void AB()
{

if(str[i]==’a’ && str[i+1]==’b’&& str[i+2]==’c’&&


str[i+4]==’d’ && str[i+5]==’$’)
{
print(“\n Parsing is successful for abcd”);
count++;
return ;
}
if(str[i]==’a’ && str[i+1]==’b’&& str[i+2]==’d&&
str[i+3]=’c’&& str[i+4]=’d && str[i+5]==’$’)
{
print(“\n Parsing is successful for abdcd”);
count++;
return ;
}
}
Output:

Enter the String: abcd$


Parsing is successful for abcd.

 Write a Program for Predictive Parser

Aim: To implement Predictive Parser.

Source Code:

#include<stdio.h>
#include<string.h>
int top=-1;
char s[20], stk[20],item, ch;
char push(char);
void error();
char pop();
int main()
{
int ip=0;
printf(“Enter the input string”);
scanf(“%s”, s);
push($);
push(E);
while(stk[top]!=’$’)
{
switch(stk[top])
{
case ‘E’: if(s[ip]==’a’|| s[ip]==’(‘)
{
pop();
push(‘A’);
push(‘T’);
}
else
error();
break;
case ‘T’: if(s[ip]==’a’|| s[ip]==’(‘)
{
pop();
push(‘B’);
push(‘F’);
}
else
error();
break;

case ‘A’: if(s[ip]==’+’)


{
push(‘A’);
push(‘T’);
ip++;
}
else
if(s[ip]==’$’|| s[ip]==’)’)
pop();
break;
case ‘B’: if(s[ip]==’*’)
{
push(‘B’);
push(‘F’);
ip++;
}
else
if(s[ip]==’+’|| s[ip]==’)’|| s[ip]==’$’)
pop();
break;
else
error();
break;
case ‘F’: if(s[ip]==’a’)
{
pop();
ip++;
}
else
if(s[ip]==’(‘)
{
push(‘)’);
push(‘E’);
ip++;
}
else
error();
break;
case ‘)’: if(s[ip]==’)’)
{
pop();
ip++;
}
else
error();
break;
}
}
if((s[ip]==’$’)&&(stk[top]==’$’))
printf(“Input String is Processed”);
else
error();
}
void error()
{
printf(“error”);
exit(0);
}
char pop()
{
top--;
}
char push(char item)
{
top++;
stk[top]=item;
}

Output:

Enter the input string: a+a*a


Input String is Processed.

Write a c program to implement Recursive Descent


parser.

char is,
char str[20];
int i=0;
main()
{
clrscr();
printf(“enter a string to be parse”);
scanf(“%s”,str);
is=str[0];
E();
getch();
}
E()
{
T();
E_prime();
if(is==’$’)
printf(“successful parsed”);
else
printf(“parsing fail”);
}

E_prime()
{
if(is==’+’)
{
Advance();
T();
E_prime();
}
}

T()
{
F();
T_prime();
}

T_prime()
{
if (is==’*’)
{
Advance();
F();
T_prime();
}
}
F()
{
if (is >=’a’&& is<=’z’)
{
Advance();
}
else
if(is==’(‘)
{
Advance();
E();
if(is==’)’)
Advance();
else
Error();
}
else
Error();
}

Advance()
{
is=str[++i];
}
Error()
{
printf(“there are some syntactic errors”);
}

Input: a+a*a$
Output: Successful parsed

1. Write a Lex program to find length of a string.


%{
int length;
%}
%%
[a-z A-Z 0-9]+ {length=yyleng; }
%%
main()
{
yylex();
printf(“length of given string is %d”,length);
}
int yywrap()
{
return 1;
}

Compile: lex str_length.l


cc lex.yy.c
Run: ./a.out
Input: sree chaitanya

Output: lenth of given string is 14


2. Write a Lex program to find Predecessor & successor of
a character

%{
%}
%%
[a-z A-Z 0-9] {printf(“given character is
%c”,yytext);
printf (“printf(“ its predecessor is %c”,yytext[0]-
1);
printf(“its successor is %c”,yytext[0]+1);
}
%%
main()
{
printf(“ enter a character”);
yylex();
}
int yywrap()
{
return 1;
}

Compile: lex ps.l


cc lex.yy.c
Run: ./a.out
Input: s
Output: given character is S
Its predecessor is r
Its successor is t
3. Write a Lex program to print no.of words
,numbers and Lines in a Text.

%{
Int num_words=0,num_numbers=0,num_lines=0;
%}
Word [a-zA-Z]
Number [0-9]+
%%
{word} {++num_words;}
{number} {++num_numbers;}
\n {++num_lines;}
. {}
%%
main()
{
yylex();
Printf(“no.of words=%d\n no.of numbers=%d\n
no.of
lines=%d\n”,num_wors,num_numbers,num_lines);
}
int yywrap()
{
return 1;
}

Compile: lex count.l


cc lex.yy.c
Run: ./a.out
Input: sree chaitanya 123 34
College 56.
Output: no.of words=3
No.of numbers=3
No.of lines=2

4. Write a Lex specification for identifying positive


integers,negative integers,positive real
numbers,negative real numbers,positive
exponential,negative exponential.

%{
%}

%%
[0-9]+ {
printf(“positive integer”);
}
[-][0-9]+ {
printf(“negative integer”);
}
[0-9]+[.] [0-9]+ {
printf(“positive real numbers”);
}
[-][0-9]+[.] [0-9]+ {
printf(“negative real numbers”);
}
[0-9]+”e”[.] [0-9]+ {
printf(“positive exponential”);
}
[0-9]+”e”[.] [0-9]+ {
printf(“negative exponential”);
}
%%
Main()
{
yylex();
}
int yywrap()
{
return 1;
}

Output :
lex idennum.l
cc lex.yy.c –ll
./a.out

Input: 3
Output : positive number
Input: -0.1
Output : negative number
Input: 1e.1
Output : positive exponential
5. Lex specification for palindrome or not.

%{
int i,j,flag;
%}
%%
[a-z A-z 0-9]* {for(i=0,j=yyleng-1;i<=j;i++,j--)
{
if (yylext[i]==yytext[j])
{
flag=1;
}
else
{
flag=0;
break;
}
}
if (flag==1)
printf(“given string is polyndrome”);
else
printf(“given string is not polyndrome”);
}
%%
main()
{
printf(“enter a string:”);
yylex();
}
int yywrap()
{
return 1;
}
Output :
lex poly.l
cc lex.yy.c
./a.out
madam
palindrome
tree
not palindrome
6. Write a Lex program to identify whethere given
string is word or number or alphanumeric.

%{
%}
%%
[a-zA-Z]* {printf(“given string is WORD”);}
[0-9]* {printf(“given string is NUMBER”);}
[a-zA-Z0-9]* {printf(“given string is
alphanumeric”);}
%%
main()
{
printf(“enter a string”);
yylex();
}
int yywrap()
{
return 1;
}

Compile: lex str.l


cc lex.yy.c
Run: ./a.out
Input: sree
Output: given string is WORD
Input:123
Output: given string is number
Input: sree123
Output: given string is alphanumeric

Das könnte Ihnen auch gefallen