You are on page 1of 8

Compiler Construction lab manual Fall 2017 By: Rabbia Mahum

Lab 16:

Topics:

 Lex
 Yacc
 Example
 Solution of Assignment

How to write a program in lex:

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 always most left side par hota hai

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*/

[0-9] {printf(“ digits”);}

(a+b)* {printf(“ Accepted”);}

%%

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

 How to run a program:


Compiler Construction lab manual Fall 2017 By: Rabbia Mahum

 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>

int lines=0, words=0,s_letters=0,c_letters=0, num=0, spl_char=0,total=0;

%}

%%

\n { lines++; words++;}

[\t ' '] 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(" This File contains ...");

printf("\n\t%d lines", lines);

printf("\n\t%d words",words);

printf("\n\t%d small letters", s_letters);

printf("\n\t%d capital letters",c_letters);

printf("\n\t%d digits", num);

printf("\n\t%d special characters",spl_char);

printf("\n\tIn total %d characters.\n",total);

Need a rest? No!!!

Any Question about lex?

Lets talk about yacc:


Compiler Construction lab manual Fall 2017 By: Rabbia Mahum

 Yacc is yet another compiler compiler.


 Yacc ek parser generator hai jo k c language mai generate krta hai parsing tree like below
 Hum jab yacc ko lex k sath use krty hain to yeh check krty hain k jo input hum ny di hai
and jo grammar k rules hum ny yacc mai mention kiye hain kia who input us grammar k
through parse hoti hai ya nhi. Or you can say hum input jo user ny di hai uski acceptance
check krty hain through grammar rules.
 You have learnt about lex.
 In yacc there are four sections
 First mai ap lex kit rah header files and extern int yylex(); include krty hain . coz aap us
lex file ko include krwa rhy hain apni yacc ki file mai
 Isk ilawa error function ko b isi part mai declare krty hain

 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:

Lex File: assi4.l

%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

. {;} //ignore all other inputs

%%

Yacc File: assi4.y

%{

#include<stdio.h>

#include<stdlib.h>

extern int yylex();

void yyerror(char *msg);

%}

%union{int x;}

%start S

%token <x> alpha

%type <x> E T F S

%%

S:E {printf("\n Valid input As S->E");}

E : E '+' T {$$=$1+$3; printf("\n E->E+T");}

|T {$$=$1; printf(" \nE->T ");}

T :T '*' F {$$=$1*$3; printf("\n T->T*F");}

|F {$$=$1; printf("\nT->F ");}

;
Compiler Construction lab manual Fall 2017 By: Rabbia Mahum

F : '(' E ')' {$$=($2); printf("\n F->(E)");}

| alpha {$$=$1;printf("\n F->a ");}

%%

int main(void)

{return yyparse();}

void yyerror(char *msg)

 Lab Task is to run this program and then update it for your project.

You might also like