LEX

LEX

Lex is a tool/computer program that generates a Lexical analyzer. Lex is developed by Vern Paxson in C around 1987. Lex works together with the YACC parser generator. It allows us to identify a Lexical analyzer by specifying regular expressions to describe patterns for tokens. The Lex language will be the input for the Lex tool, and the tool is termed as the Lex compiler. The role of the Lex compiler is to convert the input pattern into a transition diagram and produce code in a file called Lex.yy.c

Installing Lex on Ubuntu

The following commands are used to install the Lex on Ubuntu:

sudo apt-get update

sudo apt-get install flex   

Use of Lex

The below figure shows the working of Lex. The input file lex.1 is written in Lex language and describes the Lexical analyzer to be generated. Next, the Lex compiler executes the Lex.1 program and transform it into a C program, named lex.yy.c. Then, the C compiler compiles this file into a program a.out. The C compiler's output is working as a Lexical analyzer that takes a stream of input characters and produces a stream of tokens.

The output of the C compiled file, named a.out, is a subroutine of the parser. It is a C function that returns an integer value and will be a code for one of the possible token names. The global variable yy1val consists of the attribute value, symbol table's pointer or nothing. It will share it between the parser and the Lexical analyzer. Therefore making it simple to return both the token name and attribute value. 

Structure of Lex program

Any Lex program is separated by %% delimiter into three sections. The syntax of the Lex program is given below:

                          {Declarations}

                           %%

                          {Translation rules}

                           %%

                           {Auxiliary functions}

The first part contains a declaration of a variable, regular definition, and manifest constant. The text in the declaration section is enclosed in "%{%}" brackets. 

The syntax of translation rule is:

 pattern { Action }

Every pattern is a regular expression. The standard definitions declared in the declaration part may be used by a pattern of this part. The action is a C programming code. We can develop many types of Lex by using various languages. The rule section is enclosed in "%%%%."

The last section contains the C statement and some additional functions. We can compile these functions separately and loaded with a Lexical analyzer.

The Lexical analyzer created by Lex works with the parser in the following ways. When the parser invokes the Lex, the Lexical analyzer starts reading the remaining inputs by taking one input symbol at a time until it finds the longest starting symbol of the input that matches the patterns pi. Then, it performs the action Ai. Ai will return to the parser. If it does not match due to whitespace or comments in Pi, in that case, the Lexical analyzer will further proceed to find additional Lexeme until one of the corresponding actions cause a return to the parser. The Lexical analyzer produces the token name, which will be used by the parser but uses the shared integer variable yylval to forward other information regarding the Lexeme found if needed.


Related Topics

Lexical Analysis in Compiler Design

Lexical Analysis It is the first phase of the compiler. As we know, it is also known as a scanner. The input for lexical analysis is source code. After taking source...

2 minutes read.

YACC in Compiler Design

YACC  YACC is known as Yet Another Compiler Compiler. It is used to produce the source code of the syntactic analyzer of the language produced by LALR (1) grammar. The input...

5 minutes read.

Boolean Expression in Compiler Design

Boolean Expression The translation of conditional statements such as if-else statements and while-do statements is associated with Boolean expression's translation. The main use of the Boolean expression is the following: Boolean expressions...

3 minutes read.

Stack Allocation of Space

Stack Allocation of Space Almost all compilers for languages that use procedure, functions, or methods manage their run-time memory as a stack. Whenever a procedure is called, the local variable's space...

2 minutes read.

Run-Time Environments

Run-Time Environments Storage Organization Every target program has its own logical address, and an executable program runs in it. The logical address space has the location for each program value. The...

2 minutes read.

Optimization of Basic Blocks in Compiler Design

Optimization of Basic Blocks We can apply the optimization process on a basic block. While optimization, there is no need to change the set of expressions computed by the block. The basic...

3 minutes read.

Shift Reduce Parsing in Compiler Design

Like the bottom-up parsing, the shift reduce parser also builds the parse tree from the leaves (bottom) to the root (up). The LR parser is a more versatile variation of...

4 minutes read.

Basic Blocks and Flow Graphs in Compiler Design

Basic Blocks and Flow Graphs In this section, we are going to learn how to work with basic block and flow graphs in compiler design. Basic Block The basic block is a set...

3 minutes read.

Symbol Table Compiler Design

Symbol Table It is an important data structure used by the compiler. It stores information about various entities such as object, class, variable names, functions name, interfaces, procedure, literals, string etc.  The...

3 minutes read.

Storage Allocation

Storage Allocation The storage allocation represents memory management. The allocation of memory can be done in the following ways: Static AllocationStack AllocationHeap Management Static Allocation: It is a procedure used for the allocation of...

1 minute read.

Regular Expression | Compiler Design

Regular Expression A regular expression is a set of patterns that can match a character or string. It can also match alternative characters or strings. The grammar defined by the regular...

3 minutes read.

LR Parser Compiler Design

LR Parser LR parsing is a type of bottom-up parsing that is used to parse the large class of grammars. Here "L" stands for left-to-right scanning of the input "R" stands...

6 minutes read.

Code Generation

Code Generation The last phase of the compiler is code generation. It is the compiler's back-end that makes multiple passes over the IR before generating the target program. The code generator's...

4 minutes read.

Syntax-Directed Translation

Syntax-Directed Translation A context-free-grammar with some additional rules is known as a syntax-directed definition. In SDT, attributes are associated with grammar symbols and rules are associated with productions. The attributes can...

3 minutes read.

Errors in Compiler Design

Introduction Errors in compiler design refer to mistakes or issues that arise during the process of execution of the program. A compiler is a program that translates source code written in...

4 minutes read.

Translation of Array References in Compiler Design

Translation of Array References We can access the elements of an array stored in consecutive blocks very easily and quickly. In a programming language like C and Java, the size of...

2 minutes read.

Syntax Analysis Compiler Design

Syntax Analysis This article will describe the parsing method used in the compiler. The grammatical rule of programming language can be constructed with the help of context-free grammars or BNF (Backus–Naur...

3 minutes read.

Data Flow Analysis in Compiler Design

Data Flow Analysis All the optimization techniques we have learned earlier depend on data flow analysis. DFA is a technique used to know about how the data is flowing in any...

3 minutes read.

Parsing in Compiler Design

Parsing is the term used to describe the process of converting data between different formats. The parser can carry out this operation. The parser is a part of the translator...

4 minutes read.

Switch Case Statement Compiler Design

Case Statement The “case” or “switch” statement is available in various languages. The following is the syntax for the case statement: switch (E)  {             case V1: S1             case V2: S2 ...

1 minute read.