(完整版)《编译原理》词法分析程序设计方案

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验1-4 《编译原理》S语言词法分析程序设计方案

一、实验目的

了解词法分析程序的两种设计方法:1.根据状态转换图直接编程的方式;2.利用DFA 编写通用的词法分析程序。

二、实验内容

1.根据状态转换图直接编程

编写一个词法分析程序,它从左到右逐个字符的对源程序进行扫描,产生一个个的单词的二元式,形成二元式(记号)流文件输出。在此,词法分析程序作为单独的一遍,如下图所示。

具体任务有:

(1)组织源程序的输入

(2)拼出单词并查找其类别编号,形成二元式输出,得到单词流文件

(3)删除注释、空格和无用符号

(4)发现并定位词法错误,需要输出错误的位置在源程序中的第几行。将错误信息输出到屏幕上。

(5)对于普通标识符和常量,分别建立标识符表和常量表(使用线性表存储),当遇到一个标识符或常量时,查找标识符表或常量表,若存在,则返回位置,否则返回0并且填写符号表或常量表。

标识符表结构:变量名,类型(整型、实型、字符型),分配的数据区地址

注:词法分析阶段只填写变量名,其它部分在语法分析、语义分析、代码生成等阶段逐步填入。

常量表结构:常量名,常量值

2.编写DFA模拟程序

算法如下:

DFA(S=S0,MOVE[][],F[],ALPHABET[])

/*S为状态,初值为DFA的初态,MOVE[][]为状态转换矩阵,F[] 为终态集,ALPHABET[] 为字母表,其中的字母顺序与MOVE[][] 中列标题的字母顺序一致。*/

{

Char Wordbuffer[10]=“”//单词缓冲区置空

Nextchar=getchar();//读

i=0;

while(nextchar!=NULL)//NULL代表此类单词

{ if (nextcha r!∈ALPHABET[]){ERROR(“非法字符”),return(“非法字符”);}

S=MOVE[S][nextchar] //下一状态

if(S=NULL)return(“不接受”);//下一状态为空,不能识别,单词错误

wordbuffer[i]=nextchar ;//保存单词符号

i++;

nextchar=getchar();

}

Wordbuffer[i]=‘\0’;

If(S∈F)return(wordbuffer);//接受

Else return(“不接受”);

}

该算法要求:实现DFA算法,给定一个DFA(初态、状态转换矩阵、终态集、字母表),调用DFA(),识别给定源程序中的单词,查看结果是否正确。

三、实验要求

1.能对任何S语言源程序进行分析

在运行词法分析程序时,应该用问答形式输入要被分析的S源语言程序的文件名,然后对该程序完成词法分析任务。

2.能检查并处理某些词法分析错误

词法分析程序能给出的错误信息包括:总的出错个数,每个错误所在的行号,错误的编号及错误信息。

本实验要求处理以下两种错误(编号分别为1,2):

1:非法字符:单词表中不存在的字符处理为非法字符,处理方式是删除该字符,给出错误信息,“某某字符非法”。

2:源程序文件结束而注释未结束。注释格式为:/* …… */

四、保留字和特殊符号表

单词的构词规则:

字母=[A-Za-z]

数字=[0-9]

标识符=字母(字母|数字)*

数字=数字(数字)*

四、S语言表达式和语句说明

1.算术表达式:+、-、*、/、%

2.关系运算符:>、>=、<、<=、==、!=

3.赋值运算符:=,+=、-=、*=、/=、%=

4.变量说明:类型标识符变量名表;

5.类型标识符:int char float

6.If语句:if 表达式then 语句[else 语句]

7.For语句:for(表达式1;表达式2;表达式3)语句8.While语句:while 表达式do 语句

9.S语言程序:由函数构成,函数不能嵌套定义。

函数格式为:

返回值函数名(参数)

begin

数据说明

语句

end

10.复合语句构成

begin

语句序列

end

五、程序参考结构说明

#include

#include

#include

#include

#define LIST_INIT_SIZE 100

#define LISTINCREMENT 10

#define OK 1

#define ERROR 0

#define Status int

typedef struct{

char key[20];

char num[10];

}ElemType;

typedef struct{

char num[10];

char sign[10];

char mnemonics[10];

char isn[10];

}ElemType2;

typedef struct{

ElemType *elem;

int length;

int listsize;

}SqList;

SqList L;

SqList L2;

int n=1;//行数

FILE *in,*out;// 指向文件的指针

char token[LISTINCREMENT];

char order_number[LIST_INIT_SIZE][LISTINCREMENT]=

{

"0","1","2","3","4","5","6","7","8","9","10",

"11","12","13","14","15","16","17","18","19","20",

"21","22","23","24","25","26","27","28","29","30",

"31","32","33","34","35","36","37","38","39","40",

"41","42","43","44","45","46","47","48","49","50",

};

ElemType key_world[14]=

{

{"int","0"},{"char","1"},{"float","2"},

{"void","3"},{"const","4"},{"for","5"},

{"if","6"},{"else","7"},{"then","8"},

{"while","8"},{"switch","9"},{"break","10"},

{"begin","11"},{"end","12"}

};

ElemType2 symbol[25]=

{

{"0","+","+","_"},{"1","-","-","_"},{"2","*","*","_"},

{"3","/","/","_"},{"4","%","%","_"},{"5","(","(","_"},

{"6",")",")","_"},{"7","[","[","_"},{"8","]","]","_"},

{"9","<","rlop","<"},{"9",">","rlop",">"},{"9","<=","rlop","<="},

{"9",">=","rlop",">="},{"9","==","rlop","=="},{"9","!=","rlop","!="}, {"10",";",";","_"},{"11","/=","/=","_"},

{"12","+=","+=","_"},{"13","-=","-=","_"},{"14","*=","*=","_"},

{"15","%=","%=","_"},{"16","||","or","_"},{"17","&&","and","_"},

{"18","!","not","_"},{"19","=","=","_"}

};

相关文档
最新文档