#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define MAXN 20000
int syn, p, sum, kk, m, n, row, error;
double dsum, pos;
char index[800], len;
char r[6][10] = {"function", "if", "then", "while", "do", "endfunc"};
char token[MAXN], s[MAXN];
char ch;
bool is_letter(char c)
{
return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z';
}
bool is_digtial(char c)
{
return c >= '0' && c <= '9';
}
bool is_dot(char c)
{
return c == ',' || c == ';';
}
void identifier()// 标示符的判断
{
m = 0;
while (ch >= 'a' && ch <= 'z' || ch >= '0' && ch <= '9')
{
token[m++] = ch;
ch = s[++p];
}
token[m] = '\0';
ch = s[--p];
syn = 10;
for (n = 0; n < 6; n++)
if (strcmp(token, r[n]) == 0)
{
syn = n+1;
break;
}
}
void digit(bool positive)// 数字的判断
{
len = sum = 0;
ch = s[p];
while (ch >= '0' && ch <= '9')
{
sum = sum * 10 + ch - '0';
ch = s[++p];
}
if (ch == '.')
{
dsum = sum;
syn = 12;
ch = s[++p];
pos = 0.1;
while (ch >= '0' && ch <= '9')
{
dsum = dsum + (ch - '0') * pos;
pos = pos * 0.1;
ch = s[++p];
}
if (ch == 'e')
{
index[len++] = ch;
ch = s[++p];
if (ch == '-' || ch == '+')
{
index[len++] = ch;
ch = s[++p];
}
if (!(ch >= '0' && ch <= '9'))
{
//cout<<"Looooooooooook"<<endl;
syn = -1;
}
else
{
while (ch >= '0' && ch <= '9')
{
index[len++] = ch;
ch = s[++p];
}
}
}
if (syn == -1 || (ch >= 'a' && ch <= 'z') || ch == '.')
{
syn = -1; // 对数字开头的标识符进行判错。
while (ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'z' || ch == '.') ch = s[++p];
ch = s[--p];
}
else
{
ch = s[--p];
syn = 12;
if (!positive) dsum *= -1.0;
}
}
else
{
if (ch >= 'a' && ch <= 'z')
{
syn = -1; // 对数字开头的标识符进行判错。
while (ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'z') ch = s[++p];
ch = s[--p];
}
else
{
ch = s[--p];
syn = 11;
if (!positive) sum *= -1;
}
}
}
void To_double()
{
int i;
printf("(%d,%lf", syn, dsum);
if (len)
{
for (i = 0; i < len; i++) printf("%c", index[i]);
len = 0;
}
printf(")\n");
}
bool check_behind_digit()
{
int i = p + 1;
while (s[i] != '\0')
{
if (s[i] >= '0' && s[i] <= '9') return true;
else if (s[i] != ' ' && s[i] != '\t' && s[i] != '\n') return false;
i++;
}
return false;
}
bool check_pre_prog()
{
int i = p - 1;
while (i >= 0)
{
if (s[i] != ' ' && s[i] != '\t' && s[i] != '\n')
{
if (s[i] >= '0' && s[i] <= '9' || s[i] >= 'a' && s[i] <= 'z') return false;
else return true;
}
i--;
}
return true;
}
void pot()
{
while (1)
{
ch = s[++p];
if (ch == '\n')
row++;
if (!(ch == ' ' || ch == '\t' || ch == '\n'))
break;
}
}
void scaner()
{
memset(token, 0, sizeof(token));
pot();
if (is_letter(ch))
{
syn = 10;
identifier();
}
else if (is_digtial(ch))
{
digit(true);
}
else
{
if (ch == '<')
{
len = 0, token[len++] = ch;
if (s[p + 1] == '=')
{
syn = 22;
token[len++] = s[p + 1];
p++;
}
else syn = 20;
token[len] = 0;
}
else if (ch == '>')
{
len = 0, token[len++] = ch;
if (s[p + 1] == '=')
{
syn = 24;
token[len++] = s[p + 1];
p++;
}
else syn = 23;
token[len] = 0;
}
else if (ch == '=')
{
len = 0, token[len++] = ch;
if (s[p + 1] == '=')
{
syn = 25;
token[len++] = s[p + 1];
p++;
}
else syn = 18;
token[len] = 0;
}
else if (ch == '!')
{
len = 0, token[len++] = ch;
if (s[p + 1] == '=')
{
syn = 22;
token[len++] = s[p + 1];
p++;
}
else syn = -1;
}
else if (ch == '+')
{
if (check_behind_digit() && check_pre_prog())
{
pot();
digit(true);
}
else
{
syn = 13;
token[0] = ch;
}
}
else if (ch == '-')
{
if (check_behind_digit() && check_pre_prog())
{
pot();
digit(false);
}
else
{
syn = 13;
token[0] = ch;
}
}
else if (ch == '*') syn = 15, strcpy(token, "*");
else if (ch == '/') syn = 16, strcpy(token, "/");
else if (ch == ';') syn = 26, strcpy(token, ";");
else if (ch == '(') syn = 27, strcpy(token, "(");
else if (ch == ')') syn = 2
评论0