實驗目的:對循環語句和條件判斷語句編寫詞法分析編譯程序,只能通過一遍掃描完成。(用c++實現)
實驗要求:
(1)關鍵字:
for if then else while do
所有關鍵字都是小寫。
(2)運算符和分隔符:
: = + - * / < > <= <> >= ; ( ) #
(3)其他標識符(ID)和整型常數(NUM),通過以下正規式定義:
ID=letter(letter | digit)*
NUM=digit digit*
(4)空格由空白、製表符和換行符組成。空格一般用來分隔ID、NUM、運算符、分隔符和關鍵字,詞法分析階段通常被忽略。
(5) 具有一定的出錯處理功能。法單元對應的詞法記號如下:
> 詞法單元 詞法記號 詞法單元 詞法記號 for 1 : 17 if 2 := 18 then 3 < 20 else 4 <> 21
> while 5 <= 22 do 6 > 23 letter(letter+digit)* 10 >= 24 digit digit*
> 11 = 25
> + 13 ; 26
> - 14 ( 27
> * 15 ) 28 / 16 # 0
>
> 詞法單元 詞法記號 詞法單元 詞法記號 for 1 : 17 if 2 := 18 then 3 < 20 else 4 <> 21
> while 5 <= 22 do 6 > 23 letter(letter+digit)* 10 >= 24 digit digit*
> 11 = 25
> + 13 ; 26
> - 14 ( 27
> * 15 ) 28 / 16 # 0
詞法分析程序的功能
輸入:源程序
輸出:二元組(詞法記號,屬性值/其在符號表中的位置)構成的序列。
例如:對源程序
x:=5; if (x>0) then x:=2*x+1/3; else x:=2/x; #
經詞法分析後輸出如下序列:
(10,’x’)(18, :=) (11,5) (26, ;) (2, if ) (27,( )……
> 1.幾點說明: (1)關鍵字表的初值。 關鍵字作爲特殊標識符處理,把它們預先安排在一張表格中(稱爲關鍵字表),當掃描程序識別出標識符,查關鍵字表。如能查到匹配的單詞,則該單詞的關鍵字,否則爲一般標識符。關鍵表爲一個字符串數組,其描述如下:
> char *keyword[6]={”for”, ”if”, ”then” ,”else”,”while”, ”do” };
>
> (2)程序中需要用到的主要變量爲 token , id和num. 1)id用來存放構成詞法單元的字符串;
> 2)num用來存放整數(可以擴展到浮點數和科學計數法表示); 3)token用來存放詞法單元的詞法記號。
可以參考下面的do{
lexical(); //將詞法單元對應的記號保存到token中,屬性值保存到num或者id中
switch(token) {
case 11: printf ("(token, %d\n) ", num); break;
case -1: printf("error!\n");break;
default: printf("(%d,%s)\n", token, id);
}
}while (token!=0);
我的代碼
#include <iostream>
#include<string>
#include<map>
#include<sstream>
#include <fstream>
#include <list>
#define max 100
using namespace std;
static map<string ,int> dictionary;
void init()
{
dictionary.insert(map<string,int>::value_type("for",1));
dictionary.insert(map<string,int>::value_type("if",2));
dictionary.insert(map<string,int>::value_type("then",3));
dictionary.insert(map<string,int>::value_type("else",4));
dictionary.insert(map<string,int>::value_type("while",5));
dictionary.insert(map<string,int>::value_type("do",6));
dictionary.insert(map<string,int>::value_type("var",10));
dictionary.insert(map<string,int>::value_type("num",11));
dictionary.insert(map<string,int>::value_type("+",13));
dictionary.insert(map<string,int>::value_type("-",14));
dictionary.insert(map<string,int>::value_type("*",15));
dictionary.insert(map<string,int>::value_type("/",16));
dictionary.insert(map<string,int>::value_type(":",17));
dictionary.insert(map<string,int>::value_type(":=",18));
dictionary.insert(map<string,int>::value_type("<",19));
dictionary.insert(map<string,int>::value_type("<>",21));
dictionary.insert(map<string,int>::value_type("<=",22));
dictionary.insert(map<string,int>::value_type(">",23));
dictionary.insert(map<string,int>::value_type(">=",24));
dictionary.insert(map<string,int>::value_type("=",25));
dictionary.insert(map<string,int>::value_type(";",26));
dictionary.insert(map<string,int>::value_type("(",27));
dictionary.insert(map<string,int>::value_type(")",28));
dictionary.insert(map<string,int>::value_type("#",0));
}
int findbykey(string key)
{
map<string,int >::iterator l_it;;
l_it=dictionary.find(key);
if(l_it==dictionary.end())
return -1;
else
return l_it->second;
}
string keyword[6]={"for","if","then","else","while","do"};
bool isletter(char a)
{
if((a>='a'&&a<='z')||(a>='A'&&a<='Z'))
{
return true;
}
else
{
return false;
}
}
bool isdigit(char a)
{
if(a>='0'&&a<='9')
{
return true;
}
else
{
return false;
}
}
bool iskeyw(string keywords)
{
for(int i=0;i<6;i++)
{
if(keyword[i]==keywords)
{
return true;
}
}
return false;
}
bool isvar(string var) //ID=letter(letter | digit)*
{
if(isletter(var[0]))
{
for(int i=0;i<var.length();i++)
{
if(isletter(var[i])||isdigit(var[i]))
{
}
else
{
return false;
}
}
return true;
}
else
{
return false;
}
}
bool isnum(string num) //NUM=digit digit* (xiaoshudian
{
if(isdigit(num[0]))
{
int flag1=1;
int flag2=1;
for(int i=0;i<num.length();i++)
{
if(isdigit(num[i]))
{
}
else if(num[i]=='.'&&isdigit(num[i+1])&&flag1)
{
flag1=0;
}
else if (((num[i]=='E'||num[i]=='e')&&(num[i+1]=='+'||num[i+1]=='-'||isdigit(num[i+1]))&&flag2))
{
cout<<num[i]<<"dddd"<<endl;
flag2=0;
}
else if((num[i]=='+'||num[i]=='-')&&isdigit(num[i+1]))
{
}
else
{
return false;
}
}
return true;
}
else
{
return false;
}
}
string to_String(int n)
{
string temp;
stringstream ss;
ss<<n;
temp = ss.str();
return temp;
}
string packet(string test,int type)
{
int a;
if(type==0)
{
a=findbykey(test);
}
else if(type==1)
{
a=findbykey("var");
}
else if(type==2)
{
a=findbykey("num");
}
string req="";
string aa;
aa=to_String(a);
req+="(" + aa;
req+=",";
req+=test;
req+=") ";
return req;
}
string texthandle(string test,int linenum)
{
if(iskeyw(test))
{
return packet(test,0);
}
else if(isvar(test))
{
return packet(test,1);
}
else if(isnum(test))
{
return packet(test,2);
}
else if(-1==findbykey(test))
{
string b="There are some errors in";
string bb;
bb= to_String(linenum);
b+=bb;
return b;
}
else
{
return packet(test,0);
}
}
void File_output(char* filename)
{
int linenum=0;
string test;
fstream file;
string pice;
string expression="";
list<string> words;
file.open(filename,ios_base::in|ios_base::out) ;
if(!file)
{
cout<<"error"<<endl;
}
else
{
while(getline(file, test))
{
linenum++;
//處理邏輯
/*
劃分單詞,. 標點
*/
string temp="";
for(int i=0;i<test.length();i++)
{
if( test[i] == ' ' )
{
}
else
{
temp+=test[i];
if(test[i+1]==' '||test[i+1]=='\0')
{
words.push_back(temp);
pice=texthandle(temp,linenum);
expression+="\n";
expression+=pice;
temp="";
}
}
}
}
//對單詞鏈表進行處理
//list<string>::iterator i;
//for (i = words.begin(); i != words.end(); ++i)
//測試
// cout<<*i<<endl;
cout<<expression<<endl;
}
}
int main()
{
init();
File_output("a.txt");
// int b =findbykey("for");
//cout<<b<<endl;
return 0;
}
“`