#include<iostream>
#include<string>
#include<map>
using namespace std;
map<int, string>Key_words, Symbols;//存储关键字,存储算符和界符
string S[1000], Ans[1000];//存储输入,输出
int cnt = 0, wrong_cnt = 0, wrong_line[1000];//存储有效行数量,错误行数量,错误行记录
bool f = 1;//注释标记
void Initialize_Key_words() {//初始化关键字
Key_words[3] = "void", Key_words[4] = "int", Key_words[5] = "float", Key_words[6] = "char", Key_words[7] = "if", Key_words[8] = "else",
Key_words[9] = "while", Key_words[10] = "do", Key_words[11] = "for", Key_words[12] = "return", Key_words[13] = "main";
}
void Initialize_Symbols() {//初始化算符和界符
Symbols[14] = "+", Symbols[15] = "-", Symbols[16] = "*", Symbols[17] = "/", Symbols[18] = "%", Symbols[19] = "<",
Symbols[20] = "<=", Symbols[21] = ">", Symbols[22] = ">=", Symbols[23] = "==", Symbols[24] = "!=", Symbols[25] = "&&",
Symbols[26] = "||", Symbols[27] = "=", Symbols[28] = "(", Symbols[29] = ")", Symbols[30] = "[", Symbols[31] = "]",
Symbols[32] = "{", Symbols[33] = "}", Symbols[34] = ";", Symbols[35] = ",";
}
int isKey_words(string s) {//判断是否为关键字
for (int i = 3; i <= 13; i++)if (Key_words[i] == s)return i;
return -1;
}
int isSymbol(string s) {//判断是否为算符和界符
for (int i = 14; i <= 35; i++)if (Symbols[i] == s)return i;
return -1;
}
string isNum(string s) {//判断是否为数字
for (int i = 0; i < s.size(); i++)
if (s[i] >= '0' && s[i] <= '9');
else return "-1";
return s;
}
string isLetter(string s) {//判断是否为字母
for (int i = 0; i < s.size(); i++)
if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'));
else return "-1";
return s;
}
string isIdentifier(string s) {//判断是否是标识符
if ((s[0] >= 'a' && s[0] <= 'z') || (s[0] >= 'A' && s[0] <= 'Z') || s[0] == '_')return s;
else return "-1";
}
void check(string temp, int cnt) {//检测每个单词并存储答案
string print;
if (isKey_words(temp) != -1)print += "<", print += to_string(isKey_words(temp)), print += ",->,";
else if (isSymbol(temp) != -1)print += "<", print += to_string(isSymbol(temp)), print += ",->,";
else if (isNum(temp) != "-1")print += "<2,", print += temp, print += ">,";
else if (isLetter(temp) != "-1")print += "<1,", print += temp, print += ">,";
else {
if (isIdentifier(temp) != "-1")print += "<1,", print += temp, print += ">,";
else print += "LexicalError,", wrong_line[wrong_cnt++] = cnt + 1;
}
Ans[cnt] += print;
}
bool fun(int cnt) {//词法分析处理
//预处理单词
string temp;
for (int i = 0; i < S[cnt].size(); i++) {
string ch;
ch += S[cnt][i];//提取每个字符
if (isSymbol(ch) != -1) {//将界符和算符分成单独的单词
if (ch == "<" || ch == ">" || ch == "=") {//特判字符长度为二的界符和算符
if (i + 1 < S[cnt].size() && S[cnt][i + 1] == '=' && f)temp += ' ', temp += ch, temp += '=', temp += ' ', i++;
else if (f)temp += ' ', temp += ch, temp += ' ';
}
else if (ch == "/") {//特判注释
if (i + 1 < S[cnt].size() && S[cnt][i + 1] == '/')goto loop;//遇到"//"就跳过后面单词的识别
else if (i + 1 < S[cnt].size() && S[cnt][i + 1] == '*')f = 0;//遇到"/*"就封锁输入
}
else if (ch == "*" && i + 1 < S[cnt].size() && S[cnt][i + 1] == '/')f = 1;//直到遇到"*/"正常输入
else if (f)temp += ' ', temp += ch, temp += ' ';
}
else if (ch == "!" && i + 1 < S[cnt].size() && S[cnt][i + 1] == '=' && f)temp += ' ', temp += ch, temp += '=', temp += ' ', i++;//特判!=
else if (ch == "&" && i + 1 < S[cnt].size() && S[cnt][i + 1] == '&' && f)temp += ' ', temp += ch, temp += '&', temp += ' ', i++;//特判&&
else if (ch == "|" && i + 1 < S[cnt].size() && S[cnt][i + 1] == '|' && f)temp += ' ', temp += ch, temp += '|', temp += ' ', i++;//特判||
else if (f)temp += S[cnt][i];
}
loop:
temp += ' '; if (temp == " ")return 0;
//拆单词
string slice;
for (int i = 0; i < temp.size(); i++) {
if (temp[i] != ' ')slice += temp[i];//遇到连续非空字符就提取成一个单词
else if (slice.size() != 0) {
check(slice, cnt);//检测该单词
slice = "";
}
}
return 1;
}
int main() {
Initialize_Key_words(), Initialize_Symbols();//初始化
while (1) {
getline(cin, S[cnt]);//按行输入
if (S[cnt] == "#")break;
if (S[cnt].size() != 0 && fun(cnt))cnt++;//去掉注释后,如果该行字符长度不为零,则行数加以
}
for (int i = 0; i < cnt; i++)cout << Ans[i] << endl;//输出答案
if (wrong_cnt != 0) {//如果有错误行,输出错误行序号
cout << "LexicalError(s) on line(s) ";
for (int i = 0; i < wrong_cnt; i++)cout << wrong_line[i] << ",";
}
return 0;
}