Experiment 1-2 compiling principle lexical analysis program design plan
Experiment 1-2 compiling principle lexical analysis program
design plan
Experiment 2-3 lexical analyzer
Experiment 2-3 "compiling principle" S language analysis program design
One, the purpose of the experiment
To understand the two design approaches of lexical analyzer: the method of programming directly from the state transition diagram; Use DFA to write generic lexical analysis programs.
Second, experimental content
Programmatically, according to the state transition diagram
Write a lexical analysis program, it from left to right of each character to scanning source program to generate the words one by one binary type, form a binary type (mark) stream file output. Here, the lexical analyzer is used as a single time, as shown in the figure below.
Specific tasks include:
(1) the input of the organization source program
(2) spell out the words and find the number of their categories, form a binary output, and get the word flow file
(3) delete comments, Spaces, and unused symbols
(4) to find and locate the wrong part of the lexical method and to print the wrong location in the first line of the source program. Output the error message to the screen.
(5) for ordinary identifier, and constants, respectively established identifier table (using the linear table storage), and often scales, when faced with an identifier or constant identifiers table lookup or scale, if found, it returns the position, otherwise it returns 0 and fill in the symbol table or scale.
Identifier table structure: variable name, type (integer, real, character), assigned data area address
Note: the lexical analysis stage only fills in the name of the variable, and the rest is gradually filled in during the analysis, semantic analysis, and code generation.
Common scale structure: constant name, constant value
Write the DFA simulator
Algorithm is as follows:
DFA (S = S0, MOVE [], F [], ALPHABET [], ALLS [])
/ * S for state, initial value for the initial state of DFA, MOVE [] [] as the state transition matrix, F [] for the final set, ALPHABET [] as the ALPHABET, and MOVE in alphabetical order
of [] [] column headings in alphabetical order. ALLS [] is a state set * /
{
Char Wordbuffer [10] = "" / / word buffer empty
Nextchar = getchar (); / / read characters
I = 0;
While (nextchar! = NULL) / / NULL represents these words
{if (nextchar!) {ERROR (" illegal character "), return (" illegal "); }
S = MOVE [S] [nextchar] / / the next state
If (S = NULL) return (" not acceptable "); / / the next state is empty, cannot be identified, and the word is wrong
Wordbuffer [I] = nextchar; / / save the word symbol
I++;
Nextchar = getchar ();
}
Wordbuffer [I] = '\ 0';
If (S, F) return (wordbuffer); / / to accept
The Else return (" not acceptable ");
}
Requirement: the algorithm to realize DFA algorithm, given a DFA (initial state, the state transition matrix, the final state, the alphabet, state sets), call the DFA (), and identify the words in a given source program and view the results are correct.
Third, experimental requirement
Can analyze any S language source
When you run the lexical analysis program, you should enter the file name of the S source language program that you want to be analyzed in the form of a question and answer form, and then complete the lexical analysis task for the program.
Ability to check and handle certain lexical analysis errors
The error messages that the lexical analysis program can give include the total number of errors, the line number for each error, the wrong number and the error message.
This experiment requires the following two types of errors (number 1, 2) :
1 word of illegal characters: the character does not exist in
the table as the illegal character, treatment is to remove the
characters, give error messages, "so-and-so illegal" characters.
2: the source program file is over and the comment is not finished. The annotation format is: / *... * /
Reserved words and special symbols
The word code
one
2
3
4
5
6
7
8
9
The word
int
char
float
void
const
The for
The if
The else
then
The word mnemonic
int
char
float
voidconst
The for
The if
The else
then
The value of the internal code being used
-
-
-
-
-
-
-
-
-
The word code
10
11
12
13
14
15
16
17
18
The word
while
The switch
break
The begin
The end
identifier
Numbers (including integers and real Numbers)
The word mnemonic
while
The switch
break
The begin
The end
id
num
The value of the internal code being used
-
-
-
-
-
In the symbol table
In the table of constants
The word code
19
20
21
22
23
24
25
26
27
The word
+
-
*
/
%
(
)
[
]
The word mnemonic
+
-
*
/
%
(
)
[
]
The value of the internal code being used
-
-
-
-
-
-
-
-
-
The word code
28
28
29
30
The word
! =
The <
>
< =
> =
=
;
{
}
The word mnemonic
rlop
;
{
}
The value of the internal code being used
! =
The <
>
< =
> =
=
-
-
The word code
37
38
39
40
41
42
43
44
45
The word
/ =
+ =
- =
* =
% =
| |
&&
!
=
The word mnemonic
/ =
+ =
- =
* =
% =
The or
The and
The not
=
The value of the internal code being used
-
-
-
-
-
-
-
-
-
Word formation rules:
The letter = [a-za-z]
Number = [0-9]
Identifier = (| _) (| number) *
Number = number (number) * (.number + | e)
Four, S language expressions and statement statements
1. Arithmetic expressions: +, -, *, /, %
The relational operator: >, > =, <, = =, = =, =
The assignment operator: =, + =, -=, * =, =, % =
Variable description: type identifier variable name table;
Type identifier: int char float
If statement: If expression then statement [else statement]
For statements: For (expression 1; expression 2; expression 3) statements
While statements: While expressions do statements
S language program: functions are composed of functions that
cannot be nested.
The function format is:
Return value function name (parameter)
{
The data shows that
statements
}
Five, the program reference structure explanation
Initscanner function: program initialization: input and open source program files and target program files, initializing the retention word table
Scanner function: call the lexscan function to identify the words if the file is not finished.
The Lexscan function: determines the call to different word recognition functions based on the first character of the read word
Isalpha functions: identify reserved words and identifiers
The Isnumber function: identifies the integer, such as the energy, which can be added to recognize part of the real number
Isanotation function: handles the division/and comments
The Isother function identifies other special characters
Output function: Output the binary to the target file, the Output format (the word mnemonic, the word internal code value), such as (int, -) (rlop, >)...
Error function: output Error message to screen
In addition, you can also set up the symbol table, fill in the symbol form and so on, the student can design by itself.
6, experimental procedure specification
Each person completes individually.
When finished, the teacher will check in and give the grade.
After the experiment is finished, write the experiment report (ask for the writing). The report is as follows:
Adult: class, school number, name
The name of the experiment: simplify the lexical analyzer
The purpose of the experiment is to make a simplified C lexical analyzer by hand, and understand and deeply understand the working principle of compiler lexical analyzer.
Three, the experiment content:
The reserved words, common identifiers and special symbols in the source file can be distinguished from the reserved words and special symbol tables, and can make simple error handling.
............
Design the lexical analyzer module to call the structure diagram and each module flow chart.Program source code.
The execution result of the program: input file, output result file and screen information.
Problems and solutions in the experiment.
The experience, the opinion, the suggestion.
Test source code examples:
C / / aa.
Void (aa)
{
Float rate, circle;
Rate = 3;
Circle rate = 3.14 * * rate;
}
The output:
(1) output file:
(void, minus) (id, 0) ((), (), (), (), ().
(2) the symbol table for identifiers:
Name type address
0
aa
one
rate
2
circle
3
4
5
(3) table of constants:
Name value
0
3
one
3.14
2
3
Experiment site: teach 10 5th floor computer institute soft one
room
Experiment time: week 5, week 2, unit 6, week 1, 5 units
(7:30-9:00 PM)
6
本文档为【Experiment 1-2 compiling principle lexical analysis program design plan】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。