【文件属性】:
文件名称:编译原理词法分析程序,按照老师要求编写的
文件大小:160KB
文件格式:RAR
更新时间:2012-05-05 15:10:47
编译原理词法分析程序,词法分析
//
/****************************编译原理实验(词法分析)***********************************/
#include
#include
#include
#include
char ch;
char prog[80], token[8];
int i, m, n, syn, sum;
int Scan (char prog[]);
void main()
{
i = 0;
printf ("Please input the string end with # : ");
do
{
ch = getchar();
prog[i++] = ch;
}while(ch != '#');
i = 0;
do
{
syn = Scan(prog);
if (syn == 11) //syn=11代表是关键字
{
printf("(%2d,%4d)", syn, sum);
}
else if (syn == -1)
{
printf("(%d,error)", syn);
}
else
{
printf("(%2d,%4s)", syn, token);
}
}while (syn != 0);
//上面的if 语句可以用下面的switch语句代替
/* switch (syn)
{
case 11: printf("(%2d,%4d)", syn, sum); break;
case -1: printf("(%d,error)", syn); break;
default: printf("(%2d,%4s)", syn, token); break;
}
*/
}
int Scan (char prog[])
{
char *rwtab[6] = {"begin", "if", "then", "while", "do", "end"};
sum = 0;
m = 0;
ch = prog[i++]; //prog[i++]为缓冲区
for (n = 0; n < 8; n++)
{
token[n] = NULL;
}
while (ch == ' ') //如果ch为空格,则读下一个字符
{
ch = prog[i++];
}
if ((ch >= 'a') && (ch <= 'z' )) //ch
【文件预览】:
yingwh
----ying.dsw(516B)
----ying.dsp(3KB)
----ying.ncb(41KB)
----ying.opt(53KB)
----ying.cpp(4KB)
----ying.plg(1KB)
----Debug()
--------vc60.pdb(60KB)
--------ying.pdb(473KB)
--------vc60.idb(49KB)
--------ying.exe(156KB)
--------ying.obj(11KB)
--------ying.ilk(162KB)