编译原理----词法分析程序----C语言版
2012-03-20 09:30
337 查看
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char KeyWord[20][100]={
"begin",
"end",
"if",
"while",
"var",
"procedure",
"else",
"for",
"do",
"int",
"read",
"write"
};
char yunsuanfu[]="+-*/<>%=";
char fenjiefu[]=",;(){}:";
int main()
{
char test[]="var a=10;\nvar b,c;\nprocedure p; \n\tbegin\n\t\tc=a+b\n\tend\n";
int len_yunsuanfu=strlen(yunsuanfu);
int len_fenjiefu=strlen(fenjiefu);
puts(test);
int length=strlen(test),i,j,k;
for(i=0;i<length;i++)
{
if(test[i]==' '||test[i]=='\n'||test[i]=='\t')
continue;
int tag=0;
for(j=0;j<len_fenjiefu;j++)
{
if(fenjiefu [j]==test[i]){
printf("分界符\t%c\n",test[i]);
tag=1;
break;
}
}
if(tag==1)
continue;
tag=0;
for(j=0;j<len_yunsuanfu;j++)
{
if(yunsuanfu[j]==test[i]){
printf("运算符\t%c\n",test[i]);
tag=1;
break;
}
}
if(tag==1)
continue;
if(test[i]>='0'&&test[i]<='9')
{
printf("数字\t");
while(test[i]>='0'&&test[i]<='9')
{
printf("%c",test[i]);
i++;
}
printf("\n");
continue;
}
char temp[100];
j=0;
while(test[i]>='0'&&test[i]<='9'||test[i]>='a'&&test[i]<='z'||test[i]>='A'&&test[i]<='Z'||test[i]=='_')
{
temp[j++]=test[i];
i++;
}
i--;
temp[j++]='\0';
tag=0;
for(j=0;j<20;j++){
if(strcmp(temp,KeyWord[j])==0)
{
tag=1;
printf("关键字\t%s\n",temp);
break;
}
}
if(tag==0)
printf("标识符\t%s\n",temp);
}
}
#include<string.h>
#include<stdlib.h>
char KeyWord[20][100]={
"begin",
"end",
"if",
"while",
"var",
"procedure",
"else",
"for",
"do",
"int",
"read",
"write"
};
char yunsuanfu[]="+-*/<>%=";
char fenjiefu[]=",;(){}:";
int main()
{
char test[]="var a=10;\nvar b,c;\nprocedure p; \n\tbegin\n\t\tc=a+b\n\tend\n";
int len_yunsuanfu=strlen(yunsuanfu);
int len_fenjiefu=strlen(fenjiefu);
puts(test);
int length=strlen(test),i,j,k;
for(i=0;i<length;i++)
{
if(test[i]==' '||test[i]=='\n'||test[i]=='\t')
continue;
int tag=0;
for(j=0;j<len_fenjiefu;j++)
{
if(fenjiefu [j]==test[i]){
printf("分界符\t%c\n",test[i]);
tag=1;
break;
}
}
if(tag==1)
continue;
tag=0;
for(j=0;j<len_yunsuanfu;j++)
{
if(yunsuanfu[j]==test[i]){
printf("运算符\t%c\n",test[i]);
tag=1;
break;
}
}
if(tag==1)
continue;
if(test[i]>='0'&&test[i]<='9')
{
printf("数字\t");
while(test[i]>='0'&&test[i]<='9')
{
printf("%c",test[i]);
i++;
}
printf("\n");
continue;
}
char temp[100];
j=0;
while(test[i]>='0'&&test[i]<='9'||test[i]>='a'&&test[i]<='z'||test[i]>='A'&&test[i]<='Z'||test[i]=='_')
{
temp[j++]=test[i];
i++;
}
i--;
temp[j++]='\0';
tag=0;
for(j=0;j<20;j++){
if(strcmp(temp,KeyWord[j])==0)
{
tag=1;
printf("关键字\t%s\n",temp);
break;
}
}
if(tag==0)
printf("标识符\t%s\n",temp);
}
}
相关文章推荐
- 一个简单的编译原理词法语法语义分析程序
- 编译原理丨第十三周 ——1000. 输入输出LL(1)语法分析程序
- 编译原理实验一:为PL/0语言编写一个词法分析程序
- Linux程序编译执行原理之一:预处理-编译-汇编-链接过程分析
- 编译原理-词法分析01-扫描程序
- 设计有穷自动机DFA实现C++简单程序的词法分析、扫描(编译原理实验) 推荐
- 编译原理:递归向下分析程序建立语法分析树的Java实现(一)
- VC编译下对一个超简单的缓冲区溢出程序的原理解析以及c程序的汇编分析
- 编译原理 实验3 递归下降语法分析程序设计
- 关于Basic程序解释器及编译原理的简单化(1)--词法分析和代数式求值
- 简单算符优先文法分析程序(编译原理)
- 编译原理 词法分析程序
- 关于Basic程序解释器及编译原理的简单化(1)---Basic器的语法分析及主要代码
- 编译原理语法分析对循环语句和条件判断语句编写词法分析编译程序,只能通过一遍扫描完成
- 吉首大学_编译原理实验题_基于预測方法的语法分析程序的设计【通过代码】
- 编译原理学习笔记12——(魔镜魔镜告诉我—— 构造预测分析程序)——2014_1_28
- 编译原理-词法分析程序1
- 编译原理 实验1 词法分析程序的构造
- 编译原理——语法分析程序的设计
- 编译原理-用Bison构造语法分析程序-小小计算器