我有以下文件和需要分析如何創建一個解析器(lex/yacc)?
--TestFile
Start ASDF123
Name "John"
Address "#6,US"
end ASDF123
的行以--
會註釋行處理。文件開始「開始」並以end
結束。 Start
之後的字符串是UserID
,然後name
和address
將位於雙重內。
我需要解析文件並將解析的數據寫入xml文件。
所以生成的文件就會像
<ASDF123>
<Name Value="John" />
<Address Value="#6,US" />
</ASDF123>
我現在正在使用圖案匹配(Regular Expressions
)來解析上述文件。這是我的示例代碼。
/// <summary>
/// To Store the row data from the file
/// </summary>
List<String> MyList = new List<String>();
String strName = "";
String strAddress = "";
String strInfo = "";
方法:ReadFile的
/// <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();
}
方法:FormateRowData
/// <summary>
/// To remove comments
/// </summary>
private void FormateRowData()
{
MyList = MyList.Where(X => X != "").Where(X => X.StartsWith("--")==false).ToList();
}
方法:ParseData
/// <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;
}
}
方法: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#解析器的解析器生成器。