求一简单词法分析程序

ccpp1982 2004-05-06 05:47:05
随便什么语法都行,只是想看看究竟是怎么做的
...全文
80 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Cathryn0000000 2004-05-06
  • 打赏
  • 举报
回复
#define BUFLEN 256 /* 读入行缓冲区大小 */
#define MAXTOKENLEN 20 /* 存放表达式字符数组大小 */

#define ENDFILE 0 /* 文件结束标志 */
#define ERROR 1 /* 出错标志 */


#define MAIN 2 /* 关键字main */
#define IF 3 /* 关键字if */
#define WHILE 4 /* 关键字while */
#define ID 5 /* 标识符 */
#define NUM 6 /* 数字 */
#define ASSIGN 7 /* = */
#define EQ 8 /* == */
#define NE 9 /* != */
#define MORE 10 /* > */
#define ME 11 /* >= */
#define LESS 12 /* < */
#define LE 13 /* <= */
#define LBRACKET 14 /* { */
#define RBRACKET 15 /* } */
#define PLUS 16 /* + */
#define MINUS 17 /* - */
#define TIMES 18 /* * */
#define OVER 19 /* / */
#define LPAREN 20 /* ( */
#define RPAREN 21 /* ) */
#define SEMI 22 /* ; */

#include <stdio.h>
#include <ctype.h>
#include <conio.h>


typedef enum /* 状态集 */
{
START, INEQULE, INNEQULE, INMORE, INLESS, INNUM, INID, DONE
} StateType;


char tokenString[MAXTOKENLEN + 1];

static char lineBuf[BUFLEN];
static int linepos = 0;
static int lineno = 0;
static int bufsize = 0;
FILE *fpin,*fpout;

static void openInputFile(){
if((fpin=fopen("in.txt","r"))==NULL){
printf("open InputFile error!!");
exit(-1);
}
}

static void openOutputFile(){
if((fpout=fopen("out.txt","w"))==NULL){
printf("open OutputFile error!!");
exit(-1);
}
}

static char getNextChar(void)
{
if ( !(linepos < bufsize) ) {
lineno++;
if ( fgets(lineBuf, BUFLEN - 1, fpin) ) {
bufsize = strlen(lineBuf);
linepos = 0;
return lineBuf[linepos++];
}
else {
return EOF;
}
}
else
return lineBuf[linepos++];
}

static void ungetNextChar(void)
{
linepos--;
}


static struct
{
char* str;
int tok;
} reservedWords[3] =
{ {"main",MAIN},
{"if", IF },
};
int getToken(void)
{
int tokenStringIndex = 0;
int currentToken;
StateType state = START;
int save;
static j=0;

while (state != DONE) {
char c = getNextChar();
save = 1;

switch (state) {
case START:
if (isdigit(c))
state = INNUM;
else if (isalpha(c))
state = INID;
else if (c == '=')
state = INEQULE;
else if (c == '>')
state =INMORE;
else if (c == '<')
state =INLESS;
else if (c == '!')
state =INNEQULE;
else if ( (c == ' ') || (c == '\t') || (c == '\n') )
save = 0;
else {
state = DONE;
switch (c) {
case EOF:
save = 0;
currentToken = ENDFILE;
break;
case '+':
currentToken = PLUS;
break;
case '-':
currentToken = MINUS;
break;
case '*':
currentToken = TIMES;
break;
case '/':
currentToken = OVER;
break;
case '(':
currentToken = LPAREN;
break;
case ')':
currentToken = RPAREN;
break;
case ';':
currentToken = SEMI;
break;
case '{':
currentToken = LBRACKET;
break;
case '}':
currentToken = RBRACKET;
break;
default:

currentToken = ERROR;
break;
}
}
break;
case INEQULE:
state = DONE;
if (c == '=')
currentToken = EQ;
else {
ungetNextChar();
save = 0;
currentToken = ASSIGN;
}
break;
case INNEQULE:
state = DONE;
if (c == '=')
currentToken = NE;
else {
ungetNextChar();
save = 0;
currentToken = ERROR;
}
break;
case INMORE:
state = DONE;
if (c == '=')
currentToken = ME;
else {
ungetNextChar();
save = 0;
currentToken = MORE;
}
break;
case INLESS:
state = DONE;
if (c == '=')
currentToken = LE;
else {
ungetNextChar();
save = 0;
currentToken = LESS;
}
break;

case INNUM:
if (!isdigit(c)) {
if(!isalpha(c)){
ungetNextChar();
save = 0;
state = DONE;
currentToken = NUM;
}
else{
save = 1;
state = DONE;
currentToken =ERROR;
}
}
break;

case DONE:
default:

state = DONE;
currentToken = ERROR;
break;
}

if ( (save) && (tokenStringIndex <= MAXTOKENLEN) )
tokenString[tokenStringIndex++] = c;
if ( state == DONE) {
tokenString[tokenStringIndex] = '\0';
if (currentToken == ID)
currentToken = reservedLookup(tokenString);
}

}

if (currentToken!=ERROR) {
if(currentToken){
printf("(%d,%s) ",currentToken,tokenString);
fprintf(fpout,"(%d,%s)",currentToken,tokenString);
if(!(++j%10)) printf("\n");
}
else {
gotoxy(1,24);
printf("Succeed! Result been put into 'out.txt'.");
printf("\nPress any key to continue... ");
getch();
}
}
else {
gotoxy(1,24);
printf("Illegal character '%s' (Row %d,Column %d)",tokenString,lineno,linepos);
printf("\nPress any key to continue... ");
window(linepos,lineno+2,linepos,lineno+2);
textbackground(4);
clrscr();
printf("%c",tokenString[tokenStringIndex-1]);
textbackground(0);
fclose(fpin);
fclose(fpout);
getch();
exit(-1);
}


return currentToken;
}



void main(){
textbackground(0);
clrscr();
openInputFile();
openOutputFile();
while(getToken());
fclose(fpin);
fclose(fpout);
}
输入为in.txt中的程序,输出到out.txt,为二元组

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧