网站大量收购独家精品文档,联系QQ:2885784924

词法分析代码Pascal.doc

  1. 1、本文档共4页,可阅读全部内容。
  2. 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
词法分析代码Pascal

#include iostream #includestring using namespace std; #define? MAX 22 char ch = ; string key[15]={begin,end,if,then,else,while,write,read, do, call,const,char,until,procedure,repeat}; int Iskey(string c){???????? //关键字判断 int i; for(i=0;iMAX;i++) { if(key[i].compare(c)==0) return 1; } return 0; } int IsLetter(char c) {??????? //判断是否为字母 if(((c=z)(c=a))||((c=Z)(c=A))) return 1; else return 0; } int IsDigit(char c){????????? //判断是否为数字 if(c=0c=9) return 1; else return 0; } void analyse(FILE *fpin){ string arr=; while((ch=fgetc(fpin))!=EOF) { arr=; if(ch== ||ch==\t||ch==\n){} else if(IsLetter(ch)){ while(IsLetter(ch)||IsDigit(ch)) { if((ch=Z)(ch=A)) ch=ch+32; arr=arr+ch; ch=fgetc(fpin); } fseek(fpin,-1L,SEEK_CUR); if (Iskey(arr)){coutarr\t$关键字endl;} else? coutarr\t$普通标识符endl; } else if(IsDigit(ch)){ while(IsDigit(ch)||ch==.IsDigit(fgetc(fpin))){ arr=arr+ch; ch=fgetc(fpin); } fseek(fpin,-1L,SEEK_CUR); coutarr\t$无符号实数endl; } else switch(ch){ case+: case- : case* : case= : case/ :coutch\t$运算符endl;break; case( : case) : case[ : case] : case; : case. : case, : case{ : case} :coutch\t$界符endl;break; case: :{ch=fgetc(fpin); if(ch===) cout:=\t$运算符endl; else {cout=\t$运算符endl;; fseek(fpin,-1L,SEEK_CUR);} }break; case :{ch=fgetc(fpin); if(ch===) cout=\t$运算符endl; if(ch==)cout\t$输入控制符endl; else {cout\t$运算符endl; fseek(fpin,-1L,SEEK_CUR);} }break; case :{ch=fgetc(fpin); if(ch===)cout=\t$运算符endl; else if(ch==)cout\t$输出控制符endl; else if(ch==) cout\t$运算符endl; else{cout\t$运算符endl; fseek(fpin,-1L,SEEK_CUR);} }break; default : coutch\t$无法识别字符endl; } } } void main(){ char in_fn[30]; FILE * fpin; cout请输入源文件名(包括路径和后缀名):; for(;;){ cinin_fn; if((fpin=fopen(in_fn,r))!=NULL) break; else cout文件路径错误!请输入源文件名(包括路径和后缀名):; } cout\n********************分析如下*********************endl; analyse(fpin); fclose(fpin); }

您可能关注的文档

文档评论(0)

kaiss + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档