实验目的:对循环语句和条件判断语句编写词法分析编译程序,只能通过一遍扫描完成。(用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;
}
“`