宁波诺丁汉大学
编译原理 实验指导书
计算机软件教研室
实验指导一 查填符号表
实验目的:
熟悉c语言编译环境;掌握词法扫描,查填符号表; 实验要求: 参见教材”符号表”一章。 实验参考:参考附录 附录
program filltable;{文件名}
type myword=record{纪录类型,存储每一个扫描的单词} code:integer; {该单词的内部编码} content:string; {单词本身} end;
const length=8; {单词限制的长度}
var fi:text;{输入文件} fo:text; {输出文件}
symbol:array[1..100] of myword;{定义接收不同标识符的数组} j:integer;
noerror:boolean; {扫描过程中是否有错误} count:integer; {不同单词的个数}
allinput:integer; {输入单词的总数}
function found(tofind:string):boolean;{true:已经有该单词;false:现有的数组中还没有该单词} var i:integer; begin
allinput:=allinput+1; found:=false;
for i:=1 to count do
if symbol[i].content=tofind then found:=true; end;
procedure fillintotable; var ch:char;
tempword:string; i:integer; begin i:=0;
tempword:='';
while not eof(fi) do{是否到达文件尾} begin
while not eoln(fi)do{是否到达行尾} begin
read(fi,ch); case ch of
'a'..'z','A'..'Z':if i>=8 then begin
writeln(fo,'Error:Over length error');{长度超过}
2
noerror:=false; break; end else begin
tempword:=tempword+ch; i:=i+1; end; '0'..'9':begin
if i>=8 then begin
writeln(fo,'Error:Over length error'); noerror:=false; break; end; if i=0 then begin
writeln(fo,'Error:Digit first error'); {以数字开头} noerror:=false; break; end
else begin
tempword:=tempword+ch; i:=i+1; end end; ',': begin
if not found(tempword) then begin
count:=count+1;
symbol[count].content:=tempword; symbol[count].code:=100+count; end; i:=0;
tempword:=''; end;
';':if not eof(fi) then begin
writeln(fo,'Error:There are more than 2 \ {有两个或者两个以上的”;”} noerror:=false; break; end; else begin
noerror:=false;
3
writeln(fo,'Error: Illegal input symbol',ch); {非法的字符输入} break; end; end; end;
readln(fi);{换行} end; end;
procedure printid; var i:integer; begin
{结果输出到fo文件} writeln(fo);
writeln(fo,'There are ',allinput,' symbols inputed,and'); writeln(fo,'there are ',count,' different symbols');
writeln(fo,'The different symbols and according codes are as follows'); for i:=1 to count do writeln(fo,symbol[i].code,' ', symbol[i].content); end;
begin
count:=0;
assign(fi,'myinput.txt');{将fi与myinput.txt文件关联上} assign(fo,'myoutput.txt');{ 将fo与myoutput.txt文件关联上} rewrite(fo); reset(fi); noerror:=true; fillintotable; close(fi);
if noerror then printid; close(fo); end.
思考:如果在输入文件中分别出现字符串长度超过8个、有两个或者两个以上的”;”出现、出现汉字等非规定字符、以数字开头等会出现什么样的情况?
4
实验指导二 词法扫描并生成中间表达式
实验目的:
进一步熟悉c语言编译环境;掌握词法扫描,掌握中缀表达式生成逆波兰表达式的方法; 实验要求:根据逆波兰式的生成方式,将形如下列的算术表达式:
a*(b+c-d)+e/f-g 生成逆波兰式 要求:(1)从键盘输入算术表达式
(2)简单的算术表达式的两端用特殊符号“#”括起 (3)输出简单算术表达式及其生成的逆波兰表达式
实验参考:参考附录
实验报告:认真填写实验报告。报告内容包括:实验目的,实验要求、主要程序代码、实 验输入、实验输出
附录3:参考程序 program nibolan;
const n=20; {栈的大小}
type ysf=record {运算符记录类型} id:char; {运算符}
number:integer; {运算符的优先数} end;
var symbol:array[1..10]of ysf; {初始化运算符时数组}
fi,fo:text; {fi:输入文件;fo:输出文件} sybCount:integer; {运算符数组中的运算符总数} stack:array[1..n]of ysf; {实现栈结构的数组} last:integer; {记录栈顶}
procedure initial;{初始化运算符优先级} begin
symbol[1].id:='+'; symbol[1].number:=1; symbol[2].id:='-'; symbol[2].number:=1; symbol[3].id:='*'; symbol[3].number:=2; symbol[4].id:='/'; symbol[4].number:=2;
symbol[5].id:='^'; {^表示幂运算} symbol[5].number:=3;
symbol[6].id:='!'; {一目运算,表示取相反数} symbol[6].number:=4; sybCount:=6; end;
function priority(c:char):integer; {查找运算符的优先级} var i:integer;
5
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库宁波诺丁汉大学编译原理实验指导书在线全文阅读。
相关推荐: