I'm having the following file and which need to be parsed
--TestFile
Start ASDF123
Name "John"
Address "#6,US"
end ASDF123
The lines start with --
will be treated as comment lines. and the file starts 'Start' and ends with end
. The string after Start
is the UserID
and then the name
and address
will be inside the double quots.
I need to parse the file and write the parsed data into an xml file.
So the resulting file will be like
<ASDF123>
<Name Value="John" />
<Address Value="#6,US" />
</ASDF123>
now i'm using pattern matching(Regular Expressions
) to parse the above file . Here is my sample code.
/// <summary>
/// To Store the row data from the file
/// </summary>
List<String> MyList = new List<String>();
String strName = "";
String strAddress = "";
String strInfo = "";
Method : 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();
}
Method : FormateRowData
/// <summary>
/// To remove comments
/// </summary>
private void FormateRowData()
{
MyList = MyList.Where(X => X != "").Where(X => X.StartsWith("--")==false ).ToList();
}
Method : 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;
}
}
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");
}
i've heard of ParserGenerator
Please help me to write a parser using lex and yacc. The reason for this is , my exsisting parser(Pattern Matching
) is not flexible, more over its not the right way(I think so).
How to i make use of the ParserGenerator
(I've read Code Project Sample One and Code Project Sample Two but still i'm not familiar with this). Please suggest me some parser generator which outputs C# parsers.