我的lex文件文件中出现以下错误我不知道为什么会发生这种错误 关于返回解析器的每个标记
lexical.l: In function âyylexâ:
lexical.l:29: error: expected expression before â;â token
lexical.l:30: error: âCALLâ undeclared (first use in this function)
lexical.l:30: error: (Each undeclared identifier is reported only once
lexical.l:30: error: for each function it appears in.)
lexical.l:31: error: âIDâ undeclared (first use in this function)
lexical.l:32: error: âNUMâ undeclared (first use in this function)
lexical.l:36: error: âRELOPâ undeclared (first use in this function)
lexical.l:37: error: âADDOPâ undeclared (first use in this function)
lexical.l:38: error: âMULOPâ undeclared (first use in this function)
lexical.l:39: error: âASSIGNâ undeclared (first use in this function)
lexical.l:40: error: âANDâ undeclared (first use in this function)
lexical.l:41: error: âORâ undeclared (first use in this function)
lexical.l:42: error: âNOTâ undeclared (first use in this function)
这是我的lex文件,用于将令牌发送到解析器
%{
#include <stdio.h>
#include"bison.tab.h"
void showToken(char*);
%}
%option yylineno
%option noyywrap
digit [0-9]
letter [a-zA-Z]
whitespace [ \t]
%%
"else" {return ELSE;}
"real" {return REAL;}
"integer" {return INTEGER;}
"write" {return XWRITE;}
"while" {return WHILE;}
"end" {return END;}
"do" {return DO;}
"if" {return IF;}
"then" {return THEN;}
"program" {return XPROGRAM;}
"function" {return FUNCTION;}
"return" {return XRETURN;}
"read" {return XREAD;}
"var" {return VAR;}
"for" {return FOR;}
"begin" {return BEGIN;}
"call" {return CALL;}
{letter}({letter}|{digit})* {return ID;}
{digit}{digit}*(("."{digit}{digit}*)?) {return NUM;}
{digit}{digit}*({letter}|{digit})* printf("Lexical Error");
[(),:;.] return yytext[0];
^[\t ]+ ;
(==|<>|<|<=|>|>=) {return RELOP;}
(\+|-) {return ADDOP;}
(\*|\/) {return MULOP;}
(=) {return ASSIGN;}
(&&) {return AND;}
(\|\|) {return OR;}
(!) {return NOT;}
{whitespace}+ ;
"/*".*"*/" ;
. {
printf("Lexical Error");
exit(0);
}
%%
void showToken(char* name){
printf("<%s,%s>",name,yytext);
}
如果返回的代币是大写字母还是无关紧要?
我应该在这里包含解析器文件还是只包含tab.c文件?
答案 0 :(得分:1)
您不能使用名称BEGIN
作为令牌名称,因为BEGIN
已经#define
作为flex的其他内容。我不知道为什么会产生第一个错误(第29行),但你应该先尝试修复它,如果没有帮助,请粘贴bison.tab.h
。
答案 1 :(得分:1)
错误告诉您,您尝试返回的各种令牌(CALL
,ID
,NUM
,RELOP
等)尚未定义。您要包含bison.tab.h
文件,这意味着您在%token
文件中缺少.y
这些令牌的声明。