野牛解析器错误

时间:2012-12-10 19:54:14

标签: parsing bison lex flex-lexer lexical-analysis

我的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文件?

2 个答案:

答案 0 :(得分:1)

您不能使用名称BEGIN作为令牌名称,因为BEGIN已经#define作为flex的其他内容。我不知道为什么会产生第一个错误(第29行),但你应该先尝试修复它,如果没有帮助,请粘贴bison.tab.h

答案 1 :(得分:1)

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