无法通过 %define 指令上的简单 Bison 语法文件语法错误

标签 c bison

我正在使用 Bison 2.4.1 和 Flex 2.5.35 创建语言解析器。然而,我被 Bison 困住了,它一直给我同样的错误 3 个小时,我不明白原因。

我有这个 Bison 语法文件mylang.y:

%{
#include <stdio.h>
#include <stdlib.h>
#include <glib/gtree.h>
#include <glib/gnode.h>
#include "ast.h"
%}

%code requires {
struct my_value {
  enum {
    is_int, 
    is_str
  } kind;
  union {
    int ival;
    char *sval;
  } u;
};
}
%define api.value.type {struct my_value}

%token KEYWORD
%token <u.sval> ID
%token <y.ival> NUM
...
%%
... 
// Rules following, but still defining no action at all.
%%
// Nothing more

如您所见,我也在尝试定义语法和语法语义。并不是我自己编写了所有代码,我遵循了 Bison Docs - %define directive 中报告的示例.

在语义之前,一切正常:另外请注意,我的语法规则到目前为止还没有定义任何 Action 。所以它们只是一堆非常简单的规则。此外,在添加语义之前,我通过 Bison 解析器生成器处理了文件,一切都很顺利。

当我尝试将这个东西通过 Bison 时:

bison  -vd --report=state,itemset --graph  mylang.y

我收到这个错误:

mylang.y:20.24-40: syntax error, unexpected {...}

错误中报告的行是 %define 指令所在的行。 我不明白如何度过难关。我究竟做错了什么? 谢谢。

最佳答案

I am using Bison 2.4.1 and Flex 2.5.35 to create a language parser.

您尝试使用的功能已添加到 Bison 3.0 中。

History: introduced in Bison 3.0. Was introduced for Java only in 2.3b as stype.

您可以下载 Bison 3.0 from here .

关于无法通过 %define 指令上的简单 Bison 语法文件语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19333096/

相关文章:

python - 我可以从 C main() 程序返回一个字符串吗

c - 如何修复 C 代码中的段错误错误

c++ - Makefile:无需执行任何操作

c - yylineno 给出错误报告的意外结果

c++ - Bison 可以解析 UTF-8 字符吗?

bison - 如何在 Windows 上使用 flex?

c - OpenMP 错误 : 'X' is predetermined 'shared' for 'private'

C - 对结构指针数组进行排序比直接对结构进行排序 (qsort)

比较c中字符串数组中的数据

compiler-errors - 如何在 Bison 码中解决此错误?