PierreBdR
PierreBdR

Reputation: 43264

Partially parse C++ for a domain-specific language

I would like to create a domain specific language as an augmented-C++ language. I will need mostly two types of contructs:

The language will be used for scientific computing purposes, and will ultimately be translated into plain C++. C++ has been chosen as it seems to offer a good compromise between: ease of use, efficiency and availability of a wide range of libraries.

A previous attempt using flex and bison failed due to the complexity of the C++ syntax. The existing parser can still fail on some constructs. So we want to start over, but on better bases.

Do you know about similar projects? And if you attempted to do so, what tools would you use? What would be the main pitfalls? Would you have recommendations in term of syntax?

Upvotes: 6

Views: 1494

Answers (5)

Ira Baxter
Ira Baxter

Reputation: 95354

If you really want to extend C++, you'll need a full C++ parser plus name and type resolution. As you've found out, this is pretty hard. Your best solution is to get an existing one and modify it.

Our DMS Software Reengineering Toolkit is an infrastructure for implementing langauge processors. It is designed to support the construction of tools that parse languages, carry out transformations, and spit out the same language (with enhanced code) or a different language/dialect.

DMS has a full C++ Front End, that parses C++, builds abstract syntax trees and symbol tables (e.g., all that name and type resolution stuff).

The DMS/C++ front end is provided with DMS in source form, so that it can be customized to achieve the kind of effect you want. You'd define your DSL as an extension of the C++ front end, and then write transformations that convert your special constructs into "vanilla" C++ constructs, and then spit out compilable result.

DMS/C++ have been used for a wide variety of transformation tasks, including ones that involved extending C++ as you've described, and including tasks that carry out massive reorganizations of large C++ applications. (See the Publications at that website).

Upvotes: 0

Matthieu M.
Matthieu M.

Reputation: 299900

There are many (clever) attempts to have domain specific languages within the C++ language.

It's usually called DSEL for Domain Specific Embedded Language. For example, you could look up the Boost.Spirit syntax, or Boost.rdb (in the boost vault).

Those are fully compliant C++ libraries which make use of C++ syntax.

If you want to hide some complexity, you might add in a few macros.

I would be happy to provide some examples if you gave us something to work with :)

Upvotes: 3

SK-logic
SK-logic

Reputation: 9715

You can try extending an open source Elsa C++ parser (it is now a part of a Mozilla's Pork project):

https://wiki.mozilla.org/Pork

Upvotes: 2

anon
anon

Reputation:

The way to extend C++ is not to try to extend the language, which will be extremely difficult and probably break as new base compiler releases implement new features, but to write class libraries to support your problem domain. This has been what C++ programming has been all about since the language's inception.

Upvotes: 0

Didier Trosset
Didier Trosset

Reputation: 37437

To solve you first bullet, maybe you can use C++0x new features "initializer lists", and "user defined litterals" avoiding the need for a new parser. They may help for the second bullet, too.

Upvotes: -1

Related Questions