KEMBAR78
Cs8602 Lab Manual | PDF | Parsing | Computer Program
0% found this document useful (0 votes)
56 views28 pages

Cs8602 Lab Manual

Uploaded by

priyap
Copyright
© © All Rights Reserved
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)
56 views28 pages

Cs8602 Lab Manual

Uploaded by

priyap
Copyright
© © All Rights Reserved
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/ 28

AUTONOMOUS

No.90 Usha Garden, Kannigaipair, Uthukottai Taluk, Chennai-601102

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

LAB MANUAL

CS8602 COMPILER DESIGN LABORATORY

Academic Year 2023-2024

(2017 Regulation)

NAME :

REGISTER NO. :

YEAR/ SEM :
III / Sixth
:
SUBJECT CS8602 Compiler Design
Laboratory
AUTONOMOUS

No.90 Usha Garden, Kannigaipair, Uthukottai Taluk, Chennai-601102

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

LABORATORY RECORD

BONAFIDE CERTIFICATE

This is to certify that bonafide record of practical work done by


Mr./Ms_______________________________ Register Number _______________________
of the Year 2023-2024 B.E. Department of Computer science and Engineering in the CS8602
Compiler Design Laboratory for the Sixth Semester University Examination held on
______________.

_____________________ _______________________
Head of the Department Lab Course Incharge

Signature of Examiners:

Internal Examiner_______________ External Examiner


AUTONOMOUS

JNN Vision Statement


Lead the transformation of engineering and management learning experience to educate the next
generation of innovators and entrepreneurs who want to make the world a better place.

JNN Mission Statement


– To develop the required resources and infrastructure and to establish a conducive
ambience for the teaching-learning process.
– To nurture professional and ethical values in the students and to instil in them a
spirit of innovation and entrepreneurship.
– To encourage a desire for higher learning and research in the students and to equip them
to face global challenges.
– To provide opportunities for students to learn job-relevant skills to make them industry
ready.
Mission of the CSE Department

To produce highly qualified and motivated graduates through a rigourous curriculum of theory
and application that develops the ability to solve problems, individually and in teams.
Creating knowledge of fundamental principles and innovative technologies through research
within the core areas of computer science and also in inter- disciplinary topics.

Serving the communities to which we belong at local and national levels, combined with a deep
awareness of our ethical responsibilities to our profession and to society.

Vision of the CSE Department

To contribute effectively to the important national endeavour to produce quality human resource
in the information technology and related areas for sustainable development of the country’s IT
industry needs.
To advance the state of the art in computer science and engineering by working on cutting edge
research topics, publishing quality research papers and filing enduring patents.

To serve the local and the national community by creating awareness about IT related products
and to impress upon then the importance of knowledge management.
LIST OF EXPERIMENTS

Sl. No. Date List of Experiments PageNo. Sign

1 Develop a lexical analyzer to recognize a few


patterns in C. (Ex. identifiers, constants,
comments, operators etc.). Create a symbol table,
while recognizing identifiers.

2 Implement a Lexical Analyzer using Lex Tool

3 (a) Program to recognize a valid arithmetic


expression that uses operator +, - , * and / using
YACC
3 (b) Program to recognize a valid variable which starts
letter followed by any number of letters or digits

4 Implement an Arithmetic Calculator using LEX


and YACC

5 Implement simple code optimization techniques


(Constant folding, Strength reduction and
Algebraic transformation)

6 Implement back-end of the compiler for which the


three address code is given as input and the 8086
assembly language code is produced as output.

7 Generate three address code for a simple program


using LEX and YACC.
EX. NO: 1 DEVELOP A LEXICAL ANALYZER TO RECOGNIZE A FEW
PATTERNS IN C

AIM:

To Write a C program to develop a lexical analyzer to recognize a few patterns in C.

INTRODUCTION:

Lexical analysis is the process of converting a sequence of characters (such as in a computer


program of web page) into a sequence of tokens (strings with an identified “meaning”). A
program that perform lexical analysis may be called a lexer, tokenize or scanner.

TOKEN
A token is a structure representing a lexeme that explicitly indicates its categorization
for the Purpose of parsing. A category of token is what in linguistics might be called a part- of-
speech. Examples of token categories may include “identifier” and “integer literal”, although
the set of Token differ in different programming languages.
The process of forming tokens from an input stream of characters is called
tokenization

Consider this expression in the C programming language:


Sum=3 + 2;

ALGORITHM:
1. Start the program.
2. Include necessary header files.

3. Declare all the variables and file pointers.

4. Include the input program (input.c) using file function.

5. Read the file input file and display the tokens.

6. Display the header files of the input program.

7. Separate the operators of the input program and display it.

8. Print the punctuation marks.

9. Print the constant that are present in input program.

10. Print the identifiers of the input program.

11. Also count the numbers of each token that occurs in input file and print it.

12. Stop the program.

PROGRAM :
#include<stdio.h>
#include<ctype.h>
#include<string.h> void
keyw(char *p);
int i=0,id=0,kw=0,num=0,op=0;
char keys[32][10]={"auto","break","case","char","const","continue","default",
"do","double","else","enum","extern","float","for","goto","if","int","long","register","
return","short","signed","sizeof","static","struct","switch","typedef","union",
"unsigned","void","volatile","while"};
main()
{

char ch,str[25],seps[15]=" \t\n,;(){}[]#\"<>",oper[]="!%^&*-+=~|.<>/?";


int j;
char fname[50]; FILE *f1;
//clrscr();
printf("enter file path (drive:\\fold\\filename)\n"); scanf("%s",fname);
f1 = fopen(fname,"r");

//f1 = fopen("Input","r"); if(f1==NULL)


{
goto END;
}
while((ch=fgetc(f1))!=EOF)
{
for(j=0;j<=14;j++)
{
if(ch==oper[j])
{
printf("%c is an operator\n",ch); op++;
str[i]='\0'; keyw(str);
}
}
for(j=0;j<=14;j++)
{
if(i==-1) break; if(ch==seps[j])
{
if(ch=='#')
{
while(ch!='>')
{
printf("%c",ch); ch=fgetc(f1);
}
printf("%c is a header file\n",ch); i=-1;
break;
}
if(ch=='"')
{
do
{
ch=fgetc(f1); printf("%c",ch);
}while(ch!='"');
printf("\b is an argument\n");
i=-1;
break;
}
str[i]='\0'; keyw(str);
}
}
if(i!=-1)
{

}
printf("Keywords: %d\nIdentifiers: %d\nOperators: %d\nNumbers:
%d\n",kw,id,op,num);
//getch(); END:
printf("file not found");
}
void keyw(char *p)
{
int k,flag=0; for(k=0;k<=31;k++)
{
if(strcmp(keys[k],p)==0)
{
printf("%s is a keyword\n",p); kw++;
flag=1; break;
}
}
if(flag==0)
{
if(isdigit(p[0]))
{
i=-1;
}

INPUT: (INPUT.C)
#include<stdio.h>
#include<conio.h> void
main()
{
Int a,b,c; a=10;
b=5;
c=a+b;
printf(“The sum is %d”,c);
getch();
}
OUTPUT:

RESULT:
Thus the above program for developing the lexical the lexical analyzer and recognizing the
few pattern s in C is executed successfully and the output is verified.
EX.NO:2 IMPLEMENTATION OF LEXICAL ANALYZER USING LEX TOOL

AIM:

To write a program to implement the Lexical Analyzer using lex tool.

INTRODUCTION:

THEORY:

 A language for specifying lexical analyzer.


 There is a wide range of tools for construction of lexical analyzer. The
majority of these tools are based on regular expressions.
 The one of the traditional tools of that kind is lex.
LEX:
The lex is used in the manner depicted. A specification of the lexical analyzer
is preferred by creating a program lex.1 in the lex language.
 Then lex.1 is run through the lex compiler to produce a ‘c’ program lex.yy.c.
 The program lex.yy.c consists of a tabular representation of a transition diagram
constructed from the regular expression of lex.1 together with a standard
routine that uses table of recognize leximes.
 Lex.yy.c is run through the ‘C’ compiler to produce as object program a.out,
which is the lexical analyzer that transform as input stream into sequence of
tokens.
ALGORITHM:

1. Declare the variables which are used for C programs in the declaration section
%{...%}.
2. Include the pattern (regular expression) in the transition rule section %%..%%
3. Write the regular expressions for keywords, identifiers, operators, delimiters and
symbols.
4. In the main function open the file “lexy.txt” in read mode.
5. Then call the yylex() function to scan the input file and display the output.
6. Separate the keyword in the program and display it using lex.
7. Separate the operators of the input program and display it.
8. Print the punctuation marks.
9. Print the constant that are present in input program.
10. Print the identifiers of the input program using lex tool.

PROGRAM: (lexid.l)
%{
#include<stdio.h> int
e,k,c,d,i,s;
%}
%%
include|void|main|int|float|double|scanf|char|printf {printf("keyword"); i++;} [a-z]
[a-zA-Z0-9]* {printf("Identifier"); k++;}
[0-9]* {printf("digit"); e++;}
[+|-|*|/|=]* {printf("operator"); c++;} [;|:|(|)|{|}|"|'|,|\n|\
t]* {printf("delimeter"); d++;} [#|<|>|%]*
{printf("symbols"); s++;}
%%

int main(void)
{
yyin=fopen("lexy.txt","r");
yylex();
printf("\nidentifier %d\n",k);
printf("Symbols %d\n",s);
printf("digits %d\n",e); printf("
Operator %d\n",c); printf("
keywords %d\n",i);
printf("delimeter %d\n",d); return
1;
}
int yywrap()
{
return 1;
}
INPUT:
Lexyi.txt int
a=10;

OUTPUT:
C:/FlexWindow:/EditPlusPortable> lex lexid.l
C:/FlexWindow:/EditPlusPortable> cc lex.yy.c
C:/FlexWindow:/EditPlusPortable> a

Identifier 1
Digit 1
Keyword 1
Operator 1
Delimiter 1

RESULT:

Thus the program for the exercise on lexical analysis using lex has been
successfully executed and output is verified.
EX. NO 3 (a) PROGRAM TO RECOGNIZE A VALID ARITHMETIC EXPRESSION THAT
USESOPERATOR +, - , * AND / USING YACC

AIM:
To write a Yacc program to valid arithmetic expression using Yacc

ALGORITHM:

1. Declare the variables which are used for C programs in the declaration section %
{...%}.
2. Define the tokens, precedence and associativity of operators used in yacc.
3. Include the pattern (Context Free Grammar) in the transition rule section for
validating the expression between %%..%%
4. In main function get the expression from the user for validating it.
5. Call the yyparse() function to parse the given expression and it construct the
LALR parsing table using the grammar defined in transition rule .
6. Then call the yylex() function, it get the current token and store its value in yylval
variable and it is repeated until the value for given expression is computed.
7. Then it validates the expression with constructed LALR parser.
8. Print the expression is VALID if the expression given by user is derived by
the grammar, else print INVALID.
9. Stop the program.

PROGRAM:
%{
#include<ctype.h>
#include<stdlib.h>
#include<string.h> #define
YYSTYPE double
%}

%token num
%left '+' '-'
%left '*' '/'

%%
st: st expr '\n' {printf("VALID");}
|st '\n'
|
|error '\n' {printf("INVALID");}
;
expr: num
|expr '+' expr
|expr '/' expr
%%

main()
{
printf(" ENTER AN EXPRESSION TO VALIDATE");
yyparse();
}
yylex()
{
int ch; while((ch=getchar())==' ');
if(isdigit(ch)|ch=='.')
{
ungetc(ch,stdin);
scanf("%lf",&yylval); return
num;
}
return ch;
}
yyerror(char *s)
{
printf("%S",s);
}

OUTPUT:

C:\Flex Windows\EditPlusPortable>yacc -d arithval.y C:\


Flex Windows\EditPlusPortable>cc y.tab.c C:\Flex
Windows\EditPlusPortable>a
ENTER AN EXPRESSION TO VALIDATE 5+9
VALID
4+6
VALID 5-
INVALID

RESULT:

Thus the program for validating arithmetic expression was done and executed
successfully.
Ex. No: 3 (b) PROGRAM TO RECOGNIZE A VALID VARIABLE WHICH STARTS
WITH A LETTER FOLLOWED BY ANY NUMBER OF LETTERS OR
DIGITS

AIM :
To write a yacc program to check valid variable followed by letter or digits

ALGORITHM:

1. Declare the variables which are used for C programs in the declaration section
%{...%}.
2. Define the tokens let, dig used in yacc.
3. Include the pattern (Context Free Grammar) in the transition rule section for
validating the variable between %%..%%
4. In main function get the variable from the user for validating it.
5. Call the yyparse() function to parse the given expression and it construct the
LALR parsing table using the grammar defined in transition rule .
6. Then call the yylex() function, it get the current token and store its value in yylval
variable and it is repeated until the value for given variable.
7. Then it validates the variable with constructed LALR parser.
8. Print the variable is “accepted” if the variable given by user is derived by the
grammar, else print “rejected”.
9. Stop the program.

PROGRAM:
%{
#include<stdio.h>
#include<ctype.h>
%}
%token let dig
%%
%%
yylex()
{

}
sad: let recld '\n' {printf("accepted\n"); return 0;}
| let '\n' {printf("accepted\n"); return 0;}
|
|error {yyerror("rejected\n");return 0;}
;
recld: let recld
| dig recld
| let
| dig
;
char ch; while((ch=getchar())==' ');
if(isalpha(ch))
return let; if(isdigit(ch))
return dig; return ch;
yyerror(char *s)
{

}
main()
{
printf("%s",s);
printf("ENTER A variable : "); yyparse();
}

OUTPUT:
C:\Flex Windows\EditPlusPortable>yacc -d valid.y C:\
Flex Windows\EditPlusPortable> cc y.tab.c C:\Flex
Windows\EditPlusPortable> a

A1
accepted 10a
rejected

RESULT:

Thus the program for checking letter followed by letter or digits were done and executed
successfully.
Ex. No: 4 IMPLEMENTATION OF CALCULATOR USING LEX AND YACC

AIM :
To write a yacc program to implement calculator using lex and yacc

ALGORITHM:
1. Declare the variables and header files which are used for C programs in the
declaration section %{...%} of lex and yacc file.
2. Include the pattern (regular expression in lex and CFG in yacc) in the transition rule
section %%..%%
3. Define the tokens, precedence and associativity of operators used in yacc.
4. In main function get the expression from the user for calculation.
5. Call the yyparse() function to parse the given expression and it construct the LALR
parsing table using the grammar defined in transition rule of yacc.
6. Then call the yylex() function, it get the current token and store its value in yylval
variable and it is repeated until the value for given expression is computed.
7. Then it computes the expression with constructed LALR parser.
8. Print the value of expression.
9. Stop the program.

PROGRAM:
Cal.l

%{

%}
#include <stdio.h> #include "y.tab.h" int c;
extern int yylval;
[a-z]{

}
[0-9] {
c = yytext[0]; yylval = c - 'a'; return (LETTER);
c = yytext[0]; yylval = c - '0'; return(DIGIT);
[^a-z0-9\b]{
c = yytext[0];
return(c);
}

Cal.Y
%{

%}
#include <stdio.h> int regs[26];
int base;

%start list
%token DIGIT LETTER
%left '|'
%left '&'
%left '+' '-'
%left '*' '/' '%'
%left UMINUS /*supplies precedence for unary minus */

%% /* beginning of rules section */ list: /*empty */


|
list stat '\n'
|
list error '\n {yyerrok } stat: expr {printf("%d\n",$1);}
| LETTER '=' expr{ regs[$1] = $3; }
;
expr: '(' expr ')' {$$ = $2;}
|expr '*' expr {$$ = $1 * $3;}
| expr '/' expr {$$ = $1 / $3;}
| expr '%' expr {$$ = $1 % $3;}
| expr '+' expr { $$ = $1 + $3;}
| expr '-' expr {$$ = $1 - $3;}
| expr '&' expr {$$ = $1 & $3;}
| expr '|' expr {$$ = $1 | $3;}
| '-' expr %prec UMINUS {$$ = -$2;}
| LETTER {$$ = regs[$1];}
| number;
number: DIGIT {$$ = $1;base = ($1==0) ? 8 : 10;}
| number DIGIT {$$ = base * $1 + $2;}
;
%%

main()
{
return(yyparse());
}
yyerror(s) char *s;
{
fprintf(stderr, "%s\n",s);
}
yywrap()
{
return(1);
}
(OR)

%{
#include<stdio.h> int op=0,i;
float a,b;
%}

dig[0-9]+|([0-9]*)"."([0-9]+)
add "+" sub "-" mul"*"
div "/"
pow "^" ln \n
%%
%%
digi()
{
{dig}{digi();}
{add}{op=1;}
{sub}{op=2;}
{mul}{op=3;}
{div}{op=4;}
{pow}{op=5;}
{ln}{printf("\n the result:%f\n\n",a);}
if(op==0) a=atof(yytext); else
{

b=atof(yytext); switch(op)
{
case 1: a=a+b;
break; case 2: a=a-b;
break; case 3: a=a*b;
break; case 4: a=a/b;
break;
}
op=0;
}
}
case 5: for(i=a;b>1;b--)
a=a*i; break;
main(int argv,char *argc[])
{
yylex();
}
yywrap()
{
return 1;
}
OUTPUT:

C:\Flex Windows\EditPlusPortable>lex cal.l C:\Flex Windows\EditPlusPortable>yacc -d cal.y


C:\Flex Windows\EditPlusPortable> cc y.tab.c lex.yy.c C:\Flex Windows\EditPlusPortable> a
5+2
7
8*2
16
5-3
2
7/2
3

RESULT:
Thus the program to implement calculator using lex and yacc was done and executed.
Ex.No: 5 IMPLEMENTATION OF THE BACK END OF THE COMPILER

AIM:

To write a c program for implementing backend of a compiler.

INTRODUCTION:
A compiler is a computer program that implements a programming language specification
to “translate” programs, usually as a set of files which constitute the source code written in
source language, into their equivalent machine readable instructions(the target language,
often having a binary form known as object code). This translation process is called
compilation.
BACK END:
 Some local optimization
 Register allocation
 Peep-hole optimization
 Code generation
 Instruction scheduling
The main phases of the back end include the following:
 Analysis: This is the gathering of program information from the intermediate
representation derived from the input; data-flow analysis is used to build use- define
chains, together with dependence analysis, alias analysis, pointer analysis, escape
analysis etc.
 Optimization: The intermediate language representation is transformed into
functionally equivalent but faster (or smaller) forms. Popular optimizations are
expansion, dead, constant, propagation, loop transformation, register allocation and
even automatic parallelization.
 Code generation: The transformed language is translated into the output language, usually the
native machine language of the system. This involves resource and storage decisions, such as deciding
which variables to fit into registers and memory and the selection and scheduling of appropriate
machine instructions along with their associated modes. Debug data may also need to be generated to
facilitate debugging
ALGORITHM:

1. Start the program


2. Include the necessary header files.
3. Get the number of statements from the user.
4. For each variable allocate a separate register using Load or Move Instructions LD R,a or
Mov R,a
5. If the expression contains operator “+”, then generate the assembly code as ADD
6. If the expression contains operator “-”, then generate the assembly code as SUB
7. If the expression contains operator “*”, then generate the assembly code as MUL
8. If the expression contains operator “/”, then generate the assembly code as DIV
9. Result of the operand is stored to any variables ST x, Ro.
10. Stop the program.

PROGRAM:

#include<stdio.h> #include<string.h> int main()


{
char inp[100][100]; int n,i,j,len;
int reg = 1;
printf("Enter the no of statements"); scanf("%d",&n);
for(i = 0; i < n; i++) scanf("%s",&inp[i]); for(i = 0; i < n; i++)
{
len = strlen(inp[i]); for(j=2; j < len; j++)
{
if(inp[i][j] >= 97 && inp[i][j] <= 122)
{
printf("LOAD R%d %c \n",reg++,inp[i][j]);
}
if(j == len-1 && inp[i][len-j] =='=')
{
j=3; if(inp[i][j] == '+')
{
printf("ADD R%d R%d\n",reg-2,reg-1); printf("STORE %c R%d\n",inp[i][0],reg-2);
}
else if(inp[i][j]=='-')
{
printf("SUB R%d R%d\n",reg-2,reg-1); printf("STORE %c R%d\n",inp[i][0],reg-2);
}
else if(inp[i][j]=='*')
{
printf("MUL R%d R%d\n",reg-2,reg-1); printf("STORE %c R%d\n",inp[i][0],reg-2);
}
else if(inp[i][j]=='/')
{
}
break;
}
}
}

printf("DIV R%d R%d\n",reg-2,reg-1); printf("STORE %c R%d\n",inp[i][0],reg-2);

return 0;
}

OUTPUT:

RESULT:
Thus, the c program for implementing backend of the compiler was done successfully.
Ex. No : 6 IMPLEMENTATION OF SIMPLE CODE OPTIMIZATION
TECHNIQUES

AIM:
To write a C program to eliminate dead code as code optimization

INTRODUCTION:

o Optimization is a program transformation technique, which tries to improve the code by


making it consume less resource (i.e. CPU, memory) and deliver high speed.

o In optimization, high-level general programming constructs are replaced by very


efficient low level programming codes. A code optimizing process must follow the three rules
given below:
The output code must not, in any way, change the meaning of the program.

▪ Optimization should increases the speed of the program and if possible, the program
should demand less number of resources.
▪ Optimization should itself be fast and fast and should not delay the overall compiling
process.

ALGORITHM:

1. Start the program.


2. Include necessary header files.
3. Read the input from input file test.txt.
4. Instructions are kept in the basic block, Consider the instructions in the same basic block
5. Variables are initialized with values, If any variables are not used in the basic block,
identified such variables are unused variables
6. Such unused variable will be displayed as output.

PROGRAM:

#include<stdio.h> void main()


{
char all_var[10];
int ptr=0,dup=0,i,j,c,a=0; FILE *file;
file = fopen( "test.txt" , "r"); if (file)
{
while ((c=getc(file)) != EOF)
{
putchar(c);
if(c==';'||c==' '||(c>='0'&&c<='9')||c=='='||c=='\n'||c=='+') continue;

else
{
all_var[ptr]=c;
ptr++;
}
}
printf("\n The unused variables are: "); for(i=0;i<=ptr;i++)
{
for(j = i+1; j <= ptr; j++)
{
for(j = i+1; j <= ptr; j++)
{
all_var[ptr]=c; ptr++;

printf("\n The unused variables are: "); for(i=0;i<=ptr;i++)


{
for(j = i+1; j <= ptr; j++) if(all_var[i] == all_var[j])
{
dup=1;
}
if(dup==0)
{
printf("%c",all_var[i]); dup=0;
}
}
fclose(file);
}
}
INPUT:
a;
b;
c;
a; a=34; c=78; j=45; r=30;

OUTPUT:
b is unused variable

RESULT:
Thus the program for code optimization was implemented and output was obtained.
Ex. No : 7 GENERATE THREE ADDRESS CODE FOR A SIMPLE PROGRAM
USING LEX AND YACC

AIM :

To write a yacc program to Generate Three Address Code using lex and yacc

ALGORITHM:

1. Declare the variables and header files which are used for C programs in the declaration
section %{...%} of lex and yacc file.
2. Include the pattern (regular expression in lex and CFG in yacc) in the transition rule
section %%..%%
3. Define the tokens, precedence and associativity of operators used in yacc.
4. In main function get the expression from the user for calculation.
5. Call the yyparse() function to parse the given expression and it construct the LALR
parsing table using the grammar defined in transition rule of yacc.
6. Then call the yylex() function, it get the current token and store its value in yylval
variable and it is repeated until the value for given expression is computed.
7. Then it computes the expression with constructed LALR parser.
8. Print the value of expression.
9. Stop the program.

PROGRAM:
Three.l
%{
#include #include
#include “y.tab.h”
%}

%%
[0-9]+ {yylval.dval=atoi(yytext);return NUM;} [t];
n return 0;
. {return yytext[0];}
%%

void yyerror(char *str)


{
printf(“n Invalid Character…”);
}
int main()
{
printf(“Enter Expression x => “); yyparse();
return(0);
}

Three.y
%{
#include
int yylex(void); char p=’A’-1;
%}

%union
{
char dval;
}
%token NUM
%left ‘+’ ‘-‘
%left ‘*’ ‘/’
%nonassoc UMINUS
%type S
%type E

%%
S : E {printf(” x = %cn”,$$);}
;
E : NUM {}
| E ‘+’ E {p++;printf(“n %c = %c + %c “,p,$1,$3);$$=p;}
| E ‘-‘ E {p++;printf(“n %c = %c – %c “,p,$1,$3);$$=p;}
| E ‘*’ E {p++;printf(“n %c = %c * %c “,p,$1,$3);$$=p;}
| E ‘/’ E {p++;printf(“n %c = %c / %c “,p,$1,$3);$$=p;}
| ‘(‘E’)’ {$$=p;}
| ‘-‘ E %prec UMINUS {p++;printf(“n %c = -%c “,p,$2);$$=p;}
;
%%

OUTPUT:

[a40@localhost ~]$ lex threee.l [a40@localhost ~]$ yacc -d threee.y [a40@localhost ~]$ cc
lex.yy.c y.tab.c -ll [a40@localhost ~]$ ./a.out
Enter Expression x => 1+2-3*3/1+4*5 A = 1+2
B = 3*3 C = B/1 D = A-C E = 4*5 F = D+E X = F
[a40@localhost ~]$ ./a.out
Enter Expression x => 1+2*(3+4)/5 A = 3+4
B = 2*A C = B/5

D = 1+C X = D
[a40@localhost ~]$ ./a.out
Enter Expression x => 1+2*(-3+-6/1)*3 A = -3
B = -6 C = B/1
D = A+C E = 2*D F = E*3 G = 1+F X = G

RESULT:
Thus the program for code optimization was implemented and output was obtained.

You might also like