Write
Lex program for Sample language Program
%{
#include<stdio.h>
%}
fpoint
[0-9]+-[0-9]+
delimit [ \t\n]
letter [a-zA-Z]+
intconst [0-9]+
us [_]
%%
int |char |
double | float {printf("\n\t %s is a data type", yytext);}
if | static |
void | for | do | while | switch | printf | getch | stdio | include | main
{printf ("\n\t%s is a keyword", yytext);}
"+" |
"-" | "*" | "%" | "/" {printf
("\n\t %s is an arithcmatic operator",yytext);}
"&"
| "|" {printf ("\n\t%s is Bitwise operator",yytext);}
"&&"
| "||" {printf ("\n\t%s is Logical operator", yytext);}
"="
{printf ("\n\t %s is assignment operator ",yytext);}
">>"
| "<<" {printf("\n\t%s is shift operator",yytext);}
"<"
| ">" | "<=" | ">=" { printf
("\n\t%s is Relational operator",yytext); }
":" |
";" | "," {printf ("\n\t%s is a
terminator",yytext);}
"#" |
"." | "$" | "(" | ")" | "{" |
"}" | "@" | "\"" {printf ("\n \t%s is a
special symbol",yytext);}
"\\n"
| "\\t" {printf ("\n\t %s is the escape sequence",yytext);}
{intconst}
{printf ("\n\t%s is integer constant", yytext);}
{fpoint} {printf
("\n \t%s is floating point number",yytext);}
{delimit}
{printf ("\n\t%s is delimiter",yytext);}
{letter}({intconst}|{letter}|{us})*
{printf("\n\t %s is identifier",yytext);}
{intconst}({letter})*
{printf("\n\t%s is Invalid token", yytext);}
%%
main()
{
FILE *fp;
fp=fopen("lex2.txt","r");
if(fp==NULL){
printf("\n\t cannot open file");
}
else {
yyin=fp;
yylex();
}
fclose(fp);
return 1;
}
int yywrap()
{
return 1;
}
Output:-
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$lex
let.l
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$cc
lex.yy.c
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$clear
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$./a.out
# is integer constant
include is identifier
< is integer constant
stdio is identifier.
h is identifier>
is delimiter
void is identifier
is delimiter
main is identifier()
is delimiter{
is delimiter
is delimiter
int is assignment operator
is delimiter
a is identifier
= is assignment operator
3 is integer constant,
b is identifier
= is assignment operator
2 is integer constant,
c is identifier
= is assignment operator
0 is integer constant;
is delimiter
is delimiter
int is assignment operator
is delimiter
2ls is Invalid token;
is delimiter
is delimiter
c is identifier
= is assignment operator
a is identifier
+ is assignment operator
b is identifier;
is delimiter
is delimiter
printf is identifier();
is delimiter}
WRITE
A PROGRAM FOR VERIFICATION OF SWITCH CASE
LEX PROGRAM
%{
#include<stdio.h>
#include "y.tab.h"
%}
%%
[\n\t ]+ ;
switch return
SWITCH;
case return
CASE;
default return
DEFAULT;
break return
BREAK;
[a-zA-Z]+ return VAR;
[0-9]+ return CONST;
. return
yytext[0];
%%
int yywrap()
{return 1;}
YACC program
%{
%}
%token SWITCH
VAR CONST CASE DEFAULT BREAK
%%
e:SWITCH '(' VAR
')' '{' e1 '}' {printf("\nSwitch
verified..\n");
exit(0);}
;
e1:CASE CONST
':' ANYTHING ';' b
|DEFAULT ':' ANYTHING ';' b
;
ANYTHING:VAR '='
CONST
|
;
b:BREAK ';' b
|e1
|
;
%%
main()
{
yyparse();
return 0;
}
void
yyerror(const char *s)
{
printf("\n%s\n",s);
}
/* OUTPUT ::
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$lex
b.l
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$bison
-yd b1.y
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$cc
lex.yy.c y.tab.c
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$./a.out
switch(ch)
{
case 1:
a=0;
break;
}
Switch
verified..
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$./a.out
switch(ch)
{
}
syntax error
WRITE A PROGRAM FOR MAIN VERIFICATION AND INSIDE IF –
ELSE SYNTAX
LEX PROGRAM
%{
#include
"y.tab.h"
int line=1;
%}
%%
[\n] {line++;}
[\t ] {}
void {return
VOID;}
if {return IF;}
else {return
ELSE;}
main {return
MAIN;}
argc {return
ARGC;}
argv {return
ARGV;}
printf {return
PRINTF;}
int {return
INT;}
char {return
CHAR;}
[0-9]+ {return
CONST;}
[a-z]+ {return
VAR;}
. {return
yytext[0];}
%%
int yywrap()
{
return 1;
}
YACC program
%{
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
FILE *fo;
extern int line;
%}
%token MAIN VOID
ELSE IF PRINTF VAR CONST INT CHAR ARGC ARGV
%%
e:VOID MAIN '('
e1 ')' '{' e4 '}' { fprintf(fo,"\nIf-Else is
verified!\n");fprintf(fo,"\nTotal lines=%d",line); exit(0);}
;
e1:INT ARGC ','
CHAR '*' ARGV
|
;
e4:IF '(' VAR
'>' VAR ')' '{' e5 '}' e6
|
;
e5:PRINTF '('
'"' VAR '"' ')' ';'
|
;
e6:ELSE '{' e5
'}'
;
%%
main()
{
fo=fopen("b.txt","w");
yyparse();
return(0);
}
int
yyerror(const char *s)
{
printf("\n%s\n",s);
return 0;
}
//a.txt :input
file
void main()
{
if(a>b)
{
printf("a");
}
else
{
printf("b");
}
}
//b.txt : output
file
If-Else is
verified!
Total lines=11
WRITE
A PROGRAM FOR LOOP VERIFICATION
LEX PROGRAM
%{
#include
"y.tab.h"
#include<stdio.h>
%}
%%
[\n\t ]+
{printf("%s",yytext);}
"for"
{ printf("%s",yytext); return FOR;}
"int"
{ printf("%s",yytext); return INT;}
"break"
{ printf("%s",yytext); return BREAK; }
[a-zA-Z]+ {
printf("%s",yytext); return VAR; }
[0-9]+ {
printf("%s",yytext); return NUM; }
[<>=/+-/*//]+
{ printf("%s",yytext); return OPR;}
. {
printf("%s",yytext); return yytext[0]; }
%%
int yywrap()
{
return 1;
}
void
yyerror(const char *s)
{
printf("\n\t%s",s);
exit(0);
}
YACC PROGRAM
%{
#include<stdio.h>
#include<stdlib.h>
%}
%token FOR INT
BREAK VAR NUM OPR
%%
E1: FOR '(' E
')' '{' E2 '}'
;
E : INT VAR OPR
NUM ';' VAR OPR NUM ';' VAR OPR
| VAR OPR NUM
';' VAR OPR NUM ';' VAR OPR
| VAR OPR NUM
';' VAR OPR VAR ';' VAR OPR
;
E2: VAR OPR VAR
E3
|
|E1 E2
;
E3: OPR VAR E3
|';' E2
;
%%
int main()
{
yyparse();
printf("\n\tValid For Loop Statements!!!\n"
);
return 0;
}
./a.out
<ip.txt
for(i=0;i<5;i++)
{
c=a+b-c;
d=e-f;
for(j=0; j<8;j++)
{
c=a+b;
}
}
Valid For Loop Statements!!!
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$
WRITE
A PROGRAM IF- ELSE VERIFICATION
LEX PROGRAM
%{
#include"y.tab.h"
%}
%%
if {return(IF);}
else
{return(E1);}
"("
{return(OPBR);}
")"
{return(CLBR);}
[a-zA-Z]+
{return(VAR);}
[0-9]+
{return(CONST);}
"="
{return(EQU);}
"<"|">"
{return(RELOP);}
"!"
{return(NOT);}
"{"
{return(OPCB);}
"}"
{return(CLCB);}
%%
int yywrap()
{
return 1;
}
YACC Program
%{
#include<stdio.h>
int flag=0;
%}
%token IF E1
OPBR CLBR OPCB CLCB EQU RELOP NOT VAR CONST
%%
S:IF OPBR VAR
RELOP CONST CLBR OPCB CLCB
E1 OPCB CLCB
{printf("verifide");flag=1;}
%%
int main()
{
yyparse();
return;
}
int yyerror()
{
if(flag==0)
printf("not
verifide");
return;
}
................
output
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$gedit
b311.l
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$gedit
b311.l
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$lex
b311.l
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$YACC
-d sah.y
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$cc
lex.yy.c y.tab.c
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$./a.out
if(a>5)
{}
else
{}
Verified
WRITE
A PROGRAM FOR SWITCH CASE VERIFICATION
LEX PROGRAM
%{
extern FILE *fp;
#include"y.tab.h"
%}
%%
[\t]+ {}
switch {return SWITCH;}
case {return CASE;}
break {return BREAK;}
default {return DEFAULT;}
"{" {return OBRS;}
"}" {return CBRS;}
";" {return SEMIC;}
"(" {return OBRC;}
")" {return CBRC;}
":" {return COL;}
printf {return PRINTF;}
scanf {return SCANF;}
"=" {return EQL;}
["] {return DOQ;}
[a-zA-Z]+ {return VAR;}
[0-9]+ {return NUM;}
%%
int yywrap()
{
return 1;
}
YACC program
%{
#include<stdio.h>
FILE *fo=NULL;
%}
%token SWITCH
CASE NUM VAR PRINTF SCANF OBRS CBRS OBRC CBRC BREAK DEFAULT COL SEMIC EQL DOQ
%%
e1:SWITCH OBRC
VAR CBRC OBRS e2 CBRS {printf("\nSwitch is verified"); exit(0);} |
SWITCH OBRC VAR CBRC OBRS e6 CBRS {printf("\nSwitch is verified");} ;
e2:CASE NUM COL
e6 b e3;
e3:CASE NUM COL
e6 b e4;
e4:CASE NUM COL
e6 b;
b:BREAK SEMIC ;
e5:VAR | NUM ;
e6:PRINTF OBRC
DOQ e5 DOQ CBRC SEMIC | VAR EQL e5
SEMIC;
%%
main()
{
FILE *fp;
fp=fopen("p.txt","r");
if(fp!=NULL)
{
yyparse();
}
fcloseall();
}
void
yyerror(const char *s)
{
printf("\n%s",s);
}
Text file p.txt
switch(a)
{
case 1:c=4;
break;
case 2:printf("pawan");
break;
case 3:printf("sachin");
break;
}
WRITE
A PROGRAM FOR VERIFICATION OF IF ELSE WITH NESTED IF
LEX
PROGRAM
%{
#include"y.tab.h"
#include<stdio.h>
%}
%%
[\t\n ] ;
if {return IF;}
else {return
ELSE;}
printf {return
PRINTF;}
[a-zA-Z]+
{return VAR;}
[0-9]+ {return
NUM;}
. {return
yytext[0];}
%%
int yywrap()
{
return 1;
}
YACC PROGRAM
%{
#include<stdio.h>
%}
%token IF ELSE
VAR NUM PRINTF
%%
E:IF'('VAR'>'VAR')''{'e2'}'
ELSE'{'e2'}' {printf("IF-ELSE is VERIFIED");}
;
e2:E
{printf("Inner");}
|PRINTF'(''"'VAR'"'')'';'
|
;
%%
int main()
{
yyparse();
return 0;
}
void
yyerror(char *s)
{
printf("\nError %s",s);
}
//*******************
OUTPUT ********************//
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$lex
280.l
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$bison
-yd 280new3.y
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$cc
lex.yy.c y.tab.c
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$./a.out
if(a>b)
{
if(a>b)
{
if(a>b)
{
printf("a");
}
else
{
printf("b");
}
IF-ELSE is
VERIFIEDInner
}
else
{
if(a>b)
{
printf("a");
}
else
{
printf("b");
}
IF-ELSE is
VERIFIEDInner
}
IF-ELSE is
VERIFIEDInner
}
else
{
printf("x");
}
IF-ELSE is
VERIFIED[root@localhost ~]#
//** OUTPUT :
ERROR CASE **
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$./a.out
if(a>)
Error syntax
errorccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$./a.out
If(A>)
Error syntax
errorccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$
WRITE
A PROGRAM FOR VERIFICATION OF simple MAIN Program Construct
LEX
PROGRAM
%{
#include
"y.tab.h"
%}
%%
main {return MAIN;}
"(" {return OB;}
")"
{return CB;}
"{" {return O;}
"}" {return C;}
%%
int
yywrap()
{
return
1;
}
YACC
Program
%{
#include
<stdlib.h>
%}
%token
MAIN OB CB O C
%%
e:MAIN
OB CB O C {printf(" this is
validated "); exit(0);}
;
%%
int
main()
{
yyparse();
return
0;
}
yyerror()
{
printf("this
is error");
}
WRITE
A PROGRAM FOR VERIFICATION OF # Define Pre-processor construct
LEX
PROGRAM
%{
#include
"y.tab.h"
extern
int yylval;
%}
%%
"#" {return HASH;}
define {return DEFINE;}
"<" {return AS;}
"." {return DOT;}
h {return H;}
">" {return AC;}
[a-z]+ {yylval = yytext[0]; return (FN);}
%%
int
yywrap()
{
return
1;
}
YACC
PPROGRAM
%{
#include
<stdio.h>
%}
%token
HASH DEFINE AS FN DOT H AC
%%
e:HASH
DEFINE AS FN DOT H AC {printf(" this is valid");exit(0);}
;
%%
main()
{
yyparse();
return
0;
}
int
yyerror()
{
printf("
the error");
}
WRITE A PROGRAM TO GENERATE THREE ADDRESS CODE FOR IF
STATEMENT
LEX
PROGRAM
%{
#include
"y.tab.h"
int lineno=1,flag=1;
extern FILE *fp;
%}
%%
"\n" {lineno++;}
[ \t]+ {}
[0-9]+ {yylval.no=atoi(yytext); return digit;}
"<="
|
">="
|
"<"
|
">"
|
"!=" |
"==" {if(flag==1) {printf("%s",
yytext);} return rel_op;}
"{"
{return opcrl;}
"}"
{return clocrl;}
"("
{printf("("); return bracs_op;}
")"
{printf(")"); flag=0; return bracs_cl;}
"if" {printf("if"); flag=1; return
if_id;}
"else"
{printf("else"); flag=1; return else_id;}
[a-z]+ { strcpy(yylval.str,yytext); if(flag==1)
{printf("%s", yytext);} return id;}
"$" {return(yytext[0]);}
. {
return(yytext[0]);}
%%
int yywrap()
{
return 1;
}
yyerror(char *s)
{
printf("%s
at line no %d for %s ",s, lineno,yytext);
exit(1);
}
YACC
program
%{
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct intercode
{
char optor;
char op1[10];
char op2[10];
char res[10];
}intercode;
intercode ic_if[50],ic_else[50];
int allrows,temp=1,cnt_if=0;
char
goto_if[]="goto L0\0",goto_else[]="goto L1\0";
%}
%union
{
int no;
char str[10];
}
%token
<str> if_id
%token
<str> else_id
%token
<str> rel_op
%token
<str> clocrl
%token
<str> opcrl
%token
<str> bracs_op
%token
<str> bracs_cl
%token
<str> id
%token
<no> digit
%type
<str> expr
%left '+' '-'
%left '*' '/'
%nonassoc '='
%%
start : if_id
bracs_op id rel_op id bracs_cl opcrl start {}
| '$' {exit(0); }
| id '=' expr
';'{ emit_if('=',$3,"",$1); yyparse();}
|clocrl{printf("goto
L0 \n goto L1");
int i=0;
printf("\n \t L0 :\t"
);
for(i=0;i<cnt_if;i++)
{
printf("%c",ic_if[i].optor);
printf("\t\t
%s",ic_if[i].op1);
printf("\t\t
%s",ic_if[i].op2);
printf("\t\t
%s\n\t\t",ic_if[i].res);
}
yyparse();
}
;
expr :
expr'+'expr {sprintf($$,"t%d",temp); temp++; emit_if('+',$1,$3,$$);}
| expr'-'expr {sprintf($$,"t%d",temp);
temp++; emit_if('-',$1,$3,$$);}
| expr'*'expr
{sprintf($$,"t%d",temp); temp++; emit_if('*',$1,$3,$$);}
| expr'/'expr
{sprintf($$,"t%d",temp); temp++; emit_if('/',$1,$3,$$);}
|id {}
|digit{}
;
%%
main(int
argc,char *argv[])
{
FILE *fp;
fp=fopen("input","r");
if(!fp)
{
printf("cnt
open:%s",argv[1]);
exit(1);
}
yyparse();
return 0;
}
void
emit_if(char opr,char *op1, char *op2,char *res)
{
ic_if[cnt_if].optor=opr;
strcpy(ic_if[cnt_if].op1,op1);
strcpy(ic_if[cnt_if].op2,op2);
strcpy(ic_if[cnt_if].res,res);
cnt_if++;
}
************
INPUT **********************************
if(a<b)
{
c=a+b;
o=p+u;
}
$
************************
OUTPUT **********************
[Student@HW-01
~]$ lex ass7.l
[Student@HW-01
~]$ YACC -d ass7.y
[Student@HW-01
~]$ cc lex.yy.c y.tab.c
[Student@HW-01
~]$ ./a.out <input
if(a<b)goto
L0
goto L1
L0 : + a b
t1
= t1
c
+ p
u t2
= t2
o
[Student@HW-01 ~]$
*******************
INPUT(ERROR) **************************
if(ab)
{
c=a+b;
o=p+u;
}
$
************
OUTPUT **************************************
if(ab)syntax
error at line no 1 for )ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$
WRITE
A PROGRAM FOR SIMPLE EXPRESSION THREE ADDRESS CODE AND OPTIMIZATION
LEX
PROGRAM
%{
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include
"y.tab.h"
int
line=1,pos=0;
%}
%%
[\n] { line++;
pos = 0; }
[\t ] {pos = pos
+ yyleng; }
[a-zA-Z]+ {
strcpy(yylval.str,yytext); pos = pos + yyleng; return alpha; }
[0-9]+ {
yylval.no = atoi(yytext); pos = pos + yyleng; return digit; }
. { pos = pos +
yyleng; return yytext[0]; }
%%
int yywrap() {
return 1; }\
void
yyerror(char *s)
{
printf("\nERROR : %s at line
%d:%d\n",s,line,pos);
}
YACC
program
%{
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct icode
{
char
opr,op1[33],op2[33],res[33];
}ic[50];
int
icnt=0,temp=0;
void disp();
void emit(char
,char [], char [], char []);
void codeopt();
%}
%union {char
str[33]; int no; };
%token
<str> alpha
%token
<no> digit
%type
<str> E
%type
<str> E1
%left '+' '-'
%left '*' '/'
%nonassoc '='
%%
E1: '$' {
printf("\n\t\t
Intermediate Code \n");
printf("------------------------------------------------------------------");
disp();
codeopt();
printf("\n\t\t
Optimized Intermediate Code \n");
printf("------------------------------------------------------------------");
disp();
icnt = 0;temp =
0;
yyparse();
}
| alpha '=' E
';' { emit('=',$3,"",$1); yyparse(); }
;
E: E '+' E {
sprintf($$,"t%d",temp++); emit('+',$1,$3,$$); }
| E '-' E { sprintf($$,"t%d",temp++);
emit('-',$1,$3,$$); }
| E '*' E {
sprintf($$,"t%d",temp++); emit('*',$1,$3,$$); }
| E '/' E {
sprintf($$,"t%d",temp++); emit('/',$1,$3,$$); }
| '-' E { sprintf($$,"t%d",temp++);
emit('-',$2,"",$$); }
| alpha {}
| digit { sprintf($$,"%d",$1); }
;
%%
void disp()
{
int i;
printf("\n\nOperator \tOperand 1
\tOperand 2\tResult\n");
for(i=0;i<icnt;i++)
printf("%c \t\t%s
\t\t%s \t\t%s\n",ic[i].opr,ic[i].op1,ic[i].op2,ic[i].res);
}
void emit(char
opr,char op1[33],char op2[33],char res[33])
{
ic[icnt].opr = opr;
strcpy(ic[icnt].op1,op1);
strcpy(ic[icnt].op2,op2);
strcpy(ic[icnt++].res,res);
}
void codeopt()
{
int i,j;
for(i = 1;i<icnt;i++)
{
if(ic[i].opr == '='
&& (!strcmp(ic[i].op1,ic[i-1].res) || !strcmp(ic[i].op2,ic[i-1].res)))
{
strcpy(ic[i-1].res,ic[i].res);
for(j=i;j<icnt-1;j++)
{
ic[j].opr
= ic[j+1].opr;
strcpy(ic[j].op1,ic[j+1].op1);
strcpy(ic[j].op2,ic[j+1].op2);
strcpy(ic[j].res,ic[j+1].res);
}
i--;
icnt--;
}
}
}
int main()
{
yyparse();
return 0;
}
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$gedit
203.l
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$gedit
203.y
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$lex
203.l
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$bison
-yd 203.y
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$cc
lex.yy.c y.tab.c
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$./a.out
a=b+c*d-e/-f;$
Intermediate Code
------------------------------------------------------------------
Operator Operand 1 Operand 2 Result
* c d t0
+ b t0 t1
- f t2
/ e t2 t3
- t1 t3 t4
= t4 a
Optimized Intermediate Code
------------------------------------------------------------------
Operator Operand 1 Operand 2 Result
* c d t0
+ b t0 t1
- f t2
/ e t2 t3
- t1 t3 a
x=-b+c+d*t/e;$
Intermediate Code
------------------------------------------------------------------
Operator Operand 1 Operand 2 Result
- b t0
+ t0 c t1
* d t t2
/ t2 e t3
+ t1 t3 t4
= t4 x
Optimized Intermediate Code
------------------------------------------------------------------
Operator Operand 1 Operand 2 Result
- b t0
+ t0 c t1
* d t t2
/ t2 e t3
+ t1 t3 x
WRITE
A PROGRAM TO THREE ADDRESS CODE FOR EXPRESSION
LEX
PROGRAM
%{
#include
"y.tab.h"
#include<string.h>
extern FILE
*fin;
int line_no=1;
%}
%%
"\n"
{line_no++;}
[\t]+ {}
[0-9]+
{yylval.no=atoi(yytext); return DIGIT;}
[a-z]+
{strcpy(yylval.str,yytext); return ID;}
. {return
yytext[0];}
%%
void
yyerror(const char *str)
{
printf("\n\t%s is at line %d for
%s",str,line_no,yytext);
}
int yywrap()
{
return 1;
}
YACC PROGRAM
%{
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct icode{
char opr;
char
op1[10],op2[10],res[10];
}ic[10];
int
temp=1,ic_cnt=0;
FILE *fin=NULL;
%}
%union{
char str[10];
int no;
};
%token
<str> ID
%token <no>
DIGIT
%type
<str> E
%left '+''-'
%left '*''/'
%nonassoc '='
%%
st:'$' { int
i=0;
printf("\n\t======================================================\n");
printf("\n\tOperator\top1\t\top2\t\tResult");
printf("\n\t======================================================\n");
for(i=0;i<ic_cnt;i++)
{
printf("\n\t%c\t\t%s\t\t%s\t\t%s",ic[i].opr,ic[i].op1,ic[i].op2,ic[i].res);
printf("\n");
}
printf("\n\n");
exit(0);
}
|ID'='E';'
{emit('=',$3,"",$1); yyparse();};
E:E'+'E
{sprintf($$,"t%d",temp++); emit('+',$3,$1,$$);}
|E'-'E
{sprintf($$,"t%d",temp++); emit('-',$3,$1,$$);}
|E'*'E
{sprintf($$,"t%d",temp++); emit('*',$3,$1,$$);}
|E'/'E
{sprintf($$,"t%d",temp++); emit('/',$3,$1,$$);}
|ID {}
|DIGIT
{sprintf($$,"%d",$1);};
%%
void emit(char
oper,char opd1[10],char opd2[10],char result[10])
{
ic[ic_cnt].opr=oper;
strcpy(ic[ic_cnt].op1,opd1);
strcpy(ic[ic_cnt].op2,opd2);
strcpy(ic[ic_cnt].res,result);
ic_cnt++;
}
int main()
{
fin=fopen("216.txt","r");
if(!fin)
{
printf("\n\nUnable to
open file...!!!");
exit(1);
}
yyparse();
fcloseall();
return(0);
}
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$gedit
216.y
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$lex
216.l
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$YACC
-d 216.y
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$cc
lex.yy.c y.tab.c
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$./a.out
< 216.txt
======================================================
Operator op1 op2 Result
======================================================
+ b a t1
= t1 c
c=a+b;
$
WRITE
A PROGRAM TO GENERATE INTERMEDIATE CODE AND OPTIMIZATION FOR MULTIPLE
EXPRESSION
LEX PROGRAM
%{
#include"y.tab.h"
#include<string.h>
%}
%%
[0-9]+
{yylval.dval=atoi(yytext); return digit;}
[a-zA-Z]+
{strcpy(yylval.str,yytext); return id;}
. return
yytext[0];
%%
int yywrap()
{
return 1;
}
YACC PROGRAM
%{
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct code
{
char opr;
char opd1[20],opd2[20],res[20];
}ic[20];
int
icc=0,temp=1;
FILE *fp=NULL;
%}
%union
{
char str[20];
int dval;
}
%token
<dval> digit
%token
<str> id
%left '+' '-'
%left '/' '*'
%nonassoc '='
%type
<str> E
%%
start: '$' {int
i;
printf("\nINTERMEDIATE
CODE ");
printf("\n-----------
*********** -------------");
printf("\noper\topd1\topd2\tresult");
for(i=0;i<icc;i++)
{
printf("\n%c\t%s\t%s\t%s",ic[i].opr,ic[i].opd1,ic[i].opd2,ic[i].res);
};
printf("\n-----------
*********** -------------");
}
| id'='E';' {emit('=',$3," ",$1);yyparse();}
;
E:
E'+'E
{ sprintf($$,"t%d",temp++);
emit('+',$1,$3,$$);
}
|E'-'E {
sprintf($$,"t%d",temp++);
emit('-',$1,$3,$$);
}
|E'/'E { sprintf($$,"t%d",temp++);
emit('/',$1,$3,$$);
}
|E'*'E {
sprintf($$,"t%d",temp++);
emit('*',$1,$3,$$);
}
|id {}
|digit
{sprintf($$,"%d",$1);}
;
%%
void emit(char
oper,char op1[20],char op2[20],char rss[20])
{
ic[icc].opr=oper;
strcpy(ic[icc].opd1,op1);
strcpy(ic[icc].opd2,op2);
strcpy(ic[icc].res,rss);
icc++;
}
void opt()
{
int i,j;
for(i=0;i<icc;i++)
{
if(ic[i].opr=='=')
{
if((strcmp(ic[i-1].res,ic[i].opd1)==0)||(strcmp(ic[i-1].res,ic[i].opd2)==0))
{
strcpy(ic[i-1].res,ic[i].res);
for(j=i;j<icc-1;j++)
{
ic[j].opr=ic[j+1].opr;
strcpy(ic[j].opd1,ic[j+1].opd1);
strcpy(ic[j].opd2,ic[j+1].opd2);
strcpy(ic[j].res,ic[j+1].res);
}
icc--;
}
}
}
}
void opt2()
{
int i,j,k;
for(i=0;i<icc;i++)
{
for(j=i+1;j<icc;j++)
{
if(ic[i].opr==ic[j].opr&&(strcmp(ic[i].opd1,ic[j].opd1)==0)&&(strcmp(ic[i].opd2,ic[j].opd2)==0))
{
//strcpy(ic[j].opd1,ic[i].res);
strcpy(ic[j+1].opd2,ic[i].res);
for(k=j;k<icc;k++)
{
ic[k].opr=ic[k+1].opr;
strcpy(ic[k].opd1,ic[k+1].opd1);
strcpy(ic[k].opd2,ic[k+1].opd2);
strcpy(ic[k].res,ic[k+1].res);
}
icc--;
break;
}
}
}
}
void disp()
{
int i;
printf("\nOPTIMIZED CODE ");
printf("\n----------- ***********
-------------");
printf("\noper\topd1\topd2\tresult");
for(i=0;i<icc;i++)
{
printf("\n%c\t%s\t%s\t%s",ic[i].opr,ic[i].opd1,ic[i].opd2,ic[i].res);
}
printf("\n----------- ***********
-------------\n\n");
}
int main()
{
fp=fopen("a.txt","r");
yyparse();
opt();
disp();
opt2();
disp();
return;
}
void
yyerror(const char *s)
{
printf("\n");
}
/*
intput:
q=a+b/c*d;
p=a*10-b/c;
m=q;
x=m+p;
$
output:
INTERMEDIATE
CODE
-----------
*********** -------------
oper opd1
opd2 result
/ b
c t1
* t1
d t2
+ a
t2 t3
= t3 q
* a
10 t4
/ b
c t5
- t4
t5 t6
= t6 p
= q m
+ m
p t7
= t7 x
-----------
*********** -------------
OPTIMIZED CODE
-----------
*********** -------------
oper opd1
opd2 result
/ b
c t1
* t1
d t2
+ a
t2 q
* a
10 t4
/ b
c t5
- t4
t5 p
= q m
+ m
p x
-----------
*********** -------------
NEW OPTIMIZED
CODE
-----------
*********** -------------
oper opd1
opd2 result
/ b
c t1
* t1
d t2
+ a
t2 q
* a
10 t4
- t4
t1 p
= q m
+ m
p x
-----------
*********** -------------
*/
INTERMEDIATE
CODE
----------- ***********
-------------
oper opd1
opd2 result
/ b
c t1
* t1
d t2
+ a
t2 t3
= t3 q
* a
10 t4
/ b
c t5
- t4
t5 t6
= t6
p
= q m
+ m
p t7
= t7 x
-----------
*********** -------------
OPTIMIZED CODE
-----------
*********** -------------
oper opd1
opd2 result
/ b
c t1
* t1
d t2
+ a
t2 q
* a
10 t4
/ b
c t5
- t4
t5 p
= q m
+ m
p x
-----------
*********** -------------
OPTIMIZED CODE
-----------
*********** -------------
oper opd1
opd2 result
/ b
c t1
* t1
d t2
+ a
t2 q
* a
10 t4
- t4
t1 p
= q m
+ m
p x
-----------
*********** -------------
WRITE
A PROGRAM TO GENERATE INTERMEDIATE CODE FOR ASSIGNMENT STATEMENT (THREE ADDRESS
CODE).
LEX
PROGRAM
%{
#include "y.tab.h"
#include<string.h>
%}
%%
quit|QUIT {exit(0);}
[a-zA-Z0-9]+ {strcpy(yylval.value, yytext); return
SYMBOL;}
\n {return 0;}
. {return yytext[0];}
%%
int yywrap(void)
{
return 0;
}
YACC PROGRAM
%{
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int sindex = 0;
char temp[10] = "temp1";
struct T
{
char operand1[10];
char operator[4];
char operand2[10];
char tempvar[10];
}code[50];
%}
%union
{
char value[10];
}
%token
<value> SYMBOL
%type
<value> expr stmt
%left '-' '+'
%left '*'
%left '/'
%right '^'
%nonassoc UMINUS
%%
stmt : SYMBOL '=' expr {strcpy($$,
addtotable($1, $3, "=")); temp[4]
++;}
|expr {printf("%s",
$1);}
;
expr : expr '+' expr {strcpy($$,
addtotable($1, $3, "+")); temp[4]
++;}
|expr '-' expr {strcpy($$, addtotable($1, $3, "-")); temp[4] ++;}
|expr '*' expr {strcpy($$, addtotable($1, $3, "*")); temp[4] ++;}
|expr '/' expr {strcpy($$, addtotable($1, $3, "/")); temp[4] ++;}
|SYMBOL {strcpy($$, $1);}
|'('expr')' {strcpy($$, $2);}
|'-'expr %prec UMINUS {strcpy($$,
addtotable("", $2, "-")); temp[4] ++;}
;
%%
char*
addtotable(char *op1, char *op2, char* op)
{
strcpy(code[sindex].operand1, op1);
strcpy(code[sindex].operand2, op2);
strcpy(code[sindex].operator, op);
strcpy(code[sindex].tempvar, temp);
sindex ++;
return temp;
}
void display()
{
int i;
for(i = 0; i < sindex; i ++)
{
if(strcmp(code[i].operator,
"=") != 0)
printf("\n %s
= %s %s %s", code[i].tempvar, code[i].operand1, code[i].operator,
code[i].operand2);
else
printf("\n %s
= %s ", code[i].operand1, code[i-1].tempvar);
}
}
extern FILE
*yyin;
int main(void)
{
printf("\n Type quit to
exit.\n\n");
do
{
printf("\n Enter
Expression : ");
yyparse();
display();
printf("\n\n");
}while(!feof(yyin));
return 0;
}
int yyerror(char
*s)
{
printf("\n %s.\n", s);
}
//TERMINAL
**********************************
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$lex
threeassign.l
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$YACC
-d threeassign.y
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$gcc
lex.yy.c y.tab.c -ll
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$./a.out
Type quit to exit.
Enter Expression : a=-b*(c+d)/(e-f)
temp1 =
- b
temp2 = c + d
temp3 = e - f
temp4 = temp2 / temp3
temp5 = temp1 * temp4
a = temp5
Enter Expression : quit
WRITE
A PROGRAM TO GENERATE OPTIMIZED INTERMEDIATE CODE FOR ASSIGNMENT STATEMENT (THREE
ADDRESS CODE).
LEX
PROGRAM
%{
%{
#include "y.tab.h"
#include<string.h>
%}
%%
quit|QUIT {exit(0);}
[a-zA-Z0-9]+ {strcpy(yylval.value, yytext); return
SYMBOL;}
\n {return 0;}
. {return yytext[0];}
%%
int yywrap(void)
{
return 0;
}
YACC
program
%{
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char* addtotable(char *op1, char *op2,
char* op);
int sindex = 0, optsindex = 0,
repindex = 0;
int found = 0;
char temp[10] = "temp1";
struct T
{
char operand1[10];
char operator[4];
char operand2[10];
char tempvar[10];
}code[30], optcode[20];
struct tempreptable
{
char orig[10];
char replace[10];
}rep[10];
%}
%union
{
char value[10];
}
%token
<value> SYMBOL
%type
<value> expr stmt
%left '-' '+'
%left '*'
%left '/'
%right '^'
%nonassoc UMINUS
%%
stmt : SYMBOL '=' expr {strcpy($$,
addtotable($1, $3, "=")); temp[4]
++;}
|expr {printf("%s",
$1);}
;
expr : expr '+' expr {
if(strcmp($1,
"0") != 0 && strcmp($3, "0") != 0)
{
strcpy($$,
addtotable($1, $3, "+"));
temp[4]
++;
}
if(strcmp($1,
"0") == 0 && strcmp($3, "0") != 0)
{
strcpy($$,
addtotable($3, "", ""));
temp[4]
++;
}
if(strcmp($1,
"0") != 0 && strcmp($3, "0") == 0)
{
strcpy($$,
addtotable($1, "", ""));
temp[4]
++;
}
}
|expr '-' expr {
if(strcmp($3,
"0") != 0)
{
strcpy($$,
addtotable($1, $3, "-"));
temp[4]
++;
}
else
{
strcpy($$,
addtotable($1, "", ""));
temp[4]
++;
}
}
|expr '*' expr {
if(strcmp($1,
"0") == 0 || strcmp($3, "0") == 0)
{
strcpy($$,
addtotable("0", "", ""));
}
else
{
if(strcmp($1,
"1") != 0 && strcmp($3, "1") != 0)
{
strcpy($$,
addtotable($1, $3, "*"));
temp[4]
++;
}
if(strcmp($1,
"1") == 0)
{
strcpy($$,
addtotable($3, "", ""));
temp[4]
++;
}
if(strcmp($3,
"1") == 0)
{
strcpy($$,
addtotable($1, "", ""));
temp[4]
++;
}
}
}
|expr '/' expr {
if(strcmp($3,
"1") != 0 && strcmp($1, "0") != 0)
{
strcpy($$,
addtotable($1, $3, "/"));
temp[4]
++;
}
if(strcmp($3,
"1") == 0)
{
strcpy($$,
addtotable($1, "", ""));
temp[4]
++;
}
if(strcmp($1,
"0") == 0)
{
addtotable("0",
"", "");
}
if(strcmp($3,
"0") == 0)
{
printf("\n
Error : Divide by Zero.");
}
}
|SYMBOL {strcpy($$, $1);}
|'('expr')' {strcpy($$, $2);}
|'-'expr %prec UMINUS {strcpy($$,
addtotable("", $2, "-")); temp[4] ++;}
;
%%
char*
addtotable(char *op1, char *op2, char* op)
{
strcpy(code[sindex].operand1, op1);
strcpy(code[sindex].operand2, op2);
strcpy(code[sindex].operator, op);
strcpy(code[sindex].tempvar, temp);
sindex ++;
return temp;
}
void
optimizetable()
{
int i, j;
for(i = 0; i < sindex; i ++)
{
found = 0;
for(j = 0; j < optsindex;
j ++)
{
if(strcmp(code[i].operand1,
optcode[j].operand1) == 0 && strcmp(code[i].operator,
optcode[j].operator) == 0 && strcmp(code[i].operand2,
optcode[j].operand2) == 0)
{
strcpy(rep[repindex].orig,
code[i].tempvar);
strcpy(rep[repindex].replace,
optcode[j].tempvar);
repindex
++;
found =
1;
}
if(strcmp(code[i].operator,
"+") == 0 || strcmp(code[i].operator, "*") == 0)
{
if(strcmp(code[i].operand1,
optcode[j].operand2) == 0 && strcmp(code[i].operand2,
optcode[j].operand1) == 0)
{
strcpy(rep[repindex].orig,
code[i].tempvar);
strcpy(rep[repindex].replace,
optcode[j].tempvar);
repindex
++;
found
= 1;
}
}
}
if(found == 0)
{
optcode[optsindex
++] = code[i];
}
}
for(i = 0; i < optsindex; i ++)
{
for(j = 0; j < repindex;
j ++)
{
if(strcmp(optcode[i].operand1,
rep[j].orig) == 0)
strcpy(optcode[i].operand1,
rep[j].replace);
if(strcmp(optcode[i].operand2,
rep[j].orig) == 0)
strcpy(optcode[i].operand2,
rep[j].replace);
}
}
}
void display()
{
int i;
printf("\n\n Intermediate Code :
");
for(i = 0; i < sindex - 1; i ++)
{
printf("\n %s = %s %s
%s", code[i].tempvar, code[i].operand1, code[i].operator,
code[i].operand2);
}
printf("\n %s = %s ",
code[i].operand1, code[i-1].tempvar);
printf("\n\n Optimized Code :
");
for(i = 0; i < optsindex; i ++)
{
if(strcmp(optcode[i].operator,
"=") == 0)
printf("\n %s
= %s ", optcode[i].operand1, optcode[i-1].tempvar);
else
printf("\n %s
= %s %s %s", optcode[i].tempvar, optcode[i].operand1, optcode[i].operator,
optcode[i].operand2);
}
printf("\n\n Replace Table For
Optimized Table:\n Original\tReplace");
for(i = 0; i < repindex; i ++)
printf("\n
%s\t\t%s", rep[i].orig, rep[i].replace);
}
extern FILE
*yyin;
int main(void)
{
printf("\n Type quit to
exit.\n\n");
do
{
sindex = 0;
optsindex = 0;
repindex = 0;
temp[4] = '1';
printf("\n\n\n Enter
Expression : ");
yyparse();
optimizetable();
display();
}while(!feof(yyin));
return 0;
}
int yyerror(char
*s)
{
printf("\n %s.\n", s);
}
//TERMINAL
****************************************
[user1@localhost
~]$ lex optimize.l
[user1@localhost
~]$ YACC -d optimize.y
[user1@localhost
~]$ gcc lex.yy.c y.tab.c -ll
[user1@localhost
~]$ ./a.out
Type quit to exit.
Enter Expression : a=(b*c)+(b*c)
Intermediate Code :
temp1 = b * c
temp2 = b * c
temp3 = temp1 + temp2
a = temp3
Optimized Code :
temp1 = b * c
temp3 = temp1 + temp1
a = temp3
Replace Table For Optimized Table:
Original
Replace
temp2
temp1
________________________________________
Enter Expression : a=(b*0)+(0*b)
Intermediate Code :
temp1 = 0
temp1 = 0
temp1 = temp1 + temp1
a = temp1
Optimized Code :
temp1 = 0
temp1 = temp1 + temp1
a = temp1
Replace Table For Optimized Table:
Original
Replace
temp1
temp1
________________________________________
Enter Expression : a=(b+0)+(0+b)+(b-0)
Intermediate Code :
temp1 = b
temp2 = b
temp3 = temp1 + temp2
temp4 = b
temp5 = temp3 + temp4
a = temp5
Optimized Code :
temp1 = b
temp3 = temp1 + temp1
temp5 = temp3 + temp1
a = temp5
Replace Table For Optimized Table:
Original
Replace
temp2
temp1
temp4
temp1
________________________________________
Enter Expression : a=(b*1)+(1*b)-(b/1)
Intermediate Code :
temp1 = b
temp2 = b
temp3 = temp1 + temp2
temp4 = b
temp5 = temp3 - temp4
a = temp5
Optimized Code :
temp1 = b
temp3 = temp1 + temp1
temp5 = temp3 - temp1
a = temp5
Replace Table For Optimized Table:
Original
Replace
temp2
temp1
temp4
temp1
WRITE A LEX AND YACC
PROGRAMTO GENERATE ASSEBMLY LANGUAGE (TARGET) CODE FOR INTERMEDIATE CODE
OF ASSIGNMENT STATEMENT (THREE ADDRESS
CODE).
LEX
PROGRAM
%{
#include<stdio.h>
#include<string.h>
#include
"y.tab.h"
%}
%%
[a-zA-Z][a-zA-Z0-9]* {strcpy(yylval.var, yytext); return
SYMBOL;}
[+] {strcpy(yylval.var,
yytext); return PLUS;}
[-] {strcpy(yylval.var,
yytext); return SUBTRACT;}
[*] {strcpy(yylval.var,
yytext); return MULTIPLY;}
[/] {strcpy(yylval.var,
yytext); return DIVIDE;}
[=] {strcpy(yylval.var,
yytext); return EQUAL;}
[ \t]* {}
[\n] {return '\n';}
%%
int yywrap(void)
{
return 1;
}
YACC PROGRAM
%{
#include<stdio.h>
#include<ctype.h>
#include<string.h>
extern FILE
*yyin, *yyout;
%}
%union
{
char var[10];
}
%token<var>
SYMBOL PLUS EQUAL MULTIPLY DIVIDE SUBTRACT
%type<var>
exp
%right EQUAL
%left PLUS
SUBTRACT
%left MULTIPLY
%left DIVIDE
%%
input: line '\n' input
| '\n' input
| /*empty*/
;
line: SYMBOL EQUAL exp {fprintf(yyout, " MOV %s, AX\n", $1);}
|SYMBOL EQUAL SYMBOL {fprintf(yyout, " MOV AX,
%s \n MOV %s, AX\n",$3,$1);}
|SYMBOL EQUAL SUBTRACT SYMBOL {fprintf(yyout, " MOV AX, - %s \n
MOV %s, AX\n",$4,$1);}
;
exp: SYMBOL PLUS SYMBOL {fprintf(yyout, " MOV AX, %s \n ADD AX,
%s\n",$1,$3);}
|SYMBOL SUBTRACT SYMBOL {fprintf(yyout, " MOV AX, %s \n
SUB AX, %s\n",$1,$3);}
|SYMBOL MULTIPLY SYMBOL {fprintf(yyout, " MOV AX, %s \n
MUL AX, %s\n",$1,$3);}
|SYMBOL DIVIDE SYMBOL {fprintf(yyout, " MOV AX,
%s \n DIV AX, %s\n",$1,$3);}
;
%%
int yyerror(char
*s)
{
printf("\n Error : %s", s);
}
int main(int
argc, char *argv[])
{
if(argc == 3)
{
yyin = fopen(argv[1],
"r");
yyout = fopen(argv[2],
"w");
}
else
{
printf("Usage : ./a.out
file1 file2");
exit(1);
}
yyparse();
printf("\n Output File Generated
: %s\n\n", argv[2]);
return 1;
}
//INPUT FILE :
input.txt ************************************
temp1 = a * b
temp2 = temp3 /
c
temp3 = temp2 +
d
temp4 = e -
temp3
temp5 = - temp4
f = temp5
//TERMINAL**************************************************
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$lex
target.l
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$YACC
-d target.y
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$gcc
lex.yy.c y.tab.c
ccpvg@ccpvg-HP-Compaq-4000-Pro-SFF-PC:~$./a.out
input.txt output.txt
Output File Generated : output.txt
//OUTPUT FILE :
output.txt ************************************
MOV AX, a
MUL AX, b
MOV temp1, AX
MOV AX, temp3
DIV AX, c
MOV temp2, AX
MOV AX, temp2
ADD AX, d
MOV temp3, AX
MOV AX, e
SUB AX, temp3
MOV temp4, AX
MOV AX, - temp4
MOV temp5, AX
MOV AX, temp5
MOV f, AX
No comments:
Post a Comment