Ejemplo n.º 1
0
        private static CflParser CreateParser(string parserName, CnfGrammar grammar)
        {
            var parserTypeName = string.Format("{0}.{1}Parser", typeof(Program).Namespace, parserName.ToUpper());
            var parserType     = Type.GetType(parserTypeName);

            return((CflParser)Activator.CreateInstance(parserType, grammar));
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            if ((args == null) || (args.Length < 2) || string.IsNullOrEmpty(args[0]) || string.IsNullOrEmpty(args[1]))
            {
                Console.WriteLine();
                Console.WriteLine("Use: cfl {algorithm} {grammar} [ --verbose ] [ < {input} [ > {output} ] ]");
                Console.WriteLine();
                Console.WriteLine("(where   {algorithm} = \"CYK\" or \"CJ2\")");
                Console.WriteLine();
                return;
            }

            var grammarFilePath   = args[1];
            var grammarDefinition = File.ReadAllLines(grammarFilePath);
            var grammar           = new CnfGrammar().Load(grammarDefinition);
            var verbose           = (args.Length > 2) && (args[2] == "--verbose");

            var parser = CreateParser(args[0], grammar);

            var    lineNo = 0;
            var    errors = 0;
            string input;

            while ((input = Console.ReadLine()) != null)
            {
                var sw = new Stopwatch();
                sw.Start();
                var parse = parser.Parse(input);
                sw.Stop();
                var ms      = sw.ElapsedMilliseconds;
                var ok      = parse.Productions.Contains(grammar.Start.Lhs);
                var details = verbose ? string.Format(": {0} => {1}", input, parse) : string.Empty;
                errors += !ok ? 1 : 0;
                Console.WriteLine(string.Format("(in {0} ms) line #{1}: {2}{3}", sw.ElapsedMilliseconds.ToString().PadLeft(7, ' '), (++lineNo).ToString().PadLeft(7, ' '), ok ? "OK" : "KO", details));
            }
            if (lineNo > 0)
            {
                Console.WriteLine(string.Format("{0} / {1} OK: {2}%", lineNo - errors, lineNo, 100 * (lineNo - errors) / lineNo));
            }
            Console.WriteLine();
            Environment.Exit(errors > 0 ? 1 : 0);
        }
Ejemplo n.º 3
0
 public CJ2Parser(CnfGrammar grammar)
     : base(grammar)
 {
 }