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. 編譯原理哪個章節最難學
都不簡單。
由於這門課程相對抽象且內容復雜,是較難學的一門課程。編譯原理是一門理論性和實踐性較強的課程,《編譯原理》實驗教學是《編譯原理》課程教學的一個必備環節。在學習過程中,只有通過上機實驗,才能使學生對比較。