Lexical analyzer

The lexical analyzer. It is responsible for searching the lexical components or words that make up the source program, according to rules or patterns . The lexical analyzer input can be defined as a sequence of characters.

Summary

[ hide ]

  • 1 Features
  • 2 Function
  • 3 Need for a lexical analyzer
  • 4 Source

characteristics

The lexical parser has to divide the character sequence into words with its own meaning and then convert it to a terminal sequence from the parser point of view , since that is its input. The lexical analyzer recognizes the words based on a regular grammar so that their nonterminals become the input elements of later phases. At Lex , for example, this grammar is expressed through regular expressions .

Function

The lexical parser is the first phase of a compiler . Its main function is to read the input characters and to produce as output a sequence of lexical components that the parser uses to make the analysis. This interaction is usually applied by converting the lexical parser into a subroutine or coroutine of the parser. Received the command “Give me the next lexical component” from the parser, the lexical parser reads the input characters until it can identify the next lexical component. Other functions they perform are listed below:

  • Delete comments from the program.
  • Eliminate blank spaces, tabs, carriage return, etc., and in general, everything that is meaningless according to the syntax of the language.
  • Recognize the user identifiers, numbers, reserved words of the language and treat them correctly with respect to the symbol table(only in the cases that you must deal with the symbol table).
  • Keep track of the line number you are reading through, in case an error occurs, give information about where it has occurred.
  • Report lexical errors. For example, if @ does not belong to the language, report an error.
  • It can do preprocessor functions.

Need for a Lexical Analyzer

Among the reasons why it is necessary to separate the lexical analyzer from the syntactic can be mentioned:

  • A simple design is perhaps the most important consideration. Separating lexical analysis from syntactic analysis often simplifies one or the other of these phases. The lexical parser allows us to simplify the parser.
  • Compiler efficiency is improved. A separate lexical analyzer allows you to build a specialized and potentially more efficient processor for that function. Much of the time is spent reading the source program and breaking it down into lexical components. Specialized buffer handling techniques for reading input characters and processing lexical components can significantly improve the performance of a compiler.
  • Compiler portability is improved. The peculiarities of the input alphabet and other device-specific anomalies can be limited to the lexical analyzer. The representation of special or non-standard symbolscan be isolated in the lexical analyzer.
  • To focus on recognizing complex building blocks.

 

Leave a Comment

Scroll to Top