一,实验目的:
通过设计编制调试一个具体的词法分析程序,加深对词法分析原理的理解。并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析方法。
二、实验要求:
1.对给定的程序通过词法分析器弄够识别一个个单词符号,并以二元式(单词种别码,单词符号的属性值)显示。而本程序则是通过对给定路径的文件的分析后以单词符号和文字提示显示。
2.本程序自行规定:
(1)关键字"begin","end","if","then","else","while","write","read",
"do", "call","const","char","until","procedure","repeat"
(2)运算符:"+","-","*","/","="
(3)界符:"{","}","[","]",";",",",".","(",")",":"
(4)其他标记 如字符串,表示以字母开头的标识符。
(5)空格、回车、换行符跳过。
部分程序代码如下:
#include "stdio.h"
#include "string.h"
//----------------------Global Declarition---------------------------------
#define SIZE 20
#define sSIZE 12 //There are sSIZE status
#define aSIZE 6 //There will ecounter aSIZE symbol
#define gSIZE 3 //May be goto next gSIZE status
#define geSIZE 6 //There are geSIZE generate expression
#define MAXSIZE 3
//---------------------Finish defining struct-------------------------------------
typedef struct Ge
{
char head; //Leftpart of Generate Expression
char gen[4]; //Rightpart of Generate Expression
}Generate;//--------------------------------Generate Expression base datastruct
typedef struct A
{
int st[aSIZE]; //aSIZE status when encountering terminated symbol
int re[aSIZE]; //Using reduce
}Action;//----------------------------------Action table base datastruct
typedef struct G
{
char head[gSIZE]; //Nonterminated symbol :'E' 'F' 'T'..etc
int gt[gSIZE]; //Mark the next status
}GOTO;//------------------------------------GOTO table base datastruct
int status[SIZE]; //stack of status
int sta_Index; //top of stack of status
char symbol[SIZE]; //stack of symbol
int sym_Index; //Current index of symbol stack
char expression[SIZE]; //Inputed expression
int exp_Index; //index of inputed expression
int exp_top; //top of expression that inputed
int step; //accumulated steps
int IsAccept = 0; //Initlize accept flag to 0
Generate gene[geSIZE +1];
Action act[sSIZE];