Compiling principles of lexical analysis of curriculum design
DONGFANG COLLEGE, FUJIAN AGRICULTURE AND FORESTRY UNIVERSITY
Course title:
Compilation principle
Lexical analysis
Fasten:
Computer science department
Professional grade:
Class 2013 computer science and technology
Study number:
1350303059
Name:
Zhang Qingjian
Teacher:
Both zhu yan
Results:
2015
years
12
month
31
day
directory
Introduction 1
1, purpose 1 of course design
Second, the course design requirement 1
The word of the simple language for analysis is 1
Different kinds of words and symbols for the species
This method analyzes program function 3
Three, the course design report content 3
Environment 3 of course design
System analysis: 3
3 system flow chart and module 4
The 3.4 source code listing 4
3.5 program debugging situation 9
Fourth, summarize 10
Reference 11
preface
Lexical analysis (lexical analysis) is the process of converting character sequences into words (tokens) sequences in computer science. The program or function of Lexical analysis is called Lexical analyzer (Lexer), also called Scanner. Lexical profilers generally exist in the form of functions that are invoked by parsers.
The lexical analysis phase is the first stage of the compilation process, which is the foundation of compilation. The task of this stage a character in a character from left to right to read the source program, to constitute a source program character stream scanning and then according to the rules of word-formation to identify words (also called word symbol or symbol). The lexical analysis program implements this task. Lexical analysis programs can be generated automatically using tools such as Lex.
Lexical analysis is the first stage of the compiler and is
necessary; The core task of lexical analysis is scanning, identifying words and giving qualitative and definite processing to the identified words. Common ways of implementing lexical analyzer: automatic generation, manual generation.
Lexical analysis
The purpose of the course design
To design, compile and debug a lexical analysis program to deepen the understanding of the principle of lexical analysis.
Second, the requirements of the course design
The lexical method of simple language for analysis
Begin if then while do end
All keywords are lowercase.
(2) operator and limiter: = + - * / < < > > = = = :
(#)
(3) other words are identifiers (ID) and integer constants (NUM), which are defined by the following formal definition:
(4) Spaces are composed of blank \ TAB characters \ and line breaks. Spaces are commonly used to separate the ID \ NUM \ operator \ delimiter and keyword, and the lexical analysis phase is usually ignored.
Different kinds of different word symbols
Table c. 1 different kinds of different word symbols
The word symbol
The chairmanship of code
The word symbol
The chairmanship of code
The begin
one
:
17
The if
2
: =
18
then
3
The <
20
while
4
< >
21
The do
5
< =
22
The end
6
>
23
Letter (letter | digit) *
10
> =
24
Digit digit *
11
=
25
+
13
;
26
-
14
(
27
*
15
)
28
/
16
#
0
This method analyzes program function
Input: the source program string for the grammar.
Output: a sequence of binary groups (syn, token, or sum).
Among them: syn for the word germplycode; Token is the string of words that is stored; Sum is the integer constant.
For example: the source program
Begin x: = 9: if x > 9 then x: = 2 * x + 1/3; The end #
The source file, which is analyzed by lexical analysis, outputs the following sequence:
(1, begin) (10, x) (18, =) (1, 9) (26,;) (2, if) to choose a suitable text book
Third, the course design report content
The environment of course design
3.1.1 hardware environment: this system is suitable for computer under DOS, memory 1 g, 2 g is now a common memory capacity, and are equipped with a host, such as screen, keyboard and mouse hardware structure.
3.1.2 software environment: this system USES standard C language, which can be used in Visual C + + 6.0 based on the Visual integrated development environment based on the Windows operating system.
3.2 system technical analysis:
The basic task of the algorithm from the string representation of the source program to identify the independent significance of word symbols, its basic idea is according to the scanning to the types of the first character of the word symbols, spell out the corresponding word symbols. The main program diagram is shown in figure 3-1. The initial consists of two aspects:
Figure 3-1
The initial value of a keyword table.
Keyword as a special identifier, arrange them in advance in a form of keyword tables (called), when the scanner recognize the identifier, check keyword table. If a match can be found, the word is a keyword, otherwise it is a generic identifier. The keyword table is an array of strings describing the following:
Char * rwtab [6] = {" begin ", "if", "then", "while", "do", "end";
The main variables used in the program are syn, token, and sum.
3 system flow charts and modules
Start by setting three variables: the token is used to hold the string that makes up the word symbol; Sum is used for the whole word; The syn is used to store the species of the word symbols. The main part of the scan subroutine is shown in figure 3-2.
Figure 3-2
3.4 source code listings
# include < stdio, h >
# include < string. H >
Char prog [80].
Int syn, p, m, n, sum;
The char * rwtab [6] = {" begin ", "if", "then", "while", "do",
"end"};
Scaner ();
The main ()
{p = 0;
Printf (" \ n please input a string (end with '#') : / n ");
Do {
Scanf (" % c ", & ch);
Prog.
(= ")"
P = 0;
Do {
Scaner ();
The switch (syn)
{case 11: printf (" (% - 10d % 5d) \ n ", "sum" syn);
Break;
1: printf (" you have input a wrong string \ n ");
Getch ();
The exit (0);
Default: printf ((% -10s % 5d) \ n, token, syn);
Break;
}
(syn! = 0)
Getch ();
}
Scaner ()
{sum = 0;
For (m = 0; m < 8; m + +).
Ch = prog [p + +];
M = 0;
"(ch =") | | (ch = "\ n ').
If ((ch < = 'z') & (' a ') ')
{the while (((ch < = 'z') && (ch) > = 'a') | | ((ch < = 'z')
&& (ch) > = 'a') | | ((ch) > = '0' && (ch)) < = '9')
{token [m + +] = ch;
Ch = prog [p + +];
}
P --;
The syn = 10;
For (n = 0; n < 6; n + +)
If (token, rwtab [n]) = = 0
{the syn = n + 1;
Break;
}
}
Else if ((ch > = '0'))
(ch > = '0')
{sum = sum * 10 + ch - '0';
Ch = prog [p + +];
}
P --;
The syn = 11;
}
The else switch (ch)
{case '<' : token.
Ch = prog [p + +];
If (ch = = '=')
{the syn = 22;令牌[m + +]= ch;
}
其他的
{ syn = 20;
p——;
}
打破;
例“>”:令牌[m + +]= ch;
ch =学监(p + +);
如果(ch = = ' = ')
{ syn = 24;
令牌[m + +]= ch;
}
其他的
{ syn = 23;
p——;
}
打破;
例“+”:令牌[m + +]= ch;
ch =学监(p + +);
如果(ch = = ' + ')
{ syn = 17;
令牌[m + +]= ch;
}
其他的
{ syn = 13;
p——;
}
打破;
案例”——“:令牌[m + +]= ch;
ch =学监(p + +);
如果(ch = =“-”)
{ syn = 29;
令牌[m + +]= ch;
}
其他的
{ syn = 14;
p——;
}
打破;
例“!”:ch =学监(p + +);
如果(ch = = ' = ')
{ syn = 21;
令牌[m + +]= ch;
}
其他的
{ syn = 31;
p——;
}
打破;
例“=”:令牌[m + +]= ch;
ch =学监(p + +);
如果(ch = = ' = ')
{ syn = 25;
令牌[m + +]= ch;
}
其他的
{ syn = 18;
p——;
}
打破;
案例‘*’:syn = 15;
令牌[m + +]= ch;
打破;
案例‘/’:syn = 16;
令牌[m + +]= ch;
打破;
例“(”:syn = 27个;
令牌[m + +]= ch;
打破;
情况”)”:syn = 28;
令牌[m + +]= ch;
打破;
例“{”:syn = 5;
令牌[m + +]= ch;
打破;
例“}”:syn = 6;
令牌[m + +]= ch;
打破;
例“;”:syn = 26;
令牌[m + +]= ch;
打破;
例“\”:syn = 30;
令牌[m + +]= ch;
打破;
例“#”:syn = 0;
令牌[m + +]= ch;
打破;
案例‘:’:syn = 17;
令牌[m + +]= ch;
打破;
默认值:syn = 1;
打破;
}
令牌[m + +)= ' \ 0 ';
}
3.5程序调试情况
输入开始x:= 9:如果x > 0 x:= 2 * x + 1/3;#结束后经词法分析输
出如下序列:(1)开始(x 10)(:17)(= 18)(9 11)(26);(2)„„如图5 -
1所示:
图5 - 1
四。总结
通过此次课程设计,使我更加扎实的掌握了有关词法方面的知识,在
设计过程中虽然遇到了一些问
题
快递公司问题件快递公司问题件货款处理关于圆的周长面积重点题型关于解方程组的题及答案关于南海问题
,但经过一次又一次的思考,一遍又
一遍的检查终于找出了原因所在,也暴露出了前期我在这方面的知识
欠缺和经验不足。实践出真知,通过亲自动手制作,Our knowledge is
no longer a piece of paper.
It is better to change than to do. During the course design process, we constantly find fault, keep correcting, keep learning, keep getting. The final testing and debugging session is in itself the practice of "being able to change, and how to do". This course design is finally completed successfully, encountered a lot of problems in the design, finally under the guidance of the teacher, finally the swim inverse and the solution. In the future the development of the society and study practice, must unremitting efforts, can't have a problem want to retreat, be sure to bother to find the problem, and then to solve one by one, only in this way, make you want to do to be successful, can be in the future on the road to PiJingZhanJi, rather than the boat, it will never be a successful harvest, harvest joy, also can never received the recognition from the society and others for you!
reference
[1] liu Ming, xu LAN fang, luo ting. Compiling principle. Beijing: electronic industry press, 2011. 32 ~ 49
[2], luc. The compiling principle. Tsinghua university press,
2013. 52 ~ 65
[3] zhang suqin, lu yingzhi, etc. Compiling principle. Tsinghua university press, 2005. 59 ~ 72
[4] li gansheng. Compiling principle. Tsinghua university press, 2009. 36 ~ 61
- 10 -