編譯原理詞法分析小程式的設計

2021-08-09 06:23:30 字數 1954 閱讀 7272

輸入一程式,結束符用」#」;

輸出單詞數對:《類號,序號》。 輸出識別符號表,用空格分隔; 輸出無符號整數表,用空格分隔;

input:main()#output:

<1,1>

<3,0>

<3,1>

<3,2>

<1,0>

<4,0>

<2,2>

<5,0>

<3,4>

<4,1>

<2,2>

<5,1>

<3,5>

<1,2>

<5,0>

<2,1>

<4,1>

<2,0>

<4,0>

<3,5>

<3,3>

identifieres:a b

unsigned_integer:2 3

#include 

#include

#include

using

namespace

std;

vector

catagory, index; //儲存輸出的類號、序號對

void store(int a, int b)

int main(),,,',3},,};

map keyword,,};

ch=cin.get();

while(ch!='#')

if(boundaries.count(ch)==1) store(3,boundaries[ch]);//識別分界符

//識別運算子

else

if(ch=='+')

else

if(ch=='*')

else

if(ch=='=') store(2,2);

//識別無符號整數

else

if(ch>='0'&&ch<='9')

int flag=0;

for(int i=0; i//無符號整數已經在整數表中存在的情況

if(integer[i]==temp)

}if(flag==0)

continue;

}//識別識別符號

else

if((ch>='a'&&ch<='z')||(ch>='a'&&ch<='z'))

if(keyword.count(temp)==1)

else

}if(flag==0)

}continue;

}ch=cin.get();

}for(int i=0; icout

<< "

<< catagory[i] << ","

<< index[i] << ">";

}cout

<< "\nidentifier:";

for(int i=0; iif(i==identifier.size()-1) cout

<< identifier[i] << "\n";

else

cout

<< identifier[i] << " ";

}cout

<< "unsigned_integer:";

for(int i=0; iif(i==integer.size()-1) cout

<< integer[i] << "\n";

else

cout

<< integer[i] << " ";}}

執行結果:

編譯原理詞法分析程式

include include char prog 80 token 8 char ch int syn,p,m,n,sum char rwtab 6 void scaner token m 0 p 回退乙個字元 syn 10 for n 0 n 6 n if strcmp token,rwtab ...

編譯原理詞法分析程式

include include char scanin 300 scanout 300 extern int testscan char scanin 300 scanout 300 file fin,fout 指向輸入輸出檔案的指標 int main include include include...

編譯原理詞法分析

編譯原理實驗一 詞法分析練習 include include include define tokenmax 100 define progmax 1000 define k esc 27 void analytics 詞法分析 void scanner 輸入掃瞄 bool isletter cha...