BNFC / bnfc

BNF Converter
http://bnfc.digitalgrammars.com/
583 stars 162 forks source link

Option to not use `new` in C++ #293

Open s5bug opened 4 years ago

s5bug commented 4 years ago

I would love it if there was an option on BNFC to parse into either a std::shared_ptr/std::unique_ptr or to stack allocate classes because it's been a chore to track down everything that's been parsed and properly delete it.

andreasabel commented 4 years ago

I am not proficient enough in C++ to attempt an implementation, but PRs are welcome!

s5bug commented 4 years ago

I don't know that much about the tools BNFC uses to generate C++ code, but I'll do my best.

andreasabel commented 3 years ago

BNFC does not use a lot of tools; it simply prints concrete syntax, maybe with the help of the Text.Pretty pretty printing library.

bbyalcinkaya commented 3 years ago

Makefiles for C++ backend have --ansi flag and C++98 does not support shared_ptr/unique_ptr. Is it safe to switch to C++11?

andreasabel commented 3 years ago

Makefiles for C++ backend have --ansi flag and C++98 does not support shared_ptr/unique_ptr. Is it safe to switch to C++11?

Well, if there is an option

to parse into either a std::shared_ptr/std::unique_ptr

then this option would output a Makefile with the appropriate flags (like picking the necessary C++ version).

So, short answer: yes.

hangingman commented 3 years ago

I would like to do this issue if I have time... Currently BNFC generates C++ codes like delete someInstance instead of std::unique_ptr

andreasabel commented 3 years ago

I would like to do this issue if I have time...

Great!

A first step could be to write up an example how the product should look like (e.g. by modifying some BNFC-generated C++ output). I cannot help here much since I am not an active C++ programmer.

Once the design is complete, it should not be hard to get it into BNFC. I can help with the Haskell part, if needed.

hangingman commented 2 years ago

I created a PR. If it's no problem, I would like to try to modify Absyn.C/H to use smart-pointer. bnfc generating C++ code using inheritance, so this maybe a bit difficult. However, if it's successfully done, no need to use delete() function.

Clone pattern for std::shared_ptr in C++

following part will be changed.

Prog *Prog::clone() const
{
  return new Prog(*this);
}
hangingman commented 2 years ago

@andreasabel I created second pull-request at #410 I gave up using std::unique_ptr, then use std::shared_ptr. Because it's more applicable for bnfc usecase. Please take a look it.

EDIT: I would like to ask about what unit tests should be added in the bnfc code-base, because I'm afraid of breaking bnfc current behavior. We all don't like regression bug.

hangingman commented 2 years ago

@andreasabel Hi, I updated PR again. It took much time, sorry. https://github.com/BNFC/bnfc/pull/410

hangingman commented 1 year ago

@andreasabel Hi, long time no see. Is there a chance that my pull request (#410) will be merged? If regression is a problem, I can divide the pull request into small ones or make a pull request to be test functions only.

If additional function is not necessary, or it can not be maintained. Anyway, I wish to close or merge that.