[flang-dev] Question on the parser code

Timothy Keith via flang-dev flang-dev at lists.llvm.org
Tue Dec 3 15:44:15 PST 2019


You can make a minimal MainProgram like this:

  auto mainProgram{MainProgram{
    std::optional<Statement<ProgramStmt>>{},
    SpecificationPart{
      std::list<OpenMPDeclarativeConstruct>{},
      std::list<Statement<Indirection<UseStmt>>>{},
      std::list<Statement<Indirection<ImportStmt>>>{},
      ImplicitPart{std::list<ImplicitPartStmt>{}},
      std::list<DeclarationConstruct>{}},
    ExecutionPart{std::list<ExecutionPartConstruct>{}},
    std::optional<InternalSubprogramPart>{},
    Statement{std::nullopt, EndProgramStmt{std::optional<Name>{}}}}};

Then make an empty Program and add the MainProgram to it:

  auto program{Program{std::list<ProgramUnit>{}}};
  program.v.emplace_back(Indirection<MainProgram>{std::move(mainProgram)});

Tim

From: flang-dev <flang-dev-bounces at lists.llvm.org> on behalf of Mark Dewing via flang-dev <flang-dev at lists.llvm.org>
Reply-To: Mark Dewing <markdewing at gmail.com>
Date: Tuesday, December 3, 2019 at 2:26 PM
To: "flang-dev at lists.llvm.org" <flang-dev at lists.llvm.org>
Subject: [flang-dev] Question on the parser code

Hi,
  I'm trying to understand the F18 parser and the parse tree code by creating some simple examples.   I would like to create a simple parser and also create a parse tree programmatically, but I'm running into some problems.

For the first step, I added a constructor to ParseState that accepts a std::string (to avoid constructing a CookedSource object).  The following code works:

#include "token-parsers.h"
using namespace Fortran::parser;
int main()
{
    std::string test("b");
    ParseState p(test);
    auto p1 =  "a"_ch;
    auto res = p1.Parse(p);
    std::cout << " res = " << res.value_or("no result") << std::endl;
    return 0;
}

For the second step, I would like to create a parse tree programmatically and call unparse.
The first problem is I am unable to build a MainProgram with a minimal set of inputs.
So far, the code to create some of the elements is as follows:

    SpecificationPart spec{std::list<OpenMPDeclarativeConstruct>{},
                           std::list<Statement<Fortran::common::Indirection<UseStmt>>>{} ,
                           std::list<Statement<Fortran::common::Indirection<ImportStmt>>>{},
                           ImplicitPart{std::list<ImplicitPartStmt>{}},
                           std::list<DeclarationConstruct>{}};

    ExecutionPart exec{std::list<ExecutionPartConstruct>{}};

    Statement<EndProgramStmt> end{Statement<EndProgramStmt>{std::nullopt, EndProgramStmt{std::nullopt}}};

The problem is constructing the tuple for the MainProgram  - all these objects cause compile errors when placed in a tuple ("no matching constructor" errors).  If anyone has better example code, or instructions on how to do this properly, I would appreciate it.

Thanks,
Mark Dewing

-----------------------------------------------------------------------------------
This email message is for the sole use of the intended recipient(s) and may contain
confidential information.  Any unauthorized review, use, disclosure or distribution
is prohibited.  If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
-----------------------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/flang-dev/attachments/20191203/c250dd5f/attachment-0001.html>


More information about the flang-dev mailing list