/*
@author: zyf
@date:2021-06-05-09点55分
*/
#include"stdio.h" /*如果使用 TC 的话, 需要配置头文件路径*/
#include"string.h"
#define ACC -2
/************************/
#define sy_if 0
#define sy_then 1
#define sy_else 2
#define sy_while 3
#define sy_begin 4
#define sy_do 5
#define sy_end 6
#define a 7
#define semicolon 8
#define e 9
#define jinghao 10
#define S 11
#define L 12
#define tempsy 15
#define EA 18 /*E and*/
#define E0 19 /*E or*/
#define plus 34
#define times 36
#define becomes 38
#define op_and 39
#define op_or 40
#define op_not 41
#define rop 42
#define lparent 48
#define rparent 49
#define ident 56
#define intconst 57
/*********************************/
char ch='\0' ; /*从字符缓冲区读取当前字符*/
int count=0; /*词法分析结果缓冲区计数器*/
static char spelling[10]={""} ; /*存放识别的字*/
static char line[81]={""} ; /*一行字符缓冲区, 最多 80 个字符*/
char *pline; /*字符缓冲区指针*/
static char ntab1[100][10]; /*变量名表, 共 100 项, 每项长度 10*/
struct ntab
{
int tc; /*真值*/
int fc; /*假值*/
} ntab2[200]; /*在布尔表达式 E 中保存有关布尔变量的真、 假值*/
int label=0; /*指向 ntab2 的指针*/
struct rwords{ /*存放临时变量的表的定义*/
char sp[10];
int sy;
} ;
/*(保留字表) 匹配表的结构, 用来与输入缓冲区中的单词进行匹配*/
/*匹配表初始化, 大小为 10*/
struct rwords reswords[10]={{"if", sy_if} ,
{"do", sy_do} ,
{"else", sy_else} ,
{"while", sy_while},
{"then", sy_then} ,
{"begin", sy_begin},
{"end", sy_end} ,
{"and", op_and} ,
{"or", op_or} ,
{"not", op_not} } ;
struct aa{
int sy1; /*存放名字*/
int pos; /*存放名字所对应的地址*/
} buf[1000], /*词法分析结果缓冲区*/
n, /*读取二元式的当前字符*/
n1, /*当前表达式中的字符*/
E, /*非终结符*/
sstack[100], /*算术或布尔表达式加工处理使用的符号栈*/
ibuf[100], /*算术或布尔表达式使用的缓冲区*/
stack[1000]; /*语法分析加工处理使用的符号栈*/
struct aa oth; /*四元式中空白位置*/
struct fourexp{
char op[10];
struct aa arg1;
struct aa arg2;
int result;
} fexp[200]; /*四元式的结构定义*/
int ssp=0; /*指向 sstack 栈指针*/
struct aa *pbuf=buf; /*指向词法分析缓冲区的指针*/
int nlength=0; /*词法分析中记录单词的长度*/
int lnum=0; /*源程序行数记数, 源程序长度*/
int tt1=0; /*变量名表指针*/
FILE *cfile; /*源程序文件, ~为结束符*/
/*FILE *mfile; */
int newt=0; /*临时变量计数器*/
int nxq=100; /*nxq 指向下一个形成的四元式的地址*/
/*每次执行 gen() 时, 地址自动增 1*/
int lr; /*扫描 LR 分析表 1 过程中保存的当前状态值*/
int lr1; /*扫描 LR 分析表 2 或表 3 所保存的当前状态值*/
int sp=0; /*查找 LR 分析表时状态栈的栈顶指针*/
int stack1[100]; /*状态栈 1 的定义*/
int sp1=0; /*状态栈 1 的栈顶指针*/
int num=0; /*算术或布尔表达式缓冲区指针*/
struct ll{
int nxq1; /*记录下一条四元式的地址*/
int tc1; /*真值链*/
int fc1; /*假值链*/
} labelmark[10]; /*记录语句嵌套层次的数组, */
/*即记录嵌套中每层的布尔表达式 E 的首地址*/
int labeltemp[10]; /*记录语句嵌套层次的数组, */
/*即记录每层 else 之前的四元式地址*/
int pointmark=-1, /*labelmark 数组指针*/
pointtemp=-1; /*labeltemp 数组指针*/
int sign=0; /*sign=1, 为赋值语句; sign=2, 为布尔表达式。 */
/**************程序语句 LR 分析表*********************/
static int action[19][13]=
/*0*/ {{2, -1, -1, 3, 4, -1, -1, 5, -1, -1, -1, 1, -1},
/*1*/ {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, ACC, -1, -1} ,
/*2*/ {-1, -1, -1, -1, -1, -1, -1, -1, -1, 6, -1, -1, -1} ,
/*3*/ {-1, -1, -1, -1, -1, -1, -1, -1, -1, 7, -1, -1, -1} ,
/*4*/ {2, -1, -1, 3, 4, -1, -1, 5, -1, -1, -1, 9, 8} ,
/*5*/ {-1, -1, 104, -1, -1, -1, 104, -1, 104, -1, 104, -1, -1, } ,
/*6*/ {-1, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
/*7*/ {-1, -1, -1, -1, -1, 11, -1, -1, -1, -1, -1, -1, -1} ,
/*8*/ {-1, -1, -1, -1, -1, -1, 12, -1, -1, -1, -1, -1, -1} ,
/*9*/ {-1, -1, -1, -1, -1, -1, 105, -1, 13, -1, -1, -1, -1} ,
/*10*/ {2, -1, -1, 3, 4, -1, -1, 5, -1, -1, -1, 14, -1} ,
/*11*/ {2, -1, -1, 3, 4, -1, -1, 5, -1, -1, -1, 15, -1} ,
/*12*/ {-1, -1, 103, -1, -1, -1, 103, -1, 103, -1, 103, -1, -1} ,
/*13*/ {2, -1, -1, 3, 4, -1, -1, 5, -1, -1, -1, 9, 16} ,
/*14*/ {-1, -1, 17, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
/*15*/ {-1, -1, 102, -1, -1, -1, 102, -1, 102, -1, 102, -1, -1} ,
/*16*/ {-1, -1, -1, -1, -1, -1, 106, -1, -1, -1, -1, -1, -1} ,
/*17*/ {2, -1, -1, 3, 4, -1, -1, 5, -1, -1, -1, 18, -1} ,
/*18*/ {-1, -1, 101, -1, -1, -1, 101, -1, 101, -1, 101, -1, -1} } ;
/**************算术表示式的 LR 分析表************************/
static int action1[10][7]=
/*0*/ {{3, -1, -1, 2, -1, -1, 1} ,
/*1*/ {-1, 4, 5, -1, -1, ACC, -1} ,
/*2*/ {3, -1, -1, 2, -1, -1, 6} ,
/*3*/ {-1, 104, 104, -1, 104, 104, -1} ,
/*4*/ {3, -1, -1, 2, -1, -1, 7} ,
/*5*/ {3, -1, -1, 2, -1, -1, 8} ,
/*6*/ {-1, 4, 5, -1, 9, -1, -1} ,
/*7*/ {-1, 101, 5, -1, 101, 101, -1} ,
/*8*/ {-1, 102, 102, -1, 102, 102, -1} ,
/*9*/ {-1, 103, 103, -1, 103, 103, -1} } ;
/*************布尔表示式的 LR 分析表********************/
static int action2[16][11]=
/*0*/ {{1, -1, 4, -1, 5, -1, -1, -1, 13, 7, 8} ,
/*1*/ {-1, 2, -1, 101, -1, 101, 101, 101, -1, -1, -1},
/*2*/ {3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
/*3*/ {-1, -1, -1, 102, -1, 102, 102, 102, -1, -1, -1} ,
/*4*/ {1, -1, 4, -1, 5, -1, -1, -1, 11, 7, 8} ,
/*5*/ {1, -1, 4, -1, 5, -1, -1, -1, 6, 7, 8} ,
/*6*/ {-1, -1, -1, 104, -1, 9, 10, 104, -1, -1, -1} ,
/*7*/ {1, -1, 4, -1, 5, -1, -1, -1, 14, 7, 8} ,
/*8*/ {1, -1, 4, -1, 5, -1, -1, -1, 15, 7, 8} ,
/*9*/ {105, -1, 105, -1, 105, -1, -1, -1, -1, -1, -1},
/*10*/ {107, -1, 107, -1, 107, -1, -1, -1, -1, -1, -1},
/*11*/ {-1, -1, -1, 12, -1, 9, 10, -1, -1, -1, -1} ,
/*12*/ {-1, -1, -1, 103, -1, 103, 103, 103, -1, -1, -1} ,
/*13*/ {-1, -1, -1, -1, -1, 9, 10, ACC, -1, -1, -1} ,
/*14*/ {-1, -1, -1, 106, -1, 9, 10, 106, -1, -1, -1} ,
/*15*/ {-1, -1, -1, 108, -1, 9, 10, 108, -1, -1, -1} } ;
/*************从文件读一行到缓冲区*************/
void readline()
{
char ch1;
pline=line;
ch1=fgetc(cfile) ;
while(ch1!='\n' )
{
*pline=ch1;
pline++;
ch1=fgetc(cfile) ;
}
*pline='\0' ;
pline=line;
}
/*************从缓冲区读取一个字符*********************/
void readch()
{
if(ch=='\0' )
{
readline();
lnum++;
}
ch=*pline;
pline++;
}
/***************标识符和关键字的识别******************/
find(char spel[])
{
int ss1=0;
int ii=0;
while((ss1==0) &&(ii<nlength) )
{
if(!strcmp(spel, ntab1[ii]) )
ss1=1;
ii++;
}
if(ss1==1) return ii-1;
else return -1;
}
void identifier()
{
int iii=0, j, k;
int ss=0;
k=0;
do
{
spelling[k]=ch;
k++;
readch() ;
} while(((ch>='a' ) &&(ch<='z' ) ) || ((ch>='0' ) &&(ch<='9' ) ) ) ;
pline--;
spelling[k]='\0' ;
while((ss==0)&&(iii<10) )
{
if(!strcmp(spelling, reswords[iii].sp) ) ss=1;
iii++;
}
/*关键字匹配*/
if(ss==1)
{
buf[count].sy1=reswords[iii-1].sy;
}
else
{
buf[count].sy1=ident;
j=find(spelling) ;
if(j==-1)
{
buf[count].pos=tt1;
strcpy(ntab1[tt1], spelling) ;
tt1++;
nlength++;
}
else buf[count].pos=j;
}
count++;
for(k=0; k<10; k++)
spelling[k]=' ' ;
}
/***********************数字的识别**********************/
void number()
{
int ivalue=0;
int digit;
do
{
digit=ch-'0' ;
ivalue=ivalue*10+digit;
readch() ;
} while((ch>='0')&&(ch<='9')) ;
buf[count].sy1=intconst;
buf[count].pos=ivalue;
count++;
pline--;
}
/******************扫描主函数**************************/
void scan()
{
while(ch!='~' )
{
switch(ch)
{
case ' ' :
break;
case 'a' :
case 'b' :
case 'c' :
case 'd' :
case 'e' :
case 'f' :
case 'g' :
case 'h' :
case 'i' :
case 'j' :
case 'k' :
case 'l' :
case 'm' :
case 'n' :
case 'o' :
case 'p' :
case 'q' :
case 'r' :
case 's' :
case 't' :
case 'u' :
case 'v' :
case 'w' :
case 'x' :
case 'y' :
case 'z' :
identifier() ;
break;
case '0' :
case '1' :
case '2' :
case '3' :
case '4' :
cas
实现一个小型编译程序(能编译pascal语言,生成四元式,编译原理)
5星 · 超过95%的资源 需积分: 5 8 浏览量
2022-06-16
07:58:15
上传
评论 9
收藏 6KB RAR 举报
Best-Wishes
- 粉丝: 149
- 资源: 13
最新资源
- IMG_20240425_120538.jpg
- My Complete Genome_6k Base-Pairs of Phenotype SNPs_Complete Raw Data.zip
- qt 的mqtt测试demo
- 移动应用开发教程-zip.zip
- mosquitto-2.018-install-windows-x64
- FTPServer FTP 服务器,绿色免安装,单文件
- 梦畅语音点名软件,上课点名
- 利用ADNI数据集和标签,在tensorflow框架上使用tensorlayer接口,通过架构u-net实现海马体的分割
- Kutools for Word v9.0 office word 插件
- 修复Windows 10 LTSC 2021资源占用率高
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论5