Mahesh
Mahesh

Reputation: 5308

Defining dialects for a DSL using ANTLR

I am building a simple DSL to validate FIX messages using ANTLRv4. My grammar currently supports dialect 1 below (mostly comparison operators), and I am looking to additionally support dialect 2.

Is it possible to define grammars for dialects that inherit and/or extend from the parent grammar?

For instance, other developers using my grammar should be able to define their own dialects and express rules that obey that dialect, without having to write the entire DSL from scratch.

Rules in dialect 1:

tag 9 > 0 tag 59 = "A"

Same rules in dialect 2:

tag 9 must be greater than 0 tag 59 must be equal to "A"

Upvotes: 0

Views: 159

Answers (2)

Burt_Harris
Burt_Harris

Reputation: 6874

Antlr4 includes a mechanism called imports to let you break up a grammar into logical and reusable chunks. When you import one grammar into another it behaves sort of like object-oriented inheritance, which could be useful in maintaining a number of dialects of a DSL.

See this page for the basics. Details in the Definitive Antlr4 Reference book.

Upvotes: 1

Ira Baxter
Ira Baxter

Reputation: 95354

I don't think you can define grammar dialects with ANTLR's direct help.

Of course, in order to consider making any changes to your grammar, they must have a strong understanding of what is in the current grammar specification. So you must be allowing them to view the current grammar in some kind of window, before they make a change.

You could just let them view it in an editor window, and edit it directly.

Upvotes: 0

Related Questions