我正在实现我用标准C编码的简单扫描仪的 lex 版本。我遇到的问题是 I/O 的行为不像我在 lex 文件中所期望的那样。fscanf
不会存储字符串,并将整数存储为 0 在我指示的变量中。fgetc 返回我的测试文件中不存在的字符。我下面的代码中是否有任何东西(或缺少的东西)跳出来说明为什么会发生这种情况?我使用 lex 的方式完全错误吗?
扫描仪-lf.l:
%{
#include <stdio.h>
#include <stdlib.h>
extern int lineno;
extern int number;
extern char string[];
extern FILE *yyin;
#define INTEGER 1
#define FLOAT 2
#define READ 3
#define WRITE 4
#define ID 5
#define LPAREN 6
#define RPAREN 7
#define PLUS 8
#define MINUS 9
#define MULT 10
#define ASSIGN 11
#define DIV 12
#define COMMENT 13
#define ERROR 14
%}
%%
[ \t] {
printf("whitespace\n");
}
[0-9]* {
fscanf(yyin, "%d", &number);
printf("%d\n", number);
int c = fgetc(yyin);
printf("%c", c);
/*return INTEGER;*/
}
[a-zA-Z][a-zA-Z0-9]* {
fscanf(yyin, "%s", string);
printf("%s\n", string);
/*return ID;*/
}
扫描仪-lex.c:
#include <stdio.h>
#include <stdlib.h>
/* A couple of globals */
int lineno = 0;
int number;
char string[100];
FILE *yyin;
char charSet[] = { '(', ')', '+', '-', '*' };
#define INTEGER 1
#define FLOAT 2
#define READ 3
#define WRITE 4
#define ID 5
#define LPAREN 6
#define RPAREN 7
#define PLUS 8
#define MINUS 9
#define MULT 10
#define ASSIGN 11
#define DIV 12
#define COMMENT 13
#define ERROR 14
int yywrap(){
}
int main(int argc, char **argv){
int rc;
if (argc > 1){
yyin = fopen(argv[1], "r");
while ((rc=yylex())){
switch (rc){
case READ:
printf("found a read\n");
break;
case WRITE:
printf("found a write\n");
break;
case ID:
printf("found an id\n");
break;
case LPAREN:
printf("found an (\n");
break;
case RPAREN:
printf("found an )\n");
break;
case PLUS:
printf("found an +\n");
break;
case MINUS:
printf("found an -\n");
break;
case MULT:
printf("found an *\n");
break;
case ASSIGN:
printf("found an assign\n");
break;
case DIV:
printf("found a /\n");
break;
case INTEGER:
printf("found an integer\n");
break;
case FLOAT:
printf("found a float\n");
break;
case COMMENT:
printf("found a comment\n");
break;
case ERROR:
printf("Error\n");
break;
}
}
}
return 0;
}