#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define _KEY_WORD_END "warting for your expanding"
char m_getch();
void getbc();
void concat();
int letter();
int digit();
int reserve();
char * dtb();
void retract();
typedef struct {
int typenum;//种类号
char * word;//关键词
}WORD;
char input[255];//输入缓冲区
char token[255]="";//字符缓冲区
int p_input;//输入缓冲区的指针
int p_token;//单词缓冲区指针
char ch;//读取当前输入的字符串
char * rwtab[] = {"begin","if","then","while","do","end",_KEY_WORD_END};
WORD * scaner();
int main(){
int over = 1;
WORD * oneword = new WORD;
printf("Enter Your words ( end with '#'):");
scanf("%[^#]s",input);
p_input = 0;
printf("Your words:\n%s\n",input);
while(over<100&&over!=-1){
oneword = scaner();
if(oneword->typenum<100){
printf("%d,%s",oneword->typenum,oneword->word);
over = oneword->typenum;
}
}
printf("\npress # to exit:");
scanf("%[^#]s",input);
/*需要用到的自编函数的实现*/
/*从输入缓冲区中读取一个字符串到ch中*/
return 0;
}
char m_getch(){
ch = input[p_input];
p_input = p_input++;
return ch;
}
/*去掉空白符号*/
void getbc(){
while (ch == '' || ch == 10){
ch = input[p_input];
p_input = p_input++;
}
}
/*拼接单词*/
void concat(){
token[p_token] = ch;
p_token = p_token + 1;
token[p_token] = '\0';
}
/*判断是否是字母*/
int letter(){
if (ch >= 'a'&&ch <= 'z' || ch >= 'A'&&ch <= 'Z'){
return 1;
}
else{
return 0;
}
}
/*判断是否为数字*/
int digit(){
if (ch >= '0'&&ch <= '9'){
return 1;
}
else{
\
return 0;
}
}
/*检索关键字表格*/
int reserve(){
int i = 0;
while (strcmp(rwtab[i], _KEY_WORD_END)){
if (!strcmp(rwtab[i], token)){
return i + 1;
}
i++;
}
return 10;
}
/*退回一个字符*/
void retract(){
p_input = p_input - 1;
}
/*将数字转换成二进制的数字*/
char * dtb(){
return NULL;
}
/*这里还没有实现*/
/*语法扫描程序*/
WORD * scaner(){
WORD * myword = new WORD;
myword->typenum = 10;
myword->word = "";
p_token = 0;
m_getch();
getbc();
if (letter()){
while (letter() || digit()){
concat();
m_getch();
}
retract();
myword->typenum = reserve();
myword->word = token;
return (myword);
}
else if (digit()){
while (digit()){
concat();
m_getch();
}
retract();
myword->typenum = 20;
myword->word = token;
return (myword);
}
else switch (ch){
case '=': m_getch();
if (ch == '='){
myword->typenum = 39;
myword->word = '==';
return myword;
}
retract();
myword->typenum = 21;
myword->word = "=";
return myword;
break;
case '+':
myword->typenum = 22;
myword->word = "+";
return(myword);
break;
case '-':
myword->typenum = 23;
myword->word = "-";
return(myword);
break;
case '*':
myword->typenum = 24;
myword->word = "*";
return(myword);
break;
case '/':
myword->typenum = 25;
myword->word = "/";
return(myword);
break;
case '(':
myword->typenum = 26;
myword->word = "(";
return(myword);
break;
case ')':
myword->typenum = 27;
myword->word = ")";
return(myword);
break;
case '[':
myword->typenum = 28;
myword->word = "[";
return(myword);
break;
case ']':
myword->typenum = 29;
myword->word = "]";
return(myword);
break;
case '{':
myword->typenum = 30;
myword->word = "{";
return(myword);
break;
case '}':
myword->typenum = 31;
myword->word = "}";
return(myword);
break;
case ',':
myword->typenum = 32;
myword->word = ",";
return(myword);
break;
case ':':
myword->typenum = 33;
myword->word = ":";
return(myword);
break;
case ';':
myword->typenum = 34;
myword->word = ";";
return(myword);
break;
case '>': m_getch();
if (ch == '='){
myword->typenum = 37;
myword->word = '>=';
return myword;
}
retract();
myword->typenum = 35;
myword->word = ">";
return myword;
break;
case '<': m_getch();
if (ch == '='){
myword->typenum = 38;
myword->word = '<=';
return myword;
}
retract();
myword->typenum = 36;
myword->word = "<";
return myword;
break;
case '!': m_getch();
if (ch == '='){
myword->typenum = 40;
myword->word = '!=';
return myword;
}
retract();
myword->typenum = -1;
myword->word = "ERROR";
return myword;
break;
case '\0':
myword->typenum = 1000;
myword->word = "OVER";
return (mywrod);
break;
default: myword->typenum = -1;
myword->word = "ERROR";
return (myword);
}
}
这里有一个小问题要告诉大家,这个代码是有问题的,真是做个一参考作用,可不要直接复制粘贴啊。主要是给没有思路的开发者提供一个解决问题的思路顺便也是一个参考的答案
编译原理(词法分析器)
最新推荐文章于 2024-09-27 14:19:28 发布