[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Please, support easy AST generation
From: |
Kaz Kylheku |
Subject: |
Re: Please, support easy AST generation |
Date: |
Tue, 18 Dec 2018 11:18:40 -0800 |
User-agent: |
Roundcube Webmail/0.9.2 |
On 2018-12-08 16:45, Askar Safin wrote:
Hi. Often the only thing I want to do in Bison is just generate AST
and nothing else. Unfortunately, in this case code becomes very
repetitive.
I think the generation of *parse trees* can be automated.
AST's omit information from (abstract away from) the parse tree. There
are many ways to do that; it's not obvious how to automate it.
One problem with parse trees is that there are often nodes that are just
empty place holders for precedence levels and such. E.g. an "expression"
is a the same time a "term", which is also a "factor" which happens to
be a "number". In the AST, maybe I just want the number!
Like instead of (expression (term (factor (number 3)))), I want that
condensed to just 3.
Automating this condensing is probably a good research project.
That hierarchy of expression > term > factor > { number , ident } just
exists for the associativity-precedence parsing; it has nothing to do
with abstract syntax in which issues of associativity and precedence are
no longer relevant.
Moreover, certain terminal symbols also exist only for parsing.
The AST omits numerous terminal symbols as well as nonterminal symbols
that are found in the parse tree. E.g.: consider the rule:
expr : '(' expr ')'
Here, we want the AST-generating action to just be
{ $$ = $2; }
We do not introduce a new internal node, and we don't propagate the ( )
terminal symbols.
This is based on the knowledge that (expr) is semantically identical to
expr; it just has punctuators that influence parsing.