📄 main.cpp
字号:
#include <stdio.h>
#include <string.h>
#define bool int
#define false 0
#define true 1
bool not_end;
FILE *in,*out,*err;
char file_buffer[102],temp[17];
char *key[22]={
"PROGRAM","PROCEDURE","END","BEGIN","CONST",
"REAL","CHAR","VAR","WRITE","WRITELN","READ",
"READLN","FOR","IF","THEN","ELSE","TO","DO",
"WHILE","TYPE","INTEGER","FUNCTION"};
int pch,row,col,end,prev;
void next()
{
if (not_end&&feof(in))
{
end=pch-1;
not_end=false;
pch=(pch+1)%101;
}
else
if (!not_end)
{
pch=(pch+1)%101;
col++;
}
else
{
file_buffer[pch]=fgetc(in);
pch=(pch+1)%101;
col++;
}
}
void error(int row,int col,int id)
{
fprintf(err,"%d,%d,%d\n",row,col,id);
return;
}
void token(int id)
{
fprintf(out,"%d,%s\n",id,"_");
prev=id;
next();
}
void lookup_key()
{
int i;
for (i=0;i<=21;i++)
if (!strcmp(temp,key[i]))
break;
if (i<=21)
{
fprintf(out,"%d,%s\n",i+1,"_");
prev=i+1;
}
else
{
fprintf(out,"%d,%s\n",23,temp);
prev=23;
}
}
void number(int tag)
{
int i=0,s3;
bool errflag=false;
double s,s2,base;
memset(temp,NULL,17);
while ((pch!=end)&&((file_buffer[pch]>='0')&&(file_buffer[pch]<='9')))
{
if (i<16)
{
temp[i]=file_buffer[pch];
i++;
}
next();
}
if ((pch!=end)&&(file_buffer[pch]=='.'))
{
i=0;
s=temp[i]-'0';
i=1;
while (temp[i])
{
s=s*10+temp[i]-'0';
i++;
}
i=0;
memset(temp,NULL,17);
next();
while ((pch!=end)&&((file_buffer[pch]>='0')&&(file_buffer[pch]<='9')))
{
if (i<16)
{
temp[i]=file_buffer[pch];
i++;
}
else
error(row,col,103);
next();
}
s2=(temp[0]-'0')*0.1;
base=0.01;
i=1;
while (temp[i])
{
s2=s2+base*(temp[i]-'0');
base=base/10.0;
i++;
}
fprintf(out,"%d,%f\n",25,tag*(s+s2));
prev=25;
}
else
{
s3=temp[0]-'0';
i=1;
if (tag>0)
{
if (strlen(temp)==5)
{
if (strcmp(temp,"32767")>0)
{
error(row,col,102);
return;
}
}
else
if (strlen(temp)>5)
{
error(row,col,102);
return;
}
}
else
if (strlen(temp)>5)
{
error(row,col,102);
return;
}
else
if (strlen(temp)==5)
{
if (strcmp(temp,"32768")>0)
{
error(row,col,102);
return;
}
}
while (temp[i])
{
s3=s3*10+temp[i]-'0';
i++;
if (i>5)
{
error(row,col,102);
return;
}
}
fprintf(out,"%d,%d\n",24,tag*s3);
prev=24;
}
}
main(int argc, char *argv[])
{
int i;
bool firstflag=true;
prev=0;
not_end=true;
end=1000;
pch=0;
row=0;
col=0;
in=fopen(argv[1],"rb");
for (i=0;i<=100;i++)
if (feof(in))
{
end=i-1;
not_end=false;
break;
}
else
file_buffer[i]=fgetc(in);
out=fopen("token","w");
err=fopen("error","w");
while (pch!=end)
{
bool charflag=false;
switch (file_buffer[pch])
{
case ' ':
next();
break;
case 13:
next();
next();
row++;
col=0;
break;
case '{':
while ((pch!=end)&&(file_buffer[pch]!='}'))
{
if (file_buffer[pch]=='\n')
{
next();
row++;
col=0;
}
next();
}
if ((pch!=end)&&(file_buffer[pch]=='}'))
next();
break;
case '/':
token(37);
break;
case '*':
token(36);
break;
case '=':
token(33);
break;
case '>':
token(30);
break;
case '[':
token(50);
break;
case ']':
token(51);
break;
case ';':
token(52);
break;
case ',':
token(53);
break;
case '(':
token(54);
break;
case ')':
token(55);
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':
i=0;
memset(temp,NULL,17);
while ((pch!=end)&&(((file_buffer[pch]>='A')&&(file_buffer[pch]<='Z'))||((file_buffer[pch]>='0')&&(file_buffer[pch]<='9'))))
{
if (i<16)
temp[i]=file_buffer[pch];
i++;
next();
}
lookup_key();
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
number(1);
break;
case '<':
if ((((pch+1)%101)!=end)&&(file_buffer[(pch+1)%101]=='>'))
{
token(32);
next();
}
else
token(31);
break;
case '-':
if ((((pch+1)%101)!=end)&&((file_buffer[(pch+1)%101]>='0')&&(file_buffer[(pch+1)%101]<='9'))&&((prev!=23)||(prev!=24)||(prev!=25)))
{
next();
number(-1);
}
else
token(35);
break;
case '+':
token(34);
break;
case ':':
if ((((pch+1)%101)!=end)&&(file_buffer[(pch+1)%101]=='='))
{
token(38);
next();
}
else
token(56);
break;
case 39:
next();
memset(temp,NULL,17);
i=0;
while ((pch!=end)&&(file_buffer[pch]!=39))
{
if (file_buffer[pch]=='\n')
{
next();
row++;
col=0;
}
if (i<16)
{
temp[i]=file_buffer[pch];
i++;
}
else
{
if (!charflag)
{
error(row,col,105);
charflag=true;
}
}
next();
}
if ((pch!=end)&&(file_buffer[pch]==39))
{
next();
if (i==1)
{
fprintf(out,"%d,%c%c%c\n",26,39,temp[0],39);
prev=26;
}
else
{
fprintf(out,"%d,%c%s%c\n",27,39,temp,39);
prev=27;
}
}
else
if (pch==end)
if (i==1)
error(row,col,104);
else
error(row,col,105);
break;
default:
error(row,col,101);
next();
break;
}
if (firstflag&&prev)
if (prev!=1)
{
error(row,col,100);
firstflag=false;
}
else
firstflag=false;
}
fclose(in);
fclose(out);
fclose(err);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -