withdigit那里判断小数或者科学计数法的,但是现在只考虑整数就改掉了
#include <iostream>
#include<stdlib.h>
#include <cstring>
#include <ctype.h>
#include<cstdlib>
using namespace std;
class TOKEN{
public:
int typenum = 0;
// string type = "";
string symb = "";
int value = 0;
void print(){
if (typenum == -1)
return;
// printf("<%d,\t%s>\n", typenum, symb);
if (typenum == 111 || typenum == -2){
cout << '<' << typenum << ",\t" << symb << '>' << endl;
}
else if(typenum == 100){
cout << '<' << typenum << ",\t" << value << '>' << endl;
}
else {
cout << '<' << typenum << ",\t->" << endl;
}
}
};
int judge(char a){ //judeg symbol
if (isdigit(a)){
return 1;
}
else if (isalnum(a)){
return 2;
}
return 0;
}
int withdigit(char a){ // follow num
if (isdigit(a)){
return 1;
}
if (a == '.'){
return 0;
}
if (a == 'e' || a == 'E'){
return 0;
}
return 0;
}
// int find(char a, TOKEN &t){
// switch (a)
// {
// case '+':
// t.typenum = 41;
// t.symb = "+";
// break;
// default:
// t.typenum = -2;
// t.symb = "?";
// break;
// }
// }
void selfjudge(TOKEN &a){
if (a.symb == "int"){
a.typenum = 5;
}
else if (a.symb == "else"){
a.typenum = 15;
}
else if (a.symb == "if"){
a.typenum = 17;
}
else if (a.symb == "while"){
a.typenum = 20;
}
}
TOKEN lexer(char temp){
TOKEN target;
int flag = 1;
char next;
if (isdigit(temp)){ //num
flag = 0;
target.symb += temp;
while (1){
temp = getchar();
if (!withdigit(temp)){
flag = 1;
break;
}
target.symb += temp;
}
if (flag == 1){
ungetc(temp, stdin);
}
target.typenum = 100;
target.value = atoi(target.symb.c_str());
}
else if (judge(temp) == 2){ // all words
flag = 0;
target.symb += temp;
while (1){
temp = getchar();
if (judge(temp) == 0){
flag = 1;
break;
}
target.symb += temp;
}
if (flag == 1){
ungetc(temp, stdin);
}
target.typenum = 111;
}
else if (temp == ' ' || temp == '\n'){ // blank
target.typenum = -1;
}
else{ // not word
// find(temp, target);
switch (temp)
{
case '+':
next = getchar();
if (next == '+'){
target.typenum = 56;
target.symb = "++";
break;
}
ungetc(next, stdin);
target.typenum = 41;
target.symb = "+";
break;
case '-':
next = getchar();
if (next == '-'){
target.typenum = 57;
target.symb = "--";
break;
}
ungetc(next, stdin);
target.typenum = 42;
target.symb = "-";
break;
case '*':
target.typenum = 43;
target.symb = "*";
break;
case '/':
target.typenum = 44;
target.symb = "/";
break;
case '%':
target.typenum = 45;
target.symb = "%";
break;
case '>':
next = getchar();
if (next == '='){
target.typenum = 48;
target.symb = ">=";
break;
}
ungetc(next, stdin);
target.typenum = 47;
target.symb = ">";
break;
case '<':
next = getchar();
if (next == '='){
target.typenum = 50;
target.symb = "<=";
break;
}
ungetc(next, stdin);
target.typenum = 49;
target.symb = "<";
break;
case '=':
next = getchar();
if (next == '='){
target.typenum = 50;
target.symb = "==";
break;
}
ungetc(next, stdin);
target.typenum = 44;
target.symb = "=";
break;
case '!':
next = getchar();
if (next == '='){
target.typenum = 52;
target.symb = "!=";
break;
}
ungetc(next, stdin);
target.typenum = 55;
target.symb = "!";
break;
case '&':
next = getchar();
if (next == '&'){
target.typenum = 53;
target.symb = "&&";
break;
}
ungetc(next, stdin);
target.typenum = -2;
target.symb = "err";
break;
case '|':
next = getchar();
if (next == '|'){
target.typenum = 54;
target.symb = "||";
break;
}
ungetc(next, stdin);
target.typenum = -2;
target.symb = "err";
break;
case '(':
target.typenum = 81;
target.symb = ")";
break;
case ')':
target.typenum = 82;
target.symb = ")";
break;
case ';':
target.typenum = 84;
target.symb = ";";
break;
case '{':
target.typenum = 86;
target.symb = "{";
break;
case '}':
target.typenum = 87;
target.symb = "}";
break;
case '[':
target.typenum = 88;
target.symb = "[";
break;
case ']':
target.typenum = 89;
target.symb = "]";
break;
default:
target.typenum = -2;
target.symb = "err";
break;
}
}
selfjudge(target);
return target;
}
int main(){
char a;
while (a=getchar())
{
if (a != EOF) {
TOKEN token = lexer(a);
token.print();
}
}
return 1;
}
4895

被折叠的 条评论
为什么被折叠?



