导航:首页 > 源码编译 > 张芃编译原理上机

张芃编译原理上机

发布时间:2022-05-04 06:06:22

A. 编译原理词法分析器

#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<process.h> /*头文件*/

void init();
char *DchangeB(char *buf);
int search(char *buf,int type,int command);
void intdeal(char *buffer);
void chardeal(char *buffer);
void errordeal(char error,int lineno);
void scanner();

void init()
{ char *key[]={"","auto","break","case","char","const","continue","default","do","double",
"else","enum","extern","float","for","goto","if","int","long","register",
"return","short","signed","sizeof","static","struct","switch","typedef",
"union","unsigned","void","volatile","while"}; /*C语言所有关键字/
char *limit[]={" ","(",")","[","]","->",".","!","++","--","&","~",
"*","/","%","+","-","<<",">>","<","<=",">",">=","==","!=","&&","||",
"=","+=","-=","*=","/=",",",";","{","}","#","_","'"};/*运算、限界符*/
fstream outfile;
int i,j;
char *c;
outfile.open("key.txt",ios::out);
for(i=0;i<32;i++)
outfile<<key[i]<<endl;
outfile.close();
outfile.open("Limit.txt",ios::out);
for(j=0;j<38;j++)
outfile<<limit[j]<<endl;
c="";
outfile<<c;
outfile.close();
outfile.open("bsf.txt",ios::out);
outfile.close();
outfile.open("cs.txt",ios::out);
outfile.close();
outfile.open("output.txt",ios::out);
outfile.close();
}

char *DchangeB(char *buf)
{

int temp[20];
char *binary;
int value=0,i=0,j;
for(i=0;buf[i]!='\0';i++)
value=value*10+(buf[i]-48); /*将字符转化为十进制数*/
if(value==0)
{
binary=new char[2];
binary[0]='0';
binary[1]='\0';
return(binary);
}
i=0;
while(value!=0)
{
temp[i++]=value%2;
value/=2;
}
temp[i]='\0';
binary=new char[i+1];
for(j=0;j<=i-1;j++)
binary[j]=(char)(temp[i-j-1]+48);
binary[i]='\0';
return(binary); /*十进制转化为二进制*/

}

int search(char *buf,int type,int command)
{ int number=0;
fstream outfile;
char ch;
char temp[30];
int i=0;
switch(type)
{
case 1: outfile.open("key.txt",ios::in);break;
case 2: outfile.open("bsf.txt",ios::in);break;
case 3: outfile.open("cs.txt",ios::in);break;
case 4: outfile.open("limit.txt",ios::in);break;
}
outfile.get(ch);
while(ch!=EOF){
while(ch!='\n')
{
temp[i++]=ch;
outfile.get(ch);
}
temp[i]='\0';
i=0;
number++;
if(strcmp(temp,buf)==0)
{
outfile.close();
return number; /*若找到,返回在相应表中的序号*/
}
else
outfile.get(ch);
} //结束外层while循环
if(command==1)
{
outfile.close( );
return 0; /*找不到,当只需查表,返回0,否则还需造表*/

}
switch(type)
{
case 1: outfile.open("key.txt",ios::in);break;
case 2: outfile.open("bsf.txt",ios::in);break;
case 3: outfile.open("cs.txt",ios::in);break;
case 4: outfile.open("limit.txt",ios::in);break;
}
outfile<<buf;
outfile.close();
return number+1;
}

void intdeal(char *buffer){

fstream outfile;
int result;
result=search(buffer,1,1); /*先查关键字表*/
outfile.open("output.txt",ios::app);
if(result!=0)
outfile<<buffer<<result<<endl; /*若找到,写入输出文件*/
else
{
result=search(buffer,2,2); /*若找不到,则非关键字,查标识符表,还找不到则造入标识符表*/
outfile<<buffer<<result<<endl;
} /*写入输出文件*/
outfile.close();
}

void chardeal(char *buffer)
{ fstream outfile;
int result;
result=search(buffer,1,1); /*先查关键字表*/
outfile.open("output.txt",ios::app);
if(result!=0)
outfile<<buffer<<result<<endl; /*若找到,写入输出文件*/
else
{
result=search(buffer,2,2); /*若找不到,则非关键字,查标识符表,还找不到则造入标识符表*/
outfile<<buffer<<result<<endl;
} /*写入输出文件*/
outfile.close();
}

void errordeal(char error,int lineno)
{ cout<<"\nerror: "<<error<<" ,line"<<lineno;
}

void scanner()
{ fstream infile,outfile;
char filename[20];
char ch;
int err=0;
int i=0,line=1;
int count,result,errorno=0;
char array[30];
char *word;
printf("\n please input the file scanner name:");
scanf("%s",filename);
err=1;
infile.open(filename,ios::nocreate|ios::in);
while(! infile)
{
cout<<"cannot open file"<<endl;
printf("please input the file name again:\n");
scanf("%s",filename);
infile.open(filename,ios::nocreate|ios::in);
err++;
if(err==3)
{cout<<"SORROY YOU CAN'T VUEW THE PRGARME\n";
cout<<"TANKE YOU VIEW"<<endl;
exit(0);}
}
infile.get(ch);
while(ch!=EOF)
{ /*按字符依次扫描源程序,直至结束*/
i=0;
if(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z'))||(ch=='_'))
{ /*以字母开头*/
while(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z'))||(ch=='_')||((ch>='0')&&(ch<='9')))
{
array[i++]=ch;
infile.get(ch);
}
word=new char[i+1];
memcpy(word,array,i);
word[i]='\0';
intdeal(word);
if(ch!=EOF)
infile.seekg(-1,ios::cur);
}
else if(ch>='0'&&ch<='9')
{ /*以数字开头*/
while(ch>='0'&&ch<='9')
{
array[i++]=ch;
infile.get(ch);
}
word=new char[i+1];
memcpy(word,array,i);
word[i]='\0';
intdeal(word);
if(ch!=EOF)
infile.seekg(-1,ios::cur);
}
else if((ch==' ')||(ch=='\t'))
; /*消除空格符和水平制表符*/
else if(ch=='\n')
line++; /*消除回车并记录行数*/
else if(ch=='/')
{ /*消除注释*/
infile.get(ch);
if(ch=='=')
{ /*判断是否为‘/=’符号*/
outfile.open("output.txt",ios::noreplace|ios::app);
outfile<<"/=\t\t\t4\t\t\t32\n";
outfile.close();
}
else if(ch!='*')
{ /*若为除号,写入输出文件*/
outfile.open("output.txt",ios::noreplace|ios::app);
outfile<<"/\t\t\t4\t\t\t13\n";
outfile.close();
outfile.seekg(-1,ios::cur);
}
else if(ch=='*')
{ /*若为注释的开始,消除包含在里面的所有字符*/
count=0;
infile.get(ch);
while(count!=2)
{ /*当扫描到‘*’且紧接着下一个字符为‘/’才是注释的结束*/
count=0;
while(ch!='*')
infile.get(ch);
count++;
infile.get(ch);
if(ch=='/')
count++;
else
infile.get(ch);
}
}
}
else if(ch=='"')
{ /*消除包含在双引号中的字符串常量*/
outfile.open("output.txt",ios::noreplace|ios::app);
outfile<<ch<<"\t\t\t4\t\t\t37\n";
outfile.close();
while(ch!='"')
infile.get(ch);
infile<<ch<<"\t\t\t4\t\t\t37\n";
infile.close();
}
else
{ /*首字符为其它字符,即运算限界符或非法字符*/
array[0]=ch;
infile.get(ch); /*再读入下一个字符,判断是否为双字符运算、限界符*/
if(ch!=EOF)
{ /*若该字符非文件结束符*/
array[1]=ch;
word=new char[3];
memcpy(word,array,2);
word[2]='\0';
result=search(word,4,1); /*先检索是否为双字符运算、限界符*/
if(result==0)
{ /*若不是*/
word=new char[2];
memcpy(word,array,1);
word[1]='\0';
result=search(word,4,1); /*检索是否为单字符运算、限界符*/
if(result==0)
{ /*若还不是,则为非法字符*/
errordeal(array[0],line);
errorno++;
infile.seekg(-1,ios::cur);
}
else
{ /*若为单字符运算、限界符,写入输出文件并将扫描文件指针回退一个字符*/
outfile.open("output.txt",ios::noreplace|ios::app);
outfile<<word<<"\t\t\t4\t\t\t"<<result<<"\t"<<endl;
outfile.close();
infile.seekg(-1,ios::cur);
}
}
else
{ /*若为双字符运算、限界符,写入输出文件*/
outfile.open("output.txt",ios::noreplace|ios::app);
outfile<<word<<"\t\t\t4\t\t\t"<<result<<endl;;
outfile.close( );
}
}
else
{ /*若读入的下一个字符为文件结束符*/
word=new char[2];
memcpy(word,array,1);
word[1]='\0';
result=search(word,4,1); /*只考虑是否为单字符运算、限界符*/
if(result==0) /*若不是,转出错处理*/
errordeal(array[0],line);
else
{ /*若是,写输出文件*/
outfile.open("output.txt",ios::noreplace|ios::app);
outfile<<word<<"\t\t\t4\t\t\t"<<result<<"\t"<<endl;
outfile.close();
}
}
}
infile.get(ch);
}
infile.close();
cout<<"\nThere are "<<errorno<<" error(s).\n"; /*报告错误字符个数*/
}
void main()
{ char yn;

do{
init(); /*初始化*/
scanner();/*扫描源程序*/

printf("Are You continue(y/n)\n"); //判断是否继续?
yn=getch();
}while(yn=='y'||yn=='Y');
}

B. 南京大学计算机专业考研不需要考编译原理

计算机专业课是全国统一考你说的那四门课,不同学校复试的科目不同,具体可以上具体学校网站上看去,我觉得考研要想取个好成绩 最关键的是数学和专业课,这两门课是拉开距离的,一般英语和政治都不会拉开太多分,要想考上理想中的院校光有信心也是不行的,一定要有实力,所以你要慎重的考虑一下。

C. 编译原理: 画出识别如下单词的状态转换图: Char int float

(四)练习该实验的目的和思路: 程序开始变得复杂起来,可能是大家以前编过的程序中最复杂的,但相对于 以后的程序来说还是简单的。因此要认真把握这个过渡期的练习。程序规模 大概为 200 行及以上。通过练习,掌握对字符进行灵活处理的方法。 (五)为了能设计好程序,注意以下事情: 1.模块设计:将程序分成合理的多个模块(函数/类) ,每个模块(类)做具 体的同一事情。 2.写出(画出)设计方案:模块关系简图、流程图、全局变量、函数接口等。 3.编程时注意编程风格:空行的使用、注释的使用、缩进的使用等。 4.程序设计语言不限,建议使用面向对象技术及可视化编程语言,如 C++, VC,JAVA,VJ++等。

四、上交:
1.程序源代码及可执行文件(当堂检查/通过网络提交) ; 2.已经测试通过的测试数据 3 组(全部存在一个文本文件中,以“第一组输 入/输出/第二组输入/输出/第三组输入/输出”的顺序存放) ; 3.实验报告按照提供的模板填写: (1) 功能描述:该程序具有什么功能? (2) 算法描述:所采用的数据结构,基本实现算法及某些特殊过程的实 现算法(如在处理某个问题时,你所采取的好的处理方法等)注意 此处不要简单的将源程序抄上来。 (源程序将打印出来作为附录) (3) 程序结构描述:函数调用格式、参数含义、返回值描述、函数功能; 另外可以附加函数之间的调用关系图、 程序总体执行流程图及类的 层次图。 (4) 实验总结:你在编程过程中花时多少?多少时间在纸上设计?多少 时间上机输入和调试?多少时间在思考问题?遇到了哪些难题?你 是怎么克服的?你对你的程序的评价?你的收获有哪些? (5) 写出上机调试时发现的问题,以及解决的过程; (6) 附上源程序(打印的)

D. 郑大计算机复试数据库原理和编译原理各是什么教材

数据库用王珊的数据库系统概论(第四版),编译原理 清华大学出版社的陈英[等]编着 就行

E. 求编译原理的词法分析器源码

/* 我上编译原理课时的第一次作业就是这个,flex源码. */
%{
#include<math.h>
int num_lines=0;
%}
DIGIT [0-9]
ID [a-zA-Z_][a-zA-Z0-9]*
%%
"#include" {
printf("<包含头文件,请手动合并文件\\>\n");
fprintf(yyout,"<包含头文件,请手动合并文件\\>\n");
}
{DIGIT}+ {
printf("(3整数, \"%s\")\n", yytext);
fprintf(yyout,"(3整数, \"%s\")\n", yytext);
}
{DIGIT}+"."{DIGIT}* {
printf("(3浮点数, \" %s\")\n",yytext);
fprintf(yyout,"(3浮点数, \" %s\")\n",yytext);
}
auto |
break |
case |
char |
const |
continue |
default |
do |
double |
else |
enum |
extern |
float |
for |
goto |
if |
int |
long |
register |
return |
short |
signed |
sizeof |
static |
struct |
switch |
typedef |
union |
unsigned |
void |
volatile |
while {
fprintf(yyout,"(1, \"%s\")\n",yytext);
fprintf(yyout,"(1, \"%s\")\n",yytext);
}
{ID} {
printf("(2, \"%s\")\n",yytext);
fprintf(yyout,"(2, \"%s\")\n",yytext);
}
"+" |
"++" |
"+=" |
"-" |
"--" |
"-=" |
"->" |
"*" |
"**" |
"*=" |
"/" |
"/=" |
"=" |
"==" |
">" |
">>" |
">=" |
">>=" |
"<" |
"<<" |
"<=" |
"<<=" |
"!" |
"!=" |
"%" |
"%=" |
"&" |
"&&" |
"&=" |
"|" |
"||" |
"|=" |
"^" |
"^=" {
printf("(4, \"%s\")\n",yytext);
fprintf(yyout,"(4, \"%s\")\n",yytext);
}
"{" |
"}" |
"(" |
")" |
";" |
"," |
"'" |
"\"" |
"." |
"?" |
"[" |
"]" |
"\\" |
":" {
printf("(5, \"%s\")\n",yytext);
fprintf(yyout,"(5, \"%s\")\n",yytext);
}
\n {
++num_lines;
}
"/*"[^(*/)\n]*"*/"
(" ")+
[\t]+
. {
printf("(不能识别字符, \"%s\")\n",yytext);
fprintf(yyout,"(不能识别字符, \"%s\")\n",yytext);
}
%%
main(argc,argv)
int argc;
char **argv;
{
++argv,--argc;
if(argc>0)
yyin=fopen(argv[0],"r");
else
yyin=stdin;
yyout=fopen("output.txt","w");
yylex();
fclose(yyout);
}
int yywrap()
{
return 1;
}

/* 附:我们第一次作业的要求。
实验一:用高级语言编写词法分析器(用lex生成)一、实验目的:编制一个识别C语言子集的词法分析器。从输入的源程序中,识别出各个具有独立意义的记号,即基本保留字、标识符、常数、运算符、分隔符五大类。并依次输出各个记号的内部编码及记号符号自身值。(遇到错误时可显示“Error”,然后跳过错误部分继续显示)二、实验过程和指导:(一)准备:1.阅读课本有关章节,明确语言的词法,写出基本保留字、标识符、常数、运算符、分隔符和程序例。2.初步编制好程序。3.准备好多组测试数据。(二)程序要求:程序输入/输出示例:如源程序为C语言。输入如下一段:main(){ int a,b; a = 10; b = a + 20;}要求输出如下:(2,”main”)(5,”(“)(5,”)“)(5,”{“)(1,”int”)(2,”a”)(5,”,”)(2,”b”)(5,”;”)(2,”a”)(4,”=”)(3,”10”)(5,”;”)(2,”b”)(4,”=”)(2,”a”)(4,”+”)(3,”20”)(5,”;”)(5,”)“}
要求(满足以下要求可获得70%该题的得分):识别保留字:if、int、for、while、do、return、break、continue其他的都识别为标识符;常数为无符号整形数;运算符包括:+、-、*、/、=、>、<、>=、<=、!=分隔符包括:,、;、{、}、(、)以上为参考,具体可自行增删。 三、实验检查:1.程序:输入:测试数据(以文件形式);输出:二元组(以文件形式)。2.实验报告:(1)功能描述:该程序具有什么功能?(2)状态转换图。(2)程序结构描述:函数调用格式、参数含义、返回值描述、函数功能;函数之间的调用关系图、程序总体执行流程图。(4)源程序代码。(5)实验过程记录:出错次数、出错严重程度、解决办法摘要。(6)实验总结:你在编程过程中花时多少?多少时间在纸上设计?多少时间上机输入和调试?多少时间在思考问题?遇到了哪些难题?你是怎么克服的?你对你的程序的评价?你的收获有哪些?

另可附加:关键字 有符号数 符号表填写 行号记录,等
*/

F. 编译原理期末课程设计

工大学生伤不起啊锏惧害鍦板浘

本数据来源于网络地图,最终结果以网络地图最新数据为准。

G. 【南航考研】复试要考《离散数学》《编译原理》 好过……

1.南京大学复试内容为笔试、上机和面试,笔试内容为编译原理和离散数学2.每所名校的笔试内容都不尽相同,比如复旦貌似要考10门课,当然大多数学校还是只考2到3门,一般都选择初试没有考过的但很重要的专业课,比如编译原理,离散数学,还有数据库等等…3.考名校失败的话首先可以考虑调专硕,不行的话还可以考虑去软件学院或者调剂别的学校,只不过学校的档次一般都会差很多…调剂是很麻烦的···4.计算机要考数学一,还是有难度的,数学基础不好的话就要多努力了5.计算机每年跨考的很多,成功的人也不少,现在开始努力还来得及~

H. 编译原理设计题目8——SLR(1)分析法

以前做过的不过昨天因为毕业了把机子上了一些资料也删除了
1刚好这个也在里面。。。。。。。。。

I. 编译原理哪个章节最难学

都不简单。
由于这门课程相对抽象且内容复杂,是较难学的一门课程。编译原理是一门理论性和实践性较强的课程,《编译原理》实验教学是《编译原理》课程教学的一个必备环节。在学习过程中,只有通过上机实验,才能使学生对比较。

阅读全文

与张芃编译原理上机相关的资料

热点内容
喷油螺杆制冷压缩机 浏览:577
python员工信息登记表 浏览:375
高中美术pdf 浏览:159
java实现排列 浏览:511
javavector的用法 浏览:980
osi实现加密的三层 浏览:230
大众宝来原厂中控如何安装app 浏览:912
linux内核根文件系统 浏览:241
3d的命令面板不见了 浏览:524
武汉理工大学服务器ip地址 浏览:147
亚马逊云服务器登录 浏览:523
安卓手机如何进行文件处理 浏览:70
mysql执行系统命令 浏览:929
php支持curlhttps 浏览:142
新预算法责任 浏览:443
服务器如何处理5万人同时在线 浏览:249
哈夫曼编码数据压缩 浏览:424
锁定服务器是什么意思 浏览:383
场景检测算法 浏览:616
解压手机软件触屏 浏览:348