0% found this document useful (0 votes)
28 views21 pages

BITS Pilani

Uploaded by

wwpretty face
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views21 pages

BITS Pilani

Uploaded by

wwpretty face
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

BITS Pilani

BITS Pilani
Hyderabad Campus
BITS Pilani
Hyderabad Campus

Flex/Bison Tutorial
Today’s Learning objective

• List the functions of Lexer/Scanner.


• Specify regular expressions for C languages tokens.
• Write lex code for given specifications

BITS Pilani, Hyderabad Campus


Compiler Overview

BITS Pilani, Hyderabad Campus


Lexer/Scanner

• Lexical Analysis – process of converting a sequence of


characters into a sequence of tokens.

BITS Pilani, Hyderabad Campus


Lex/Flex background

• lex – is a tool to generator lexical analyzers.


• It was written by Mike Lesk and Eric Schmidt (the
Google guy).
• It isn’t used anymore.
• flex (fast lexical analyzer generator)
• Free and open source alternative.
• You’ll be using this.

BITS Pilani, Hyderabad Campus


Lex/Flex and Yacc/Bison relation to
a compiler toolchain

.l spec file .y spec file

BITS Pilani, Hyderabad Campus


Working of Flex

• Flex uses a .l spec file to generate a tokenizer/scanner.

• The tokenizer reads an input file and chunks it into a


series of tokens which are passed to the parser.

BITS Pilani, Hyderabad Campus


Working of Flex

BITS Pilani, Hyderabad Campus


Flex .l specification file
Definitions Section Definitions: All code
%% between %{ and %} is
Rules Section copied to the beginning of
the resulting C file.
%%
Two types of definitions:
User Code(Auxillary) Section
• C code: must be written between %{ %}
• Example %{ int count=0; %}
• Flex definitions: A definition is very much like a #define cpp
directive. For example
letter [a-zA-Z] digit [0-9]
punct [,.:;!?] nonblank [ˆ \t]
BITS Pilani, Hyderabad Campus
Flex .l specification file: Rules
section
Rules: A number of combinations of pattern and action: if the
action is more than a single command it needs to be in
braces.
Pattern { Action(s) }
• Each pattern is a regular expression, which may use the
regular definitions of the declaration section.
• The actions are fragments of code, typically written in C.

Ex:
{letter} { printf(“alphabet”); }
{digit} { printf(“Number”); }
for { printf(“keyword”); }

BITS Pilani, Hyderabad Campus


Flex .l specification file: user
code
This can be very elaborate, but the main ingredient is the call
to yylex(), the lexical analyser. If the code segment is left
out, a default main is used which only calls yylex.

int main()
{
yylex();
user defined functions ;
…….

BITS Pilani, Hyderabad Campus


Pattern Matching primitives in
flex

BITS Pilani, Hyderabad Campus


Flex specification

• Write a flex specification to find whether an input Number


is an Integer or a Float. (no leading zeros)

DIGIT [0-9]
NDIGIT [1-9]
PERIOD [.]
%%
{NDIGIT}{DIGIT}*
{NDIGIT}{DIGIT}* {PERIOD} ( {DIGIT})+
%%

BITS Pilani, Hyderabad Campus


LEX: Handling of Regular
Expressions

• Disambiguating in case of multiple regular


expression matching:
• Longest input sequence is selected
• If same length, first in specification is selected

BITS Pilani, Hyderabad Campus


Rule of longest match

• If one RE is a subset of the other then the ambiguity is


resolved using this rule.
• int8 must be recognized as id not as 2 tokens; keyword and
digit

%%
begin printf("Compiler");
beginning printf("Compiler Design");
%%

BITS Pilani, Hyderabad Campus


First rule first

%%
begin printf("Compiler");
[a-z]+ printf("Compiler Design");
%%

BITS Pilani, Hyderabad Campus


Exercise-1

• Write a flex specification to count the number of vowels


and consonants in a given string.
%{
#include<stdio.h>
int vowels=0;
int cons=0;
%}
%%
[aeiouAEIOU] {vowels++;}
[a-zA-Z] {cons++;}
%%
main(){
printf(“Enter the string: \n”);
yylex();
printf(“No of vowels=%d\nNo of consonants=
%d\n”,vowels,cons);
}
BITS Pilani, Hyderabad Campus
Exercise-2

• Write a Lex Program to find octal and hexadecimal


numbers

Oct [o][0-7]+
Hex [0][x|X][0-9A-F]+
%%
{Hex} printf("this is a hexadecimal number");
{Oct} printf("this is an octal number");

BITS Pilani, Hyderabad Campus


Exercise-3

• Write a Lex Program to identify the comments

%%
//(.)* \n
/* (. | \n)* */
%%

BITS Pilani, Hyderabad Campus


Thank you

BITS Pilani, Hyderabad Campus

You might also like