中南大學軟件學院編譯原理實驗報告_第1頁
中南大學軟件學院編譯原理實驗報告_第2頁
中南大學軟件學院編譯原理實驗報告_第3頁
中南大學軟件學院編譯原理實驗報告_第4頁
中南大學軟件學院編譯原理實驗報告_第5頁
已閱讀5頁,還剩16頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

《320144X1(編譯原理)》實驗報告項目名稱編譯原理專業(yè)班級軟件工程1403學號姓名溫睿誠實驗成績:批閱教師:年月日

第一部分詞法分析(實驗一必作)實驗一詞法分析程序設計與實現(xiàn)實驗報告要求詳細說明你的程序的設計思路和實現(xiàn)過程。用有限自動機或者文法的形式對詞法定義做出詳細說明,說明詞法分析程序的工作過程,說明錯誤處理的實現(xiàn)。設計思路:首先把單詞進行分類,分為String[]keyword={"if","int"};

ArrayList<String>biaoshi=newArrayList<>();

ArrayList<Integer>changshu=newArrayList<>();

String[]yunsuan={"+","=","-",">","==","!="};

String[]spilt={",","(",")","{","}",";"};五類,分別是關鍵字、標識符、常數(shù)、運算符以及分隔符。然后逐個字符讀入,對于關鍵字或標識符這類英文單詞開始的單詞,用空格隔開,其他可以用分隔符、運算符、空格、回車等。讀入一個單詞后對照一開始的五類分析出是哪一類,符合后交給語法分析器處理。實現(xiàn)過程:importjava.io.*;

importjava.util.ArrayList;

/**

*Createdby溫睿誠on2016/5/11/0011.

*/

publicclassCiFa{

String[]keyword={"if","int"};

ArrayList<String>biaoshi=newArrayList<>();

ArrayList<Integer>changshu=newArrayList<>();

String[]yunsuan={"+","=","-",">","==","!="};

String[]spilt={",","(",")","{","}",";"};

//記錄結(jié)果的符號表

//用什么數(shù)據(jù)結(jié)構(gòu)呢?

//當前單詞

StringBuilderstr=newStringBuilder("");

//下一個要讀的字符

charnow;

//一個棧

ArrayList<Character>stack=newArrayList<>();

privatevoidput(){

stack.add(newCharacter(now));

}

privatecharpop(){

if(stack.size()>0){

return((Character)stack.remove(stack.size()-1)).charValue();

}else{

return0;

}

}

//錯誤信息

StringerrorMsg;

Readerreader=null;

publicstaticvoidmain(String[]args){

CiFacifa=newCiFa();

cifa.fenXi(args[0]);

}

privatevoidfenXi(Stringfilename){

//讀取文件

Filefile=newFile(filename);

try{

reader=newInputStreamReader(newFileInputStream(file));

}catch(FileNotFoundExceptione){

System.out.println("讀取文件字符失??!");

e.printStackTrace();

}

//不斷讀取字符直到結(jié)束

getChar();

intresult;

//使用預測分析法

YuFayuFa=newYuCeFenXi();

booleanflag=true;

while(!(now<0||now>=65535)){

//根據(jù)返回數(shù)值查找或插入,錯誤則打印并提示。正確則記錄到map

result=read();

if(result!=6){

System.out.println("("+result+",\""+str+"\")");

if(!yuFa.fenxi(result,str.toString())){

flag=false;

System.err.println("語法分析出錯!出錯單詞:"+str.toString());

}

}else{

System.err.println("("+errorMsg+",\""+str+"\")");

}

str.delete(0,str.length());

}

//結(jié)束

booleantempResult=false;

if(yuFa!=null)

tempResult=yuFa.fenxi(6,"#");

if(tempResult&&flag)

System.out.println("語法分析通過!");

}

//判斷是否為數(shù)字

privatebooleanisDigit(){

if('0'<=now&&now<='9')

returntrue;

else

returnfalse;

}

//判斷是否為字母

privatebooleanisLetter(){

if(('a'<=now&&now<='z')||('A'<=now&&now<='Z'))

returntrue;

else

returnfalse;

}

//賦值下一字符給now,返回true表示讀到空格、換行等空白字符

privatebooleangetChar(){

booleanflag=false;

try{

now=(char)reader.read();

while(now==0||now=='\t'||now=='\r'||now=='\n'||now==32){

flag=true;

now=(char)reader.read();

}

}catch(IOExceptione){

e.printStackTrace();

}

returnflag;

}

//連接字符到單詞

privatevoidconcat(){

str.append(now);

}

privatebooleanisSpilt(){

Stringtemp=String.valueOf(now);

for(Stringstr:spilt){

if(str.startsWith(temp)){

returntrue;

}

}

returnfalse;

}

privatebooleaninAL(Stringstr,ArrayList<String>strings){

if(strings.contains(str))

returntrue;

returnfalse;

}

privatebooleaninShuzu(Stringstr,String[]strings){

for(Stringstr1:strings){

if(str.equals(str1)){

returntrue;

}

}

returnfalse;

}

privatebooleanisYun(){

Stringtemp=String.valueOf(now);

for(Stringstr:yunsuan){

if(str.startsWith(temp)){

returntrue;

}

}

returnfalse;

}

//詞法分析器

privateintread(){

if(isLetter()){

//字母開始的,要么關鍵字,要么標識符,其用空格、tab、回車之類的分隔,

//而標識符還可以用分割符號、運算符號分割。暫不判斷標識符是否定義

concat();

booleanflag;

flag=getChar();

while((isDigit()||isLetter())&&flag==false){

concat();

flag=getChar();

}

if(inShuzu(str.toString(),keyword)){

return1;

}else{

if(!inAL(toString(),biaoshi)){

biaoshi.add(str.toString());

}

return2;

}

}elseif(isDigit()){

//數(shù)字開頭的,是常數(shù),以空格、tab等以及分隔符、運算符分割

concat();

getChar();

while(isDigit()){

concat();

getChar();

}

return3;

}elseif(isSpilt()){

//分隔符,當出現(xiàn){、(時入棧,接收到}、)時判斷是否符合。單個字符,不需要分隔

if(now=='{'||now=='('){

put();

}elseif(now=='}'){

chartemp=pop();

if(temp!='{'){

errorMsg="沒有'{'與'}'匹配";

concat();

getChar();

return6;

}

}elseif(now==')'){

chartemp=pop();

if(temp!='('){

errorMsg="沒有'('與')'匹配";

concat();

getChar();

return6;

}

}

concat();

getChar();

return5;

}elseif(isYun()){

//運算符,一般為單個符號,例外如下

concat();

if(now=='<'||now=='>'||now=='!'){

getChar();

if(now=='='){

concat();

getChar();

}

}else{

getChar();

}

return4;

}else{

//ERROR

errorMsg="無法識別\""+now+"\"";

concat();

getChar();

return6;

}

}

}結(jié)果:對于文件:輸出:等第二部分語法分析(任選其中一個做實驗)實驗二預測分析法設計與實現(xiàn)實驗報告要求詳細說明你的程序的設計思路和實現(xiàn)過程。實驗報告要求用文法的形式對語法定義做出詳細說明,說明語法分析程序的工作過程,說明錯誤處理的實現(xiàn)。設計思路:首先寫出文法,然后作出預測分析表,再根據(jù)算法查表判斷是否符合改文法。這是自己寫的文法。模仿C++文法,但有不少局限性。實現(xiàn)過程:importjava.util.Stack;

/**

*Createdby溫睿誠on2016/6/1/0001.

*預測分析法

*/

publicclassYuCeFenXiimplementsYuFa{

Stack<String>stack;

String[]t={"int"};

String[]o={"+","-"};

String[]k={"if"};

String[]c={">","==","!="};

String[]spilt={",","(",")","{","}",";"};

privatestaticString[][]map=newString[15][14];

//橫排SABCNDEFGHIJKL

//對應1234567891011121314

//豎排tbz(o),;=k{}#

//對應12345678910111213

YuCeFenXi(){

//null即出錯,空字符串即空字

map[1][1]="tb(A){N}S";

map[1][13]="#";

map[2][1]="B";

map[2][6]="";

map[3][1]="tbC";

map[4][6]="";

map[4][7]=",tbC";

map[5][1]="tbD;N";

map[5][2]="bI;N";

map[5][10]="k(E){N}N";

map[5][12]="";

map[6][7]=",bD";

map[6][8]="";

map[7][2]="bJ";

map[7][3]="F";

map[8][3]="zG";

map[9][5]="oHcH";

map[9][6]="";

map[10][2]="b";

map[10][3]="z";

map[11][4]="(A)";

map[11][9]="=K";

map[12][4]="(A)";

map[12][5]="oHcH";

map[12][6]="";

map[13][2]="HL";

map[13][3]="HL";

map[14][5]="oHL";

map[14][8]="";

stack=newStack<>();

stack.push("#");

stack.push("S");

}

//判斷是否非終結(jié)符

privatebooleanisN(Stringstr){

if(str.length()!=1)

returnfalse;

chartemp=str.charAt(0);

if((temp>='A'&&temp<='L')||(temp=='S')||temp=='N')

returntrue;

returnfalse;

}

//把非終結(jié)符轉(zhuǎn)換為表中對應數(shù)字

privateintn2I(Stringstr){

chartemp=str.charAt(0);

switch(temp){

case'S':

return1;

case'A':

return2;

case'B':

return3;

case'C':

return4;

case'N':

return5;

default:

returntemp-'A'+3;

}

}

//把終結(jié)符轉(zhuǎn)換為表中對應數(shù)字

privateintt2I(Stringstr){

chartemp=str.charAt(0);

switch(temp){

case't':

return1;

case'b':

return2;

case'z':

return3;

case'(':

return4;

case'o':

return5;

case')':

return6;

case',':

return7;

case';':

return8;

case'=':

return9;

case'k':

return10;

case'{':

return11;

case'}':

return12;

case'#':

return13;

default:

return-1;

}

}

privatebooleaninArray(Stringstr,String[]array){

for(Stringtemp:array){

if(str.equals(temp))

returntrue;

}

returnfalse;

}

publicstaticbooleanisNumeric(Stringstr){

for(inti=0;i<str.length();i++){

System.out.println(str.charAt(i));

if(!Character.isDigit(str.charAt(i))){

returnfalse;

}

}

returntrue;

}

//若word是常規(guī)終結(jié)符如{,;則返回原值,若是int這種,則返回t

publicStringword2T(inttype,Stringword){

switch(type){

case1:

if(inArray(word,t))

return"t";

elseif(inArray(word,k))

return"k";

else

returnnull;

case2:

return"b";

case3:

return"z";

case4:

if(inArray(word,o))

return"o";

elseif(inArray(word,c))

return"c";

elseif(word.equals("="))

returnword;

else

returnnull;

case5:

returnword;

case6:

returnword;

default:

returnnull;

}

}

@Override

publicbooleanfenxi(inttype,Stringstr){

Stringstr1=stack.peek();//str1是棧頂符號

//str是當前輸入符號

if(!isN(str1)){

//棧頂符號是終結(jié)符

if(str1.equals("#")&&str.equals(str1)){

//System.out.println("語法分析通過");

returntrue;

}elseif(str1.equals(word2T(type,str))){

stack.pop();

returntrue;

}else{

returnfalse;

}

}else{

//棧頂符號是非終結(jié)符

Stringchanshengshi=map[n2I(str1)][t2I(word2T(type,str))];

if(chanshengshi==null)

returnfalse;

else{

stack.pop();

for(inti=chanshengshi.length()-1;i>=0;i--){

stack.push(String.valueOf(chanshengshi.charAt(i)));

}

//這里寫語義分析

returnfenxi(type,str);

}

}

}

}結(jié)果:對于輸入文件:輸出:對于輸入文件:輸出:實驗四遞歸下降分析法設計與實現(xiàn)實驗報告要求詳細說明遞歸下降分析法程序的工作過程,并且詳細說明你的程序的設計思路和實現(xiàn)。設計思路:根據(jù)文法寫幾個函數(shù)即可。這是這次實驗的文法(之前自己寫的文法畫出的LL(1)分析表實在太大了,畫不下去……就換了個實驗做):/*文法

E→T{+T}

T→F{*F}

F→i

F→(E)

*/實現(xiàn):importjava.util.Scanner;

/**

*Createdby溫睿誠on2016/6/12/0012.

*/

publicclassDiGuiDown{

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論