University of Azad Jammu & Kashmir (Muzaffarabad AJK) Department of Computer Science & Information Technology
University of Azad Jammu & Kashmir (Muzaffarabad AJK) Department of Computer Science & Information Technology
University of Azad Jammu & Kashmir (Muzaffarabad AJK) Department of Computer Science & Information Technology
(Muzaffarabad AJK)
Department of Computer Science &
Information Technology
Table of Contents:
(1)
Pg. No Contents
09 Write a program that display the output is Integer, Alphabets, and Floating-points
found?
10 Write a program that display the output is Integer, String, and Floating-
points with \n?
11 Write a program that found the input num is even or odd number including length?
14 Write a Flex program to check whether the input is positive, negative integer and
fraction?
15 Write a Flex program to enter a string and then calculate the number is consonant
or vowels?
16 Write a Flex program to check whether the input is positive, negative integer and
fraction are found?
(2)
How to Compile & Run LEX / YACC Programs on
Windows?
If you are installing Ubuntu (or any Linux based OS) on your system either through Virtual Box
or by making your system Multi-Bootable, just to execute your Lex & Yacc programs; then you
might be wasting your HDD space & your valuable time. You can easily skip this annoying
process and run your programs in Windows OS without any hassles. Here’s how you do it:
Installing Software’s:
1. Download Flex 2.5.4a
2. Download Bison 2.4.1
3. Download GCC compiler DevC++
4. Install Flex at "C:\GnuWin32"
5. Install Bison at "C:\GnuWin32"
6. Install DevC++ at "C:\Dev-Cpp"
7. Open Environment Variables.
8. Add "C:\GnuWin32\bin; C:\Dev-Cpp\bin;" to path.
Introduction to tools
In 1975 Lesk and Johnson gave research paper by name Lex and yacc. According to these papers
Lex can be used to implement the lexical analysis phase of compiler and yacc can be used as
syntax and semantic analysis phase of compiler.
In 2006 the contents in these papers were implemented practically.
Lex
Lexical analyzer phase of compiler is implemented through Lex. Lex’s input is finite automata or
patterns or regular expressions.
Lex reads the source code written by the user and matches it with the rules written inside it and
then produces valid tokens from the source code.
Lex’s tool extension is.l.
(3)
Flex
Syntax and semantic analysis phase of compiler is implemented through bison or yacc.
Yacc’s input is context free grammar (CFG) or productions which is the header file content of
Lex file.
Tokens from Lex file are passed to bison file which makes parse tree from the valid tokens and
also performs the syntax checking and type checking of these tokens.
Yacc tool extension is.y.
(4)
Working of flex and Bison:
Flex Working:
Acts as the scanner part of the compiler or we can say Lex is a way of writing a scanner. Lex is
written in C language. First of all, compiler’s own code will be scanned and then user’s source
code will be scanned. Input is recognized on the basis of finite automata.
Each flex program has three sections:
1. Declaration or definition Section: links header files with parser.
% {header files %}
Ends with %%
2. Rules Section: contains rules according to which input is recognized or validated.
Also ends with %%.
3. User substitution Section: scanning will start from here.
Yylex Scanning is started from this point. Reads the Input that yytext or yyin is
indicating.
(5)
Bison Working:
Acts as the parser part of the compiler. Whenever bison is used as parser part a local header file
is added in lex declaration section. This local file is written in double quotes with extension
abc.tab. h. bison always works in accordance with flex program
Bison program also consists of 3 sections:
1. Declaration section: starts with % {header files %}
% tokens from lex
2. Grammar section / production rules section associated with actions:
contains rules from which parse tree is generated.
Starts with grammar sequence or start symbol and after each rules “ ; “ symbol is
used to end the rule.
For each rule action to be taken is also written.
Results of actions starting from start symbol will be stored in $$.
In action part each token will be stored in the form of $1, $2 etc.
Single characters will be mentioned as it is.
Single characters do not ne token name so they will be mentioned as it anywhere in
the program.
3. Main function: execution of program will stop from here.
(6)
Software’s:
Flex
Bison
Dev-cpp
OR
Flex window
Hardware’s:
(7)
(8)
Lab #01: 13.04.2018
Q: Write a program that display the output is Integer, Alphabets, and
Floating-points Found?
Program:
Output:
(9)
Lab #02: 20.04.2018
Write a program that display the output is Integer, String, and Floating-
points with \n?
Program:
Output:
(10)
Lab #03 04-05-2018
Q: Write a program that found the input num is even or odd number
including length?
Program:(01)
Outputs:
(11)
Lab #03 04-05-2018
Q: Write a program that found the length of even or odd number?
Program:(02)
Output:
(12)
Lab #04 20-05-2018
Q: Write a Flex program to check whether the input is vowel or consonant?
Program:(01)
Output:
(13)
Lab #04 20-05-2018
Q: Write a Flex program to check whether the input is positive, negative integer and
fraction?
Program:(02)
Output:
(14)
Lab #05 31-08-2018
Q: Write a Flex program to enter a string and then calculate the number is consonant or
vowels?
Program:(01)
\\\]\
Output:
(15)
Lab #05 31-08-2018
Write a Flex program to check whether the input is positive, negative integer and fraction
are found?
Program:(02)
Output:
(16)
Lab #06 07-09-2018
Q: Write a program that display the output is Simple or Compound
Sentence?
Program:
Output:
(17)
Lab #07 21-09-2018
Q: write a Program that Found the operator and keyword?
Program:
Output:
(18)
Lab #08 28-09-2018
Q: Flex program to find factorial of a no?
Program:
%option noyywrap
%{
#include<stdio.h>
#include<stdlib.h>
%}
%%
[0-9]+ {return atoi(yytext);}
%%
int main
{
int n,i,c;
printf(“Enter a no.”);
c=yylex();
n=1;
for(i=1; i<=c; i++) or for (i=c; i>=1; i--)
{
n=n*i;
}
printf(“factorial of the no=%d”,n);
}
(19)
Lab #08 28-09-2018
Lex
#define yywrap()
%{
#include<math.h>
#include"y.tab.h"
%}
%%
[\t];
\n return 0;
. return yytext[0];
%%
(20)
YACC
%{
#include<stdio.h>
#include<math.h>
%}
%token<p>num
%%
|exp'-'exp {$$=$1-$3;}
|exp'*'exp {$$=$1*$3;}
|'-'exp {$$=-$2;}
|'('exp')' {$$=$2;}
|SIN'('exp')'{ $$=sin($3);}
|COS'('exp')'{ $$=cos($3);}
(21)
|TAN'('exp')'{ $$=tan($3);}
|SQRT'('exp')'{ $$=sqrt($3);}
|SQURE'('exp')'{ $$=$3*$3;}
|CUBE'('exp')'{ $$=$3*$3*$3;}
|num;
%%
main()
do
yyparse();
} while(1);
yyerror(s)
char *s;
printf("ERROR");
(22)