private static void LoadBnfFile(string fileName, Builder mainBuilder) { Console.WriteLine("Parse BNF file: {0}", fileName); var bnf = File.ReadAllText(fileName); var metaParser = new MetaParser(); var meta = metaParser.Parse(bnf); var oprimizedBnf = Optimize(bnf); var parser = new Parser(new BnfGrammar(meta.Mode)); var tree = parser.Parse(oprimizedBnf, fileName); if (tree.Status == ParseTreeStatus.Error) { throw new Exception((tree.ParserMessages.Count > 0) ? string.Format("{0}, in {3} file at line {1}, column {2}", tree.ParserMessages[0].Message, tree.ParserMessages[0].Location.Line, tree.ParserMessages[0].Location.Column, fileName) : string.Format(@"Unknow error in BNF file {0}", fileName)); } var builder = new Builder(tree, mainBuilder); builder.BuildExpressions(); foreach (var @using in meta.Usings) LoadBnfFile(@using, mainBuilder); }
private static void LoadBnfFile(string fileName, Builder mainBuilder) { Console.WriteLine("Parse BNF file: {0}", fileName); var bnf = File.ReadAllText(fileName); var metaParser = new MetaParser(); var meta = metaParser.Parse(bnf); var oprimizedBnf = Optimize(bnf); var parser = new Parser(new BnfGrammar(meta.Mode)); var tree = parser.Parse(oprimizedBnf, fileName); if (tree.Status == ParseTreeStatus.Error) { throw new Exception((tree.ParserMessages.Count > 0) ? string.Format("{0}, in {3} file at line {1}, column {2}", tree.ParserMessages[0].Message, tree.ParserMessages[0].Location.Line, tree.ParserMessages[0].Location.Column, fileName) : string.Format(@"Unknow error in BNF file {0}", fileName)); } var builder = new Builder(tree, mainBuilder); builder.BuildExpressions(); foreach (var @using in meta.Usings) { LoadBnfFile(@using, mainBuilder); } }
static int Main(string[] args) { try { //api.xbnf.txt api.mark.txt API var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\"; bool mode2 = (((args.Length >= 4) ? args[3] : "") == "mode2"); var rootRule = args[2]; Console.WriteLine("Load grammar"); var grammar = new XbnfGrammar(mode2 ? XbnfGrammar.Mode.HttpCompatible : XbnfGrammar.Mode.Strict); Console.WriteLine("Create parser"); var parser = new Parser(grammar); Console.WriteLine("Read XBNF from {0}", args[0]); var xbnf = File.ReadAllText(args[0]); Console.WriteLine("Optimize"); var oprimized = Optimize(xbnf); Console.WriteLine("Parse"); var tree = parser.Parse(oprimized, "<source>"); if (tree == null) throw new Exception(@"Failed to parse"); Console.WriteLine("Build expressions"); var builder = new Builder(tree); builder.BuildExpressions(); Console.WriteLine("Load marks"); var marker = new Marker(); if (args.Length >= 2) marker.LoadMarks(path + args[1]); //if (args.Length >= 3) // marker.LoadSuppressWarngin(path + args[2]); Console.WriteLine("Build NFA"); var nfa = builder.CreateNfa(rootRule, marker.MarkRuleHandler); nfa.MarkFinal(); Console.WriteLine("Max NFA state id: {0}", Fsm.State.MaxId); Console.WriteLine("Check unused rules"); foreach (var unused in marker.GetUnusedRules()) Console.WriteLine("UNUSED: {0}", unused); Console.WriteLine("Pack NFA"); PackNfa.Pack(nfa, true); int count; var dfa = nfa.ToDfa3(out count, true); Console.WriteLine("DFA Complied States: {0}", count); var minCount = dfa.Minimize(true); Console.WriteLine("Minimized DFA States: {0}", minCount); Console.WriteLine("Write DFA"); Writer.Write(dfa, "dfa.xml"); //Console.WriteLine("Convert to C#"); //var csharp = grammar.RunSample(tree); //Console.WriteLine("Write C# to {0}", args[1]); //File.WriteAllText(args[1], AddHeaderFooter(csharp)); } catch (Exception ex) { Console.Write(ex.ToString()); return -1; } return 0; }
static int Main(string[] args) { try { //api.xbnf.txt api.mark.txt API var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\"; bool mode2 = (((args.Length >= 4) ? args[3] : "") == "mode2"); var rootRule = args[2]; Console.WriteLine("Load grammar"); var grammar = new XbnfGrammar(mode2 ? XbnfGrammar.Mode.HttpCompatible : XbnfGrammar.Mode.Strict); Console.WriteLine("Create parser"); var parser = new Parser(grammar); Console.WriteLine("Read XBNF from {0}", args[0]); var xbnf = File.ReadAllText(args[0]); Console.WriteLine("Optimize"); var oprimized = Optimize(xbnf); Console.WriteLine("Parse"); var tree = parser.Parse(oprimized, "<source>"); if (tree == null) { throw new Exception(@"Failed to parse"); } Console.WriteLine("Build expressions"); var builder = new Builder(tree); builder.BuildExpressions(); Console.WriteLine("Load marks"); var marker = new Marker(); if (args.Length >= 2) { marker.LoadMarks(path + args[1]); } //if (args.Length >= 3) // marker.LoadSuppressWarngin(path + args[2]); Console.WriteLine("Build NFA"); var nfa = builder.CreateNfa(rootRule, marker.MarkRuleHandler); nfa.MarkFinal(); Console.WriteLine("Max NFA state id: {0}", Fsm.State.MaxId); Console.WriteLine("Check unused rules"); foreach (var unused in marker.GetUnusedRules()) { Console.WriteLine("UNUSED: {0}", unused); } Console.WriteLine("Pack NFA"); PackNfa.Pack(nfa, true); int count; var dfa = nfa.ToDfa3(out count, true); Console.WriteLine("DFA Complied States: {0}", count); var minCount = dfa.Minimize(true); Console.WriteLine("Minimized DFA States: {0}", minCount); Console.WriteLine("Write DFA"); Writer.Write(dfa, "dfa.xml"); //Console.WriteLine("Convert to C#"); //var csharp = grammar.RunSample(tree); //Console.WriteLine("Write C# to {0}", args[1]); //File.WriteAllText(args[1], AddHeaderFooter(csharp)); } catch (Exception ex) { Console.Write(ex.ToString()); return(-1); } return(0); }