Пример #1
0
        static int _Lalr1Tree(string[] args)
        {
            if (1 > args.Length || args.Length > 2)
            {
                _PrintUsageLalr1Tree();
                return(1);
            }
            var    pckspec = args[0];
            string pck;

            using (var sr = File.OpenText(pckspec))
                pck = sr.ReadToEnd();

            var cfg = CfgDocument.Parse(pck);
            var lex = LexDocument.Parse(pck);

            var tokenizer = lex.ToTokenizer(
                (1 < args.Length) ? (TextReaderEnumerable) new FileReaderEnumerable(args[1]) :
                new ConsoleReaderEnumerable(),
                cfg.EnumSymbols(), new _TokenizerConsoleProgress());
            var parser = cfg.ToLalr1Parser(tokenizer, new _Lalr1ConsoleProgress());

            Console.Error.WriteLine();
            parser.ShowHidden = true;
            while (LRNodeType.EndDocument != parser.NodeType)
            {
                Console.WriteLine(parser.ParseReductions());
            }

            return(1);
        }
Пример #2
0
        static int _Fagen(string[] args)
        {
            string specFile   = null;
            string outFile    = null;
            string @namespace = null;
            string @class     = null;
            string language   = null;          // "c#";
            var    optIndex   = -1;

            for (var i = 0; i < args.Length; ++i)
            {
                if ("--help" == args[i] || "/?" == args[i] || "/help" == args[i])
                {
                    Console.Error.Write("Usage: ");
                    _PrintUsageFAGen();
                    return(0);
                }
                if (args[i].StartsWith("/"))
                {
                    optIndex = i;
                    if (i == args.Length - 1)
                    {
                        Console.Error.Write("Usage: ");
                        _PrintUsageFAGen();
                        return(1);
                    }
                    switch (args[i])
                    {
                    case "/language":
                        ++i;
                        language = args[i];
                        break;

                    case "/namespace":
                        ++i;
                        @namespace = args[i];
                        break;

                    case "/class":
                        ++i;
                        @class = args[i];
                        break;

                    default:
                        _PrintUsageFAGen();
                        return(1);
                    }
                }
                else
                {
                    if (-1 != optIndex)
                    {
                        Console.Error.Write("Usage: ");
                        _PrintUsageFAGen();
                        return(1);
                    }
                    if (0 == i)
                    {
                        specFile = args[i];
                    }
                    else if (1 == i)
                    {
                        outFile = args[i];
                    }
                    else
                    {
                        Console.Error.Write("Usage: ");
                        _PrintUsageFAGen();

                        return(1);
                    }
                }
            }
            TextReader inp  = null;
            TextWriter outp = null;

            try
            {
                if (null == specFile)
                {
                    inp = Console.In;
                }
                else
                {
                    inp = new StreamReader(specFile);
                }
                if (null == outFile)
                {
                    outp = Console.Out;
                }
                else
                {
                    outp = new StreamWriter(outFile);
                }

                var buf  = inp.ReadToEnd();
                var lex  = LexDocument.Parse(buf);
                var cfg  = CfgDocument.Parse(buf);
                var syms = cfg.FillSymbols();
                if (string.IsNullOrEmpty(@class))
                {
                    if (null != outFile)
                    {
                        @class = Path.GetFileNameWithoutExtension(outFile);
                    }
                    else if (0 < syms.Count)
                    {
                        foreach (var attrs in lex.AttributeSets)
                        {
                            var i = attrs.Value.IndexOf("start");
                            if (-1 < i && attrs.Value[i].Value is bool && (bool)attrs.Value[i].Value)
                            {
                                @class = string.Concat(attrs.Key, "Tokenizer");
                                break;
                            }
                        }
                        if (null == @class)
                        {
                            @class = string.Concat(syms[0], "Tokenizer");
                        }
                    }
                }
                TokenizerCodeGenerator.WriteClassTo(lex, syms, @class, @namespace, language, new _TokenizerConsoleProgress(), outp);
                Console.Error.WriteLine();
                outp.Flush();
                return(0);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
                return(1);
            }
            finally
            {
                inp.Close();
                outp.Close();
            }
        }