akimd / bison

GNU Bison
GNU General Public License v3.0
332 stars 89 forks source link

Incorrect D code generated when enabling locations. #81

Closed streaksu closed 2 years ago

streaksu commented 2 years ago

Hello,

When generating a parser for the following file with location support:

%language "D"
%define api.parser.class {Parser}
%locations

/* Types the parser handles for return and input */
%union {
    AST           tast;
    Declaration[] tdecls;
    Declaration   tdecl;
    Statement[]   tstmts;
    Statement     tstmt;
    Type          ttype;
    string        str;
    char          chr;
    long          lng;
    double        dbl;
}

/* Single-char token declarations */
%token SEMICOLON    ";"
       PERIOD       "."
       OPENPAREN    "("
       CLOSEPAREN   ")"
       OPENBRACE    "{"
       CLOSEBRACE   "}"
       OPENBRACKET  "["
       CLOSEBRACKET "]"
       PLUS         "+"
       MINUS        "-"
       ASTERISK     "*"
       SLASH        "/"

/* Keywords */
%token RETURN "return"
       VOID   "void"
       INT    "int"
       FLOAT  "float"
       CHAR   "char"
       STRING "string"

/* Values */
%token <str> IDENTIFIER STRINGVALUE
%token <chr> CHARACTER
%token <lng> INTEGER
%token <dbl> FLOATVALUE

%%
ast : /* Empty*/
    | declarations
    ;

declarations : declaration
             | declaration declarations
             ;

declaration : type IDENTIFIER "(" ")" "{" statements "}"
            ;

statements : statement
           | statement statements
           ;

statement : "return" ";"
          ;

type : "void"
     ;
%%

struct AST {}
class Declaration {}
class Statement   {}
final class Function : Declaration {}
final class Return : Statement {}
enum Type {}

Compiling the generated code yields the following error, which does not happen when not enabling locations:

cdc.p/parser.d(764): Error: function `parser.Parser.YYStack.push(int state, YYSemanticType value, ref YYLocation loc)` is not callable using argument types `(int, immutable(YYSemanticType), YYLocation)`
cdc.p/parser.d(764):        cannot pass argument `yy_semantic_null` of type `immutable(YYSemanticType)` to parameter `YYSemanticType value`
cdc.p/parser.d(765): Error: function `parser.Parser.YYStack.push(int state, YYSemanticType value, ref YYLocation loc)` is not callable using argument types `(int, immutable(YYSemanticType), YYLocation)`
cdc.p/parser.d(765):        cannot pass argument `yy_semantic_null` of type `immutable(YYSemanticType)` to parameter `YYSemanticType value`

using ldc2.

Removing the user-defined classes from the union seems to fix it, but this cannot be done in the original code as they serve a purpose there.

What is there at fault here? What could be a fix for this issue? Thanks in advance.

streaksu commented 2 years ago

Moving it to bug-bison@gnu.org, sorry for not noticing the other channel for bug reporting.