Compiler Design: Aim: Program For 3-Address Code
Compiler Design: Aim: Program For 3-Address Code
SA|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()
{
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;
Output:
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
%{
%}
%%
[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;
}
%{
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;
}
%{
%}
%%
[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;
}