📄 cifa.cpp
字号:
// cifa.cpp : Defines the entry point for the console application.
//词法分析程序
#include <conio.h>
#include "stdafx.h"
#include "stdio.h"
#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 EO 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]={""}; //一行字符缓冲区
char *pline ; //字符缓冲区指针
static char ntab1[100][10];
struct ntab
{
int tc;
int fc;
}ntab2[200];
int label=0; //存放临时变量的表的定义
struct rwords{
char sp[10];
int sy;
};
//存放文件的结构
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];
int nlength=10;
int tt1=0;
int lnum=0;
FILE *cfile,*mfile;
void readline() //读文件读一行至缓冲区
{
char ch1;
pline=line;
ch1=getc(cfile);
while(ch1 !='\n')
{
*pline=ch1;
pline++;
ch1=getc(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()
{
int i;
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':
case '5':
case '6':
case '7':
case '8':
case '9':
number();
break;
case '<':
readch();
if(ch=='=')
{
buf[count].pos=0;
}
else
{
if(ch=='>')buf[count].pos=4;
else
{
buf[count].pos=1;
pline--;
}
}
buf[count].sy1=rop;
count++;
break;
case '>':
readch();
if(ch=='=')
{
buf[count].pos=2;
}
else
{
buf[count].pos=3;
pline--;
}
buf[count].sy1=rop;
count++;
break;
case '(':
buf[count].sy1=lparent;
count++;
break;
case ')':
buf[count].sy1=rparent;
count++;
break;
case '#':
buf[count].sy1=jinghao;
count++;
break;
case '+':
buf[count].sy1=plus;
count++;
break;
case '*':
buf[count].sy1=times;
count++;
break;
case ':':
readch();
if(ch=' ')
buf[count].sy1=becomes;
count++;
break;
case '=':
buf[count].sy1=rop;
buf[count].pos=5;
count++;
break;
case ';':
buf[count].sy1=semicolon;
count++;
break;
}
readch();
}
buf[count].sy1=-1;
}
void disp1()
{
int temp1=0;
printf("\n**********词法分析结果***********\n");
printf("\n**类别编码*******内码值**\n");
for(temp1=0;temp1<count;temp1++)
{
printf("%d\t%d\n",buf[temp1].sy1,buf[count].pos);
}
getch();
}
void disp3()
{
int tttt;
printf("二元式的个数和行数为:\n");
printf("%d%d",lnum,count);
getch();
printf("\n*************变量表************\n");
for(tttt=0;tttt<tt1;tttt++)
printf("%d\t%s\n",tttt,ntab1[tttt]);
getch();
}
void main()
{
cfile=fopen("pas.c","r");
readch();
scan();
disp1();
disp3();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -