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

Flex/Le X: Javeria Akram (276) Ifra Zahid

Flex and Lex are tools used to generate lexical analyzers (scanners). Flex is a free and open-source alternative to Lex. Both tools work by reading input files containing rules with regular expressions paired with code actions. They generate C code implementing a finite state machine that can recognize lexical patterns in text and tokenize it. The generated code defines functions like yylex() that scan for tokens which are then returned.

Uploaded by

Ifrah Zahid
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)
31 views21 pages

Flex/Le X: Javeria Akram (276) Ifra Zahid

Flex and Lex are tools used to generate lexical analyzers (scanners). Flex is a free and open-source alternative to Lex. Both tools work by reading input files containing rules with regular expressions paired with code actions. They generate C code implementing a finite state machine that can recognize lexical patterns in text and tokenize it. The generated code defines functions like yylex() that scan for tokens which are then returned.

Uploaded by

Ifrah Zahid
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

FLEX/LE

X
JAVERIA AKRAM (276)
IFRA ZAHID (277)
FLEX
FLEX stands for fast lexical analyzer generator.

Flex is a free and open-source software alternative to lex.

It is a computer program that recognize lexical patterns in text generates lexical


analyzers (also known as "scanners" or "lexers").

flex reads the given input files, or its standard input if no file names are given, for
a description of a scanner to generate. The description is in the form of pairs of
regular expressions and C code, called rules.
LEX
Lex is a program designed to generate scanners, also known as tokenizers, which recognize
lexical patterns in text.

LEX is a tool used to generate a lexical analyzer.

It is intended primarily for Unix-based systems.

The main job of a lexical analyzer (scanner) is to break up an input stream into more usable
elements (tokens)

a = b + c * d ;

ID ASSIGN ID PLUS ID MULT ID SEMI

Lex will read patterns (regular expressions); then produces C code for a lexical analyzer that
scans for identifiers.
Continued..
A rule in a LEX program comprises of a 'pattern' part (specified by a
regular expression) and a corresponding (semantic) 'action' part (a
sequence of C statements)..
The statements in the action part will be executed when the pattern is
detected in the input.
“integer” {return ID_TYPE_INTEGER}

In the above example, “integer” is the pattern and {return


ID_TYPE_INTEGER;} is the corresponding action

LEX translates a set of regular expression specifications (given as


input in input_file.l) into a C implementation of a corresponding
finite state machine (lex.yy.c). This C program, when compiled,
yields an executable lexical analyzer.
Continued..
Continued..
There are some limitations in lex, it cannot be used to recognize nested structures such as

parentheses, since it only has states and transitions between states.

So, Lex is good at pattern matching


Pattern Matching Primitives
Pattern Matching Primitives
Metacharacter Matches
. Any character except newline
\n Newline
* Zero or more copies of the preceding expression
+ One or more copies of he preceding expression
? Zero or one copy of the preceding expression
^ Beginning of line
$ End of line
a| b a or b
(ab)+ One or more copies of ab
“a+b” a+b
[] Character class
Examples
Expression Matches
abc abc
abc* ab abc abcc abccc…
abc+ abc abcc abccc …
a(bc)+ abc abcbc abcbcbc…
a(bc)? a abc
[abc] One of : a,b,c
[a-z] Any letter a,-,z
[A-Za-z0-9]+ One or more alphanumeric characters
[\t\n]+ whitespace
[^ab] Anything except a, b
[a^b] One of a, ^, b
[a|b] One of a , | , b
a|b One of a, b
Lex predefined variables.
Name Function
Int yylex (void) Call to invoke lexer, returns token
Char *yytext Pointer to matched string
yyleng Length of matched string
yylval Value associated with token
Int yywrap (void) Wrapup, return 1 if done, 0 if not done
FILE *yyout Output file
FILE *yyin Input file
INTIAL Initial start condition
BEGIN Condition switch start condition
ECHO Write matched string
yyleng Gives the length of the matched pattern.
yylineno Provides current line number
STRUCTURE OF LEX PROGRAMS
Files are divided into three sections, separated by lines that contain only two percent signs, as
follows:
DECLARATIONS
%%
RULES
%%
AUXILIARY FUNCTIONS
Continued..
DECLARATION:
This section contains global declarations and header files written in C.

RULES:
The rules section associates regular expression patterns with C statements. When the lexer
sees text in the input matching a given pattern, it will execute the associated C code.

AUXILARY FUNCTIONS:
This section contains C statements and functions. These statements

presumably contain code called by the rules in the rules section.


Procedure
Open a notepad and write
a program. Save the file
with “.l” extension.
(Select save as type to
all files).
Continued..

Now open cmd and goto the folder in


which “.l” extension file was saved
using cd command.
Continued..

Write the command flex


file_name.l to open the file.
Continued..
Write the command gcc lex.yy.c to
compile the file into c.
Continued..

Write the command a.exe to execute the file .


Example
Program to check if the given string is a digit or
a word
Continued..
Continued..
Output
How to install FLEX?
https://www.youtube.com/watch?v=ilwXAchl4uw&feature=emb_logo
https://codedost.com/flex/

You might also like