#include "test.h"
int main(int argc, char **argv)
{
struct Token token;
if (argc < 2) { printf("input source file!\n"); return -1; }
if (!InitScanner(argv[1])) {printf("Open source file ERROR !\n"); return -1; }
printf("type string value pointer \n");
printf("---------------------------------------------\n");
while (1) {
token = GetToken();
if (token.type != NONTOKEN) {
printf("%4d %12s %12f %12x\n",
token.type, token.lexeme, token.value, token.FuncPtr);
}
else break;
}
printf("---------------------------------------------\n");
CloseScanner();
return 0;
}
int InitScanner(char *fname)
{
fp = fopen(fname, "r");
if (fp) { return 1; }
else { fclose(fp); return 0; }
}
struct Token GetToken()
{
struct Token token = {ERRTOKEN, NULL, 0.0, NULL};
token.lexeme = TokenBuf;
char ch;
int i=0, j;
while ((ch = fgetc(fp)) != EOF){
if (isalpha(ch)) {
TokenBuf[i++] = toupper(ch);
if (!isalpha(fgetc(fp))) {
if (!feof(fp)) {
fseek(fp, -1L, 1);
}
TokenBuf[i] = '\0';
for (j = 0; j < 18; j++) {
if (!strcmp(TokenBuf, TokenTab[j].lexeme)){
return TokenTab[j];
}
}
return token;
}
else {
if (!feof(fp)) {
fseek(fp, -1L, 1);
}
}
}
else if (isdigit(ch)) {
TokenBuf[i++] = ch;
if (!isdigit(ch=fgetc(fp)) && ch != '.') {
if (!feof(fp)) {
fseek(fp, -1L, 1);
}
TokenBuf[i] = '\0';
token.value = atof(TokenBuf);
token.type = CONST_ID;
return token;
}
else {
if (!feof(fp)) {
fseek(fp, -1L, 1);
}
}
}
else {
switch (ch) {
case ';':
token.type = SEMICO;
TokenBuf[i++] = ch;
TokenBuf[i] = '\0';
return token;
case '+':
token.type = PLUS;
TokenBuf[i++] = ch;
TokenBuf[i] = '\0';
return token;
case ',':
token.type = COMMA;
TokenBuf[i++] = ch;
TokenBuf[i] = '\0';
return token;
case '(':
token.type = L_BRACKET;
TokenBuf[i++] = ch;
TokenBuf[i] = '\0';
return token;
case ')':
token.type = R_BRACKET;
TokenBuf[i++] = ch;
TokenBuf[i] = '\0';
return token;
case '.':
if(isdigit(fgetc(fp)) && isdigit(TokenBuf[i-1])) {
fseek(fp, -1L, 1);
TokenBuf[i++] = ch;
break;
}
else {
fseek(fp, -1L, 1);
return token;
}
case '*':
if (fgetc(fp) == '*') {
token.type = POWER;
TokenBuf[i++] = ch;
TokenBuf[i++] = ch;
TokenBuf[i] = '\0';
return token;
}
else {
if (!feof(fp)) {
fseek(fp, -1L, 1);
}
token.type = MUL;
TokenBuf[i++] = ch;
TokenBuf[i] = '\0';
return token;
}
case '-':
if (fgetc(fp) == '-') {
while ((ch = fgetc(fp)) != '\n' && ch != EOF);
break;
}
else {
if (!feof(fp)) {
fseek(fp, -1L, 1);
}
token.type = MINUS;
TokenBuf[i++] = ch;
TokenBuf[i] = '\0';
return token;
}
case '/':
if (fgetc(fp) == '/') {
while ((ch = fgetc(fp)) != '\n' && ch != EOF);
break;
}
else {
if (!feof(fp)) {
fseek(fp, -1L, 1);
}
token.type = DIV;
TokenBuf[i++] = ch;
TokenBuf[i] = '\0';
return token;
}
case '\t':
case '\n':
case ' ':
break;
default : return token;
}
}
}
token.type = NONTOKEN;
return token;
}
void CloseScanner()
{
fclose(fp);
}
词法分析器(编译原理)
最新推荐文章于 2024-06-11 17:27:40 发布