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

My Complete CC

The document describes a program to generate code for a given expression. It uses a stack to store operands and operators. It pops two operands and the operator and generates intermediate code by printing the operator and operands. It also stores result of operation in a temporary variable if the next character is not an operator, and prints the temporary variable. This continues till the full expression is parsed and corresponding intermediate code is generated.

Uploaded by

Irfan Ahmed
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
330 views

My Complete CC

The document describes a program to generate code for a given expression. It uses a stack to store operands and operators. It pops two operands and the operator and generates intermediate code by printing the operator and operands. It also stores result of operation in a temporary variable if the next character is not an operator, and prints the temporary variable. This continues till the full expression is parsed and corresponding intermediate code is generated.

Uploaded by

Irfan Ahmed
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

1.Write a program to implement standalone scanner.

#include<stdio.h>
#include<string.h>
#include<ctype.h>
main()
{
int ln,tn,i=0,f;
char ch,a[10],b[10][10]={"main","int","printf"},c[10];
FILE *f1,*f2;
f1=fopen("input.txt","r");
f2=fopen("result.txt","w");
ln=1;
tn=1;
fprintf(f2,"lineno tokenno token lexeme\n");
while(!feof(f1))
{
ch=fgetc(f1);
l1:
f=0;
if(ch=='\n')
ln++;
if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
{
fprintf(f2,"%d\t%d\t%c\toperators\n",ln,tn,ch);
tn++;
}
if(isdigit(ch))
{
i=0;
c[i]=ch;
i++;
ch=fgetc(f1);
while(isdigit(ch))
{
c[i]=ch;
ch=fgetc(f1);
i++;
}
c[i]='\0';
fprintf(f2,"%d\t%d\t%s\tdigit\n",ln,tn,c);
tn++;
goto l1;
}
if(ch=='('||ch==')'||ch=='{'||ch=='}'||ch==';'||ch==',')
{
fprintf(f2,"%d\t%d\t%c\tspecial symbol\n",ln,tn,ch);
tn++;
[Cse0939]

}
if(isalpha(ch))
{
i=0;
a[i]=ch;
i++;
ch=fgetc(f1);
while(isalnum(ch))
{
a[i]=ch;
ch=fgetc(f1);
i++;
}
a[i]='\0';
for(i=0;i<3;i++)
{
if(strcmp(a,b[i])==0)
f=1;
}
if(f==1)
{
fprintf(f2,"%d\t%d\t%s\tkeyword\n",ln,tn,a);
tn++;
goto l1;
}
else if(f==0)
{
fprintf(f2,"%d\t%d\t%s\tidentifier\n",ln,tn,a);
tn++;
goto l1;
}
}
}
}

Output:
cc scanner.c
./a.out
input.txt
main()
{
int a,b,c;
c=a+b+15;
printf;
}

[Cse0939]

output.txt
lineno
1
1
1
2
3
3
3
3
3
3
3
4
4
4
4
4
4
4
5
5
6

[Cse0939]

tokenno
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

token lexeme
main keyword
(
special symbol
)
special symbol
{
special symbol
int
keyword
a
identifier
,
special symbol
b
identifier
,
special symbol
c
identifier
;
special symbol
c
identifier
a
identifier
+
operators
b
identifier
+
operators
15
digit
;
special symbol
printf keyword
;
special symbol
}
special symbol

2.Write a lex program to implement if the number is octal or


hexadecimal.
letter[A-Z a-z]
dig[0-9]
oct[0-7]
hex[0-9 A-F a-f]
%%
0({oct})+ printf("octal \n");
0({hex})*h printf("hexadecimal \n");
{dig}+ printf("decimal \n");
. printf("not defined \n");
%%
main()
{
yylex();
}

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

07
octal
0xA1
hexadecimal
4
decimal

[Cse0939]

3.Write a lex program to capitalize comments in a c program.


%{ int c=0;
%}
comment [//]
%%
{comment} {c=1;}
[A-Za-z] {if(c==1)printf("%s",caps(yytext));}
%%
main()
{ yylex();
}
caps(char *str)
{
int i=0;
for(i=0;str[i]!='\0';i++)
printf("%c",toupper(str[i]));
}
Output:
lex cap1.l
cc lex.yy.c ll
./a.out
//irfan shaik
IRFAN SHAIK

[Cse0939]

4.Write a program to implement a lex scanner.


%{
#include<stdio.h>
FILE *f1;
int l=0,m=0;
void data(char *,int,int);
%}
Letter [A-Z a-z]
Dig [0-9]
%%
\n {l++;}
"if"|"main"|"for"|"char" {data(yytext,1,l);}
{Letter}|({Letter}{Dig}*) {data(yytext,2,l);}
"{"|"}"|"("|")"|";" {data(yytext,3,l);}
%%
main()
{
f1=fopen("out.txt","w");
fprintf(f1," token no.\t line no. \t tokens \t lexeme \n");
yylex();
}
void data(char *s,int n,int m)
{
switch(n)
{
case 1:
fprintf(f1,"%d \t %d \t keywords \t %s\t \n",n,l,s);
break;
case 2:
fprintf(f1,"%d \t %d \t Identifier \t %s\t \n",n,l,s);
break;
case 3:
fprintf(f1,"%d \t %d \t Splsym \t %s\t \n",n,l,s);
}
}

Output:
token no.
1
3
3
1
2
2

[Cse0939]

line no.
0
0
0
1
1
1

tokens
keywords
Splsym
Splsym
keywords
Identifier
Splsym

lexeme
main
(
)
char
ch
;

5.Write a program to find whether the given string is left recursive or


right recursive.
#include<stdio.h>
#include<ctype.h>
#include<string.h>
main()
{
int i,j=4,m;
char a[10][15],b[10][15];
printf("Enter the no. of productions \n");
scanf("%d",&m);
for(i=0;i<m;i++)
{
scanf("%s",a[i]);
}
for(i=0;i<m;i++)
{
if(a[i][0]==a[i][3])
{
printf("Left recursive grammar\n ");
}
else
{
j=0;
while(a[i][j]!='\0')
j++;
if(a[i][0]==a[i][j-1])
printf("Right recursive grammar\n ");
else
printf("Grammar is not recursive\n ");
}
}
}

Output:
Enter the no. of productions
3
A->Aa
B->ba
C->cC
Left recursive grammar
Grammar is not recursive
Right recursive grammar

[Cse0939]

6.Write a program to eliminate left recursion.


//eliminating left recursion
#include<stdio.h>
#include<ctype.h>
#include<string.h>
main()
{
int i,j=4,l=0,q,z;
char a[10],b[10],c[10];
printf("Enter the production ");
scanf("%s",a);
if(a[0]==a[3])
{
j=4;
printf("%c ->",a[0]);
q=0;
while(a[j]!='/')
{
b[q]=a[j];
j++;
q++;
}
z=++j;
l=0;
while(a[z]!='\0')
{
c[l]=a[z];
l++;
z++;
}
}
printf("%sX\n",c);
printf("X->%sX/e\n",b);
}
Output:
Enter the production
A->Aa/b
A-> bX
X->aX/e

[Cse0939]

7.Write a program to find canonical LR(0) collections i.e., closure.


#include<stdio.h>
#include<string.h>
main()
{
char A[10][10],items[20][20]={},closure[10][10]={},I[20][20],inputs[20][20];
int i,j,k,l=0,m=0,p=3,n=0,ilen[10],numi;
int B[10][10];
char c,ch;
int flag=0,numprods,temp,temp1,q;
printf("enter the number of productions\n");
scanf("%d",&numprods);
printf("Enter productions:\n");
for(i=0;i<=numprods;i++)
{
gets(A[i]);
}
//finding items:
for(i=0;i<=numprods;i++)
{
n = strlen(A[i]);
for(j=0;j<(n-2);j++)
{
for(k=0;k<=(strlen(A[i]));k++)
{
if(p==k)
{
items[l][m] = '.';
m++;
items[l][m] = A[i][k];
m++;
}
else
{
items[l][m++] = A[i][k];
}
}//k loop
l++;
p++;
m = 0;
}//j loop
p = 3;
}//i loop
[Cse0939]

i = 0;
printf("Items are:\n");
while(l!=i)
{
printf("%d)",i);
puts(items[i]);
printf("\n");
i++;
}
printf("\n");
//Finding Closure:
strcpy(closure[0],items[0]);
m=1;
j=0;
do
{
if(closure[j][4]>='A' && closure[j][4] <= 'Z')
{
for(i=0;i<l;i++)// l gives the number of items
{
if((closure[j][4]==items[i][0])&&(items[i][4]=='.'))
{
strcpy(closure[++j],items[i-1]);
m++;
}
}
}
else
flag=1;
}
while(flag==0);
printf("Closure is:\n");
for(j=0;j<m;j++)
puts(closure[j]);
printf("\n");
}

[Cse0939]

Output:
enter the number of productions
2
Enter productions:
A->C
C->b
Items are:
0)A->.C
1)A->C.
2)C->.b
3)C->b.
Closure is:
A->.C
C->.b

[Cse0939]

9.Write a program using yacc


a) Using lex
yacclex.l
%{
#include "y.tab.h"
extern int yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext); return NUM;}
[\t]
\n return 0;
. return yytext[0];
%%
yacclex.y:
%{#include<stdio.h>
#include<ctype.h>
%}
%token DIG
%%
cmd: E '\n' {printf("%d\n",$1);}
E:E'+'T {$$=$1+$3;}
|T {$$=$1;}
;
T:T'*'F {$$=$1*$3;}
|F {$$=$1;}
;
F:'('E')' {$$=$2;}
|DIG {$$=$1;}
;
%%
yylex()
{
int c;
c=getchar();
if(isdigit(c))
{
yylval=c-'0';
return DIG;
}
else
return c;
}
Execution:
lex yacclex.l
yacc yacclex.y
yacc vd yacc.y

[Cse0939]

cc y.tab.c lex.yy.c -ly -ll


./a.out
Output:
5*4
20

b) Without using lex


yacc.y
%{ #include<stdio.h>%}
%token DIG
%%
cmd: E
'\n'
{ printf("%d", $1);}
;
E : E '+' T {$$=$1+$3;}
|T
{$$=$1;}
;
T : T '-' B {$$=$1-$3;}
|B
{$$=$1;}
;
B : B '/' Q {$$=$1/$3;}
|Q
{$$=$1;}
;
Q : Q '*' F {$$=$1*$3;}
|F
{$$=$1;}
;
F : '(' E ')' {$$=$2;}
|DIG
{$$=$1;}
;
%%
yylex()
{
int c;
c=getchar();
if(isdigit(c))
{
yylval=c-'0';
return DIG;
}
return c;
}
Execution:
yacc yacc.y
cc y.tab.c -ly
./a.out
Output:
5*3
15
[Cse0939]

10. Write a program to generate code.


#include<stdio.h>
char stk[100],stktop=-1,cnt=0;
void push(char pchar)
{
stk[++stktop]=pchar;
}
char pop()
{
return stk[stktop--];
}
char checkoperation(char char1)
{
char oper;
if(char1=='+')
oper='A';
else if(char1=='-')
oper='S';
else if(char1=='*')
oper='M';
else if(char1=='/')
oper='D';
return oper;
}
int checknstore(char check)
{
int ret;
if(check!='+' && check!='-' && check!='*' && check!='/' && check!='@')
{
push(++cnt);
if(stktop>0)
printf("ST$%d\n",cnt);
ret=1;
}
else
ret=0;
return ret;
}
int main()
{
char msg[100],op1,op2,operation;
int i,val;
while(scanf("%s",msg)!=EOF)
{
cnt=0;
stktop=-1;
for(i=0;msg[i]!='\0';i++)
{
[Cse0939]

if((msg[i]>='A' && msg[i]<='Z') || (msg[i]>='a' && msg[i]<='z'))


push(msg[i]);
else
{
op1=pop();
op2=pop();
printf("L%c\n",op2);
operation=checkoperation(msg[i]);
printf("%c%c\n",operation,op1);
val=checknstore(msg[i+1]);
while(val==0)
{
op1=pop();
cnt--;
operation=checkoperation(msg[++i]);
if(operation=='S' && stktop>=-1)
{
printf("N\n");
operation='A';
}
printf("%c%s\n", operation,op1);
val=checknstore(msg[i+1]);
}
}
}
}
}

Output:
AB+C*D+
LA
AB
L
MC
L
AD

[Cse0939]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy