报告成绩
编译原理课程设计
项目名称 PL/0
语言编译器
学 院 计算机学院
专业班级 软件工程
0604
班
学 号 3106007039
姓 名 蔡东赟
指导教师 黄剑锋
2008 年 12 月 日
2
3
一、课程题目
我在本次实验中实现了以下内容:
1)增加单词:保留字 ELSE,FOR,TO,DOWNTO,RETURN
运算符 +=,-=,++,――
其中 FOR,TO,DOWNTO,RETURN 属于选做内容。
(2)修改单词:不等号# 改为 <>
(3)增加条件语句的 ELSE 子句
( 4 ) 增 加 单 词 : 保 留 字 ELSE , FOR , TO , DOWNTO , REPEAT,
DOWHILE, UNTIL。
(5)增加运算:++ 和 --。(已做,前++ 后++ 前-- 后--)
(6)其他典型语言设施。(已做 for 语句)
(7)添加编译错误提示功能
二、题目分析与设计
1。插入关键字修改
typedef enum { NUL, IDENT, NUMBER, PLUS, MINUS, TIMES,
SLASH, ODDSYM, EQL, NEQ, LSS, LEQ, GTR, GEQ,
LPAREN, RPAREN, COMMA, SEMICOLON, PERIOD,
BECOMES, BEGINSYM, ENDSYM, IFSYM, THENSYM,
WHILESYM, WRITESYM, READSYM, DOSYM, CALLSYM,
CONSTSYM, VARSYM, PROCSYM, PROGSYM, ELSESYM,
FORSYM,TOSYM,DOWNTOSYM,RETURNSYM,INC,DEC,
PLUSBK,MINUSBK,REPEATSYM,DOWHILESYM,UNTILSYM
} SYMBOL;
char *SYMOUT[] = {"NUL", "IDENT", "NUMBER", "PLUS", "MINUS",
"TIMES",
"SLASH", "ODDSYM", "EQL", "NEQ", "LSS", "LEQ", "GTR",
"GEQ",
"LPAREN", "RPAREN", "COMMA", "SEMICOLON", "PERIOD",
"BECOMES", "BEGINSYM", "ENDSYM", "IFSYM", "THENSYM",
"WHILESYM", "WRITESYM", "READSYM", "DOSYM", "CALLSYM",
"CONSTSYM", "VARSYM", "PROCSYM", "PROGSYM" ,"ELSESYM"
"FORSYM","TOSYM","DOWNTOSYM","RETURNSYM","INC","DE
C",
"PLUSBK","MINUSBK","REPEATSYM","DOWHILESYM","UNTILS
YM"};
4
修改插入
void __fastcall TForm1::ButtonRunClick(TObject *Sender) {
for (CH=' '; CH<='^'; CH++) SSYM[CH]=NUL;
strcpy(KWORD[ 1],"BEGIN"); strcpy(KWORD[ 2],"CALL");
strcpy(KWORD[ 3],"CONST"); strcpy(KWORD[ 4],"DO");
strcpy(KWORD[ 5],"DOWHILE"); strcpy(KWORD[ 6],"DOWNTO");
strcpy(KWORD[ 7],"ELSE"); strcpy(KWORD[ 8],"END");
strcpy(KWORD[ 9],"FOR"); strcpy(KWORD[10],"IF");
strcpy(KWORD[11],"ODD"); strcpy(KWORD[12],"PROCEDURE");
strcpy(KWORD[13],"PROGRAM"); strcpy(KWORD[14],"READ");
strcpy(KWORD[15],"REPEAT"); strcpy(KWORD[16],"RETURN");
strcpy(KWORD[17],"THEN"); strcpy(KWORD[18],"TO");
strcpy(KWORD[19],"UNTIL"); strcpy(KWORD[20],"VAR");
strcpy(KWORD[21],"WHILE"); strcpy(KWORD[22],"WRITE");
WSYM[ 1]=BEGINSYM; WSYM[ 2]=CALLSYM;
WSYM[ 3]=CONSTSYM; WSYM[ 4]=DOSYM;
WSYM[ 5]=DOWHILESYM; WSYM[ 6]=DOWNTOSYM;
WSYM[ 7]=ELSESYM; WSYM[ 8]=ENDSYM;
WSYM[ 9]=FORSYM; WSYM[10]=IFSYM;
WSYM[11]=ODDSYM; WSYM[12]=PROCSYM;
WSYM[13]=PROGSYM; WSYM[14]=READSYM;
WSYM[15]=REPEATSYM; WSYM[16]=RETURNSYM;
WSYM[17]=THENSYM; WSYM[18]=TOSYM;
WSYM[19]=UNTILSYM; WSYM[20]=VARSYM;
WSYM[21]=WHILESYM; WSYM[22]=WRITESYM;
修改 const NORW = 22; /* # OF RESERVED WORDS */预留字修改为
22
添加 全局常数 const SMAX =45; //max of SYMBOL 表示符号的多少
然后把整个程序中 除 Error(33)外的 33 改为 SMAX,这样便于扩展功能。
2.<>不等号的添加 和++ --,+= -=单词的添加
在 void GetSym() 中添加如下代码:
else /* THE FOLLOWING TWO CHECK WERE ADDED
BECAUSE ASCII DOES NOT HAVE A SINGLE CHARACTER FOR <= OR >= */
if (CH=='<') {
GetCh();
if (CH=='=') { SYM=LEQ; GetCh(); }
else if (CH=='>') { SYM=NEQ;GetCh(); } //增加<>为不等于号;
else SYM=LSS;
}
else if(CH=='>') {
GetCh();
if (CH=='=') { SYM=GEQ; GetCh(); }
else SYM=GTR;
5
评论0
最新资源