Andry
Andry

Reputation: 16845

Can't get through simple Bison grammar file syntax error on %define directive

I am using Bison 2.4.1 and Flex 2.5.35 to create a language parser. However I am stuck with Bison which has been giving me the same error for 3 hours and I am not understanding the reason why.

I have this Bison grammar file 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

As you can see I am trying to define the grammar and the grammar semantics as well. And it is not like I coded everything on my own, I followed the example reported in the Bison Docs - %define directive.

Before semantics, everything ok: Also please note that my grammar rules have no actions defined so far. So they are just a bunch of very simple rules. Furthermore, before adding semantics, I processed the file through the Bison parser generator and everything went smoothly.

When I try to put this thing through Bison:

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

I get this error:

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

The line reported in the error is the one where the %define directive is located at. I do not understand how to get through this. What am I doing wrong? Thank you.

Upvotes: 2

Views: 934

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726589

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

The feature that you are trying to use has been added in Bison 3.0.

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

You can download Bison 3.0 from here.

Upvotes: 1

Related Questions