Пример #1
0
        private static string AnalyzeString(GrammarAnalyzer analyzer, string str)
        {
            var symbols = str.Select(c => new Terminal(c));

            var result = new List <string>();

            void OnIteration(IEnumerable <Symbol> stack, IEnumerable <Symbol> left)
            {
                result.Add($"{string.Join("", stack.Reverse().Select(s => s.Value))} {string.Join("", left.Select(s => s.Value))}");
            }

            if (!analyzer.IsAccepted(symbols, OnIteration))
            {
                result.Add("error");
            }

            return(string.Join("\r\n", result) + "\r\n");
        }
Пример #2
0
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine($"FormalGrammar.exe [in-filename] [out-filename]");
                return;
            }

            var(rules, strings, ntOrder) = ReadInput(args[0]);

            var grammar           = GrammarParser.Parse(rules);
            var grammarPrecedence = new GrammarPrecedence(grammar);

            var view  = new GrammarPrecedenceView(s => OrderSymbols(s, ntOrder));
            var table = view.ToTable(grammarPrecedence);

            var analyzer = new GrammarAnalyzer(grammarPrecedence);
            var type     = analyzer.IsSimplePrecedence ? "S" : (analyzer.IsWeakPrecedence ? "W" : "N");

            var analyzings = analyzer.IsWeakPrecedence ? strings.Select(s => AnalyzeString(analyzer, s)) : new List <string>();

            File.WriteAllText(args[1], $"{table}\r\n{type}\r\n\r\n{string.Join("\r\n", analyzings)}", Encoding.UTF8);
        }
Пример #3
0
 /**
  * <summary>Creates a new parser.</summary>
  *
  * <param name='input'>the input stream to read from</param>
  *
  * <param name='analyzer'>the analyzer to parse with</param>
  *
  * <exception cref='ParserCreationException'>if the parser
  * couldn't be initialized correctly</exception>
  */
 public GrammarParser(TextReader input, GrammarAnalyzer analyzer)
     : base(input, analyzer)
 {
     CreatePatterns();
 }