我正在尝试用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;
}
答案 0 :(得分:3)
首先要注意的是,当您获得一个字符并将其与EOF进行比较时,必须将字符保存在 int 中,而不是保存在字符中,所以
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)