Note:
这学期修了严厉的Mr.J的编译原理,不定期更新笔记!
欢迎指正!
TEST 语言的词法规:
标识符:字母打头,后接任意字母或数字
保留字:标识符的子集,包括:if,else,for,while,do, int,write,read,
无符号整数:由数字组成,但最高位不能为0,允许一位的0,
分界符:(、)、;、{、}
运算符:+、-、*、/、=、<、>、>=、<=、!=、==
注释符:/* */
正则表达式:
标识符: ( a|b|……|z|A|B……|Z )( 0|1|……|9| a|b|……|z|A|B……|Z )*
保留字: 标识符的子集
无符号整数: ( (1……|9 )( 0|1|……|9)* )|0
分界符: ( | ) | ; | { | }
运算符: + | - | * | / | = | < | > | >= | <= | != | ==
- 注释符:
/* (other)* */
NFA
DFA
将NFA确定化之后得到的DFA
程序测试
依据DFA编写词法分析程序
- 测试数据:
/*This a test program.*/
int abc;
int 123;
int A$@;
int i;
int n;
int b,c;
int 2a;
int a2;
read n;
n = 012345;
for (i=1;i<=n; i= i+1)
{
abc=abc+i;
}
if (!n) b = b+c;
/*The loop ended
write abc;
}
- 测试结果:
- 分词结果:
SourceCode
#include<stdio.h>