picoe / Eto.Parse

Recursive descent LL(k) parser for .NET with Fluent API, BNF, EBNF and Gold Grammars
MIT License
148 stars 30 forks source link

get all productions of ebnf #23

Open furesoft opened 9 years ago

furesoft commented 9 years ago

hi,

how can i get all productions of an ebnf grammar (left part)?

i want to make a visual studio grammar language service

KarmyDev commented 2 years ago

Hi @furesoft !

I know this might be outdated, but i dont wanna leave it unanswered.

If this is what you want to get: image from this example EBNF grammar file: image

then the code is very simple:

foreach(var child in grammar.Children) {     if (!string.IsNullOrEmpty(child.Name))     {         Console.WriteLine("Found grammar child:\t" + child.Name);     } }

In this code, you loop through grammar Children, and just reject those who dont have anything (Those are usually newlines in the EBNF source file)

By the way, you might ask what is grammar in this context. It's the new EbnfGrammar() object!

var grammar = new EbnfGrammar(EbnfStyle.Iso14977).Build(File.ReadAllText(args[0]), args[2]);

and args[2] is the start sequence keyword.