首页 词法分析1

词法分析1

举报
开通vip

词法分析1实验一:编写C语言的词法分析器 输入文件名:*.c                输出文件名:*.tok 【实验内容】 实现C语言的词法分析程序。从输入的C语言源程序中,识别出各个具有独立意义的单词,以二元组(单词的类别,单词的值)的形式输出。保留字、运算符和分隔符以一字一类的方式编码,所有的标识符作为一类,所有的常量作为一类。 【实验目的】 明确词法分析的任务,了解词法分析器的设计与实现。 【实验要求】 (1)程序 (2)实验报告:C语言的词法正规表达式,状态转换图。 【实验说明】 上机两次4小时,第7周检查。 ...

词法分析1
实验一:编写C语言的词法分析器 输入文件名:*.c                输出文件名:*.tok 【实验内容】 实现C语言的词法分析程序。从输入的C语言源程序中,识别出各个具有独立意义的单词,以二元组(单词的类别,单词的值)的形式输出。保留字、运算符和分隔符以一字一类的方式编码,所有的标识符作为一类,所有的常量作为一类。 【实验目的】 明确词法分析的任务,了解词法分析器的设计与实现。 【实验要求】 (1)程序 (2)实验 报告 软件系统测试报告下载sgs报告如何下载关于路面塌陷情况报告535n,sgs报告怎么下载竣工报告下载 :C语言的词法正规表达式,状态转换图。 【实验说明】 上机两次4小时,第7周检查。 【输入样例】 main() { int  a,b; a = 10; b = a + 20; } 【输出样例】 (id,main) ((,_) (),_) ({,_} (int,_) (id,a) (,,_) (id,b) (;,_) (id,a) (=,_) (const,10) (;,_) (id,b) (=,_) (id,a) (+,_) (const,20) (;,_) (),_) [/watermark] #include #include #include #include #define LEN sizeof(struct Node) #define NULL 0 struct Node {    char data;    struct Node *next; /* 定义动态链表数据结构 */ }; void output(struct Node*);/*扫描输出 函数 excel方差函数excelsd函数已知函数     2 f x m x mx m      2 1 4 2拉格朗日函数pdf函数公式下载 */ void scaner(); /*词法分析*/ void getbc(); void getch(); void concat(); int letter(char ch); int degit(char ch); int reserve(); void retract(); void back(char *a,char *b); struct Node *head,*p; char ch; /*全局变量*/ char *key[]={"float","int","char","if","else","for","while"}; /*关键字表*/ char token[20]; /*字符数组,存放构成单词的符号串*/ int main(void) {    head=(struct Node *)malloc(LEN); /*分配头节点存储空间*/    if(!head)    {        printf("error");        exit(1);    }    head->next=NULL;    head->data=' ';    p=head;    printf("When input a \'$\' at the beigining of an line,this programe will be over.\n");    printf("And the programe will output the codes you inputed just now.\n");    printf("Please input your codes:\n");    while(1)    {        int i=0;        char temp[256];/*每行长度不超过256个字符*/        gets(temp); /*输入源程序,以行为单位*/        if(temp[0]=='$') break;/*当输入的第一个字符为$时表示输入源代码结束*/        p->next=(struct Node *)malloc(LEN);        if(!(head->next))        {            printf("error");            exit(1);        }        p=p->next;        while(temp[i]!='\0' && i<256)      /*将输入的代码以行为单位存入缓冲区*/        {            p->data=temp[i];            p->next=(struct Node *)malloc(LEN);            if(!(p->next))            {                printf("error");                exit(1);            }            p=p->next;            i++;        }        p->data='\n';        p->next=NULL;      /*尾结点*/    }    output(head);     /*扫描缓冲区,输出结果*/    p=head->next;    while(p->next!=NULL)    scaner();     /*词法分析*/    system("pause");    return 0; } void output(struct Node *head)             /*扫描缓冲区函数*/ {    if(!head) {printf("error");exit(1);}    p=head->next;    while(p->next!=NULL)    {        printf("%c",p->data);        p=p->next;    }  printf("\n"); } void getbc() /*若ch中是空白字符,则不停调用getch()直到读入的不是空白字符为止*/ {    while (ch==' ')    getch(); } void getch()    /*从缓冲区读入一字符*/ {    ch=p->data;    p=p->next; } void concat()   /*将ch中的字符连接到token的后面*/ {    unsigned int i;    i=strlen(token);    token[i]=ch;    token[i+1]='\0'; } int letter(char ch) /*判断ch中的是否是字母*/ {    return isalpha((int)ch); } int digit(char ch)  /*判断ch中的是否是数字*/ {    return isdigit((int)ch); } int reserve() /*判断token中的字符串是否是关键字或是标识符*/ {    int k;    for(k=0;k<6;k++)    {        if(strcmp(key[k],token)==0)        return (k+1);    }    return 0; } void retract() /*指针回退一个字符*/ {    struct Node *Q;    Q=head->next;    while(Q->next!=p)        Q=Q->next;    p=Q; } void back(char *a,char *b)    /*返回函数,输出序列*/ {    printf("(%s,%s)\n",a,b); } void scaner() /*词法分析函数*/ {    int c;    token[0]=NULL; /*将token清空*/    getch();    getbc(); /*读入一个单词*/    if(letter(ch)) /*处理字符的情况*/    {        while(letter(ch)||digit(ch))        {            concat();            getch();        }        retract();        c=reserve();        if(c!=0)            back(token,"_");        else            back("id",token);    }    else if(digit(ch)) /*处理数字的情况*/    {        while(digit(ch))        {            concat();            getch();        }    retract();    printf("(num,%d)\n",atoi(token));    }    else    switch(ch)  /*处理特殊符号的情况*/    {        case'+': back("+","_");break;        case'-': back("-","_");break;        case'*': back("*","_");break;        case'/': back("/","_");break;        case'<': getch();                            if(ch=='=')                                back("<=","_");                            retract();                            back("<","_");                            break;        case'>': getch();                            if(ch=='=') back(">=","_");                            retract();                            back(">","_");                            break;        case';': back(";","_");break;        case'{': back("{","_");break;        case'}': back("}","_");break;        case'(': back("(","_");break;        case')': back(")","_");break;        case'=': back("=","_");break;        case',': back(",","_");break;        case'\n': break;        default: printf("error");break;    } } 照着课件上敲的 p.s. 老师的【输出样例】最后一个错了  :) 这个是上传的 txt 格式文件 [点击查看] [/watermark] 另外一个此法分析器 #include "stdafx.h" #include #include #include #include #include #include #include #define NULL 0 FILE *fp; char ch; char *key视频教程'>word[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("\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("\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("\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(); } 本文章来自www.21shipin.com 21视频教程网 词法分析器_C语言程序设计教程 原文链接:http://www.21shipin.com/html/61641.shtml
本文档为【词法分析1】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_495388
暂无简介~
格式:doc
大小:49KB
软件:Word
页数:10
分类:工学
上传时间:2011-04-04
浏览量:19