词法、语法、语义分析结合
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
词法、语法、语义分析结合
一、实验目的与要求
在实现词法、语法分析程序的基础上,编写相应的语义子程序,进行语义处理,加深对语法制导翻译原理的理解,进一步掌握将语法分析所识别的语法范畴变换为某种中间代码(四元式)的语义分析方法,并完成相关语义分析器的代码开发。
二、实验内容
语法制导翻译模式是在语法分析的基础上,增加语义操作来实现的。对于给定文法中的每一产生式,编写相应的语义子程序。在语法分析过程中,每当用一个产生式进行推导或归约时,语法分析程序除执行相应的语法分析动作之外,还要调用相应的语义子程序,以便完成生成中间代码、查填有关表格、检查并报告源程序中的语义错误等工作。每个语义子程序需指明相应产生式中各个符号的具体含义,并规定使用该产生式进行分析时所应采取的语义动作。这样,语法制导翻译程序在对源程序从左到右进行的一遍扫描中,既完成语法分析任务,又完成语义分析和中间代码生成方面的工作。
输入:包含测试用例,如由无符号数和+、−、*、/、(、)构成的算术表达式的源程序文件。
输出:将源程序转换为中间代码形式表示,并将中间代码序列输出到文件中。若源程序中有错误,应指出错误信息。
三、一般实现方法
语法制导翻译模式实际上是对前后文无关文法的一种扩展。一般而言,首先需要根据进行的语义工作,完成对文法的必要拆分和语义动作的编写,从而为每个产生式都配备相应的语义子程序,以便在进行语法分析的同时进行语义解释。要求从编译器的整体设计出发,重点通过对实验二中语法分析程序的扩展,完成一个编译器前端程序的编写、调试和测试工作,形成一个将源程序翻译为中间代码序列的编译系统。
四、基本实验题目
题目:对文法G3[<算术表达式>]中的产生式添加语义处理子程序,完成无符号数的四则运算的计值处理,将输入的四则运算转换为四元式形式的中间代码。本实验只进行了算术表达式四元式的翻译。
五、源代码
*****************************词法分析.h文件
# include
# include
# include
# include
# include
# define UNKNOWN -1
# define LB 0//左括号
# define RB 1//右括号
# define PL 2
# define MI 3
# define MU 4
# define DI 5
# define UCON 6 //Suppose the class number of unsigned constant is 7
# define OVER 7
//# define INT 7
# define LT 8
# define LE 9
# define EQ 10
# define NE 11
# define GT 12
# define GE 13
# define IS 19//14至18被五个关键字占用
# define ID 20
#define MAX_KEY_NUMBER 20 /*关键字的数量*/
#define KEY_WORD_END "waiting for your expanding" /*关键字结束标记*/ char *KeyWordTable[MAX_KEY_NUMBER]={"begin","end", "if", "then", "else", KEY_WORD_END};
char TOKEN[20]="";//存储已扫描的单词
char ch=' ';//用于存储带判断的字符
int row=1;//row标识错误在第几行
//无符号数部分
#define DIGIT 1
#define POINT 2
#define OTHER 3
#define POWER 4
#define PLUS 5
#define MINUS 6
#define ClassOther 200
#define EndState -1
int index=0;//保存已读的字符串的索引
//char JudgeStr[256];//存储已读的字符串
int w,n,p,e,d;
int Class; //Used to indicate class of the word
int ICON;
float FCON;
static int CurrentState; //Used to present current state, the initial value:0
///////语法分析部分
//产生式
/* 1、E->E+T 2、E->E-T 3、E->T 4、T->T*F 5、T->T/F 6、T->F 7、F->(E) 8、F->i
*/
# define SMAX 256
//goto表的列项
# define E 0
# define T 1
# define F 2
int StateStack[SMAX];//状态栈
int StackPoint;//状态栈指针
int TopState;//作为状态栈盏栈顶指针
int InputWordType;//输入的单词类型
// ( ) + - * / i
#
char Action[16][8][4]={"s4", " ", " ", " ", " ", " ", "s5", " ",
" ", " ", "s6", "s7", " ", " ", " ", "A",
" ", "r3", "r3", "r3", "s8", "s9", " ", "r3",
" ", "r6", "r6", "r6", "r6", "r6", " ", "r6",
"s4", " ", " ", " ", " ", " ", "s5", " ",
" ", "r8", "r8", "r8", "r8", "r8", " ", "r8",
"s4", " ", " ", " ", " ", " ", "s5", " ",
"s4", " ", " ", " ", " ", " ", "s5", " ",
"s4", " ", " ", " ", " ", " ", "s5", " ",
"s4", " ", " ", " ", " ", " ", "s5", " ",
" ", "s15", "s6", "s7", " ", " ", " ", " ",
" ", "r1", "r1", "r1", "s8", "s9", " ", "r1",
" ", "r2", "r2", "r2", "s8", "s9", " ", "r2",
" ", "r4", "r4", "r4", "r4", "r4", " ", "r4",
" ", "r5", "r5", "r5", "r5", "r5", " ", "r5",
" ", "r7", "r7", "r7", "r7", "r7", " ",
"r7",};//action表
// E T F
int Goto[16][3]={ { 1, 2, 3},
{-1, -1, -1},
{-1, -1, -1},
{-1, -1, -1},
{10, 2, 3},
{-1, -1, -1},
{-1, 11, 3},
{-1, 12, 3},
{-1, -1, 13},