JU CD Lab Manual
JU CD Lab Manual
Lab Manual
On
Compiler Design
4 Write a C program to count blank space and count the no. of lines 5
7 Write a Lex code to check if characters other than alphabets occur in a string 9
8 Write a Lex code to count words that are less than 10 and greater than 5. 10
10 Write a Lex code to identify and count positive and negative numbers 12
CO1: To apply the knowledge of lex tool & yacc tool to develop a scanner & parser.
CO4: To learn the new code optimization techniques to improve the performance of a program in
terms of speed & space.
CO5: To use the knowledge of patterns, tokens & regular expressions for solving a problem.
Course
Program Outcomes
Outcomes
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
CO1 H H L H L
CO2 H L
CO3 L H L
CO4 H H
CO5 H L H
PSO3 H
PROGRAM 1
OBJECTIVE:
Write a C program to identify whether a given line is a comment or not.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
char com[30];
int i=2,a=0;
clrscr();
printf("\n Enter comment:");
gets(com);
if(com[0]=='/')
{
if(com[1]=='/')
printf("\n It is a comment");
else
if(com[1]=='*')
{
for(i=2;i<=30;i++)
{
if(com[i]=='*'&&com[i+1]=='/')
{
printf("\n It is a comment");
a=1;
break;
}
else
continue;
}
if(a==0)
printf("\n It is not a comment");
}
else
printf("\n It is not a comment");
}
else
printf("\n It is not a comment");
getch();
}
1
PROGRAM 2
OBJECTIVE:
Write a C program to test whether a given identifier is valid or not
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char a[10];
int flag, i=1;
clrscr();
printf("\n Enter an identifier:");
gets(a);
if(isalpha(a[0]))
flag=1;
else
printf("\n Not a valid identifier");
while(a[i]!='\0')
{
if(!isdigit(a[i])&&!isalpha(a[i]))
{
flag=0;
break;
}
i++;
}
if(flag==1)
printf("\n Valid identifier");
getch();
}
2
PROGRAM 3
OBJECTIVE:
Write a C program to simulate lexical analyzer for validating operators.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
char s[5];
clrscr();
printf("\n Enter any operator:");
gets(s);
switch(s[0])
{
case'>': if(s[1]=='=')
printf("\n Greater than or equal");
else
printf("\n Greater than");
break;
case'<': if(s[1]=='=')
printf("\n Less than or equal");
else
printf("\nLess than");
break;
case'=': if(s[1]=='=')
printf("\nEqual to");
else
printf("\nAssignment");
break;
case'!': if(s[1]=='=')
printf("\nNot Equal");
else
printf("\n Bit Not");
break;
case'&': if(s[1]=='&')
printf("\nLogical AND");
else
printf("\n Bitwise AND");
3
break;
case'|': if(s[1]=='|')
printf("\nLogical OR");10
else
printf("\nBitwise OR");
break;
case'+': printf("\n Addition");
break;
case'-': printf("\nSubstraction");
break;
case'*': printf("\nMultiplication");
break;
case'/': printf("\nDivision");
break;
case'%': printf("Modulus");
break;
default: printf("\n Not a operator");
}
getch();
}
4
PROGRAM 4
OBJECTIVE:
Write a C program to count blank space and count the no. of lines
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int flag=1;
char i,j=0,temp[100];
clrscr();
printf("Enter the Sentence (add '$' at the end) :: \n\n");
while((i=getchar())!='$')
{
if(i==' ')
i='*';
else if(i=='\t')
i='@';
else if(i=='\n')
flag++;
temp[j++]=i;
}
temp[j]=NULL;
printf("\n\n\nAltered Sentence :: \n\n");
puts(temp);
printf("\n\nNo. of lines = %d",flag);
getch();
}
5
PROGRAM 5
OBJECTIVE:
Write a C program to recognize strings under 'a*', 'a*b+', 'abb'.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char s[20],c;
int state=0,i=0;
clrscr();
printf("\n Enter a string:");
gets(s);
while(s[i]!='\0')
{
switch(state)
{
case 0: c=s[i++];
if(c=='a')
state=1;
else if(c=='b')
state=2;
else
state=6;
break;
case 1: c=s[i++];
if(c=='a')
state=3;
else if(c=='b')
state=4;
else
state=6;
break;
case 2: c=s[i++];
if(c=='a')
state=6;
6
else if(c=='b')
state=2;
else
state=6;
break;
case 3: c=s[i++];
if(c=='a')
state=3;
else if(c=='b')
state=2;
else
state=6;
break;
case 4: c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=5;
else
state=6;
break;
case 5: c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=2;
else
state=6;
break;
case 6: printf("\n %s is not recognised.",s);
exit(0);
}}
if(state==1)
printf("\n %s is accepted under rule 'a'",s);
else if((state==2)||(state==4))
printf("\n %s is accepted under rule 'a*b+'",s);
else if(state==5)
printf("\n %s is accepted under rule 'abb'",s);
getch();
}
7
PROGRAM 6
OBJECTIVE:
Write a Lex code to count total number of tokens
PROGRAM:
%{
int n = 0 ;
%}
// rule section
%%
//count number of keywords
"while" | "if" | "else" { n++;
printf("\t keywords : %s", yytext);}
// count number of keywords
"int" | "float" { n++;
printf("\t keywords : %s", yytext);}
// count number of identifiers
[a-zA-Z_] [a-zA-Z0-9_]* { n++;
printf("\t identifier : %s", yytext);}
// count number of operators
"<=" | "==" | "=" | "++" | "-" | "*" | "+" { n++;
printf("\t operator : %s", yytext);}
// count number of separators
[ () {} | , ; ] { n++;
printf("\t separator : %s", yytext);}
// count number of floats
[0-9]* "." [0-9]+ { n++;
printf("\t float : %s", yytext);}
// count number of integers
[0-9]+ {n++;
printf("\t integer : %s", yytext);}
.;
%%
int main()
{
yylex();
printf("\n total no. of token = %d\n", n);
}
8
PROGRAM 7
OBJECTIVE:
Write a Lex code to check if characters other than alphabets occur in a string
PROGRAM:
%{
int len=0;
%}
/* here . will match any other character than alphabets because alphabets are already matched
above will matches 0 or more characters in front of it. */
// code section
int yywrap() { }
int main()
{
yylex();
return 0;
}
9
PROGRAM 8
OBJECTIVE:
Write a Lex code to count words that are less than 10 and greater than 5.
PROGRAM:
%{
int len=0, counter=0;
%}
%%
[a-zA-Z]+ { len=strlen(yytext);
if(len<10 && len>5)
{counter++;} }
%%
int main()
{
printf("Enter the string:");
yylex();
printf("\n %d", counter);
return 0;
}
10
PROGRAM 9
OBJECTIVE:
Write a Lex code to check valid Mobile Number
PROGRAM:
%{
/* Definition section */
%}
/* Rule Section */
%%
[1-9] [0-9] {9} { printf("\nMobile Number Valid\n"); }
// driver code
int main()
{
printf("\nEnter Mobile Number : ");
yylex();
printf("\n");
return 0;
}
11
PROGRAM 10
OBJECTIVE:
Write a Lex code to identify and count positive and negative numbers
PROGRAM:
%{
int positive_no = 0, negative_no = 0;
%}
[0-9] + { positive_no++;
printf("positive number = %s\n", yytext);} // positive number
%%
int main()
{
yylex();
printf ("number of positive numbers = %d, \n number of negative numbers = %d\n",
positive_no, negative_no);
return 0;
}
12
PROGRAM 11
OBJECTIVE:
Write a C program to generate a three-address code for a given expression.
PROGRAM:
#include<stdio.h>
#include<string.h>
void pm();
void plus();
void div();
int i, ch, j, l, addr=100;
char ex[10], exp[10] , exp1[10], exp2[10], id1[5], op[5], id2[5];
void main()
{
clrscr();
while(1)
{
printf("\n1.assignment\n2.arithmetic\n3.relational\n4.Exit\nEnter the choice:");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("\nEnter the expression with assignment operator:");
scanf("%s", exp);
l=strlen(exp);
exp2[0]='\0';
i=0;
while(exp[i]!='=')
{
i++;
}
strncat(exp2, exp, i);
13
strrev(exp);
exp1[0]='\0';
strncat(exp1, exp, l-(i+1));
strrev(exp1);
printf("Three address code:\ntemp=%s\n%s=temp\n", exp1, exp2);
break;
case 2:
printf("\nEnter the expression with arithmetic operator:");
scanf("%s", ex);
strcpy(exp, ex);
l=strlen(exp);
exp1[0]='\0';
for(i=0;i<l;i++)
{
if(exp[i]=='+'||exp[i]=='-')
{
if(exp[i+2]=='/'||exp[i+2]=='*')
{
pm();
break;
}
else
{
plus();
break;
}
}
else if(exp[i]=='/'||exp[i]=='*')
{
div();
break;
14
}
}
break;
case 3:
printf("Enter the expression with relational operator");
scanf("%s%s%s", &id1, &op, &id2);
if(((strcmp(op,"<")==0)||(strcmp(op,">")==0)||(strcmp(op,<=")==0)||
(strcmp(op,">=")==0)||(strcmp(op,"==")==0)||(strcmp(op,"!=")==0))==0)
printf("Expression is error");
else
{
printf("\n%d\tif %s%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);
j=l-i-1;
strncat(exp1, exp, j);
strrev(exp1);
15
printf("Three address code:\ntemp=%s\ntemp1=%c%ctemp\n", exp1, exp[j+1], exp[j]);
}
void div()
{
strncat(exp1, exp, i+2);
printf("Three address code:\ntemp=%s\ntemp1=temp%c%c\n", exp1, exp[i+2],
exp[i+3]);
}
void plus()
{
strncat(exp1, exp, i+2);
printf("Three address code:\ntemp=%s\ntemp1=temp%c%c\n", exp1, exp[i+2],
exp[i+3]);
}
16
PROGRAM 12
OBJECTIVE:
Write a C program to implement Recursive Descent Parsing.
PROGRAM:
#include<stdio.h>
#include<conio.h>
int c=0;
char p[20];
void s();
void l();
void lprime();
void l()
{
s();
lprime();
}
void lprime()
{
if(p[c]==',')
{
c++;
s();
lprime();
}
}
void s()
{
if(p[c]=='a')
c++;
else if(p[c]=='(')
{
c++;
l();
if(p[c]==')')
c++;
else
c--;
}
17
else
printf("\nInvalid Expression");
}
void main()
{
clrscr();
printf("\nImplementation of RECURSIVE DESCENT PARSER\n");
printf("\nEnter the Expression:\n");
scanf("%s",p);
s();
if(p[c]=='$')
printf("\nThe String is accepted");
else
printf("\nThe string is rejected");
getch();
}
18
PROGRAM 13
OBJECTIVE:
Write a C program to implement Shift Reduce Parsing.
PROGRAM:
#include<stdio.h>
#include<iostream.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>
struct stru1
{
char non_ter[1], pro[25];
}cfg[25];
int n, st=-1, j, i, t=-1, m;
int v, c, p=1;
char str[20], stack[20], ch, tmp[10];
void match(int k);
void matchl(int k);
void main()
{
clrscr();
cprintf("Enter the number of productions:\n\r");
cscanf("%d", &n);
cprintf("\n\r");
cprintf("Enter the productions on LEFT and RIGHT sides:\n\r");
for(i=0; i<n; i++)
{
cscanf("%s", cfg[i].non_ter);
cprintf("\n\r");
cprintf("->\n\r");
cscanf("%s", cfg[i].pro);
cprintf("\n\r");
}
cprintf("Enter the input string:\n\r");
cscanf("%s", str);
cprintf("\n\r");
i=0;
do
19
{
ch=str[i];
stack[++st]=ch;
tmp[0]=ch;
match(1);
i++;
} while(str[i]!='\0');
c=st;
v=st;
cputs(stack);
cprintf("\n\r");
while(st!=0)
{
v=--st;
t=-1;
p=0;
while(v<=c)
{
tmp[++t]=stack[v++];
p++;
}
matchl(p);
}
cfg[0].non_ter[1]='\0';
if(strcmp(stack, cfg[0].non_ter)==0)
cprintf("String is present in Grammar G\n\r");
else
cprintf("String is not present in Grammar G\n\r");
}
void match (int k)
{
for(j=0;j<n;j++)
{
if(strlen(cfg[j].pro)==k)
{
if(strcmp(tmp, cfg[j].pro)==0)
{
stack[st]=cfg[j].non_ter[0];
break;
}
20
}
}
}
void matchl (int k)
{
int x=1, y;
y=k-1;
for(j=0;j<n;j++)
{
if(strlen(cfg[j].pro)==k)
{
if(strcmp(tmp, cfg[j].pro)==0)
{
k=c-k+1;
stack[k]=cfg[j].non_ter[0];
do
{
stack[k+x]='\0';
tmp[t--]='\0';
c--;
x++;
} while(x<=y);
tmp[t]='\0';
cputs(stack);
cprintf("\n\r");
break;
}
}
}
}
21
PROGRAM 14
OBJECTIVE:
Write a C program to implement a symbol table.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
void main()
{
int i=0, j=0, x=0, n, flag=0; void *p, *add[15];
char ch, srch, b[15], d[15], c;
clrscr();
printf("expression terminated by $:");
while((c=getchar())!='$')
{
b[i]=c; i++;
}
n=i-1;
printf("given expression:");
i=0;
while(i<=n)
{
printf("%c", b[i]);
i++;
}
printf("symbol table\n");
printf("symbol\taddr\ttype\n");
while(j<=n)
{
c=b[j];
if(isalpha(toascii(c)))
{
if(j==n)
{
p=malloc(c);
22
add[x]=p;
d[x]=c;
printf("%c\t%d\tidentifier\n", c, p);
}
else
{
ch=b[j+1];
if(ch=='+'||ch=='-'||ch=='*'||ch=='=')
{
p=malloc(c);
add[x]=p;
d[x]=c;
printf("%c\t%d\tidentifier\n", c, p);
x++;
}
}
}
j++;
}
printf("the symbol is to be searched\n");
srch=getch();
for(i=0;i<=x;i++)
{
if(srch==d[i])
{
printf("symbol found\n");
printf("%c%s%d\n", srch, "@address", add[i]);
flag=1;
}
}
if(flag==0)
printf("symbol not found\n");
getch();
}
23