What is the output of a lexical analyzer?
Sequence of tokens
A lexical analyzer, also known as a lexer or scanner, is the first phase of a compiler. Its main task is to read the input character stream and group characters into meaningful sequences called tokens.
Let's break down what a lexical analyzer does:
Consider a simple statement in a programming language:
int count = 0;
A lexical analyzer would process this character by character and produce the following sequence of tokens:
| Characters | Token Type | Lexeme (Value) |
|---|---|---|
| int | KEYWORD | int |
| <space> | (ignored) | - |
| count | IDENTIFIER | count |
| <space> | (ignored) | - |
| = | ASSIGN_OP | = |
| <space> | (ignored) | - |
| 0 | INTEGER_LITERAL | 0 |
| ; | SEMICOLON | ; |
The output is the list of tokens: KEYWORD, IDENTIFIER, ASSIGN_OP, INTEGER_LITERAL, SEMICOLON. This is the sequence of tokens that the next phase, the parser, will use.
Therefore, the correct output of a lexical analyzer is a sequence of tokens.
| Compiler Phase | Input | Output |
|---|---|---|
| Lexical Analyzer | Source Code (Character Stream) | Sequence of Tokens |
| Syntax Analyzer (Parser) | Sequence of Tokens | Syntax Tree (Parse Tree/Abstract Syntax Tree) |
| Semantic Analyzer | Syntax Tree | Annotated Syntax Tree |
| Intermediate Code Generator | Annotated Syntax Tree | Intermediate Code |
| Code Optimizer | Intermediate Code | Optimized Intermediate Code |
| Code Generator | Optimized Intermediate Code | Target Machine Code |
Lexical analysis involves several key tasks:
The sequence of tokens produced by the lexical analyzer is fundamental for the subsequent phases of the compiler, especially the parser.
Which of the following are applications of symbol table ?
(A) Storage allocation
(B) Checking type compatability
(C) Suppressing duplicate error messages
Choose the correct answer from the options given below:
Arrange the given compilation process in the correct order.
a. Linking
b. Assembling
c. Compiling
d. Pre-processing
Which ONE of the following statements is FALSE regarding the symbol table?