兰州营销型网站,seo网络营销案例分析,学ui设计适合什么样的人,少儿编程加盟学校一、实验项目要求
1.实验目的
通过设计编制调试一个具体的词法分析程序#xff0c;加深对词法分析原理的理解。并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析方法。
编制一个读单词过程#xff0c;从输入的源程序中#xff0c;识别出各个具有…一、实验项目要求
1.实验目的
通过设计编制调试一个具体的词法分析程序加深对词法分析原理的理解。并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析方法。
编制一个读单词过程从输入的源程序中识别出各个具有独立意义的单词即基本保留字、标识符、常数、运算符、分隔符五大类。并依次输出各个单词的内部编码及单词符号自身值。遇到错误时可显示“Error”然后跳过错误部分继续显示
2.实验要求
识别保留字if、int、for、while、do、return、break、continue等C语言的保留字单词种别码为1。
其他的都识别为标识符单词种别码为2。
常数为无符号整形数单词种别码为3。
运算符包括、-、*、/、、、、、、! 单词种别码为4。
分隔符包括,、;、{、}、(、)、[、] 单词种别码为5。
二、理论分析或算法分析
在词法分析中自文件头开始扫描源程序字符一旦发现符合“单词”定义的源程序字符串时将它翻译成固定长度的单词内部表示并查填适当的信息表。经过词法分析后源程序字符串源程序的外部表示被翻译成具有等长信息的单词串源程序的内部表示并产生两个表格常数表和标识符表它们分别包含了源程序中的所有常数和所有标识符。
0.定义部分定义常量、变量、数据结构。
1.初始化从文件将源程序全部输入到字符缓冲区中。
2.取单词前去掉多余空白。
3.取单词利用实验一的成果读出单词的每一个字符组成单词分析类型。关键是如何判断取单词结束取到的单词是什么类型的单词
4.显示结果。
三实现方法
程序流程图如图所示 四实验结果分析
实验结果图 遇到的问题
1当第一次将代码写进去之后出现如图所示的错误。 2一直找不到example.txt文件一直出现那个error;
解决办法
1将Test1的属性下的C/C下面的语言的符合模式改为“否”即可。 2在目录下导入并且在自建目录下导入就可以解决这个问题。
在这次实验中对之前学到的词法分析有了进一步的了解加深了对于词法分析的步骤的理解与领悟对于今后对编译原理的学习有很大的帮助。刚开始把已有代码导入的时候各种错误然后就在网上一个一个搜索直到解决问题在同学的帮助下顺利的把代码问题搞对。
五、代码
#include stdio.h
#include ctype.h
#include malloc.h
#include stdlib.h
#include string.h
#define NULL 0
#pragma warning(disable:4996)FILE *fp;
char cbuffer;
char *key[32] { if,else,for,break,continue,int,float,double,auto,case,char,const,default,do,enum,long,extern,goto,register,return,short,signed,sizeof,static,struct,switch,typedef,union,unsigned,void,volatile,while };
char *border[8] { ,,;,{,},(,),[,] };
char *arithmetic[4] { ,-,*,/ };
char *relation[6] { ,,,,, };
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 31; i){if (strcmp(key[i], searchchar) 0)return(i 1);}return 0;case 2:{for (i 0; i 7; i){if (strcmp(border[i], searchchar) 0)return(i 1);}return(0);}case 3:{for (i 0; i 3; i){if (strcmp(arithmetic[i], searchchar) 0){return(i 1);}}return(0);}case 4:{for (i 0; i 5; i)if (strcmp(relation[i], searchchar) 0)return(i 1);return(0);}case 5:{for (i 0; i constnum; i){if (consts[i] (strcmp(consts[i], searchchar) 0))return(i 1);}consts[i - 1] (char *)malloc(sizeof(searchchar));strcpy(consts[i - 1], searchchar);constnum;return(i);}case 6:{for (i 0; i labelnum; i)if (label[i] (strcmp(label[i], searchchar) 0))return(i 1);label[i - 1] (char *)malloc(sizeof(searchchar));strcpy(label[i - 1], searchchar);labelnum;return(i);}default: return 0;}
}
char alphaprocess(char buffer)
{// int atype;int i -1;char alphatp[20];while ((isalpha(buffer)) || (isdigit(buffer))){alphatp[i] buffer;buffer fgetc(fp);}alphatp[i 1] \0;if (/*atype*/search(alphatp, 1))// printf(%s (1,%d)\n,alphatp,atype-1);printf((1, \%s\)\n, alphatp);else{search(alphatp, 6);// printf(%s (6,%d)\n,alphatp,atype-1);printf((2, \%s\)\n, alphatp);}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;search(digittp, 5);// printf(%s (5,%d)\n,digittp,dtype-1);printf((3, \%s\)\n, digittp);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(%s (3,%d)\n,othertp,otype-1);printf((4, \%s\)\n, othertp);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(%s (4,%d)\n,othertp,otypetp-1);printf((4, \%s\)\n, othertp);goto out;}elseothertp[1] \0;// printf(%s (4,%d)\n,othertp,otype-1);printf((4, \%s\)\n, othertp);goto out;}if (buffer :){buffer fgetc(fp);if (buffer )printf(: (2,2)\n);buffer fgetc(fp);goto out;}else{if (/*otype*/search(othertp, 2)){// printf(%s (2,%d)\n,othertp,otype-1);printf((5, \%s\)\n, othertp);buffer fgetc(fp);goto out;}}if ((buffer ! \n) (buffer ! ))printf(%c error,not a word\n, buffer);buffer fgetc(fp);out: return(buffer);
}void main()
{int i;for (i 0; i 20; i){label[i] NULL;consts[i] NULL;}if ((fp fopen(example.txt, 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);}printf(over\n);getchar();}
}
example.txt
#inlcludestdio.h int main() { int b, a, c; a 10; cab; printf(%d%d,a,b);; return 0; }