The lexical analysis is the first step in the compiler. It is the basis for compilation steps: syntax analysis, semantic analysis, and code generation. Currently, lex can be written by lexing, if the language to translate is large, you should use Lex, this Just learn the use of Lex, this article does not use the tool, and yourself write one, aim to lift everyone's mystery to compile the system, I hope to give you a prompt. Online some lexical analysis procedures, I feel It is quite complex written. In fact, the principle of the lexical analysis program is the same, so I just want to understand the simple lexical analysis program, it is not difficult to write complicated, nothing more than a few keywords, write a few conditions to determine the statement. The lexical analysis is the basis of the compiler and is the simplest. Ok, let's get started now.
Let's take a look at this small language.
G [
<项> ∷ (* | /)
According to this grade, construct a lexical analysis program. Enter the end of "#" as ending
According to this grammum, find the language of the language, such as Program, Begin, End, IF, THEN, ELSE, and some other special symbols, and then construct an analysis table, as follows:
Word symbol
Category number
Word symbol
Category number
Identifier 1) 14 constant 2>
15 IF
3> = 16 THEN
4 <17 else
5 <= 18 program
6 <> 19 begin
7: = 20 End
8; 21
9.22 -
10, 23 *
11
/
12
(
13