词法分析程序(Lexical Analyzer)要求:
- 从左至右扫描构成源程序的字符流
- 识别出有词法意义的单词(Lexemes)
- 返回单词记录(单词类别,单词本身)
- 滤掉空格
- 跳过注释
- 发现词法错误
程序结构:
输入:字符流(什么输入方式,什么数据结构保存)
处理:
–遍历(什么遍历方式)
–词法规则
输出:单词流(什么输出形式)
–二元组
单词类别:
1.标识符(13)
2.无符号数(10)
3.保留字(一词一码)
4.运算符(11)
5.界符(5)
单词符号 | 种别码 | 单词符号 | 种别码 |
begin | 0 | * | 15 |
call | 1 | / | 16 |
const | 2 | = | 17 |
do | 3 | # | 18 |
end | 4 | < | 19 |
if | 5 | <= | 20 |
odd | 6 | > | 21 |
procedure | 7 | >= | 22 |
read | 8 | := | 23 |
then | 9 | ( | 24 |
var | 10 | ) | 25 |
while | 11 | , | 26 |
write | 12 | ; | 27 |
+ | 13 | , | 28 |
- | 14 | l(l|d)* | 29 |
dd* | 30 |
测试代码:
1 int a=1;
2 int b=2;
3 if(a<b){
4 printf("1");
5 }
6 else{
7 printf("2");
8 }
运行代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 #include <string.h> 3 #define reserve 13 4 #define operator 11 5 6 FILE* fiar; 7 char ch; //getch读取的字符 8 char id[operator+1]; 9 int num; 10 int sum, length; 11 char line[81]; 12 char a[operator+1]; //读取的符号存在这里 13 char word[reserve][operator]; //保留字13个 14 FILE* fin; 15 FILE* fout; 16 char fname[operator]; //输入的文件名 17 int err; 18 19 #define getchdo if(-1==getch()) return -1; 20 21 void init() { 22 strcpy(&(word[0][0]), "begin"); 23 strcpy(&(word[1][0]), "call"); 24 strcpy(&(word[2][0]), "const"); 25 strcpy(&(word[3][0]), "do"); 26 strcpy(&(word[4][0]), "end"); 27 strcpy(&(word[5][0]), "if"); 28 strcpy(&(word[6][0]), "odd"); 29 strcpy(&(word[7][0]), "procedure"); 30 strcpy(&(word[8][0]), "read"); 31 strcpy(&(word[9][0]), "then"); 32 strcpy(&(word[10][0]), "var"); 33 strcpy(&(word[11][0]), "while"); 34 strcpy(&(word[12][0]), "write"); 35 } 36 37 int getch() { 38 if(sum == length) { 39 if(feof(fin)) { 40 return -1; 41 } 42 length = 0; 43 sum = 0; 44 ch = ' '; 45 while(ch != 10) { 46 if(fscanf(fin, "%c", &ch) == EOF) { 47 line[length] = 0; 48 break; 49 } 50 line[length] = ch; 51 length++; 52 } 53 } 54 ch = line[sum]; 55 sum++; 56 return 0; 57 } 58 59 int getsym() { 60 int i, j, k; 61 while(ch == ' ' || ch == 10 || ch == 9||ch== 13) { 62 getchdo; 63 } 64 if(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') { 65 k = 0; 66 do { 67 if(k < operator) { 68 a[k] = ch; 69 k++; 70 } 71 getchdo; 72 } while(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9'); 73 a[k] = '