Always C++

简单词法分析程序

  前段时间编译原理的实验..写个简单的词法分析程序.下面是代码
1 #include<iostream> 2 #include<fstream> 3 #include<string> 4 #include<vector> 5 #include<stdlib.h> 6 7 using namespace std; 8 9 string src=""; 10 unsigned int begin = 0; 11 int variable = 0; //记录单词种别 12 13 vector<string> rew; // 保留字 14 vector<char> dl; //界符 15 vector<char> op; //操作符 16 vector<string> num; //常数 17 vector<string> ide; //标识符 18 19 //将文件中的保留字导出到rew容器中. 20 void inputReservedWord() 21 { 22 ifstream infile; 23 infile.open("表2-reservedWord.txt"); 24 if(!infile) 25 { 26 cout << "Cannot open the named file." << endl; 27 return; 28 } 29 string s=""; 30 while(!infile.eof()) 31 { 32 infile>>s; 33 rew.push_back(s); 34 } 35 infile.close(); 36 } 37 38 //将文件中的标识符导出到ide容器中. 39 void inputIdentifier() 40 { 41 ifstream infile; 42 infile.open("表1-identifier.txt"); 43 if(!infile) 44 { 45 cout << "Cannot open the named file." << endl; 46 return; 47 } 48 string s=""; 49 while(!infile.eof()) 50 { 51 infile>>s; 52 ide.push_back(s); 53 } 54 infile.close(); 55 } 56 57 //将文件中的常数导出到num容器中. 58 void inputNumber() 59 { 60 ifstream infile; 61 infile.open("表3-number.txt"); 62 if(!infile) 63 { 64 cout << "Cannot open the named file." << endl; 65 return; 66 } 67 string s=""; 68 while(!infile.eof()) 69 { 70 infile>>s; 71 num.push_back(s); 72 } 73 infile.close(); 74 } 75 76 //将文件中的运算符导出到op容器中. 77 void inputOperator() 78 { 79 ifstream infile; 80 infile.open("表4-operator.txt"); 81 if(!infile) 82 { 83 cout << "Cannot open the named file." << endl; 84 return; 85 } 86 char c; 87 while(!infile.eof()) 88 { 89 infile.get(c); 90 op.push_back(c); 91 } 92 infile.close(); 93 } 94 95 //将文件中的界符导出到dl容器中. 96 void inputDelimiter() 97 { 98 ifstream infile; 99 infile.open("表5-delimiter.txt"); 100 if(!infile) 101 { 102 cout << "Cannot open the named file." << endl; 103 return; 104 } 105 char c; 106 while(!infile.eof()) 107 { 108 infile.get(c); 109 dl.push_back(c); 110 } 111 infile.close(); 112 } 113 114 //判断是不是保留字 115 bool isReservedWord(string s) 116 { 117 bool flag = false; 118 for(vector<string>::size_type i = 0;i < rew.size();++i) 119 { 120 if(!s.compare(rew[i])) 121 { 122 variable = 2; 123 flag = true; 124 break; 125 } 126 } 127 return flag; 128 } 129 130 //判断标识符或者保留字的开头字符 131 bool isIdentifierOrReservedWordChar(char c) 132 { 133 if((c>='a' && c<='z') || (c>='A' && c<='Z') || c=='_') 134 return true; 135 else return false; 136 } 137 138 //获得以下标i开头的标识符或者保留字 139 string getIdentifierOrReservedWordChar(int i) 140 { 141 if((src[i]>='a' && src[i]<='z') || (src[i]>='A' && src[i]<='Z') || src[i]=='_') 142 { 143 ++i; 144 } 145 while((src[i]>='a' && src[i]<='z') || (src[i]>='A' && src[i]<='Z') || 146 (src[i]>='0' && src[i]<='9')) 147 { 148 ++i; 149 } 150 string word=src.substr(begin,(i-begin)); 151 begin=i-1; 152 return word; 153 } 154 155 //判断是不是标识符 156 bool isIdentifier(char c) 157 { 158 if((c>='a' && c<='z') || (c>='A' && c<='Z')) 159 { 160 // variable = 1; 161 return true; 162 } 163 else return false; 164 } 165 166 //判断是不是常数 167 bool isNumber(char c) 168 { 169 if(c>='0' && c<='9') 170 { 171 variable = 3; 172 return true; 173 } 174 else return false; 175 } 176 177 //获得以下标i开始的常数,并判断中间是否出现字符 178 string getNumber(int i) 179 { 180 while(src[i]>='0' && src[i]<='9') 181 { 182 ++i; 183 } 184 string word=src.substr(begin,(i-begin)); 185 begin = i-1; 186 return word; 187 } 188 189 //判断是不是操作符 190 bool isOperator(char c) 191 { 192 bool flag=false; 193 for(vector<char>::size_type i=0;i<op.size();++i) 194 { 195 if(c==op[i]) 196 { 197 variable = 4; 198 flag=true; 199 break; 200 } 201 } 202 return flag; 203 } 204 205 //判断是不是界符 206 bool isDelimiter(char c) 207 { 208 bool flag=false; 209 for(vector<char>::size_type i=0;i<dl.size();++i) 210 { 211 if(c == dl[i]) 212 { 213 variable = 5; 214 flag = true; 215 break; 216 } 217 } 218 return flag; 219 } 220 221 void main() 222 { 223 ifstream infile; 224 ofstream outfile; 225 ofstream result; 226 result.open("result.txt"); 227 if(!result) 228 { 229 cout<<"Cannot open the named file."<<endl; 230 } 231 inputReservedWord(); 232 inputIdentifier(); 233 inputNumber(); 234 inputOperator(); 235 inputDelimiter(); 236 infile.open("test.txt"); 237 if(!infile) 238 { 239 cout << "Cannot open the named file." << endl; 240 return; 241 } 242 char ch = ' '; 243 string s; 244 char csrc; 245 while (!infile.eof()) //得到源程序文本 246 { 247 infile.get(csrc); 248 s+= csrc; 249 csrc = ' '; 250 } 251 src = s; 252 infile.close(); 253 cout<<src<<endl; 254 255 ch = ' '; 256 while(begin<src.length()) 257 { 258 while(src[begin]==' ' || src[begin]=='\\n') //消除空格和回车 259 { 260 ++begin; 261 } 262 ch = src[begin]; 263 if(isIdentifierOrReservedWordChar(ch)) 264 { 265 string s = getIdentifierOrReservedWordChar(begin); 266 if(isReservedWord(s)) //s为保留字 267 { 268 vector<string>::size_type i= 0; 269 while(s!=rew[i] && i<rew.size()) 270 { 271 i++; 272 } 273 cout<<s<<" "<<variable<<" "<<i<<endl; 274 result<<s<<" "<<variable<<" "<<i<<endl; 275 } 276 else //s为标识符 277 { 278 variable = 1; 279 vector<string>::size_type i=1; 280 while(s!=ide[i-1] && i<ide.size()) 281 { 282 ++i; 283 } 284 if(i>=ide.size() || ide.empty()) 285 { 286 outfile.open("表1-identifier.txt",ios::app); 287 outfile.write(s.c_str(),s.length()); 288 outfile<<endl; 289 ide.push_back(s); 290 i=ide.size(); 291 outfile.close(); 292 } 293 cout<<s<<" "<<variable<<" "<<i<<endl; 294 result<<s<<" "<<variable<<" "<<i<<endl; 295 } 296 } 297 else if(isNumber(ch)) 298 { 299 vector<string>::size_type i = 1; 300 string s = getNumber(begin); 301 while(s!=num[i-1] && i<num.size()) 302 { 303 ++i; 304 } 305 if(i>=num.size() || num.empty()) 306 { 307 outfile.open("表3-number.txt",ios::app); 308 outfile.write(s.c_str(),s.length()); 309 outfile<<endl; 310 ide.push_back(s); 311 i=ide.size(); 312 outfile.close(); 313 } 314 cout<<s<<" "<<variable<<" "<<i<<endl; 315 result<<s<<" "<<variable<<" "<<i<<endl; 316 } 317 else if(isOperator(ch)) 318 { 319 int i = 0; 320 while(ch!=op[i]) 321 { 322 i++; 323 } 324 cout<<ch<<" "<<variable<<" "<<i<<endl; 325 result<<ch<<" "<<variable<<" "<<i<<endl; 326 } 327 else if(isDelimiter(ch)) 328 { 329 int i = 0; 330 while(ch!=dl[i]) 331 { 332 i++; 333 } 334 cout<<ch<<" "<<variable<<" "<<i<<endl; 335 result<<ch<<" "<<variable<<" "<<i<<endl; 336 } 337 else 338 { 339 cout<<"Unkown char!"<<endl; 340 } 341 ++begin; 342 } 343 }

需包含文件 表2-reservedWord.txt(存放保留字表) ,表1-identifier.txt(存放标识符表),表3-number.txt(存放常量表),表4-operator.txt(存放操作符表),表5-delimiter.txt(存放界符表),test.txt(存放测试代码),result.txt(存放分析结果) 代码缺点太多..欢迎大家大力批评..
在VC++6.0上运行会有4个warning.不影响结果.

posted on 2009-11-02 21:57 Amelie 阅读(580) 评论(2)  编辑 收藏 引用 所属分类: Always C++

评论

# re: 简单词法分析程序 2009-11-04 11:57 sink

呵呵~你都已经是友情链接了,还提交友情链接~真有意思  回复  更多评论   

# re: 简单词法分析程序 2009-11-06 21:33 Amelie

@sink
那是之前的..春得死!  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理


导航

<2026年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

统计

常用链接

留言簿

文章分类

文章档案

不可思议

搜索

积分与排名

最新评论