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

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.

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.

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

3 minutes read.

LR Parser in Compiler Design

LR Parser The most popular type of bottom-up parsing is LR(K) parsing. The LR() parser scans the input from left – to – right, which is the actual abbreviation of L...

2 minutes read.

CLR Parsing Compiler Design

CLR Parsing CLR parsing refers to the canonical lookahead. We will use the canonical collection of LR(1) items for the construction of the CLR(1) parsing table. Generally, CLR(1) parsing has more...

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

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.

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.

S-attributed and L-attributed SDTs

S-attributed and L-attributed SDTs STD stands for Syntax Directed Translation. When we associate some informal notations called semantic rules and the grammar, they are known as STD. So we can say...

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.

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.

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.

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.

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.

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.

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.

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.

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.

Three-Address Code

Three-Address Code If there is at most one operator on the right side of the instruction, then the instruction will be the three-address code so that no arithmetic expressions are permitted....

2 minutes read.

Evolution of Programming Languages in Compiler Design

The Evolution of Programming Languages The first computer came in the 1940s and was programmed in a binary language that told the computer what operations are to be performed and in...

4 minutes read.