Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Notes  >  The Phases of a Compiler - Lexical Analysis, Computer Science and IT Engineering

The Phases of a Compiler - Lexical Analysis, Computer Science and IT Engineering - Computer Science Engineering (CSE) PDF Download

THE PHASES OF A COMPILER

1. Lexical analysis (“scanning”)

Reads in program, groups characters into “tokens”

2. Syntax analysis (“parsing”)

Structures token sequence according to grammar rules of the language.

3. Semantic analysis

Checks semantic constraints of the language.

4. Intermediate code generation

Translates to “lower level” representation.

5.     Program analysis and code optimization Improves code quality.

6.     Final code generation.

The Phases of a Compiler - Lexical Analysis, Computer Science and IT Engineering - Computer Science Engineering (CSE)

Conceptually, a compiler operates in phases, each of which transforms the source program from one representation to another. A typical decomposition of a compiler is shown in Fig 1.5 The first three phases, forms the bulk of the analysis portion of a compiler. Two other activities, Symbol table management and error handling, are shown interacting with the six phases.

Symbol table management

 

An essential function of a compiler is to record the identifiers used in the source program and collect information about various attributes of each identifier. A symbol table is a data structure containing a record for each identifier, with fields for the attributes of the identifier. The data structure allows us to find the record for each identifier quickly and to store or retrieve data from that record quickly. When an identifier in the source program is detected by the lex analyzer, the identifier is entered into the symbol table.

Error Detection and Reporting

Each phase can encounter errors. A compiler that stops when it finds the first error.

The syntax and semantic analysis phases usually handle a large fraction of the errors detectable by the compiler. The lexical phase can detect errors where the characters remaining in the input do not form any token of the language. Errors when the token stream violates the syntax of the language are determined by the syntax analysis phase. During semantic analysis the compiler tries to detect constructs that have the right syntactic structure but no meaning to the operation involved.

 

The Analysis phases

As translation progresses, the compiler‟s internal representation of the source program changes. Consider the statement,

position := initial + rate * 10

The lexical analysis phase reads the characters in the source pgm and groups them into a stream of tokens in which each token represents a logically cohesive sequence of characters, such as an identifier, a keyword etc. The character sequence forming a token is called the lexeme for the token. Certain tokens will be augmented by a „lexical value‟. For example, for any identifier the lex analyzer generates not only the token id but also enter s the lexeme into the symbol table, if it is not already present there. The lexical value associated this occurrence of id points to the symbol table entry for this lexeme. The representation of the statement given above after the lexical analysis would be: id1: = id2 + id3 * 10

Syntax analysis imposes a hierarchical structure on the token stream, which is shown by syntax trees (fig ).

The Phases of a Compiler - Lexical Analysis, Computer Science and IT Engineering - Computer Science Engineering (CSE)

Intermediate Code Generation

After syntax and semantic analysis, some compilers generate an explicit intermediate representation of the source program. This intermediate representation can have a variety of forms. In three-address code, the source pgm might look like this,

temp1: = inttoreal (10)

temp2: = id3 * temp1

temp3: = id2 + temp2

id1: = temp3


Code Optimization

The code optimization phase attempts to improve the intermediate code, so that faster running machine codes will result. Some optimizations are trivial. There is a great variation in the amount of code optimization different compilers perform. In those that do the most, called “optimising compilers‟, a significant fraction of the time of the compiler is spent on this phase.

Code Generation

The final phase of the compiler is the generation of target code, consisting normally of relocatable machine code or assembly code. Memory locations are selected for each of the variables used by the program. Then, intermediate instructions are each translated into a sequence of machine instructions that perform the same task. A crucial aspect is the assignment of variables to registers.

The document The Phases of a Compiler - Lexical Analysis, Computer Science and IT Engineering - Computer Science Engineering (CSE) is a part of Computer Science Engineering (CSE) category.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)

Top Courses for Computer Science Engineering (CSE)

FAQs on The Phases of a Compiler - Lexical Analysis, Computer Science and IT Engineering - Computer Science Engineering (CSE)

1. What is the purpose of lexical analysis in the phases of a compiler?
Ans. Lexical analysis is the first phase of a compiler and its purpose is to break down the source code into a sequence of meaningful tokens. It scans the source code character by character, identifies the tokens based on predefined rules, and generates a token stream as output.
2. How does lexical analysis help in the compilation process?
Ans. Lexical analysis plays a crucial role in the compilation process by converting the source code into a token stream. This token stream serves as an input for the subsequent phases of the compiler, such as syntax analysis and semantic analysis. It ensures that the subsequent phases operate on a well-defined set of tokens, making it easier to analyze and generate the final executable code.
3. What are the key components of lexical analysis?
Ans. The key components of lexical analysis are: 1. Regular Expressions: These are used to define the patterns of tokens that are recognized by the lexical analyzer. 2. Finite Automaton: It is a mathematical model used to implement the lexical analyzer. It scans the source code based on the regular expressions and generates tokens as output. 3. Lexical Errors: Lexical analysis also handles lexical errors, such as invalid characters or identifiers, and reports them to the user.
4. Can lexical analysis handle all types of programming languages?
Ans. Yes, lexical analysis can handle all types of programming languages. The regular expressions used in lexical analysis can be customized to match the syntax and rules of different programming languages. By defining appropriate regular expressions, the lexical analyzer can effectively recognize and classify tokens in any programming language.
5. Is lexical analysis a time-consuming phase in the compilation process?
Ans. No, lexical analysis is not a time-consuming phase in the compilation process. It typically scans the source code in a linear manner and performs simple pattern matching operations. The time complexity of lexical analysis is linear, proportional to the size of the source code. Therefore, even for large programs, the overhead of lexical analysis is minimal compared to the other phases of the compiler.
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Computer Science and IT Engineering - Computer Science Engineering (CSE)

,

Computer Science and IT Engineering - Computer Science Engineering (CSE)

,

Important questions

,

video lectures

,

practice quizzes

,

Computer Science and IT Engineering - Computer Science Engineering (CSE)

,

study material

,

Extra Questions

,

Objective type Questions

,

Viva Questions

,

shortcuts and tricks

,

The Phases of a Compiler - Lexical Analysis

,

mock tests for examination

,

ppt

,

MCQs

,

Previous Year Questions with Solutions

,

Exam

,

The Phases of a Compiler - Lexical Analysis

,

Summary

,

past year papers

,

Semester Notes

,

The Phases of a Compiler - Lexical Analysis

,

Free

,

pdf

,

Sample Paper

;