词法分析
一、 目的
设计、编制并调式一个词法分析程序,加深对词法分析原理的理解。
二、 要求:
各种单词符号及对应的种别码:
单词符号 种别码 单词符号 种别码
begin 1 : 17
if 2 := 18
then 3 < 20
while 4 <> 21
do 5 <= 22
end 6 > 23
letter(letter|digit)* 10 >= 24
digit digit* 11 = 25
+ 13 ; 26
- 14 ( 27
* 15 ) 28
/ 16 # 0
功能:
输入:所给文法的源程序字符串文件。
输出:二元组(syn,token或sum)构成的序列 文件。
其中:syn为单词种别码
token存放的单词自身字符串;
sum为整型常数
例如:对输入源程序:
s.txt文件:
begin
x:=9;
if x > 0 then x:=2 * x + 1/3;
end#
输出为t.txt文件:
(1, begin)(10, ‘x’) (18,:=) (11,9) (26,;) (2,if) ….
输入:
begin
x:=9;
if x > 0 then x:=2 * x + 1/3;
age1:=25;
while age1<>20 do
begin
age1:=age1-1;
end
end#