Hello,<br>I am trying out the happy parser.<br>Take the second example Binary to Decimal given on the happy site: <a href="http://www.haskell.org/happy/doc/html/sec-AttributeGrammarExample.html">http://www.haskell.org/happy/doc/html/sec-AttributeGrammarExample.html</a> <br>
<br>This example computes the decimal value equivalent to the given binary string.<br><br>I want to modify it so that along with the decimal value, it should compute a list of data values, like MyLeft, MyRight. <br>I have modified the grammar given on the site as shown below (in the end).<br>
<br>What I want to do is generate a list of MyLeft, MyRight such that MyLeft denotes 0 and MyRight denotes 1.<br>So, if my input is <span style="font-family:courier new,monospace">"1011\n"</span> then I expect the list attribute to be: <br>
<span style="font-family:courier new,monospace">[MyRight, Myleft, MyRight, MyRight]</span><br><br>The happy compiles this thing without any problem, ghci compiles and loads it correctly. But when I run the following command I always get only the decimal value and not the list:<br>
<br>So my questions are:<br><ol><li><b>How shall one compute, access multiple attributes in happy first? </b><b>(use in Haskell code, write to file etc)</b></li><li><b>How shall one access any one or multiple of the computed attributes in GHCi then?</b></li>
<li><b>Am I computing the list correctly?<br></b></li></ol><br>Thanks in advance.<br> kak <br><br>Here is my happy grammar code:<br>-----------------------------<br><span style="font-family:courier new,monospace">{</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">module BitsParser (parse) where</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">test = parse "1011\n"</span><br>
<br>-- how to write the list attribute to a file here?<br><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">data Dirs = MyLeft | MyRight deriving Show</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">fun a b = a^b</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">}</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">%tokentype { Char }</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">%token minus { '-' }</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">%token plus { '+' }</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">%token one { '1' }</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">%token zero { '0' }</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">%token newline { '\n' }</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">%attributetype { Attrs }</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">%attribute value { Integer }</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">%attribute pos { Int }</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">%attribute list { [Dirs] }</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">%name parse start</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">%%</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">start </span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> : num newline { $$ = $1 }</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">num </span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> : bits { $$ = $1 ; $1.pos = 0 ; $1.list = [] }</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> | plus bits { $$ = $2 ; $2.pos = 0 ; $2.list = [] }</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> | minus bits { $$ = negate $2; $2.pos = 0 ; $2.list = [] }</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">bits</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> : bit { $$ = $1</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> ; $1.pos = $$.pos ; $1.list = $$.list </span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> }</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> | bits bit { $$ = $1 + $2 ; $$.list = $1.list ++ $2.list</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> ; $1.pos = $$.pos + 1 </span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> ; $2.pos = $$.pos</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> }</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">bit</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> : zero { $$ = 0 ; $$.list = [MyLeft] }</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> | one { $$ = fun 2 ($$.pos) ; $$.list = [MyRight] }</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> </span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">{</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">happyError = error "parse error"</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">}</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace">
<br>