[go: up one dir, main page]

0% found this document useful (0 votes)
97 views22 pages

University of Azad Jammu & Kashmir (Muzaffarabad AJK) Department of Computer Science & Information Technology

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 22

University of Azad Jammu & Kashmir

(Muzaffarabad AJK)
Department of Computer Science &
Information Technology

LAB MANUAL: Compiler Construction


Class: BS (CS) 6th semester

Submitted to: MAM SABA ZAIB


Submitted by: Sher Ali Khawaja
Roll #: 36

Table of Contents:
(1)
Pg. No Contents

03 How to Compile & Run LEX / YACC Programs on Windows?

05 Working of flex and bison:

07 Software’s, Hardware’s and Installation of Flex Window.

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?

12 Write a program that found the length of even or odd number?

13 Write a Flex program to check whether the input is vowel or consonant?

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?

17 Write a program that display the output is Simple or Compound Sentence?

18 write a Program that Found the operator and keyword?

19 Flex program to find factorial of a no?

20 Write a Lex and yacc program for calculator?

(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.

GCC DEV C++


GCC is compiler of GNU project. It was started in 1984 by Richard Stallman. Initially GCC
stands for GNU C Language Compiler but now the name is changed to GNU Compiler
Collection. It is an OS resembling to UNIX but not UNIX OS. The main purpose of this
compiler is to produce assembly code.
Lex generates a C file with extension lex.yy.c and yacc generates a C file lex.tab.c. Both these
files go to GCC compiler DEV C++. It behaves as the last three phases of compiler. GCC
generates a file with extension .exe. Now this is the compiler for our language. Source code will
be given to this compiler

Compilation & Execution of your Program:


1. Open Command prompt and switch to your working directory where you have
stored your Lex file (".l") and yacc file (".y")
2. Let your lex and yacc files be "hello.l" and "hello.y". Now, follow the preceding
steps to compile and run your program.
1. For Compiling Lex file only:
1. flex hello.l
2. gcc lex.yy.c
2. For Compiling Lex & Yacc file both:
1. flex hello.l
2. bison -dy hello.y
3. gcc lex.yy.c y.tab.c
3. For Executing the Program
1. a.exe

(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.

Built in variables/functions of Lex:


Functions Purpose

Yyin File pointer. Used in file handling.

Yytext Character pointer. Stores the data in the form of character.

Yywrap Used when input is taken from a file.

Yylex Scanning is started from this point. Reads the Input that yytext or yyin is
indicating.

Atoiyytext Used to convert ASCII to Integer

(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.

Built in keywords and functions of bison


Variable name Purpose
yyparse Starts the parser. This function is called only once and calls yylex
multiple times until lex function ends. Stops when yylex brings 0 And
whenever yylex returns 1 it passes it to yyerror. zxzxx
yylval Used to take tokens to yacc program in the form of extern variables
extern Serves as messenger. Brings values from boundary of one program to
other.
yyerror Always outside the body of main function yyerror is used whenever
error occurs.

(6)
Software’s:

 Flex
 Bison
 Dev-cpp
OR
 Flex window

Hardware’s:

Processor: Intel(R) core (TM) M-5Y10c CPU @0.80GHz 1.00 GHz


RAM: 4.00 GB
System type: 64-bit operating system, x64-based processor.

Installation of Flex Window:

1. First double click on flex icon.


2. Click on next button.
3. Select I accept the agreement and click on next.
4. Then further choose next option and finally installation starts.

(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

Q: Write a Lex and yacc program for calculator?


Program:

Lex
#define yywrap()

%{

#include<math.h>

#include"y.tab.h"

%}

%%

[0-9]+[0-9]*\.[0-9]+ {yylval.p = atof(yytext); return num; }

sin {return SIN;} //return token SIN to YACC

cos {return COS;} //return token COS to YACC

cube {return CUBE;}

squre {return SQURE;}

tan {return TAN;} //return token TAN to YACC

sqrt {return SQRT;} //itret tun token SQRT to YACC*/

[\t];

\n return 0;

. return yytext[0];

%%

(20)
YACC
%{

#include<stdio.h>

#include<math.h>

%}

%union {double p;}

%token<p>num

%token SIN COS TAN SQRT SQURE CUBE

/*Defining the Precedence and Associativity*/

%left '+''-' //lowest precedence

%left '*''/' //highest precedenc

%nonassoc uminu //no associativity

%type<p>exp //Sets the type for non - terminal

%%

ss: exp {printf("Answer=%g\n",$1);}

exp: exp'+'exp {$$=$1+$3;}

|exp'-'exp {$$=$1-$3;}

|exp'*'exp {$$=$1*$3;}

|exp'/'exp {if($3==0) {printf("Divide By Zero"); return(0);}else $$=$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

{ printf("\n enter expression:");

yyparse();

} while(1);

yyerror(s)

char *s;

printf("ERROR");

(22)

You might also like