Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed

Conversation

hangingman
Copy link

ref #293

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

PR contents

  • Makefile changes:

    • If bnfc received --ansi flag, Makefile would have -std=c++14 compile option otherwise --ansi.
    • If bnfc not received --ansi flag, bnfc will generate .ll and .yy files
  • Fix PARSER_HEADER_FILE

    • C++ source should have same name interface like "psProgram" in header file and source file. But, currently it looks Parser.H doesn't match function name.
  • C++ AST/Test code generator switching

    • If bnfc not received --ansi flag, bnfc would genrate codes using smart-pointer (std::unique_ptr), otherwise bnfc generate codes same as before.

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;
};
  • Impl classes have each copy constructors
    • I referred the book "More Effective C++" to implement it.
class Prog : public Program
{
private:
  std::unique_ptr<ListStatement> liststatement_;

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<Program> clone() const override;
};
  • [WIP] Bison/Flex code genrator switching

    • Still work in progress, but code might require Bison3.2
    • 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
  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;
  }

* Modify include file and Parser header file name as psXXX
* Add compile option to `src/BNFC/Backend/CPP/Makefile.hs`
* Implement switching bison %union and variant
* Implement C++ parser with driver setting
* Implement C++ driver/scanner class
* Fix file extension
* Fix token type
* Fix flex buffer related code
* Refactor makefile compile option
* Implement reverse function in ListXXX class
* add driver entry codes
* modify SkelSTL.hs for header file extension
* Enable debugging of lexer/parser
@hangingman hangingman closed this Jan 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant