标签:style blog ar io color os sp for on
1 // lex_analyzer.cpp : 定义控制台应用程序的入口点。 2 //词法分析器 3 /* 4 sys: 5 1-10:关键字 6 10:标识符 7 11:数字 8 //比较区 9 20:< 10 21:<= 11 22:> 12 23:>= 13 24:= 14 25:== 15 //运算区 16 26:* 17 27:/ 18 28:+ 19 29:- 20 //分隔符号区 21 30:; 22 31:( 23 32:) 24 33:{ 25 34:} 26 35:\n 27 -1 28 1000:# 29 */ 30 #include "stdafx.h" 31 #include <iostream> 32 #include <sstream> 33 #include <string> 34 using namespace std; 35 string ss; 36 int scanner(); 37 const string key[] = {"int","main","return","if","for"};//定义关键字 38 stringstream prog; 39 int _tmain(int argc, _TCHAR* argv[]) 40 { 41 char ch = NULL; 42 stringstream s; 43 int row=0;//行号 44 int id_code;//识别码 45 cout << "please input some C/C++ code" << endl; 46 while (ch != ‘#‘) 47 { 48 cin.get(ch); 49 s << ch; 50 } 51 ss = s.str(); 52 do{ 53 id_code= scanner(); 54 switch (id_code) 55 { 56 case -1: cout << "Error in row " << row << "!" << endl; break; 57 case 35: row = row++; break; 58 case 1000:break; 59 default: cout << "(" << id_code << "," << prog.str() << ")" << endl; break; 60 break; 61 } 62 } while (id_code !=1000); 63 return 0; 64 } 65 int scanner() 66 { 67 static int point=0; 68 prog.str("");//清空 69 char cha = NULL; 70 int syn; 71 //过滤掉空格 72 do 73 { 74 cha = ss[point++]; 75 } while (cha == ‘ ‘); 76 //识别标识符或关键字,标识符以_或字母开头 77 if (cha >= ‘a‘&&cha <= ‘z‘ || cha >= ‘A‘&&cha <= ‘Z‘ || cha == ‘_‘) 78 { 79 while (cha >= ‘a‘&&cha <= ‘z‘ || cha >= ‘A‘&&cha <= ‘Z‘ || cha == ‘_‘ || cha >= ‘0‘&&cha <= ‘9‘) 80 { 81 prog << cha; 82 cha = ss[point++]; 83 } 84 --point; 85 syn = 10; 86 //识别关键字 87 for (int i = 0; i < sizeof(key) / sizeof(key[0]); i++) 88 { 89 if (prog.str() == key[i]) 90 syn = i; 91 } 92 } 93 //识别数字 94 else if (cha >= ‘0‘&&cha <= ‘9‘) 95 { 96 while (cha >= ‘0‘&&cha <= ‘9‘) 97 { 98 prog << cha; 99 cha = ss[point++]; 100 } 101 --point; 102 syn = 11; 103 } 104 else switch (cha) 105 { 106 case ‘<‘: 107 prog << cha; 108 cha = ss[point++]; 109 if (cha == ‘=‘) 110 { 111 syn = 21; 112 prog << cha; 113 }else { 114 syn = 20; 115 --point; 116 } 117 break; 118 case ‘>‘: 119 prog << cha; 120 cha = ss[point++]; 121 if (cha == ‘=‘) 122 { 123 syn = 23; 124 prog << cha; 125 } 126 else { 127 syn = 22; 128 --point; 129 } 130 break; 131 case ‘=‘: 132 prog << cha; 133 cha = ss[point++]; 134 if (cha == ‘=‘) 135 { 136 syn = 25; 137 prog << cha; 138 } 139 else { 140 syn = 24; 141 --point; 142 } 143 break; 144 case‘*‘:syn = 26; prog << cha; break; 145 case‘/‘:syn = 27; prog << cha; break; 146 case‘+‘:syn = 28; prog << cha; break; 147 case‘-‘:syn = 29; prog << cha; break; 148 case‘;‘:syn = 30; prog << cha; break; 149 case‘(‘:syn = 31; prog << cha; break; 150 case‘)‘:syn = 32; prog << cha; break; 151 case‘{‘:syn = 33; prog << cha; break; 152 case‘}‘:syn = 34; prog << cha; break; 153 case‘\n‘:syn = 35; break; 154 case ‘#‘:syn = 1000; prog << cha; break; 155 default: syn = -1; break; 156 } 157 return syn; 158 }
标签:style blog ar io color os sp for on
原文地址:http://www.cnblogs.com/ccode/p/4167833.html