BNFC / bnfc

BNF Converter
http://bnfc.digitalgrammars.com/
582 stars 161 forks source link

Use smart-pointer and generate C++ code for bison/flex/bnfc #409

Closed hangingman closed 2 years ago

hangingman commented 2 years ago

ref #293

PR description: I modified C++ code related with issue #293 .

PR contents

Sample code:

use using instead of typedef

/********************   TypeDef Section    ********************/

using Integer = int;
using Char = char;
using Double = double;
using String = std::string;
using Ident = std::string;

clone() returns instance wrapped std::unique_ptr

/********************   Abstract Syntax Classes    ********************/

class Program : public Visitable
{
public:
  virtual std::unique_ptr<Program> clone() const = 0;
};

public: // for "std::move" Prog(Prog&& rhs); Prog& operator=(Prog&& rhs); // for normal copy Prog(const Prog& rhs); Prog& operator=(const Prog& rhs); Prog(const ListStatement& p1); ~Prog(); virtual void accept(Visitor *v); std::unique_ptr clone() const override; };


- [WIP] Bison/Flex code genrator switching
  - Still work in progress, but code might require [Bison3.2](https://lwn.net/Articles/770138/)
  - I think I will modify process switching some code in bison

- Use smart pointer in test C++ source
  - This enables test.C to use unique_ptr, however this is just a example. I think this kind of smart-pointer can apply for other C++ code generated by bnfc also. To use smart pointer, I added `#include <memory>`.
  - will generate following code
```cpp
  if (parse_tree)
  {
    printf("\nParse Successful!\n");
    if (!quiet) {
      printf("\n[Abstract Syntax]\n");
      std::unique_ptr<ShowAbsyn> s(new ShowAbsyn());
      printf("%s\n\n", s->show(parse_tree));
      printf("[Linearized Tree]\n");
      std::unique_ptr<PrintAbsyn> p(new PrintAbsyn());
      printf("%s\n\n", p->print(parse_tree));
    }
    delete(parse_tree);
    return 0;
  }