Lex Yacc Program Practice
Lex Yacc Program Practice
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; }
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]
%%
[ \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
%%
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.
Lex Program
%{
#include "y.tab.h"
void yyerror(char *);
%}
%%
[\t ]+ /* ignore */ ;
[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
%%
C : ID Rel ID '\n' { }
;
%%
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]
%%
[ \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
%%
%%
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
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;}
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;
}
main()
{
yyparse();
}
Program 8:- Yacc program for Intermediate Code Generation
COND -> ID OP ID
Lex program
%{
#include "y.tab.h"
#include<stdio.h>
%}
%%
if {return IF;}
[\t ]+
Yacc Program
%{
#include<stdio.h>
#include<string.h>
void yyerror(char*);
void Gen(char*);
char temp2[50];
%}
%union
{
char *string_value;
}
%%
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); }
|
;
%%
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 '*''/'
%%
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;};
%%
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 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
Lex Program
%{
#include "y.tab.h"
#include<stdio.h>
%}
%%
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;
}
%%
%%
1) lex file.l
2) gcc lex.yyc.
3) ./a.out
1) lex file1.l
2) yacc –d file2.y
3) gcc lex.yy.c y.tab.h
4) ./a.out