我正在尝试用C语言编写一个词法分析器,我已经尝试逐步完成代码,看看问题出在哪里,但我看不到它。此代码从文件中读取一行,假设第一行,文件中没有其他内容。所以我用"a = (b + 2) * c"
测试了它。
它工作,并打印出a
,=
但没有任何东西。我认为它在我的switch
函数中的lookup()
语句有问题,因为它似乎与UNKNOWN
中的任何lex()
都没有问题。任何见解都会有所帮助和赞赏。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int charClass;
char lexeme[100];
char nextChar;
int lexLen;
int token;
int nextToken;
FILE *fp;
void addChar();
void getChar();
void getNonBlank();
int lex();
#define LETTER 0
#define DIGIT 1
#define UNKNOWN 99
#define INT_LIT 10
#define IDENT 11
#define ASSIGN_OP 20
#define ADD_OP 21
#define SUB_OP 22
#define MULT_OP 23
#define DIV_OP 24
#define LEFT_PARENT 25
#define RIGHT_PARENT 26
int main(int argc, char *argv[])
{
fp = fopen(argv[1], "r");
if (fopen == NULL)
printf("File can not be opened");
else {
getChar();
while (nextToken != EOF) {
lex();
}
}
return 0;
}
int lookup(char ch) {
switch (ch) {
case '(':
addChar();
nextToken = LEFT_PARENT;
break;
case ')':
addChar();
nextToken = RIGHT_PARENT;
break;
case '+':
addChar();
nextToken = ADD_OP;
break;
case '-':
addChar();
nextToken = SUB_OP;
break;
case '*':
addChar();
nextToken = MULT_OP;
break;
case '/':
addChar();
nextToken = DIV_OP;
break;
default:
addChar();
nextToken = EOF;
}
return nextToken;
}
void addChar() {
if (lexLen <= 98) {
lexeme[lexLen++] = nextChar;
lexeme[lexLen] = 0;
} else
printf("Error- lexele is too long...\n");
}
void getChar() {
if ((nextChar = getc(fp)) != EOF) {
if (isalpha(nextChar))
charClass = LETTER;
else if(isdigit(nextChar))
charClass = DIGIT;
else
charClass = UNKNOWN;
} else
charClass =EOF;
}
void getNonBlank() {
while (isspace(nextChar))
getChar();
}
int lex() {
lexLen = 0;
getNonBlank();
switch (charClass) {
case LETTER:
addChar();
getChar();
while (charClass == LETTER || charClass == DIGIT) {
addChar();
getChar();
}
nextToken = IDENT;
break;
case DIGIT:
addChar();
getChar();
while (charClass == DIGIT) {
addChar();
getChar();
}
nextToken = INT_LIT;
break;
case UNKNOWN:
lookup(nextChar);
getChar();
break;
case EOF:
nextToken = EOF;
lexeme[0] = 'E';
lexeme[1] = 'O';
lexeme[2] = 'F';
lexeme[3] = 0;
}
printf("Next token is :%d, next lexeme is %s\n", nextToken, lexeme);
return nextToken;
}
第一个注意事项,当你得到一个字符并希望将它与EOF进行比较时,你必须将字符保存在int中,而不是在char中,所以
void getChar()
{
if((nextChar=getc(fp))!=EOF)
{
if(isalpha(nextChar))
charClass=LETTER;
else if(isdigit(nextChar))
charClass=DIGIT;
else
charClass=UNKNOWN;
}
else
charClass=EOF;
}
不好管理EOF案例,因为nextChar是一个char,可以是:
void getChar()
{
if((charClass=getc(fp))!=EOF)
{
nextChar = charClass;
if(isalpha(nextChar))
charClass=LETTER;
else if(isdigit(nextChar))
charClass=DIGIT;
else
charClass=UNKNOWN;
}
}
第二个评论是你忘记管理案例'=',所以我认为在lookup(char ch)
你必须添加案例:
case '=':addChar();
nextToken=ASSIGN_OP;
break;
这就是你读'='后停下来的原因。
如果我做这些改变:
pi@raspberrypi:/tmp $ gcc -g -Wextra q.c
pi@raspberrypi:/tmp $ cat in
a = (b + 2) * c
pi@raspberrypi:/tmp $ ./a.out in
Next token is :11, next lexeme is a
Next token is :20, next lexeme is =
Next token is :25, next lexeme is (
Next token is :11, next lexeme is b
Next token is :21, next lexeme is +
Next token is :10, next lexeme is 2
Next token is :26, next lexeme is )
Next token is :23, next lexeme is *
Next token is :11, next lexeme is c
^C
我必须杀死执行因为程序循环,这是因为在getNonBlank()
中EOF案例不受管理,所以:
void getNonBlank()
{
while((charClass != EOF) && isspace(nextChar))
getChar();
}
在那之后改变:
pi@raspberrypi:/tmp $ ./a.out in
Next token is :11, next lexeme is a
Next token is :20, next lexeme is =
Next token is :25, next lexeme is (
Next token is :11, next lexeme is b
Next token is :21, next lexeme is +
Next token is :10, next lexeme is 2
Next token is :26, next lexeme is )
Next token is :23, next lexeme is *
Next token is :11, next lexeme is c
Next token is :-1, next lexeme is EOF
正如chqrlie在一篇评论中所说的那样,if(fopen == NULL)
也取代了if (fp == NULL)