语法分析_编译原理

实验目的:对循环语句和条件判断语句编写词法分析编译程序,只能通过一遍扫描完成。(用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); 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8


我的代码

#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;
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274

 
 
  • 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值