Extra fields in C++ AST classes

12 views
Skip to first unread message

Léo Sarrazin

unread,
Apr 26, 2020, 11:03:26 PM4/26/20
to BNFC Developers and Users
Is there a standard way to add fields to AST classes in C++? for example a reference resolution or the inferred type of an expression. I did not found any article or paper about that on the net.

Andreas Abel

unread,
Apr 27, 2020, 5:06:00 AM4/27/20
to bnfc...@googlegroups.com, Léo Sarrazin
No, there is no such mechanism in BNFC. I used a workaround where I
patch the BNFC-generated parent classes automatically (in the Makefile)
after they have been generated. By "parent class" I mean for instance
"Exp", when the child classes are the particular forms of an expression,
like EInt, and EAdd, link in the LBNF grammar:

EInt. Exp ::= Integer;
EAdd. Exp ::= Exp "+" Exp;

Léo Sarrazin

unread,
Apr 27, 2020, 9:22:00 AM4/27/20
to BNFC Developers and Users
I try to know the best way to do things. Actually i use 2 visitors, one for pre-referencing classes and namespaces in the language, and another visitor to type check expressions or check other kind of language rules. The first one take a pointer to the second to fill intern states that i need for the second pass, i dont know if this is the best way to do it.
If you have any other advice for the backend in C++ you welcome, thank you for the tip!

Andreas Abel

unread,
Apr 27, 2020, 1:11:43 PM4/27/20
to bnfc...@googlegroups.com, Léo Sarrazin
I am afraid I do not have enough insight in your problem to give
specific advice.

You might consider to use the BNFC-generated AST only for parsing, and
roll your own syntax trees for later passes.

Léo Sarrazin

unread,
Apr 27, 2020, 2:39:08 PM4/27/20
to BNFC Developers and Users
Reconstructing the entire ast would be overkill i think, i will consider your first advice and try to do something. Anyway if someone have documents or other references about this topic im more than interrested.
Reply all
Reply to author
Forward
0 new messages