patrickhuber / Pliant

MIT License
26 stars 4 forks source link

Request for feature: including another partial grammar #74

Open ArsenShnurkov opened 7 years ago

ArsenShnurkov commented 7 years ago

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)

patrickhuber commented 7 years ago

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.