<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Feb 20, 2013 at 1:09 AM, Sean Cormican <span dir="ltr">&lt;<a href="mailto:seancormican1@gmail.com" target="_blank">seancormican1@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Thanks that is exactly what I was looking for, one further question I might ask is how I might allow for either a integer or a string to be parsed. As it is now I get a complaint if I try and parse either a String or an Integer without creating a data declaration for say &quot;Express&quot; containing:<div>


<div><br></div><div>data Express = ID String</div><div>              | Num Integer </div></div><div><br></div><div>is there a way around this without a need for a data declaration? </div><div>As far as I know the parser will only accept (in this case) either Strings or Integers but not both, for example:</div>


<div><br></div><div>expr8 = name</div><div>&lt;|&gt; number</div><div class="gmail_extra"><br></div><div class="gmail_extra">name :: String</div><div class="gmail_extra">number :: Integer</div><div class="gmail_extra">
<br></div></div></blockquote><div><br></div><div>I don&#39;t see how expr8 type checks at all.  (&lt;|&gt;) is for &quot;alternative&quot; functors.</div><div><br></div><div style>In any case, I think we&#39;re using vocabulary differently.  To me, a parser &quot;accepts&quot; strings and &quot;returns&quot; whatever value was constructed by parsing the string.  So, if you want to &quot;return&quot; a value that could be a String or an Integer, you&#39;ll have to return (Either String Integer) or (Either Integer String) or make an algebraic data type.</div>
<div style><br></div><div style>Note that Either a b is an algebraic data type defined by:</div><div style><br></div><div style>&gt; data Either a b = Left a | Right b</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div dir="ltr"><div class="gmail_extra"></div><div class="gmail_extra">will cause an error unless name and number are created using the value constructors ID and Num and are both the data type Express. Anybody have any thoughts on this ?</div>

<div class="gmail_extra">
<br></div><div class="gmail_extra">Thanks in Advance,</div><div class="gmail_extra">Seán</div><div><div><div class="gmail_extra"><br></div><div class="gmail_extra"><br></div><div class="gmail_extra"><br>
</div><div class="gmail_extra"><div class="gmail_quote">On Tue, Feb 19, 2013 at 11:22 PM, Alexander Solla <span dir="ltr">&lt;<a href="mailto:alex.solla@gmail.com" target="_blank">alex.solla@gmail.com</a>&gt;</span> wrote:<br>


<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr">Come to think of it, a parsec parser already wraps over Either, so if all you want to do is check if a result is valid, you can abuse the Either semantics so that your type is:<div>


<br></div><div>Parser () -- the parser which returns nothing on success or an error on failure.</div>
</div><div><div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Feb 19, 2013 at 3:20 PM, Alexander Solla <span dir="ltr">&lt;<a href="mailto:alex.solla@gmail.com" target="_blank">alex.solla@gmail.com</a>&gt;</span> wrote:<br>



<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr">If all you want to do is check that the code is valid (i.e., you aren&#39;t going to interpret the code), you can just return a Bool.  If you want to interpret it, but don&#39;t want to have a Stmt type, you can return IO () actions.  In that case, the parser&#39;s type will be<div>




<br></div><div>Parser (IO ())<br><div><br></div><div>I think an algebraic AST (or even a functorial/monadic one) will help separate concerns, and will eventually help when it comes time to optimize your compiler.  It really isn&#39;t as much boilerplate as it looks like (in fact, there&#39;s hardly any boilerplate if you target free monads and interpret those in IO), and you get the type safety for which Haskell is well-known.</div>




</div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote"><div><div>On Tue, Feb 19, 2013 at 3:02 PM, Sean Cormican <span dir="ltr">&lt;<a href="mailto:seancormican1@gmail.com" target="_blank">seancormican1@gmail.com</a>&gt;</span> wrote:<br>




</div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div><div><div dir="ltr">I have been trying to create a parser for a functional programming language, but there is no need to create an AST but merely check that the code is valid according to the grammar.<div>




<br></div><div>In the following tutorial I have been trying to take some pointers from, data declarations are used to create an AST for the language, There is, as I understand a way to parse the language without an AST. </div>





<div><br></div><div><a href="http://www.haskell.org/haskellwiki/Parsing_a_simple_imperative_language" target="_blank">http://www.haskell.org/haskellwiki/Parsing_a_simple_imperative_language</a><br></div><div><br></div><div>





My question is what should the type signatures for example parseFile function instead of &quot;Stmt&quot; accept as input if the parser is to accept Strings and numerical expressions alike ? </div><div><br></div><div>
Thanks for any help,</div><div>Seán </div></div>
<br></div></div>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div></div></div></div>
</blockquote></div><br></div></div>