Lex and Yacc Program for Practice
Program 1 :- Lex program to implement validate tokens using regular expression.
Lex Program
digit [0-9]
alpha [a-zA-Z_]
%%
{alpha}({alpha}|{digit})* { printf("Identifier=%s",yytext); }
({digit})+ { printf("Number=%s",yytext); }
%%
int yywrap(void)
{ return 1; }
int main()
{
yylex();
return 0;
}
Program 2:- Lex program to implement to count valid tokens using regular expression.
Lex Programm
%{
int count1,count2;
%}
digit [0-9]
alpha [a-zA-Z_]
%%
{alpha}({alpha}|{digit})* { count1++; }
({digit})+ { count2++; }
%%
int yywrap(void)
{ return 1; }
int main(int argc,char* argv[])
{
yyin=fopen(argv[1],"r");
yylex();
printf("Number of Identifier=%d,Number of Numeric=%d",count1,count2);
return 0;
}
Program 3:- Yacc program to implement syntax analyzer for declaration statement using context
free grammar.
S -> D V
D -> INT
| FLOAT
V : ID ;
| ID , V
Lex Program
%{
#include "y.tab.h"
void yyerror(char*);
%}
alpha [a-zA-Z_]
digit [0-9]
%%
"float" { return FLOAT; }
"int" { return INT; }
{alpha}({alpha}|{digit})* { return ID; }
[,;\n] { return *yytext; }
[ \t] { ; }
. { yyerror("invalid character"); exit(0); }
%%
int yywrap(void)
{ return 1; }
Yacc Program
%{
#include<stdlib.h>
#include<stdio.h>
int yylex(void);
void yyerror(char*);
%}
%token ID
%token FLOAT INT
%%
S : D V '\n' { printf("Valid statement"); }
;
D : INT { }
| FLOAT { }
;
V : ID ';' { }
| ID ',' V { }
;
%%
void yyerror(char* s)
{
fprintf(stderr,"%s",s);
}
int main()
{
yyparse();
return 0;
}
Program 4:-Yacc program to implement syntax analyzer for while statement using context free
grammar.
stmt -> stmt S
| stmt '\n'
S -> While C ExpStmt End While
C -> ID Rel ID
ExpStmt -> ExpStmt Exp
Exp-> ID '=' Exp
| Exp '+' Exp
| Exp '-' Exp
| ID
Lex Program
%{
#include "y.tab.h"
void yyerror(char *);
%}
%%
[\t ]+ /* ignore */ ;
while { return While; }
End { return End; }
"<" { yylval.string_value=strdup(yytext);
return Rel; }
[-=+*\n,;] { return *yytext; }
int { return INT; }
float { return FLOAT; }
[a-zA-Z0-9]+ {
yylval.string_value = strdup(yytext );
return ID;
};
. { yyerror("Invalid Tokens"); }
%%
Yacc Program
%{
#include<stdio.h>
#include<string.h>
void yyerror(char*);
void Gen(char*);
int i,j,f,k=0,count=0,l=0;
char *Array[50][50],temp[50];
char *prev="t0";
extern FILE *yyin;
%}
%union {
char *string_value;
}
%token <string_value> ID
%token <string_value> Rel
%type <string_value> stmt S ExpStmt Exp
%type <string_value> C
%token INT FLOAT While End
%%
stmt : stmt S { printf("Valid While"); }
| stmt '\n' { }
| { }
;
S : While C ExpStmt End While '\n' { }
;
C : ID Rel ID '\n' { }
;
ExpStmt : ExpStmt Exp '\n' { }
| { }
;
Exp: ID '=' Exp { }
| Exp '+' Exp { }
| Exp '-' Exp { }
| ID { }
;
%%
int yywrap() { return 1; }
void yyerror(char *s)
{
fprintf(stderr,"%s",s);
}
int main( int argc, char **argv ) {
yyin=fopen(argv[1],"r");
yyparse();
return 1;
}
Program 5:- Yacc program to implement semantic analyzer for given context free grammar.
S -> E
E -> E + E
|E–E
| id
| num
Lex Program
%{
#include "y.tab.h"
void yyerror(char*);
%}
alpha [a-zA-Z_]
digit [0-9]
%%
{alpha}({alpha}|{digit})* { return ID; }
{digit}+ { return NUM }
[,;\n] { return *yytext; }
[ \t] { ; }
. { yyerror("invalid character"); exit(0); }
%%
int yywrap(void)
{ return 1; }
Yacc Program
%{
#include<stdlib.h>
#include<stdio.h>
int yylex(void);
void yyerror(char*);
%}
%token ID
%token FLOAT INT
%%
S : E '\n' { printf(" Value of Expression is = %d”,$1); }
;
E : E ‘+’ E { $$=$1 + $3; }
| E ‘-’ E { $$=$1 - $3; }
| id { $$=$1; }
| num { $$=$1; }
;
%%
void yyerror(char* s)
{
fprintf(stderr,"%s",s);
}
int main()
{
yyparse();
return 0;
}
Program 6:- Yacc program to validate syntax of Declaration and For statement using context free
grammar.
S -> D V NL
| FOR ob1 ass SC cond SC itr cb1 ob2 stmt cb2
ass -> ID EQ ID | ID EQ INTEGER
cond -> ID OP ID | ID OP INTEGER
itr -> ID EQ exp
exp -> exp '+' exp
| exp '-' exp
| exp '*' exp
| ID
| INTEGER
stmt -> S
|ID EQ exp SC stmt
D -> BUILTIN
V -> ID N
N -> ob INTEGER cb N | SC
Lex Program
%{
#include<stdio.h>
#include "y.tab.h"
%}
%%
for return FOR;
int|float|char return BUILTIN;
"," return COMMA;
";" return SC;
"[" return ob;
"]" return cb;
"(" return ob1;
")" return cb1;
"{" return ob2;
"}" return cb2;
"=" return EQ;
">"|"<"|">="|"<="|"!="|"==" return OP;
[a-zA-Z]+[a-zA-Z0-9]* return ID;
[0-9]+ return INTEGER;
\n return NL;
[-+*\n] return *yytext;
%%
Yacc Program
%{
#include<stdio.h>
%}
%token ID BUILTIN SC NL COMMA ob cb INTEGER FOR ob1 cb1 ob2 cb2 EQ OP
%%
s :D V NL {printf("Valid\n"); return 0;}
|FOR ob1 ass SC cond SC itr cb1 ob2 stmt cb2 {printf("Valid\n"); return 0;}
ass :ID EQ ID | ID EQ INTEGER
cond :ID OP ID | ID OP INTEGER
itr :ID EQ exp
exp : exp '+' exp
| exp '-' exp
| exp '*' exp
| ID
| INTEGER
;
stmt :s
|ID EQ exp SC stmt
|
;
D:BUILTIN
V:ID N
N:ob INTEGER cb N | SC
%%
void yyerror()
{
printf("Error\n");
}
int yywrap()
{
return 1;
}
main()
{
yyparse();
}
Program 7: - Yacc program for Intermediate Code Generation
s1 -> s1 s NL
s -> INT V1
| FLOAT V2
| CHAR V3
V1 -> ID SC
|ID COMMA V1
V2 -> ID SC
| ID COMMA V2
V3 -> ID SC
| ID COMMA V3
Lex Program
%{
#include<stdio.h>
#include "y.tab.h"
int yyval;
int c;
%}
%%
int return INT;
float return FLOAT;
char return CHAR;
"," return COMMA;
";" return SC;
[a-z]+ {c = yytext[0]; yylval = c; return ID;}
\n return NL;
%%
Yacc Program
%{
#include<stdio.h>
#include<string.h>
char* temp[50];
%}
%token ID INT FLOAT CHAR SC NL COMMA
%%
s1: |s1 s NL {printf("Valid\n");}
s: INT V1
|FLOAT V2
|CHAR V3
V1: ID SC { sprintf(temp,"\n%c\tint\t%d",$1,sizeof(int));Gen(temp); }
|ID COMMA V1 { sprintf(temp,"\n%c\tint\t%d",$1,sizeof(int));Gen(temp);
}
V2: ID SC { sprintf(temp,"\n%c\tfloat\t%d",$1,sizeof(float));Gen(temp); }
| ID COMMA V2 { sprintf(temp,"\n%c\tfloat\t%d",
$1,sizeof(float));Gen(temp); }
V3: ID SC { sprintf(temp,"\n%c\tchar\t%d",$1,sizeof(char));Gen(temp); }
| ID COMMA V3 { sprintf(temp,"\n%c\tchar\t%d",
$1,sizeof(char));Gen(temp); }
%%
void yyerror()
{
printf("Error\n");
}
int yywrap()
{
return 1;
}
void Gen(char *val)
{
FILE *fo ;
fo=fopen("output.txt","a");
fputs(val,fo);
fclose(fo);
}
main()
{
yyparse();
}
Program 8:- Yacc program for Intermediate Code Generation
S -> IF ob1 COND cb1 ob2 stmt cb2
stmt -> S
| ID EQ exp SC stmt
exp -> exp '+' exp
| exp '-' exp
| exp '*' exp
| ID
COND -> ID OP ID
Lex program
%{
#include "y.tab.h"
#include<stdio.h>
%}
%%
if {return IF;}
"(" return ob1;
")" return cb1;
"{" return ob2;
"}" return cb2;
";" return SC;
[\t ]+
[-+*\n] {return *yytext;}
"=" return EQ;
[a-zA-Z0-9]+ {yylval.string_value = strdup(yytext );return ID;}
">"|"<"|">="|"<="|"!="|"==" {yylval.string_value = strdup(yytext );return OP;}
%%
Yacc Program
%{
#include<stdio.h>
#include<string.h>
void yyerror(char*);
void Gen(char*);
char temp2[50];
%}
%union
{
char *string_value;
}
%type <string_value> exp
%type <string_value> COND
%type <string_value> s
%type <string_value> stmt
%token <string_value> ID
%token <string_value> OP
%token IF ob1 cb1 ob2 cb2 EQ SC
%%
s : IF ob1 COND cb1 ob2 stmt cb2 { sprintf(temp2,"\nt=if(%s){%s}",$3,$6);$
$="t"; Gen(temp2); }
;
stmt :s
|ID EQ exp SC stmt { sprintf(temp2,"\nt=%s=%s",$1,$3); $$="t"; Gen(temp2); }
|
;
exp : exp '+' exp { sprintf(temp2,"\nt=%s+%s",$1,$3); $$="t"; Gen(temp2); }
| exp '-' exp { sprintf(temp2,"\nt=%s-%s",$1,$3); $$="t"; Gen(temp2); }
| exp '*' exp { sprintf(temp2,"\nt=%s*%s",$1,$3); $$="t"; Gen(temp2); }
| ID { $$=$1; }
;
COND :ID OP ID { sprintf(temp2,"\nt=%s %s %s",
$1,$2,$3); $$="t"; Gen(temp2); }
;
%%
void Gen(char *val)
{
FILE *f;
f=fopen("output.txt","a");
fputs(val,f);
fclose(f);
}
int yywrap() { return 1; }
void yyerror(char *s)
{
return;
}
int main( int argc, char **argv ) {
yyparse();
return 1;
}
Program 9:- Yacc program for Code Optimization
st -> ID = exp
exp-> exp + exp
| exp - exp
| exp / exp
| exp * exp
| ( exp )
| NUMBER
| ID
Lex Program
%{
#include"y.tab.h"
extern char yyval;
%}
%%
[0-9]+ {yylval.symbol = (char)yytext[0];return NUMBER;}
[a-zA-Z]+ {yylval.symbol =(char) yytext[0];return ID;}
. {return yytext[0];}
\n {return 0;}
%%
Yacc Program
%{
#include"y.tab.h"
#include<stdio.h>
#include<stdlib.h>
char temp ='A'-1;
int index1=0;
char addtotable(char, char, char);
struct expr{
char operand1;
char operand2;
char operator;
char result;
};
%}
%union{
char symbol;
}
%left '+''-'
%left '*''/'
%token <symbol> NUMBER ID
%type <symbol> exp
%%
st: ID '=' exp ';' {addtotable((char)$1,(char)$3,'=');};
exp: exp '+' exp {$$ = addtotable((char)$1,(char)$3,'+');}
|exp '-' exp {$$ = addtotable((char)$1,(char)$3,'-');}
|exp '/' exp {$$ = addtotable((char)$1,(char)$3,'/');}
|exp '*' exp {$$ = addtotable((char)$1,(char)$3,'*');}
|'(' exp ')' {$$ = (char)$2;}
|NUMBER {$$ = (char)$1;}
|ID {$$=(char)$1;};
%%
struct expr arr[20];
void quad(){
int i;
FILE *f;
f=fopen("output.txt","a");
for(i=0;i<index1;i++){
if(arr[i].operator=='!') continue;
printf("%c:=\t",arr[i].result);
printf("%c\t",arr[i].operand1);
printf("%c\t",arr[i].operand2);
printf("%c\n",arr[i].operator);
fprintf(f,"%c:=\t",arr[i].result);
fprintf(f,"%c\t",arr[i].operand1);
fprintf(f,"%c\t",arr[i].operand2);
fprintf(f,"%c\n",arr[i].operator);
}
fprintf(f,"\n\n\n");
fclose(f);
}
int main(){
temp='A'-1;
FILE *f;
f=fopen("output.txt","a");
printf("Enter the expression\n");
yyparse();
quad();
opt();
printf("After Optimization\n");
quad();
fclose(f);
int yywrap(){
return 1;
}
void yyerror(char *s){
printf("Error %s",s);
}
char addtotable(char a, char b, char c){
temp++;
arr[index1].operand1=a;
arr[index1].operand2=b;
arr[index1].operator=c;
arr[index1].result=temp;
index1++;
return temp;
}
void opt(){
int i,j;
for(i=0;i<index1;i++)
for(int j=i+1;j<index1;j++){
if(arr[i].operator==arr[j].operator && arr[i].operand1 ==arr[j].operand1 && arr[i].operand2 ==
arr[j].operand2){
int z;
for(int z=j+1;z<index1;z++){
if(arr[z].operand1==arr[j].result)
arr[z].operand1=arr[i].result;
if(arr[z].operand2==arr[j].result)
arr[z].operand2=arr[i].result;
}
arr[j].operator='!';
}
}
}
Program 10:- Yacc program for code generation
stmt -> stmt exp1
exp1 -> ID = exp
exp -> exp + exp
| exp '-' exp
| exp '*' exp
| ID
| INTEGER
Lex Program
%{
#include "y.tab.h"
#include<stdio.h>
%}
%%
[\t ]+ /* ignore the blank spaces */ ;
[-+*=\n,;(){}] { return *yytext; }
[a-zA-Z]+ { // return valid tokens to yacc program
yylval.string_value = strdup(yytext );
return ID;
};
[0-9]+ { // return valid tokens to yacc program
yylval.string_value = strdup(yytext );
return INTEGER;
};
%%
Yacc Program
%{
#include<stdio.h>
#include<string.h>
void yyerror(char*);
void Gen(char*);
int i=1,c=0,j,f,count=0,prev,label=100;
char *Array[10][10],*temp,temp1[50],temp2[50];
extern FILE *yyin;
%}
%union {
char *string_value;
}
%type <string_value> exp exp1
%token <string_value> ID
%token <string_value> INTEGER
%%
stmt : stmt exp1 { }
|
;
exp1 :ID '=' exp { sprintf(temp2,"\n%s=%s",$1,$3); $$="t"; Gen(temp2); sprintf(temp2,"\tmov
%s, %s",$1,$3); Gen(temp2); }
exp : exp '+' exp {sprintf(temp2,"\nt=%s+%s",$1,$3); $$="t"; Gen(temp2);sprintf(temp2,"\tmov
A, %s add A, %s mov t, A",$1,$3);Gen(temp2);}
| exp '-' exp {sprintf(temp2,"\nt=%s-%s",$1,$3); $$="t"; Gen(temp2);sprintf(temp2,"\tmov A,
%s sub A, %s mov t, A",$1,$3);Gen(temp2);}
| exp '*' exp {sprintf(temp2,"\nt=%s*%s",$1,$3); $$="t"; Gen(temp2);sprintf(temp2,"\tmov A,
%s mul A, %s mov t, A",$1,$3);Gen(temp2);}
| ID { $$=$1; }
| INTEGER { $$=$1; }
;
%%
void Gen(char *val)
{
FILE *f;
f=fopen("output.txt","a");
fputs(val,f);
fclose(f);
}
int yywrap() { return 1; }
void yyerror(char *s)
{
}
int main( int argc, char **argv ) {
yyparse();
return 1;
}
Command to execute Lex program ( On Linux Platform)
1) lex file.l
2) gcc lex.yyc.
3) ./a.out
Command to execute Yacc program ( On Linux Platform)
1) lex file1.l
2) yacc –d file2.y
3) gcc lex.yy.c y.tab.h
4) ./a.out