Vous êtes sur la page 1sur 50

Systems Programming Lab

Exp 1:
Date:
KEYWORD COUNT

AIM
Write a C program to count the number of keywords in a C program file.

ALGORITHM

Step 1 : Start
Step 2: Store the 32 keywords in a 2-D array and declare a count array to store count.
Step 3 : Open the source file in read mode.
Step 4 : Read each characters from the file and store it in a character array until detects a
delimiter.
Step 5 : Compare the string stored in the character array with the 32 keywords.
Step 6 : If match found, increment the corresponding keyword count.
Step 7 : Print the keyword and its count whose number of occurrences is not zero.
Step 8 : Stop.

1
Dept. of CSE
Systems Programming Lab

PROGRAM

#include<stdio.h>
#include<string.h>
int main()
{
FILE *fp;
char kw[32][10] = {"auto","case","break","char","continue","const",
"default","do","double","else","enum","extern",
"float","for","goto","if","int","long","register",
"return","short","sizeof","signed","static","struct",
"switch","typedef","union","unsigned","void",
"volatile","while"};
char c,temp[32],source[100];
int count[32],i,j;

printf("Enter the file name: ");


gets(source);
fp=fopen(source,"r");
for(i=0;i<32;i++)
count[i]=0;
c=fgetc(fp);
while(c!=EOF)
{
i=0;
while(c!=' '&&c!='(' &&c!='\n'&&c!=')')
{
temp[i]=c;
c=fgetc(fp);
i++;
}
temp[i]='\0';
for(j=0;j<32;j++)
{
if((strcmp(temp,kw[j]))==0)
count[j]=count[j]+1;
}
c=fgetc(fp);
}
printf("KEYWORD COUNTING");
printf("\n.....................\n");
for(i=0;i<32;i++)
{
if(count[i]!=0)

2
Dept. of CSE
Systems Programming Lab

printf("%s",kw[i]);
printf("\t%d\n",count[i]);
}
}
fclose(fp);
}

input.c

int main( )
{
int a;
int i;
char c;
for(i=0;i<10;i++)
{
printf("Hai");
}

}
OUTPUT

Enter the file name: input.c


KEYWORD COUNTING
.....................
char 1
for 1
int 3

RESULT

The program is successfully executed and output obtained.

3
Dept. of CSE
Systems Programming Lab

Exp 2:
Date:

ABSOLUTE LOADER

AIM
To implement an Absolute loader in C language.

ALGORITHM

Step 1: Start
Step 2: Open input file in read mode and output file in write mode.
Step 3: Read the Header record.
Step 4: Read start address and length.
Step 5: Do steps 6 and 7 until input equals End Record.
Step 6: Read each Text record from the input file.
Step 7: Write the memory address and data to the output file.
Step 8: Close files.
Step 9: Stop

4
Dept. of CSE
Systems Programming Lab

PROGRAM
#include<stdio.h>
#include<string.h>
main()
{
char input[10];
int start,length,address;
FILE *fp1,*fp2;
fp1=fopen("input1","r");
fp2=fopen("output1","w");
fscanf(fp1,"%s",input);
while(strcmp(input,"E")!=0)
{
if(strcmp(input,"H")==0)
{
fscanf(fp1,"%d",&start);
fscanf(fp1,"%d",&length);
fscanf(fp1,"%s",input);
}
else if(strcmp(input,"T")==0)
{
fscanf(fp1,"%d",&address);
fscanf(fp1,"%s",input);
fprintf(fp2,"%d\t%c%c\n",address,input[0],input[1]);
fprintf(fp2,"%d\t%c%c\n",(address+1),input[2],input[3]);
fprintf(fp2,"%d\t%c%c\n",(address+2),input[4],input[5]);
address+=3;
fscanf(fp1,"%s",input);
}
else
{
fprintf(fp2,"%d\t%c%c\n",address,input[0],input[1]);
fprintf(fp2,"%d\t%c%c\n",(address+1),input[2],input[3]);
fprintf(fp2,"%d\t%c%c\n",(address+2),input[4],input[5]);
address+=3;
fscanf(fp1,"%s",input);
}
}
fclose(fp1);
fclose(fp2);
printf("FINISHED");
}

5
Dept. of CSE
Systems Programming Lab

input1:
H 1000 232
T 1000 142033 483039 102036
T 2000 298300 230000 282030 302015
E

output1

1000 14
1001 20
1002 33
1003 48
1004 30
1005 39
1006 10
1007 20
1008 36
2000 29
2001 83
2002 00
2003 23
2004 00
2005 00
2006 28
2007 20
2008 30
2009 30
2010 20
2011 15

RESULT

The program is successfully executed and output obtained.

6
Dept. of CSE
Systems Programming Lab

Exp 3:
Date:

MACRO PREPROCESSOR
AIM

To implement a single pass macro processor in C language.

ALGORITHM

Step 1: Start
Step 2:Read the macro input file line by line and get label,operation and operand
Step 3:Save the macro expansions in files
3.1: If the operation field is “MACRO” then copy the label to NAMTAB
3.2: Open a file with the name of the label(which is the macro name) and get the file
pointer in DEFTAB
3.3: Read the macro expansion and write it to the file until MEND
Step 4: Replace Macros with expansion
4.1: Check whether the operation field have a macro name by comparing it with the Macro
names stored in NAMTAB.
4.2: If it is a macro then replace it with the contents stored in the corresponding Macro file.
Step 5:Write the expanded output to another file and print it
Step 6:Stop

7
Dept. of CSE
Systems Programming Lab

PROGRAM

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
int n,flag,i;
char ilab[20],iopd[20],oper[20],NAMTAB[20][20];
FILE *fp1,*fp2,*DEFTAB;
fp1=fopen("macroin","r");
fp2=fopen("macroout","w");
n=0;
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
while(!feof(fp1))
{
if(strcmp(iopd,"MACRO")==0)
{
strcpy(NAMTAB[n],ilab);
DEFTAB=fopen(NAMTAB[n],"w");
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
while(strcmp(iopd,"MEND")!=0)
{
fprintf(DEFTAB,"%s\t%s\t%s\n",ilab,iopd,oper);
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
}
fclose(DEFTAB);n++;}
else{flag=0;for(i=0;i<n;i++)
{
if(strcmp(iopd,NAMTAB[i])==0)
{flag=1;
DEFTAB=fopen(NAMTAB[i],"r");
fscanf(DEFTAB,"%s%s%s\n",ilab,iopd,oper);
while(!feof(DEFTAB))
{
fprintf(fp2,"%s\t%s\t%s\n",ilab,iopd,oper);
fscanf(DEFTAB,"%s%s%s",ilab,iopd,oper);
}
break;
}
}
if(flag==0)
fprintf(fp2,"%s\t%s\t%s\n",ilab,iopd,oper);
}
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
}

8
Dept. of CSE
Systems Programming Lab

fclose(fp1);
fclose(fp2);
printf("Finished");
}

INPUT

macroin

M1 MACRO **
** LDA N1
** ADD N2
** STA N3
** MEND **
M2 MACRO **
** LDA N1
** SUB N2
** STA N4
** MEND **
M3 MACRO **
** LDA N1
** MUL N2
** STA N5
** MEND **
** START 1000
** M3 **
** M2 **
** M1 **
** END **

9
Dept. of CSE
Systems Programming Lab

OUTPUT

macroout

** START 1000
** LDA N1
** MUL N2
** STA N5
** LDA N1
** SUB N2
** STA N4
** LDA N1
** ADD N2
** STA N3
** END **

RESULT

The program is successfully executed and output obtained.

10
Dept. of CSE
Systems Programming Lab

Exp 4:
Date:

CODE GENERATOR FOR ARITHMETIC EXPRESSION

AIM

To write a program to design a code generator for arithmetic expression by taking a set
of Intermediate Code as input

ALGORITHM

Step 1: Start
Step 2: Enter the filename of the intermediate code.
Step3: Read each line and make the target code correspondingly by using 2 statements:-
Step 3.1 Use MOV instructions to store anyone of the operand in a temporary
register.
Step 3.2 Use the corresponding command according to the operator ie ADD, MUL,
SUB, DIV to convert in to target code
Step 4: Repeat Step3 until the end of the file
Step 5: Stop

11
Dept. of CSE
Systems Programming Lab

PROGRAM
#include<stdio.h>
main()
{
int i=2,j=0,c=0;
char ip[10],kk[10];
FILE *fp;
printf("\n Enter the filename of the intermediate code");
scanf("%s", kk);
fp=fopen(kk,"r");
if(fp==NULL)
{
printf("\nError in Opening the file");
}
while(!feof(fp))
{
fscanf(fp,"%s\n",ip);
printf("\t\t%s\n",ip);
c++;
}
printf("\n---------------------------------------------------------\n");
printf("\tStatement \t\t target code\n");
printf("\n---------------------------------------------------------\n");
rewind(fp);
while(!feof(fp)&&j<c)
{
fscanf(fp,"%s",ip);
printf("\t%s",ip);
printf("\t\t\tMOV %c,R%d\n\t",ip[i],j);
if(ip[i+1]=='+')
printf("\t\t\tADD");
else if(ip[i+1]=='-')
printf("\t\t\tSUB");
else if(ip[i+1]=='*')
printf("\t\t\tMUL");
else
printf("\t\t\tDIV");
printf(" %c,R%d\n\t",ip[i+2],j);
printf("\t\t\tMOV R%d,%c\n",j,ip[0]);
printf("\n");
j++;
}
printf("\n-------------------------------------------------------\n");
fclose(fp);
}

12
Dept. of CSE
Systems Programming Lab

input.txt

X=a-b
Y=a/b
Z=a*b
F=Z+b

Output

Enter the filename of the intermediate code input.txt


X=a-b
Y=a/b
Z=a*b
F=Z+b

---------------------------------------------------------
Statement target code

---------------------------------------------------------
X=a-b MOV a,R0
SUB b,R0
MOV R0,X

Y=a/b MOV a,R1


DIV b,R1
MOV R1,Y

Z=a*b MOV a,R2


MUL b,R2
MOV R2,Z

F=Z+b MOV Z,R3


ADD b,R3
MOV R3,F

-------------------------------------------------------

RESULT

The program is successfully executed and output obtained.

13
Dept. of CSE
Systems Programming Lab

Exp 5:
Date:

GENERATION OF INTERMEDIATE CODE

AIM
To write a program to generate intermediate code for the given arithmetic expression.

ALGORITHM

Step 1:Start

Step 2:Declare expr,postfix,three_addr,stack

Step 2:Get the input string in expr

Step 3:Generate the postfix notation of the input expression

3.1:Read the character of the input

3.2:If it is '(' then pop the contents of the stack into postfix until ')' is found.

3.3:If it is '+','-','*',/','(' then compare the priority of the element on the stack top and the input. If

the priority of the element in the stack top has a higher priority than the input then pop the

elements in the stack until the stacktop has an element with lower priority,

3.4:The priority is defined such that '(' has highest priority when in input and has lowest priority in

stack. The priority of others are in the order ^ greater than /, * greater than +,-

3.5:If the input is none of the above put it in postfix

3.6:Repeat steps 3.1 to 3.5 till the end of the input expression.

3.7:If the stack is not empty pop the contents of the stack to postfix.

Step 4:Analyze the postfix notation and generate three address code.

4.1 If the postfix notation has an alphabet put it in stack

4.2:Otherwise pop the top two elements from the stack and write the corresponding three address

code into three_addr

Step 5: Print the three address code

Step 6: Stop

14
Dept. of CSE
Systems Programming Lab

PROGRAM
#include<stdio.h>
int isp(char ch)
{
return(ch=='('?0:ch=='+'||ch=='-'?1:ch=='*'||ch=='/'?2:-1);
}
int icp(char ch)
{
return(ch=='('?4:ch=='+'||ch=='-'?1:ch=='*'||ch=='/'?2:-1);
}

void main()
{
char expr[80],postfix[80];
char three_addr[20][20],ch,stack[50],opr1,opr2;
int i,c,R=0,top=-1,ip=0;
printf("\nEnter the expression:");
gets(expr);
for(i=0;expr[i]!='\0';i++)
{
ch=expr[i];
switch(ch)
{
case')':while(stack[top]!='(')
postfix[ip++]=stack[top--];
top--;
break;
case'+':
case'-':
case'/':
case'*':
case'(':while(isp(stack[top])>=icp(ch))
postfix[ip++]=stack[top--];
stack[++top]=ch;
break;
default:postfix[ip]=ch;
ip++;
}
}
while(top>=0)
postfix[ip++]=stack[top--];
postfix[ip]='\0';
top=-1;
for(i=0;postfix[i]!='\0';i++)
{
ch=postfix[i];

15
Dept. of CSE
Systems Programming Lab

if(isalpha(ch))
stack[++top]=ch;
else
{
opr2=stack[top--];
opr1=stack[top--];
c=0;
three_addr[R][c++]='t';
three_addr[R][c++]=R+48;
three_addr[R][c++]='=';
if(opr1>='0'&&opr1<='9')
{
three_addr[R][c++]='t';
three_addr[R][c++]=opr1;
}
else
three_addr[R][c++]=opr1;
three_addr[R][c++]=ch;
if(opr2>='0'&&opr2<='9')
{
three_addr[R][c++]='t';
three_addr[R][c++]=opr2;
}
else
three_addr[R][c++]=opr2;
three_addr[R][c++]='\0';
stack[++top]=R+48;
R++;
}
}
printf("\n GENERATION OF INTERMEDIATE CODE \n");
for(i=0;i<R;i++)
printf("\n%s",three_addr[i]);
return;
}

OUTPUT

Enter the expression: A+(B-C)*D


GENERATION OF INTERMEDIATE CODE
t0=B-C
t1=t0*D
t2=A+t1

16
Dept. of CSE
Systems Programming Lab

Exp 6:
Date:

LEXICAL ANALYSER

AIM

To write a program in C to implement lexical analyzer for C program

ALGORITHM

Step 1 : Start
Step 2 : Open source file in read mode
Step 3 : Declare the 32 keywords in a 2-D array and set counts for each keywords and
clear the count array. Also declare an array for storing the operator count for
arithmetic operators
Step 4: Declare array for storing count of Identifiers and Punctuators.
Step 5: Read each characters from the file and store in a character array until detects a
delimiter
Step 6 : Make it a string and compare it with the 32 keywords
Step 7 : If match found, increment the corresponding keyword count
Step 8 : If the string is basic data type ( int, char, double or float), then store the following
strings as identifiers.
Step 9 : If the character is an arithmetic operator ( +,-,*,/ ), increment the corresponding
count.
Step 10: If the character is punctuator ('(',')','{','}',',',';','=','#','[',']'), then increment the
corresponding count.
Step 11: Print the keyword and its count whose number of occurrences is not zero.
Step 12 : Print the identifier array.
Step 13 : Print the operator and its count whose number of occurrences is not zero.
Step 14 : Print the punctuator and its count whose number of occurrences is not zero.
Step 15 : Stop

17
Dept. of CSE
Systems Programming Lab

PROGRAM
#include<stdio.h>
#include<string.h>
char op[4]={'+','-','*','/'};
char pun[10]={'(',')','{','}',',',';','=','#','[',']'};
int opc[4],punc[10];
void opcount(char z)
{
int p;
for(p=0;p<4;p++)
if(z==op[p])
opc[p]=opc[p]+1;
}
void pun_count(char z)
{
int q;
for(q=0;q<10;q++)
if(z==pun[q])
punc[q]=punc[q]+1;
}
main()
{
FILE *ifp;
int i,b[32],j,m=0,n=0;
char ide[20][15];
char a[32][10]={"auto","break","case","char","continue","const","default","do","double","else",
"enum","extern","float","for","goto","if","int","long","register","return","short","sizeof","signed"
,"static","struct","switch","typedef","union","unsigned","void","volatile","while"};
char source[20],c,temp[30];
char id[4][7]={"int","float","char","double"};

18
Dept. of CSE
Systems Programming Lab

for(i=0;i<=4;i++)
opc[i]=0;
printf("\nEnter the source filename: ");
gets(source);
ifp=fopen(source,"r");
for(i=0;i<32;i++)
b[i]=0;
for(i=0;i<20;i++)
for(j=0;j<15;j++)
ide[i][j]=' ';
c=fgetc(ifp);
while(c!=EOF)
{
opcount(c);
pun_count(c);
i=0;
while((c!=' ')&&(c!='(')&&(c!=')')&&(c!='\n'))
{
temp[i]=c;
c=fgetc(ifp);
pun_count(c);
opcount(c);
i++;
}
temp[i]='\0';
for(j=0;j<32;j++)
{
if(strcmp(temp,a[j])==0)
b[j]=b[j]+1;
}

19
Dept. of CSE
Systems Programming Lab

for(j=0;j<4;j++)
{
if((strcmp(temp,id[j])==0))
{
c=fgetc(ifp);
while(c!='\n')
{
while(c==' ')
c=fgetc(ifp);
while((c!=' ')&&(c!=';')&&(c!='(')&&(c!=')')&&(c!='='))
{
ide[m][n]=c;
n++;
c=fgetc(ifp);
pun_count(c);
}
ide[m][n]='\0';
m++;
n=0;
if(c=='=')
while((c!=',')&&(c!=';'))
c=fgetc(ifp);
c=fgetc(ifp);
opcount(c);
pun_count(c);
}
}
}
c=fgetc(ifp);
}
printf("\n\n Keyword count\n");

20
Dept. of CSE
Systems Programming Lab

for(i=0;i<32;i++)
{
if(b[i]!=0)
{
for(j=0;j<10;j++)
printf("%c",a[i][j]);
printf("=%d\n",b[i]);
}
}
printf("\n Identifier\n");
for(i=0;i<m;i++)
{
for(j=0;j<15;j++)
printf("%c",ide[i][j]);
printf("\n");
}
printf("\n Operators\n");
for(i=0;i<4;i++)
if(opc[i]!=0)
printf("%c\t=%d\n",op[i],opc[i]);
printf("\n Punctuator\n");
for(i=0;i<10;i++)
if(punc[i]!=0)
printf("%c\t=%d\n",pun[i],punc[i]);
}

21
Dept. of CSE
Systems Programming Lab

INPUT
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
char d;
double id;
a=10;
b=5;
c=a+b;
printf("The sum is %d",c);
getch();
}

OUTPUT
Enter the source filename: input.c
Keyword count
char =1
double =1
int =1
void =1

Identifier
a,b,c
d
id

Operators
+ =1

Punctuator

( =3
) =3
{ =1
} =1
, =3
; =8
= =3
# =2

RESULT

The program is successfully executed and output obtained.

22
Dept. of CSE
Systems Programming Lab

Exp 7:
Date:

RECURSIVE DESCENT PARSING FOR ARITHMETIC GRAMMAR

AIM

To write a program to implement arithmetic grammar


E->TE’
E’->+TE’/-TE'/£
T->FT’
T’->/FT'/*FT’/£
F->(E)/id

ALGORITHM

Step1: Start
Step 2: Accept the input string
Step 3. Check the input with the given grammar
Step 4: Choose an A-production , A->X1X2.......Xk
Step 5: Repeat Steps 6 to Step 7 from i=1 to k
Step 6: If Xi is a nonterminal call procedure Xi( )
Step 7: If Xi equals the current input symbol a, advance the input to the next symbol.
Step 8: If the grammar is matched, then print “Successfully parsed”.Otherwise Print “Error”
Step 9: Stop

23
Dept. of CSE
Systems Programming Lab

PROGRAM
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char str1[15];
char *str=str1;
void error()
{
printf("Syntax error");
exit(0);
}
void advance()
{
str=str+1;
return;
}

F()
{
if(str[0]=='i')
{
advance();
if(str[0]=='d')
{
advance();
}
else
error();
}
else if(str[0]=='(')
{
advance();
E();
if(str[0]==')')
advance();
else
error();
}
else
error();
return;
}

EO()
{

24
Dept. of CSE
Systems Programming Lab

if(str[0]=='+'||str[0]=='-')
{
advance();
T();
EO();
}
return;
}

T()
{
F();
TO();
return;
}

TO()
{
if(str[0]=='*'||str[0]=='/')
{
advance();
F();
TO();
}
return;
}
E()
{
T();
EO();
return;
}

void main()
{
printf("Enter the string");
scanf("%s",str);
E();
printf("Successfully Parsed");
}

25
Dept. of CSE
Systems Programming Lab

OUTPUT
Enter the string
id+id-(id*id)/id
Successfully Parsed

Enter the string


(id+id)*
Syntax error

RESULT

The program is successfully executed and output obtained.

26
Dept. of CSE
Systems Programming Lab

Exp 8:
Date:
DESIGN OF LL(1) PARSER
AIM:

To write a C program to implement non recursive predictive parsing.

E->TE’
E’->+TE’/£
T->FT’
T’->*FT’/£
F->(E)/i

ALGORITHM:

Step 1: Start
Step 2:Check the grammer by using LL1 parsing table shown below:-

INPUT SYMBOL
NON-
TERMINA i + * ( ) $
L

E E TE’ E TE’

E’ E->+TE' E’ ε E’ ε

T FT’ T FT’
T

T’ ε T’ *FT’ T’ ε T’ ε
T’

F F i F (E)

Step 3: Accept the input. Append ‘$’ to the input. Place ‘$’ and ‘E’ in the stack.
Step 4: If the terminal present in the top of the stack is matching with the input symbol in
the input buffer then pop the symbol from the stack and increment the input
pointer
Step 5:If a non-terminal symbol is present at the stack top then replace it with the right
hand side of the corresponding production rule.
Step 6:If there is no matching rule for the current input print “Syntax Error”.

27
Dept. of CSE
Systems Programming Lab

Step 7: Repeat steps until $ is present in the stack top and input pointer points to ‘$’.
Step 8: Stop

PROGRAM

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char stack1[50];
int top=0;
char ip_sym[15];
int ip_ptr=0;
void E()
{
if((ip_sym[ip_ptr]=='i')||ip_sym[ip_ptr]=='(')
{
printf("\n\t\tE->TE'");
top++;
stack1[top]='O';
top++;
stack1[top]='E';
top++;
stack1[top]='T';
}
else
{
printf("Syntax Error");
exit(0);
}
}

void EO()
{
if(ip_sym[ip_ptr]=='+')
{
printf("\n\t\tE'->+TE'");
top++;
stack1[top]='O';
top++;
stack1[top]='E';
top++;
stack1[top]='T';
top++;
stack1[top]='+';
}

28
Dept. of CSE
Systems Programming Lab

else if(ip_sym[ip_ptr]==')'|| ip_sym[ip_ptr]=='$')


{
printf("\n\t\tE'->e");
}
else
{
printf("Syntax Error");
exit(0);
}
}

void T()
{
if(ip_sym[ip_ptr]=='i'|| ip_sym[ip_ptr]=='(')
{
printf("\n\t\tT->FT'");
top++;
stack1[top]='O';
top++;
stack1[top]='T';
top++;
stack1[top]='F';

}
else
{
printf("Syntax Error");
exit(0);
}
}

void TO()
{
if(ip_sym[ip_ptr]=='*')
{
printf("\n\t\tT'->*FT'");
top++;
stack1[top]='O';
top++;
stack1[top]='T';
top++;
stack1[top]='F';
top++;
stack1[top]='*';
}

29
Dept. of CSE
Systems Programming Lab

else if(ip_sym[ip_ptr]==')'|| ip_sym[ip_ptr]=='$' || ip_sym[ip_ptr]=='+')


printf("\n\t\tT'->e");
else
{
printf("Syntax Error");
exit(0);
}
}

void F()
{
if((ip_sym[ip_ptr]=='i'))
{
printf("\n\t\tF->i");
top++;
stack1[top]='i';
}
else if(ip_sym[ip_ptr]=='(')
{
printf("\n\t\tF->(E)");
top++;
stack1[top]=')';
top++;
stack1[top]='E';
top++;
stack1[top]='(';
}
else
{
printf("\n\t\tSyntax Error");
exit(0);
}
}

void advance()
{
ip_ptr++;
}

void main()
{
int i;
printf("\n\tGrammar:");
printf("\n\t\tE->TE'\n\t\tE'->+TE'/e\n\t\tT->FT'");
printf("\n\t\tT'->*FT'/e\n\t\tF->(E)/i");
printf("\n\t\tEnter the input");

30
Dept. of CSE
Systems Programming Lab

gets(ip_sym);
strcat(ip_sym,"$");
stack1[top]='$';
top++;
stack1[top]='E';
printf("\n\nInput:");
for(i=ip_ptr;i<strlen(ip_sym);i++)
printf("%c",ip_sym[i]);
printf("\tStack:");
for(i=0;i<=top;i++)
printf("%c",stack1[i]);
printf("\n");
while(ip_sym[ip_ptr]!='$'||stack1[top]!='$')
{
if(ip_sym[ip_ptr]==stack1[top])
{
top--;
advance();
}
else if(stack1[top]=='E'&&stack1[top-1]=='O')
{
top--;
top--;
EO();
}
else if(stack1[top]=='E')
{
top--;
E();
}
else if( stack1[top]=='T'&&stack1[top-1]=='O')
{

top--;
top--;
TO();
}
else if( stack1[top]=='T')
{

top--;
T();
}
else if(stack1[top]=='F')
{
top--;

31
Dept. of CSE
Systems Programming Lab

F();
}
printf("\n\nInput:");
for(i=ip_ptr;i<strlen(ip_sym);i++)
printf("%c",ip_sym[i]);
printf("\tStack:");
for(i=0;i<=top;i++)
printf("%c",stack1[i]);
printf("\n");
}
printf("Successfully parsed");

OUTPUT

Grammar:
E->TE'
E'->+TE'/e
T->FT'
T'->*FT'/e
F->(E)/i
Enter the inputi+i*i

Input:i+i*i$ Stack:$E
E->TE'
Input:i+i*i$ Stack:$OET
T->FT'
Input:i+i*i$ Stack:$OEOTF
F->i
Input:i+i*i$ Stack:$OEOTi
Input:+i*i$ Stack:$OEOT
T'->e
Input:+i*i$ Stack:$OE
E'->+TE'
Input:+i*i$ Stack:$OET+
Input:i*i$ Stack:$OET
T->FT'

32
Dept. of CSE
Systems Programming Lab

Input:i*i$ Stack:$OEOTF
F->i
Input:i*i$ Stack:$OEOTi
Input:*i$ Stack:$OEOT
T'->*FT'
Input:*i$ Stack:$OEOTF*
Input:i$ Stack:$OEOTF
F->i
Input:i$ Stack:$OEOTi
Input:$ Stack:$OEOT
T'->e
Input:$ Stack:$OE
E'->e
Input:$ Stack:$
Successfully parsed

RESULT

The program is successfully executed and output obtained.

33
Dept. of CSE
Systems Programming Lab

Exp 9:
Date:

IMPLEMENTATION OF OPERATOR PRECEDENCE PARSER


AIM

To write a program to implement operator precedence parser for the following grammar

E E+E|E*E|i

ALGORITHM

Step1: Start
Step 2: Enter the input string (i+i*i) with delimiter '$' ($i+i*i$).
Step 3: Set the pointer to the first symbol of the input string.
Step 4:Apply precedence by using the operator precedence table given below:-

i + * $
i - > > >
+ < > < >
* < > > >
$ < < < -

Step 5: If any of the input symbol is in between the 'handle 'symbol ie '< >'.Then we can
eliminate that input symbol. Then repeat step 4 and 5 until the input string
become $$ otherwise print Error.
Step 6: Stop

34
Dept. of CSE
Systems Programming Lab

PROGRAM
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int scan(int);
int number(char);
int findingG();
int findingL();
int erase(char);
char opers[4]={'i','+','*','$'},input[50];
char table[4][4]={'-','>','>','>',
'<','>','<','>',
'<','>','>','>',
'<','<','<','-',};
int scan(int position)
{
int i;
for(i=strlen(input);i>=position;i--)
input[i]=input[i-1];
return i;
}
int number(char operator)
{
int i;
for(i=0;i<sizeof(opers);i++)
if(opers[i]==operator)
return i;
return -1;
}

35
Dept. of CSE
Systems Programming Lab

int findingG()
{
int i;
for(i=0;i<strlen(input);i++)
if(input[i]=='>')
return i;
return-1;
}
int findingL(int position)
{
int i;
for(i=position;i>=0;i--)
if(input[i]=='<')
return i;
return -1;
}
int erase(char ch)
{
int i,j;
for(i=0;i<strlen(input);i++)
if(input[i]==ch)
for(j=i;j<strlen(input);j++)
input[j]=input[j+1];
return -1;
}
void main()
{
int i,G,L;
printf("\n\n\t\t***OPERATOR PRECEDENCE PARSING***\n\n");
printf("\tEnter the input:");
scanf("%s",input);

36
Dept. of CSE
Systems Programming Lab

for(i=1;i<strlen(input);i+=2)
{
scan(i);
input[i]=table[number(input[i])][number(input[i+1])];
}
printf("\n\tThe parsed output is \n");
printf("\nNext stage:\n");
printf("%s",input);
while(strcmp(input,"$$"))
{
G=findingG();
L=findingL(G);input[L]='x';
input[L+1]=table[number(input[L-1])][number(input[G+1])];
input[G]='x';
erase('x');
erase('-');
printf("\nNext stage:\n");
printf("%s",input);
}
}

37
Dept. of CSE
Systems Programming Lab

OUTPUT

***OPERATOR PRECEDENCE PARSING***


Enter the input:$i+i*i$
The parsed output is
Next stage:
$<i>+<i>*<i>$
Next stage:
$<+<i>*<i>$
Next stage:
$<+<*<i>$
Next stage:
$<+<*>$
Next stage:
$<+>$
Next stage:
$$

RESULT

The program is successfully executed and output obtained.

38
Dept. of CSE
Systems Programming Lab

Exp 10:
Date:
FAMILIARIZATION WITH LEX

AIM

Write a program using lex to count the number of consonants and vowels in a string.

DESCRIPTION

The unix utility lex parses a file of characters. It uses regular expression matching;
typically it is used to ‘tokenize’ the contents of the file. In that context, it is often used together
with the yacc utility.

Structure of a lex file

A lex file looks like


…definitions…
%%
…rules…
%%
…code…

definitions
All code between %{ and %} is copied to the beginning of the resulting C file.
Rules
A number of combinations of pattern and action: if the action is more than a single command it
needs to be in braces.
Code
This can be very elaborate, but the main ingredient is the call to yylex, the lexical analyser. If the
code segment is left out, a default main is used which only calls yylex.

Definitions section

There are three things that can go in the definitions section:


C code Any indented code between %{ and %} is copied to the C file. This is typically used for
defining file variables, and for prototypes of routines that are defined in the code segment.
Definitions A definition is very much like a #define cpp directive. For example
letter [a-zA-Z]
digit [0-9]
punct [,.:;!?]
nonblank [ˆ \t]
These definitions can be used in the rules section: one could start a rule
{letter}+ {…

39
Dept. of CSE
Systems Programming Lab

state definitions If a rule depends on context, it’s possible to introduce states and incorporate
those in the rules. A state definition looks like %s STATE, and by default a state INITIAL is
already given.

Rules section

The rules section has a number of pattern-action pairs. The patterns are regular expressions.
If more than one rule matches the input, the longer match is taken. If two matches are the same
length, the earlier one in the list is taken.

Matched text
When a rule matches part of the input, the matched text is available to the programmer as a
variable char* yytext of length int yyleng.

Regular expressions

. Match any character except newlines.


\n A newline character.
\t A tab character.
ˆ The beginning of the line.
$ The end of the line.
<expr>* Zero or more occurrences of the expression.
<expr>+ One or more occurrences of the expression.
(<expr1>|<expr2>) One expression or another.

User code section


If the lex program is to be used on its own, this section will contain a main program. If
you leave this section empty you will get the default main:
int main()
{
yylex();
return 0;
}
where yylex is the parser that is built from the rules.

ALGORITHM
Step1: Start
Step 2:vowels=0,cons=0
Step 3:Define rules to increment vowels and cons when the input contains vowels & consonants.
Step 4:Define yywrap to return 1
Step 5:Accept the input and call yylex()
Step 6:Print the number of vowels and consonants
Step 7: Stop

40
Dept. of CSE
Systems Programming Lab

PROGRAM
vow.l

%{

#include<stdio.h>

int vowels=0;

int cons=0;

%}

%%

[aeiouAEIOU] {vowels++;}

[a-zA-Z] {cons++;}

%%

int yywrap()
{
return 1;
}
main()
{
printf(“Enter the string.. at end press ^d\n”);
yylex();
printf(“\nNo of vowels=%d No of consonants=%d”,vowels,cons);
}
OUTPUT

user@lab1c11:~/SP$ lex vow.l


user@lab1c11:~/SP$ cc lex.yy.c –lfl
user@lab1c11:~/SP$ ./a.out
Enter the string.. at end press ^d
hello
<ctrl>+d

No of vowels=2 No of consonants=3user@lab1c11:~/SP$

RESULT

The program is successfully executed and output obtained.

41
Dept. of CSE
Systems Programming Lab

Exp 11:
Date:

LEXICAL ANALYSER USING LEX


AIM

Write a program using lexical analyser using lex that extracts tokens-
keywords,identifiers,constants, punctutators.

ALGORITHM
Step 1:Start
Step 2:In rules section define the rules to identify keywords,identifier,constants,punctuators
Step 3:Define yywrap() to return 1
Step 4:Accept the input file and open it in read mode to yyin
Step 5:Call yylex()
Step 6:Stop

42
Dept. of CSE
Systems Programming Lab

PROGRAM

%{
#include<stdio.h>
#include<string.h>
%}
%%
auto|break|case|char|continue|const|default|do|double|else|enum|extern|float|for|goto {printf( "%s:
Keyword\n",yytext);}

if|int|long|register|return|short|sizeof|signed|static|struct|switch|typedef {printf( "%s:


Keyword\n",yytext);}
union|unsigned|void|volatile|while {printf( "%s: Keyword\n",yytext);}

main|printf|scanf|strlen|strcpy|strcat {printf( "%s: Function\n",yytext);}

[a-zA-Z][a-zA-Z0-9_]* {printf( "%s: Identifier\n",yytext);}

[0-9]+ {printf( "%d: Constant\n",atoi(yytext));}

"="|";"|"{"|"}"|"("|")"|"{"|"}"|","|"#"|"$" {printf( "%s: Punctuators\n",yytext);}

"+"|"-"|"*"|"/"|"^"|"%" {printf( "%s: Operators\n",yytext);}

["][a-zA-Z%s%d%f ]+["] {printf( "%s: String\n",yytext);}


. {}

%%

int yywrap()
{
return 1;
}
main(int argc, char *argv[])
{
if(argc!=2)
{
printf("Usage: <./a.out> <sourcefile>\n");
exit(0);
}

yyin=fopen(argv[1],"r");
yylex();
}

43
Dept. of CSE
Systems Programming Lab

INPUT
void main()
{
int a,b,sum;
a=5;
b=3;
sum=a+b;
printf("%d",sum);
}

OUTPUT

user@Lab4c10:~$ lex pg4.l


user@Lab4c10:~$ cc lex.yy.c -lfl
user@Lab4c10:~$ ./a.out hello
void: Keyword
main: Function
(: Punctuators
): Punctuators
{: Punctuators
int: Keyword
a: Identifier
,: Punctuators
b: Identifier
,: Punctuators
sum: Identifier
;: Punctuators
a: Identifier
=: Punctuators
5: Constant
;: Punctuators
b: Identifier
=: Punctuators
3: Constant
;: Punctuators
sum: Identifier
=: Punctuators
a: Identifier
+: Operators
b: Identifier
;: Punctuators
printf: Function
(: Punctuators
"%d": String
,: Punctuators

44
Dept. of CSE
Systems Programming Lab

sum: Identifier
): Punctuators
;: Punctuators
}: Punctuators

RESULT

The program is successfully executed and output obtained.

45
Dept. of CSE
Systems Programming Lab

Exp 12:
Date:

FAMILIARIZATION WITH YACC

AIM

Write a program using lex and yacc to test the validity of a simple expression involving
operators +,-,* and /

DESCRIPTION
The unix utility yacc (Yet Another Compiler Compiler) parses a stream of token, typically
generated by lex, according to a user-specified grammar.

Structure of a yacc file


A yacc file looks much like a lex file:
...definitions...
%%
...rules...
%%
...code...
Definitions
As with lex, all code between %{ and %} is copied to the beginning of the resulting C file.
Rules
As with lex, a number of combinations of pattern and action. The patterns are now those of a
context-free grammar, rather than of a regular grammar as was the case with lex.
Code
This can be very elaborate, but the main ingredient is the call to yyparse, the grammatical parse.

Definitions section
There are three things that can go in the definitions section:
C code
Any code between %{ and %} is copied to the C file. This is typically used for defining file
variables, and for prototypes of routines that are defined in the code segment.
Definitions
The definitions section of a lex file was concerned with characters; in yacc this is tokens. These
token definitions are written to a .h file when yacc compiles this file.
Associativity rules
These handle associativity and priority of operators.

Rules section
The rules section contains the grammar of the language you want to parse. This looks like
name1 : THING something OTHERTHING {action}
| othersomething THING {other action}
name2 :
.....

46
Dept. of CSE
Systems Programming Lab

This is the general form of context-free grammars, with a set of actions associated with
each matching right-hand side.

Lex Yacc interaction


Conceptually, lex parses a file of characters and outputs a stream of tokens; yacc accepts a
stream of tokens and parses it, performing actions as appropriate. In practice, they are more
tightly coupled.
If your lex program is supplying a tokenizer, the yacc program will repeatedly call the yylex
routine. The lex rules will probably function by calling return every time they have parsed a
token. lex returns information in such a way that yacc can use it for parsing.
Header file:
If lex is to return tokens that yacc will process, they have to agree on what tokens there are.
This is done as follows.
• The yacc file will have token definitions
%token NUMBER
in the definitions section.
• When the yacc file is translated with yacc -d it generates a header file that has to be included in
lex program

ALGORITHM
Lex

Step 1: Start
Step 2: Include y.tab.h
Step 3: Write rules to return DIGIT,ID,NL tokens
Step 4: Stop

yacc

Step 1: Start
Step 2:Define tokens DIGIT,NL,ID
Step 3:Define left associativity for +,-,*,/
Step 4: Define rules to test the validity of the expression
4.1: If expression is followed by a new line,then print “Valid Expression”
4.2: Define expression as exp+exp or exp-exp or exp*exp or exp/exp or (exp) or ID or
DIGIT

Step 5: In the code section define yyerror which prints “Invalid Expression”
Step 6:Define main function that accepts the expression and calls yyparse()
Step 7:Stop

47
Dept. of CSE
Systems Programming Lab

PROGRAM

Lex part

%{

#include <stdio.h>
#include <stdlib.h>
#include "y.tab.h"
%}

%%

[0-9]+ { return DIGIT; }

[a-zA-Z][a-zA-Z0-9_]* { return ID; }

\n { return NL ;}

. { return yytext[0]; }

%%

Yacc Part

%{

#include<stdio.h>

#include<stdlib.h>

%}

%token DIGIT ID NL

%left '+' '-'

%left '*' '/'

%%

stmt : exp NL { printf("Valid Expression"); exit(0);}

exp : exp '+' exp

48
Dept. of CSE
Systems Programming Lab

| exp '-' exp

| exp '*' exp

| exp '/' exp

| '(' exp ')'

| ID

| DIGIT

%%

int yyerror(char *msg)


{

printf("Invalid Expression\n");

exit(0);
}

main ()

printf("Enter the expression\n");

yyparse();

49
Dept. of CSE
Systems Programming Lab

OUTPUT

user@Lab4c10:~/SP$ lex pgm2.l

user@Lab4c10:~/SP$ yacc -d pgm2.y

user@Lab4c10:~/SP$ cc lex.yy.c y.tab.c -lfl

user@Lab4c10:~/SP$ ./a.out

Enter the expression

a+b

Valid Expression

user@Lab4c10:~/SP$ ./a.out

Enter the expression

a+b+

Invalid Expression

user@Lab4c10:~/SP$ ./a.out

Enter the expression

a+b-10
Valid Expression

RESULT

The program is successfully executed and output obtained.

50
Dept. of CSE

Vous aimerez peut-être aussi