我有以下需要解析的文件
--TestFile
Start ASDF123
Name "John"
Address "#6,US"
end ASDF123
以--
开头的行将被视为注释行。文件以'Start'开始,以end
结束。在Start
之后的字符串是UserID
,然后name
和address
将在双引号内。
我需要解析该文件并将解析后的数据写入xml文件。
因此,生成的文件将如下所示:
<ASDF123>
<Name Value="John" />
<Address Value="#6,US" />
</ASDF123>
现在我正在使用模式匹配(正则表达式
)解析上述文件。这是我的示例代码:
/// <summary>
/// To Store the row data from the file
/// </summary>
List<String> MyList = new List<String>();
String strName = "";
String strAddress = "";
String strInfo = "";
方法: 读取文件
/// <summary>
/// To read the file into a List
/// </summary>
private void ReadFile()
{
StreamReader Reader = new StreamReader(Application.StartupPath + "\\TestFile.txt");
while (!Reader.EndOfStream)
{
MyList.Add(Reader.ReadLine());
}
Reader.Close();
}
方法:格式化行数据
/// <summary>
/// To remove comments
/// </summary>
private void FormateRowData()
{
MyList = MyList.Where(X => X != "").Where(X => X.StartsWith("--")==false ).ToList();
}
方法:解析数据
/// <summary>
/// To Parse the data from the List
/// </summary>
private void ParseData()
{
Match l_mMatch;
Regex RegData = new Regex("start[ \t\r\n]*(?<Data>[a-z0-9]*)", RegexOptions.IgnoreCase);
Regex RegName = new Regex("name [ \t\r\n]*\"(?<Name>[a-z]*)\"", RegexOptions.IgnoreCase);
Regex RegAddress = new Regex("address [ \t\r\n]*\"(?<Address>[a-z0-9 #,]*)\"", RegexOptions.IgnoreCase);
for (int Index = 0; Index < MyList.Count; Index++)
{
l_mMatch = RegData.Match(MyList[Index]);
if (l_mMatch.Success)
strInfo = l_mMatch.Groups["Data"].Value;
l_mMatch = RegName.Match(MyList[Index]);
if (l_mMatch.Success)
strName = l_mMatch.Groups["Name"].Value;
l_mMatch = RegAddress.Match(MyList[Index]);
if (l_mMatch.Success)
strAddress = l_mMatch.Groups["Address"].Value;
}
}
Method : WriteFile
/// <summary>
/// To write parsed information into file.
/// </summary>
private void WriteFile()
{
XDocument XD = new XDocument(
new XElement(strInfo,
new XElement("Name",
new XAttribute("Value", strName)),
new XElement("Address",
new XAttribute("Value", strAddress))));
XD.Save(Application.StartupPath + "\\File.xml");
}
我听说过ParserGenerator
请帮我使用lex和yacc编写解析器。这是因为我的现有解析器(Pattern Matching
)不够灵活,而且我认为它不是正确的方法。
如何使用ParserGenerator
(我已经阅读了Code Project Sample One 和 Code Project Sample Two,但我仍然不熟悉它)。请建议一些可以输出C#解析器的解析器生成器。