1

我开始使用 Bison/YACC 和 Flex/Lex,但我无法编译最简单的解析器。

文件:Ruby.y

%{ 
#include <stdio.h>
#include <stdlib.h> 
%}

%start program
%token NUMBER

%%

program : NUMBER;

%%

main( int argc, char* argv[] ) {
    yyparse();
} 

yyerror(char *s){
    printf("%s\n", s);
}

文件:Ruby.l

%{
#define "Ruby.tab.h"
%}
DIGIT   [0-9]
%%
{DIGIT}+    { return(NUMBER);           }
[ \t\n]+
.           { return(yytext[0]);        }
%%

我使用“Bison -vd Ruby.y”编译Ruby.y,然后使用“Flex Ruby.l”,然后尝试使用带有“GCC -c Ruby.tab.c”和“GCC -c lex”的GCC编译整个东西。 yy.c”,但我在后者上收到以下错误:

Ruby.l:2:9:错误:宏名称必须是标识符 Ruby.l:在函数“yylex”中:Ruby.l:6:10:错误:未声明“NUMBER”(在此函数中首次使用)Ruby.l: 6:10:注意:每个未声明的标识符对于它出现的每个函数只报告一次

我一无所知,有什么办法吗?

谢谢你。

4

1 回答 1

0

在 Ruby.l 文件中:

%{
#define "Ruby.tab.h"
%}

应该:

%{
#include "Ruby.tab.h"
%}
于 2012-06-01T01:52:49.500 回答