0% found this document useful (0 votes)
132 views3 pages

Practical - 2: Aim: Introduction To Lex Tool. Lex

The document provides an introduction to the Lex tool, which is used to generate lexical analyzers by taking input code written in the Lex language and producing a C program that can tokenize input streams. It explains the basic format and functions of Lex programs, including definitions, rules, and user subroutines sections, and provides examples of regular expressions that can be used in Lex rules. The document also includes a sample Lex program that counts the number of words in an input text by tokenizing words separated by whitespace.

Uploaded by

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

Practical - 2: Aim: Introduction To Lex Tool. Lex

The document provides an introduction to the Lex tool, which is used to generate lexical analyzers by taking input code written in the Lex language and producing a C program that can tokenize input streams. It explains the basic format and functions of Lex programs, including definitions, rules, and user subroutines sections, and provides examples of regular expressions that can be used in Lex rules. The document also includes a sample Lex program that counts the number of words in an input text by tokenizing words separated by whitespace.

Uploaded by

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

180220131060 | 2021

Practical – 2
Aim: Introduction to Lex Tool.
Lex
o Lex is a program that generates lexical analyzer. It is used with YACC parser generator.
o The lexical analyser is a program that transforms an input stream into a sequence of tokens.
o It reads the input stream and produces the source code as output through implementing the lexical
analyser in the C program.

The function of Lex is as follows:

o Firstly, lexical analyser creates a program lex.1 in the Lex language. Then Lex compiler runs the lex.1
program and produces a C program lex.yy.c.
o Finally, C compiler runs the lex.yy.c program and produces an object program a.out.
o a.out is lexical analyser that transforms an input stream into a sequence of tokens.
180220131060 | 2021

Lex file format


A Lex program is separated into three sections by %% delimiters. The formal of Lex source is as follows:

1. { definitions }
2. %%
3. { rules }
4. %%
5. { user subroutines }

Some Examples of Regular Expressions

dog matches the string "dog"


[dog] matches matches one character: a "d" an "o" or a "g"
matches matches a string of zero or more characters from the set {"d" an "o" or a
[dog]*
"g"}
(dog|cat) matches the string "dog" or the string "cat"
dog.*cat matches the string "dog" followed by the string "cat" somewhere later in the string
x(dog|cat)x matches the string "dog" or the string "cat" between two "x"s
xx* matches a string of one or more "x"s
x+ matches a string of one or more "x"s
matches two "x"s with optionally the string "dog" or the string "cat" between the
x(dog|cat)?x
"x"'s
[aeiou] matches a single vowel
[A-Z]+ matches a string of one or more uppercase characters
[az-]+ matches a string of one characters from the set or three characters "a", "z", "-"
[^a-z]+ matches a string of one or more characters that are not lowercaase letters
"[a-z]" in flex matches exactly the five character string "[a-z]"
[a-zA-Z][a-zA-Z0-
matches a letter optionally followed by letters or digits
9]*
[1-9][0-9]*|0 matches a positive integer with no leading zero except when the number is zero
[+-]?[0-9]+ matches an integer with optional sign (note that leading zeroes are allowed
matches an even number of characters where every odd numbered character is a
([0-9].)*
digit
matches an integer with no leading zero except when the number is zero. The
[+-]?[1-9][0-9]*|0
number may have an optional sign
[\^\+\-\:\*\]] matches one of the 6 characters: "^", "+", "-", ":", "*", "]"
180220131060 | 2021

Program
/*lex program to count number of words*/
%{
#include<stdio.h>
#include<string.h>
int i = 0;
%}

/* Rules Section*/
%%
([a-zA-Z0-9])* {i++;} /* Rule for counting
number of words*/

"\n" {printf("%d\n", i); i = 0;}


%%

int yywrap(void){}

int main()
{
// The function that starts the analysis
yylex();

return 0;
}

Output

You might also like