Você está na página 1de 17

Presentation By:

Garima kansal
CSE VII A
ROLL NO. 1506810111
→ Summer Training at CDAC, Noida from 11 June , 2018 to
12 July, 2018.
→ An amazing experience as I got to see , how makes the
compiler and working of flex and bison.
→Main objective was to learn how the compiler is build using
flex and bison software.
1. Introduction
2. Bison and how its working
3. Structure of bison
4. Process of complete compilation using flex and bison
5. Examples
Flex and Bison are tools for building programs that handle
Structured input. They were original tools for building compiler
One of the key insights was to break the job into two parts:
1. lexical analysis (also called lexing or scanning)
2. syntax analysis (or parsing).
For example, consider this snippet of C code:
alpha = beta + gamma ;
A scanner divides this into the tokens alpha, equal sign, beta, plus
sign, gamma, and semicolon. Then the parser determines that beta
+ gamma is an expression, and that the expression is assigned to
alpha.
Bison is an upgraded version of the older tool yacc, "yet another
compiler compiler" and it is probably the most common of the
LALR tools out there. Our programming projects are configured
to use the updated version bison, a close relative of the yak, but all
of the features we use are present in the original tool, so this serves
as a brief overview of both.

Bison is designed for use with C code and generates parser written
in C.The parser is configured for use in conjunction with a flex
generated scanner and relies on standard shared features (token
types, yylval) and calls the function yylex as a scanner coroutine.
%
{
C declarations
%}

Bison declarations

%%
Grammar rules
%%

Additional C code
Download latest version of flex and Bison..
1.flex-2.5.4a-1.exe
2.bison-2.4.1-setup.exe
3.download Dev-Cpp or CodeBlocks
* Dev-Cpp
* CodeBlocks

4.Install flex, bison and Dev-cpp or codeblocks. I have installed it in my


C drive.
5. Set environment variable paths
for windows Xp :
goto MyComputer -> Advanced -> Environment Variable -> path ->
edit

for windows 7,8:


Computer -> properties -> advanced settings -> Environment Variable -
> path -> edit
C:\Program Files\GnuWin32\bin;
;

;C:\Program Files\CodeBlocks\MinGW\bin;

commands for flex:


---------------------------------------
to build type : [for the lex file being calc.l]
flex calc.l
to compile type :
gcc -c lex.yy.c
gcc -o test1.exe lex.yy.o
to run/execute type:
test1.exe
-------------------------------------------------
Commands for yacc:
-------------------------------------------------
flex lextest3.l
yacc -y -d yacctest3.y
gcc -c lex.yy.c y.tab.c gcc y.tab.o lex.yy.o -o test.exe
Calculator using File:

Bison file for calculator using input as a file


%{
#include<stdio.h>
int flag=0;
extern FILE *yyin;
int yyerror(char *s);
%}
%token NUMBER
%left '+' '-'
%left '*' '/' '%'
%left '(' ')'
%%
ArithmeticExpression: E{
printf("\nResult=%d\n",$$);
return 0;
}
E:E'+'E {$$=$1+$3;}
|E'-'E {$$=$1-$3;}
|E'*'E {$$=$1*$3;}
|E'/'E {$$=$1/$3;}
|E'%'E {$$=$1%$3;}
|'('E')' {$$=$2;}
| NUMBER {$$=$1;}
;
%%

void main()
{
char ch;
FILE *fp; int i;
fp=fopen("sample.txt","r");
yyin=fp;

for(i=0;i<4;i++)
yyparse();
if(flag==0)
printf("\nEntered arithmetic expression
is Valid\n\n");
getch();
}
int yyerror(char *s)
{
printf("\nEntered arithmetic
expression is Invalid\n\n");
flag=1;
}
OUTPUT:
Flex and Bison are a powerful combination for parsing grammars. By using
the tips and tricks I made .y file of Bison which can parse student details for
Attendance , Authentication and record maintenance of Employees ,
Students of any organization.