Pliant icon indicating copy to clipboard operation
Pliant copied to clipboard

Request for feature: including another partial grammar

Open ArsenShnurkov opened this issue 8 years ago • 1 comments

I have 2 similar grammars. One should process preprocessor directives, another should process all other directives in some syntax. These 2 grammars have some rules in common. I want to create 3 files, one common file, and 2 specific files, and include common file from both specific files.

In ISO EBNF I think it is possible to do with ? ... ? syntax, but is not very convinient.

Antlr allows "Composite grammars" - https://theantlrguy.atlassian.net/wiki/display/ANTLR3/Composite+Grammars

But Antlr's syntax differs from all 3 public standarts (ABNF, ISO EBNF, W3C EBNF)

ArsenShnurkov avatar Apr 03 '17 16:04 ArsenShnurkov

Grammar rules use namespaces, so this is possible using the code implementation of Grammar simply by referencing the grammar rule in the new grammar you are creating. You can do it by hand when crafting a grammar using the Grammar class.

You can also do this using grammar expressions. You can see an example where the RegexGrammar is referenced from the EbnfGrammar (to be changed to Dsl grammar).

https://github.com/patrickhuber/Pliant/blob/master/libraries/Pliant/Ebnf/EbnfGrammar.cs

If using Builder Expressions, the ProductionReferenceExpression can be used to wrap a grammar and use its start as a rule in another grammar. I imagine you would want to reference any production within a given grammar and also use a DSL like Ebnf or Bnf based on your comments above.

patrickhuber avatar May 09 '17 21:05 patrickhuber