• 大小: 11KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-06-12
  • 语言: C/C++
  • 标签: pl0扩充  c语言  

资源简介

c语言实现pl0编译器的扩充: if 条件 then 语句 else 语句 for 变量=初始值 to 终止值 begin 语句 end dowhile 语句 until 条件

资源截图

代码片段和文件信息

/*
 * PL/0 complier program for win32 platform (implemented in C)
 *
 * The program has been test on Visual C++ 6.0 Visual C++.NET and
 * Visual C++.NET 2003 on Win98 WinNT Win2000 WinXP and Win2003
 *
 * 使用方法:
 * 运行后输入PL/0源程序文件?
 * 回答是否输出虚拟机代码
 * 回答是否输出名字表
 * fa.tmp输出虚拟机代码
 * fa1.tmp输出源文件及其各行对应的首地址
 * fa2.tmp输出结?
 * fas.tmp输出名字表
 */

#include 

#include “pl0.h“
#include “string.h“

/* 解释执行时使用的栈 */
#define stacksize 500


int main()
{
bool nxtlev[symnum];

printf(“Input pl/0 file?   “);
scanf(“%s“ fname);     /* 输入文件名 */

fin = fopen(fname “r“);//char fname[al];

if (fin)
{
printf(“List object code?(Y/N)“);   /* 是否输出虚拟机代码 */
scanf(“%s“ fname);
listswitch = (fname[0]==‘y‘ || fname[0]==‘Y‘);/* 显示虚拟机代码与否 */

printf(“List symbol table?(Y/N)“);  /* 是否输出名字表 */
scanf(“%s“ fname);
tableswitch = (fname[0]==‘y‘ || fname[0]==‘Y‘);

fa1 = fopen(“fa1.tmp“ “w“);
fprintf(fa1“Input pl/0 file?   “);//??
fprintf(fa1“%s\n“fname);//??

init();     /* 初始化 */

err = 0;
cc = cx = ll = 0;
ch = ‘ ‘;

if(-1 != getsym())
{
fa = fopen(“fa.tmp“ “w“);
fas = fopen(“fas.tmp“ “w“);
addset(nxtlev declbegsys statbegsys symnum);
nxtlev[period] = true;

if(-1 == block(0 0 nxtlev))   /* 调用编译程序 */
{
fclose(fa);
fclose(fa1);
fclose(fas);
fclose(fin);
printf(“\n“);
return 0;
}
fclose(fa);
fclose(fa1);
fclose(fas);

if (sym != period)
{
error(9);
}

if (err == 0)
{
fa2 = fopen(“fa2.tmp“ “w“);
interpret();    /* 调用解释执行程序 */
fclose(fa2);
}
else
{
printf(“Errors in pl/0 program“);
}
}

fclose(fin);
}
else
{
printf(“Can‘t open file!\n“);
}

printf(“\n“);
return 0;
}

/*
* 初始化
*/
void init()
{
int i;

/* 设置单字符符号 */
for (i=0; i<=255; i++)
{
ssym[i] = nul;
}
ssym[‘+‘] = plus;
ssym[‘-‘] = minus;
ssym[‘*‘] = times;
ssym[‘/‘] = slash;
ssym[‘(‘] = lparen;
ssym[‘)‘] = rparen;
ssym[‘=‘] = eql;
ssym[‘‘] = comma;
ssym[‘.‘] = period;
ssym[‘#‘] = neq;
ssym[‘;‘] = semicolon;

/* 设置保留字名字按照字母顺序,便于折半查找 */
strcpy(&(word[0][0]) “begin“);
strcpy(&(word[1][0]) “call“);
strcpy(&(word[2][0]) “const“);
strcpy(&(word[3][0]) “do“);
strcpy(&(word[4][0]) “dowhile“);
    strcpy(&(word[5][0]) “else“);//************添加else*****************************************************
strcpy(&(word[6][0]) “end“);
    strcpy(&(word[7][0]) “for“);//*****************for*****
strcpy(&(word[8][0]) “if“);
strcpy(&(word[9][0]) “odd“);
strcpy(&(word[10][0]) “procedure“);
strcpy(&(word[11][0]) “read“);
strcpy(&(word[12][0]) “then“);
strcpy(&(word[13][0]) “to“);//*****************to*****
strcpy(&(word[14][0]) “until“);
strcpy(&(word[15][0]) “var“);
strcpy(&(word[16][0]) “while“);
strcpy(&(word[17][0]) “write“);

/* 设置保留字符号 即begin对应beginsym...*/
wsym[0] = beginsym;

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      31525  2009-12-08 01:19  pl0.c

     文件       4848  2009-12-07 20:50  pl0.h

     文件         61  2009-12-07 20:35  for.PL0

     文件         89  2009-12-08 00:50  dowhile.PL0

     文件        110  2009-12-08 01:18  else.pl0

----------- ---------  ---------- -----  ----

                36633                    5


评论

共有 条评论