?? wordpars.cpp
字號:
// WordPars.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <io.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <iostream.h>
#include <fstream.h>
#include <malloc.h>
FILE *fp;
char cbuffer;
char *key[21]={"AND","BEGIN","CONST","DIV","DO","ELSE","END",
"FUNCTION","IF","INTEGER","NOT","OR","PROCEDURE",
"PROGRAM","READ","REAL","THEN","TYPE","VAR","WHILE",
"WRITE"};
char *border[8]={",",";",":",".","(",")","[","]"};
char *arithmetic[4]={"+","-","*","/"};
char *relation[10]={"=","<",">","<>","<=",">=",":=","{","}","#"};
char *consts[20];
char *label[20];
int constnum=0,labelnum=0;
int search(char searchchar[],int wordtype)
{
int i=0;
switch (wordtype)
{
case 1:for (i=0;i<=20;i++)
{
if (strcmp(key[i],searchchar)==0)
return(i+1);
};
case 2:
{
for (i=0;i<=7;i++)
{
if (strcmp(border[i],searchchar)==0)
return(i+24);
}
return(0);
};
case 3:
{
for (i=0;i<=3;i++)
{
if (strcmp(arithmetic[i],searchchar)==0)
{
return(i+35);
}
}
return(0);
};
case 4:
{
for (i=0;i<=9;i++)
{
if (strcmp(relation[i],searchchar)==0)
{
return(i+39);
}
}
return(0);
};
case 5:
{
for (i=0;i<=constnum;i++)
{
if (strcmp(consts[i],searchchar)==0)
{
return(23);
}
}
consts[i-1]=(char *)malloc(sizeof(searchchar));
strcpy(consts[i-1],searchchar);
constnum++;
return(23);
};
case 6:
{
//printf("%s",searchchar);
for (i=0;i<=labelnum;i++)
{
// printf("%s",searchchar);
if (strcmp(label[i],searchchar)==0)
{
return(22);
}
}
label[i-1]=(char *)malloc(sizeof(searchchar));
strcpy(label[i-1],searchchar);
labelnum++;
return(22);
};
};
return 0;
};
char alphaprocess(char buffer)
{
int atype;
int i=-1;
char alphatp[20];
while ((isalpha(buffer))||(isdigit(buffer)))
{
// printf("%c",buffer);
alphatp[++i]=buffer;
buffer=fgetc(fp);
};
alphatp[i+1]='\0';
//printf("%s",alphatp);
if (atype=search(alphatp,1))
{
printf(" %-10s, %-5d\n",alphatp,atype-1);
buffer=fgetc(fp);
// printf("%c",buffer);
}
else
{
atype=search(alphatp,6);
printf(" %-10s, %-5d\n",alphatp,atype-1);
//buffer=fgetc(fp);
}
// printf("%c",buffer);
return(buffer);
}
char digitprocess(char buffer)
{
int i=-1;
char digittp[20];
int dtype;
while ((isdigit(buffer)))
{
digittp[++i]=buffer;
buffer=fgetc(fp);
}
digittp[i+1]='\0';
dtype=search(digittp,5);
printf(" %-10s, %-5d\n",digittp,dtype-1);
return(buffer);
}
char otherprocess(char buffer)
{
int i=-1;
char othertp[20];
int otype,otypetp;
othertp[0]=buffer;
othertp[1]='\0';
if (otype=search(othertp,3))
{
printf(" %-10s, %-5d\n",othertp,otype-1);
buffer=fgetc(fp);
goto out;
};
if (otype=search(othertp,4))
{
buffer=fgetc(fp);
othertp[1]=buffer;
othertp[2]='\0';
if (otypetp=search(othertp,4))
{
printf(" %-10s, %-5d\n",othertp,otypetp-1);
goto out;
}
else
othertp[1]='\0';
printf(" %-10s, %-5d\n",othertp,otypetp-1);
goto out;
};
if(buffer==':')
{
buffer=fgetc(fp);
if (buffer=='=')
printf(" := , 44 \n");
buffer=fgetc(fp);
goto out;
}
else
{
if (otype=search(othertp,2))
{
printf(" %-10s, %-5d\n",othertp,otype-1);
buffer=fgetc(fp);
goto out;
}
};
if ((buffer!='\n')&&(buffer!=' '))
printf("%c error,not a word\n",buffer);
buffer=fgetc(fp);
out: return(buffer);
};
int main(int argc, char* argv[])
{
int i;
printf(" The Result :\n");
for (i=0;i<=20;i++)
{
label[i]="";//不能用NULL
consts[i]="";//不能用NULL
};
if ((fp=fopen("pas.PAS","r"))==NULL)
printf("error");
else
{
cbuffer = fgetc(fp);
while (cbuffer!=EOF)
{
if (isalpha(cbuffer))
{
cbuffer=alphaprocess(cbuffer);
}
else if (isdigit(cbuffer))
cbuffer=digitprocess(cbuffer);
else cbuffer=otherprocess(cbuffer);
// cbuffer=fgetc(fp);
//printf(" %c", cbuffer);
};
printf(" OVER !\n");
};
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -