-1

还是 C# 的新手,所以要友好:)

S -> NP VP
S -> Aux NP VP
NP -> Proper-Noun
NP -> Nominal
Nominal -> Noun
Nominal -> Nominal Noun
Nominal -> Nominal PP
VP -> Verb NP
VP -> Verb NP PP
PP -> Preposition NP

如何将这些产生式存储在数组中或以表格形式存储在列表中,如下面的 c# 中。以上产品是从文件中读取的。

Table Structure
terminals -> all non terminals after that specific terminal
S -> NP VP Aux NP VP VP
VP -> Verb NP Verb NP PP Verb PP
4

2 回答 2

2

这样的事情应该会有所帮助:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

class Production
{
   public string Name;
   public string[] Components;

   public static IEnumerable<Production> Parse(string contents)
   {
      var rdr = new System.IO.StringReader(contents);
      string line;
      var productions = new List<Production>();
      while(null != (line = rdr.ReadLine()))
      {
         if(string.IsNullOrEmpty(line))
            continue;
         productions.Add(ParseOne(line));
      }

      return productions;
   }

   public static Production ParseOne(string line)
   {
      var parts = line.Split(new [] {' '}, StringSplitOptions.RemoveEmptyEntries);
      return new Production{Name = parts[0], Components = parts.Skip(2).ToArray()};
   }

}

您可以像这样使用 Production.Parse 方法:

static void Main()
{      
   var productions = Production.Parse(@"S -> NP VP
S -> Aux NP VP
NP -> Proper-Noun
NP -> Nominal
Nominal -> Noun
Nominal -> Nominal Noun
Nominal -> Nominal PP
VP -> Verb NP
VP -> Verb NP PP
PP -> Preposition NP");
   // Or from a file like this:
   productions = Production.Parse(File.ReadAllText("myProductions.txt));
}

编辑:要加入制作,请考虑按名称分组。

productions.GroupBy(p => p.Name, p => new Production{Name=p.Key, Components=p.SelectMany(x => x)});
于 2012-12-21T04:52:51.937 回答
0

这是一个演示 linq 解决方案。它不会进行任何类型的错误检查,它假定数据位于名为lines.txt 的输入文件中:

编辑:简化查询

static void Main(string[] args)
        {
            var lines = System.IO.File.ReadAllLines("lines.txt");

            var results = from line in lines
                     let tokens = line.Split(' ')
                     group tokens by tokens.First() into tokenGroup
                     select new
                     {
                         Terminal = tokenGroup.Key,
                         NonTerminals = tokenGroup.SelectMany(tokens => tokens.Skip(2))
                     };

            foreach (var line in results)
            {
                var sb = new StringBuilder(string.Format("{0} ->", line.Terminal));
                foreach (var nonTerminal in line.NonTerminals)
                {
                    sb.Append(string.Format(" {0}", nonTerminal));
                }
                Console.WriteLine(sb.ToString());
            }
        }
于 2012-12-21T05:09:30.977 回答