?? c.txt
字號:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <ctype.h>
#include <conio.h>
#define NULL 0
FILE *fp;
char ch;
char *keyword[8]={"do","begin","else","end","if","then","var","while"};
char *operatornum[4]={"+","-","*","/"};
char *comparison[6]={"<","<=","=",">",">=","<>"};
char *interpunction[6]={",",";",":=",".","(",")"};
//////////////////////////////////////////////////////////////////////////////////////////
bool search(char searchstr[],int wordtype)
{
int i;
switch (wordtype)
{
case 1:for(i=0;i<=7;i++)
{
if(strcmp(keyword[i],searchstr)==0)
return(true);
}
case 2:{
for(i=0;i<=3;i++)
{
if(strcmp(operatornum[i],searchstr)==0)
return(true);
}
break;
}
case 3: for(i=0;i<=5;i++)
{
if(strcmp(comparison[i],searchstr)==0)
return(true);
}
case 4: for(i=0;i<=5;i++)
{
if(strcmp(interpunction[i],searchstr)==0)
return(true);
}
}
return(false);
}
///////////////////////////////////////////////////////////////////////////////////////////
char letterprocess (char ch)//字母處理函數
{
int i=-1;
char letter[20];
while (isalnum(ch)!=0)
{
letter[++i]=ch;
ch=fgetc(fp);
};
letter[i+1]='\0';
if (search(letter,1))
{
printf("<%s,->\n",letter);
//strcat(letter,"\n");
//fputs('<' letter '>\n',outp);
}
else
{
printf("<indentifier,%s>\n",letter);
//strcat(letter,"\n");
//fputs(letter,outp);
}
return(ch);
}
///////////////////////////////////////////////////////////////////////////////////////////
char numberprocess(char ch)//數字處理程序
{
int i=-1;
char num[20];
while (isdigit(ch)!=0)
{
num[++i]=ch;
ch=fgetc(fp);
}
if(isalpha(ch)!=0)
{
while(isspace(ch)==0)
{
num[++i]=ch;
ch=fgetc(fp);
}
num[i+1]='\0';
printf("錯誤!非法標識符:%s\n",num);
goto u;
}
num[i+1]='\0';
printf("<num,%s>\n",num);
//strcat(num,"\n");
//fputs(num,outp);
u: return(ch);
}
//////////////////////////////////////////////////////////////////////////////////////////////
char otherprocess(char ch)
{
int i=-1;
char other[20];
if (isspace(ch)!=0)
{
ch=fgetc(fp);
goto u;
}
while ((isspace(ch)==0)&&(isalnum(ch)==0))
{
other[++i]=ch;
ch=fgetc(fp);
}
other[i+1]='\0';
if (search(other,2))
printf("<relop,%s>\n",other);
else
if (search(other,3))
printf("<%s,->\n",other);
else
if (search(other,4))
printf("<%s,->\n",other);
else
printf("錯誤!非法字符:%s\n",other);
u: return (ch);
}
/////////////////////////////////////////////////////////////////////////////////////////////
void main ()
{
char str,c;
printf("**********************************詞法分析器************************************\n");
//outp=fopen("二元式表.txt","w");
if ((fp=fopen("目標程序.txt","r"))==NULL)
printf("源程序無法打開!\n");
else
{
str =fgetc(fp);
while (str!=EOF)
{
if (isalpha(str)!=0)
str=letterprocess(str);
else
{
if (isdigit(str)!=0)
str=numberprocess(str);
else
str=otherprocess(str);
}
};
printf("詞法分析結束,謝謝使用!\n");
printf("點任意鍵退出!\n");
}
c=getch();
}
目標程序:
ht1984=1000
程序運行結果:
**********************************詞法分析器************************************
<indentifier,ht1984>
<=,->
<num,1000>
詞法分析結束,謝謝使用!
點任意鍵退出!
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -