Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create a parser(lex/yacc)?
    text
    copied!<p>I'm having the following file and which need to be parsed</p> <pre><code>--TestFile Start ASDF123 Name "John" Address "#6,US" end ASDF123 </code></pre> <p>The lines start with <code>--</code> will be treated as comment lines. and the file starts 'Start' and ends with <code>end</code>. The string after <code>Start</code> is the <code>UserID</code> and then the <code>name</code> and <code>address</code> will be inside the double quots.</p> <p>I need to parse the file and write the parsed data into an xml file.</p> <p>So the resulting file will be like</p> <pre><code>&lt;ASDF123&gt; &lt;Name Value="John" /&gt; &lt;Address Value="#6,US" /&gt; &lt;/ASDF123&gt; </code></pre> <p>now i'm using pattern matching(<code>Regular Expressions</code>) to parse the above file . Here is my sample code.</p> <pre><code> /// &lt;summary&gt; /// To Store the row data from the file /// &lt;/summary&gt; List&lt;String&gt; MyList = new List&lt;String&gt;(); String strName = ""; String strAddress = ""; String strInfo = ""; </code></pre> <p><strong>Method</strong> : ReadFile</p> <pre><code> /// &lt;summary&gt; /// To read the file into a List /// &lt;/summary&gt; private void ReadFile() { StreamReader Reader = new StreamReader(Application.StartupPath + "\\TestFile.txt"); while (!Reader.EndOfStream) { MyList.Add(Reader.ReadLine()); } Reader.Close(); } </code></pre> <p><strong>Method</strong> : FormateRowData</p> <pre><code> /// &lt;summary&gt; /// To remove comments /// &lt;/summary&gt; private void FormateRowData() { MyList = MyList.Where(X =&gt; X != "").Where(X =&gt; X.StartsWith("--")==false ).ToList(); } </code></pre> <p><strong>Method</strong> : ParseData</p> <pre><code> /// &lt;summary&gt; /// To Parse the data from the List /// &lt;/summary&gt; private void ParseData() { Match l_mMatch; Regex RegData = new Regex("start[ \t\r\n]*(?&lt;Data&gt;[a-z0-9]*)", RegexOptions.IgnoreCase); Regex RegName = new Regex("name [ \t\r\n]*\"(?&lt;Name&gt;[a-z]*)\"", RegexOptions.IgnoreCase); Regex RegAddress = new Regex("address [ \t\r\n]*\"(?&lt;Address&gt;[a-z0-9 #,]*)\"", RegexOptions.IgnoreCase); for (int Index = 0; Index &lt; 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; } } </code></pre> <p><strong>Method</strong> : WriteFile</p> <pre><code> /// &lt;summary&gt; /// To write parsed information into file. /// &lt;/summary&gt; 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"); } </code></pre> <p>i've heard of <a href="http://www.bumblebeesoftware.com/" rel="nofollow">ParserGenerator</a></p> <p>Please help me to write a parser using lex and yacc. The reason for this is , my exsisting parser(<code>Pattern Matching</code>) is not flexible, more over its not the right way(I think so).</p> <p>How to i make use of the <code>ParserGenerator</code>(I've read <a href="http://www.codeproject.com/KB/recipes/introlexyacc.aspx" rel="nofollow">Code Project Sample One</a> and <a href="http://www.codeproject.com/KB/recipes/lexyacc2.aspx?msg=2067628" rel="nofollow">Code Project Sample Two</a> but still i'm not familiar with this). Please suggest me some parser generator which outputs C# parsers.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload