1

我在我的 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 文件就足够了?

4

3 回答 3

1

您不能将该名称BEGIN用作标记名称,因为flexBEGIN已经将其作为其他名称。#define我不知道为什么会在第一个错误(第 29 行)之后产生错误,但您应该先尝试修复它,如果没有帮助,请粘贴bison.tab.h.

于 2012-12-10T23:10:11.797 回答
1

这些错误告诉您,您尝试返回的各种标记(CALLIDNUMRELOP等)尚未定义。您包含该bison.tab.h文件,这意味着您在文件中缺少%token这些标记的声明.y

于 2012-12-11T05:06:07.037 回答
0

我遇到了类似的问题-未声明的令牌(在此函数中首次使用)。所以我尝试将我的 lex 和 yacc 文件复制到另一个文件夹并执行 bison 和 flex 命令并再次编译文件,问题得到解决。

于 2022-02-19T05:06:44.877 回答