Professional Documents
Culture Documents
Compiler Construction Lab Manual Fall 2017 By: Rabbia Mahum
Compiler Construction Lab Manual Fall 2017 By: Rabbia Mahum
Lab 16:
Topics:
Lex
Yacc
Example
Solution of Assignment
Ist Part:
%{
/* Here you write header files or if there is need of any variable in rules section then you
declare that variable here.*/
#include<stdio.h>
int a=0;
%}
Second Part:
%%
/* is section mai aap two parts define karty hain. First is rules means regular expression. Let
say I want only those input strings which have a’s and b’s only so my regular expression is
(a+b)*. Ab user jo b input dy ga agar to who is k according hai or is mai accept hoti hai to he
myra right wala part means action wala part execute hoga.
Agar nahi match hoti input is expression sy to next line py jo b rule hoga us sy matching
check hogi and so on.
Compiler Construction lab manual Fall 2017 By: Rabbia Mahum
Regular expression and pattern k mid mai minimum one space honi chahye
Pattern mai aap {} brackets k andar jitni marzi c statements likhna chahen likh skty hain*/
%%
Third Part:
Is part mai main function hota hai sir flex mai coding krty huye aap is main k function
mai yylex(); likh k yylex function ko must call krty hain bcoz upar jitna b rules wala
section hai who yylex function ki body hai.
main()
//program yahan sy run hona start hoga. Agar aap ny kisi variable ki output ko display krwana
hai to who aap upr jo c statements likhi hain wahen krwao gy. Agr app ny ek he dafa last mai
apny variables ko display krwana hai then you will use abc.txt file, jis mai aap apni input string
write kr k save kr do jahan apka program save hai. Then you can display your variables in main
function
yyin=fopen(“filename.txt”, “r”);
then you will use printf statement to display your variables.
Printf(“enter a string”);
yylex();}
Agar apk compiler mai upar option nahi hai %option noyywrap so apko ek function
yywrap ka b include krna parta hai who program ki processing ko handle kar raha hota
hai. Usually program jb tk run hota rhta hai who function return 0 krta hai .
Int yywrap()
{
return 1: //agar program crash kr jaye to return 1 krta hai
Within flex: Go to tools compile lex file then build lex file.
Open cmd and write a.exe you will get output
With cmd: open cmd write flex filename.l
Then write gcc lex.yy.cc
Then a.exe
Example:
%{
#include<stdio.h>
%}
%%
\n { lines++; words++;}
[A-Z] c_letters++;
[a-z] s_letters++;
[0-9] num++;
. spl_char++;
%%
int yywrap()
return 1;
main(void)
yyin= fopen("abc.txt","r");
yylex();
Compiler Construction lab manual Fall 2017 By: Rabbia Mahum
total=s_letters+c_letters+num+spl_char;
printf("\n\t%d words",words);
Second part mai aap union ki through variable declare krty hain or sath he apki grammar
k tokens and non terminals b. start symbol hamesha S hoga jahan sy apki grammar k
rules start hoty hain.
Third part mai ap lex ki trah he left side py apny rules and right side py c statements jo k
rules complete hony par he execute hongi otherwise nahi hongi.
Fourth part mai aap apna main ka function and error function define krty ho. But note
this k jab lex or yacc dono files aap use kr rhy ho to dono mai sy kisi ek mai main ka
function include hoga.
Compiler Construction lab manual Fall 2017 By: Rabbia Mahum
Solution:
%option noyywrap
%{
#include<stdio.h>
#include<stdlib.h>
#include "y.tab.h" // yeh wh file hai jo yacc ki file compile hony py bnti hai. Yacc ki file
ko include krwaia hai is lex ki file mai so that dono combine ho k output den.
%}
%%
[b-z]+ {printf("Invalid Input");} // user koi b alphabet dy skta hai input mai so hmari
grammar k according srf a’s he acceptable hain but hum ny possibility sb klye rkhni hai
[a]+ {yylval.x = atoi(yytext); return alpha;} // jab b user a enter kry ga wh left
side sy match hojaye ga then right side k according wh phly int main convert hoga r x variable
mai jaye ga jo k hum ny yacc file mai declare kia hua hai. Then yeh return hoga grammar k sb sy
last waly token mai jis ka nam alpha rakha hai. See in yacc file
[+()*] {return yytext[0];} //agr user brackets or operator enter kry ga tow h as it is pass hoga
yacc file mai.
[\t\n\f\v] {return alpha;} //spaces ignore kr di by usin ; \f is for page breake and full form is
form feed, \v is for vertical tab.
Compiler Construction lab manual Fall 2017 By: Rabbia Mahum
%%
%{
#include<stdio.h>
#include<stdlib.h>
%}
%union{int x;}
%start S
%type <x> E T F S
%%
;
Compiler Construction lab manual Fall 2017 By: Rabbia Mahum
%%
int main(void)
{return yyparse();}
Lab Task is to run this program and then update it for your project.