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 code as an input, it breaks them into valid tokens by removing whitespace, comment from source code. If there are any invalid tokens present in the source code, it will show an error. The output of the lexical analysis is a sequence of tokens, which will be further sent to the syntax analysis as an input.

    If the source program consists of a macro – preprocessor, then the lexical analyzer will also perform the expansion of macros.

Lexical Analysis

Tokens:

Token pairs consist of a token name and an optional attribute value. The sequence of characters in any tokens is known as lexemes. To check whether the lexemes are valid token or not, there are some predefined rules. These rules are defined with the help of the grammar of programming language, which is also known as a pattern.

There are various kinds of tokens in a programming language. Some of these are keyword, string, identifiers, operators, separators, numbers, punctuations are considered as tokens. 

Example:

int value = 10;

Solution:

Here in this example, int (keyword), value (identifiers), = (operator), 100 (constant), and ; (symbol) are the tokens. So here, we can see that the total number of tokens present in this example is 5.

Example:

printf ("Tutorialandexample");

Solution:

'printf', '(', ' )' , 'Tutorialandexample', ';' are the tokens present in this example.

So here, we can say that there are five valid tokens.

Specification of Tokens:

Following are the various kinds of tokens present in any programming language:

Alphabets:

An alphabet is any finite set of symbols. Binary alphabets (0,1) and sets of hexadecimal (a-z, A-Z) are known as alphabets in the English language. 

String:

In a programming language, a string is a finite set of characters. The number of alphabets present in any string is the length of the string. ? is known as an empty string

Example:

Tutorialandexample: There are 18 alphabets present in Tutorialandexample. So the length of the string is 18.

Operators/Symbols :

Arithmetic Operators+, -, *, /, %
Relational Operator !=, <, <=, >, >=, ==
Logical Operator&, &&, |, ||, !
Assignment Operators+=, /=, *=, -=, =
Shift Operators>>, >>>, <<, <<<
PunctuationComma(,), Semicolon(;), dot(.)

Language:

In general, language is a collection of words made up of a finite set of alphabets. Computer language is a set of instructions, and some kind of output is associated with it. 


Related Topics

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.

Run-Time Storage Management

Run-Time Storage Management Every executing program has its own logical address space. Logical address space is partitioned into: Code: It is responsible for storing the executable target code. Static: It is used to...

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.

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.

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.

SLR 1 Parsing Compiler Design

SLR(1) Parsing It is a simple LR parsing. Most of the function of this parsing is the same as LR(0) parsing. The parsing table for both the parser vary. The SLR(1)...

5 minutes read.

Ambiguity Elimination Compiler Design

Ambiguity Elimination Ambiguity elimination makes the sentence clear and readable. A sentence is grammatically ambiguous if it can produce more than one parse tree for a particular grammar. In this article,...

3 minutes read.

Derivation and Parse Tree in Compiler Design

Derivation and Parse Tree In this article, we will learn Derivation and Parse Tree. Derivations The parse tree can be constructed by taking a derivational view in which production is treated as rewriting...

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

Bottom-Up Parsing in Compiler Design

Bottom-Up Parsing A bottom-up parsing constructs the parse tree for an input string beginning from the bottom (the leaves) and moves to work towards the top (the root). Bottom-up parsing is...

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

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.

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.

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.

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.

Machine-Independent Optimizations in Compiler Design

Machine-Independent Optimizations The main aim of machine-independent optimization is to improve the generated intermediate code so that compiler can get better target code. Eliminating unwanted code from the object code or replacing...

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

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.

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.