bug-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Please, support easy AST generation


From: Akim Demaille
Subject: Re: Please, support easy AST generation
Date: Sun, 9 Dec 2018 07:17:44 +0100


> Le 9 déc. 2018 à 07:05, Akim Demaille <address@hidden> a écrit :
> 
>> For example, this is Bison input I used to generate AST for small JavaScript 
>> subset: 
>> https://zerobin.net/?b9af68c9aa7a31a9#JB4wZseYTq9aKfZOwwZrDqBNCqlEZRj/+DM9bKdgtKU=
>>  .

Nicely written!

        • bse_FunctionStatementList:
        •   %empty
        •     {
        •     }
        • | bse_StatementList
        •     {
        •       $$ = std::move ($bse_StatementList);
        •     }
        •  
        • bse_Script:
        •   bse_FunctionStatementList
        •     {
        •       ast = std::move ($bse_FunctionStatementList);
        •     }
        •  

I think Bison should not force you to have the empty action
for an %empty RHS.  That's wrong, in particular in the case
of C++, since $$ is already perfect.

But you should not do anything in the case of $$ = $1.
Leave that to Bison.

        • %{
        • #include <err.h>
        •  
        • #include "parser.tab.hpp"
        •  
        • #define MS(type, alternative, ...) std::make_shared<type> 
(std::in_place_index<(std::size_t)type ## _k::alternative> __VA_OPT__(,) 
__VA_ARGS__)
        •  
        • int
        • yylex (yy::parser::semantic_type *yylval);
        •  
        • void
        • yy::parser::error (const std::string &m)
        • {
        •   warnx ("%s", m.c_str ());
        • }
        •  
        • std::vector<std::shared_ptr<const s_stmtdecl>> ast;
        • %}

Avoid %{...%}, prefer %code.  And don't include the parser's header, let Bison 
do its job, you might break something.  And a global to store the result???


reply via email to

[Prev in Thread] Current Thread [Next in Thread]