MGrammar Equivalent of Yacc's $$?Consider the following simple calculator implemeted in yacc:<br><br> <div style="border-bottom:#7f9db9 1px solid;border-left:#7f9db9 1px solid;line-height:100% !important;background-color:white;font-family:Courier New;font-size:11px;overflow:auto;border-top:#7f9db9 1px solid;border-right:#7f9db9 1px solid"> <table style="border-bottom:#eee 0px solid;border-right-width:0px;background-color:#fff;margin:2px 0px;width:99%;border-collapse:collapse;border-top-width:0px;border-left-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="text-align:right;background-color:#eee;padding-left:10px;width:5px;padding-right:5px;font-family:Courier New;color:gray;font-size:11px;vertical-align:top;border-right:gray 1px solid"> <col style="border-bottom:#f7f7f7 1px solid;padding-left:10px;font-family:Courier New;white-space:nowrap;font-size:11px"> <tbody> <tr> <td><nobr>1</nobr></td> <td><font style="font-size:11px">%{ </font><font style="color:gray"></font></td></tr> <tr> <td><nobr>2</nobr></td> <td style="background-color:#f7f7f7"><font style="color:gray">    #include &lt;stdio.h&gt; </font><font style="font-size:11px"> </font></td></tr> <tr> <td><nobr>3</nobr></td> <td>    </font><font style="color:blue">int</font><font style="font-size:11px"> yylex(</font><font style="color:blue">void</font><font style="font-size:11px">);  </font></td></tr> <tr> <td><nobr>4</nobr></td> <td style="background-color:#f7f7f7">    </font><font style="color:blue">void</font><font style="font-size:11px"> yyerror(</font><font style="color:blue">char</font><font style="font-size:11px"> *);  </font></td></tr> <tr> <td><nobr>5</nobr></td> <td>%}  </td></tr> <tr> <td><nobr>6</nobr></td> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td><nobr>7</nobr></td> <td>%token INTEGER  </td></tr> <tr> <td><nobr>8</nobr></td> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td><nobr>9</nobr></td> <td>%%  </td></tr> <tr> <td><nobr>10</nobr></td> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td><nobr>11</nobr></td> <td>program:  </td></tr> <tr> <td><nobr>12</nobr></td> <td style="background-color:#f7f7f7">        program expr '\n'         { printf(</font><font style="color:blue">&quot;%d\n&quot;</font><font style="font-size:11px">, $2); }  </font></td></tr> <tr> <td><nobr>13</nobr></td> <td>        |   </td></tr> <tr> <td><nobr>14</nobr></td> <td style="background-color:#f7f7f7">        ;  </td></tr> <tr> <td><nobr>15</nobr></td> <td> </td></tr> <tr> <td><nobr>16</nobr></td> <td style="background-color:#f7f7f7">expr:  </td></tr> <tr> <td><nobr>17</nobr></td> <td>        INTEGER  </td></tr> <tr> <td><nobr>18</nobr></td> <td style="background-color:#f7f7f7">        | expr '+' expr           { $$ = $1 + $3; }  </td></tr> <tr> <td><nobr>19</nobr></td> <td>        | expr '-' expr           { $$ = $1 - $3; }  </td></tr> <tr> <td><nobr>20</nobr></td> <td style="background-color:#f7f7f7">        ;  </td></tr> <tr> <td><nobr>21</nobr></td> <td> </td></tr> <tr> <td><nobr>22</nobr></td> <td style="background-color:#f7f7f7">%%  </td></tr> <tr> <td><nobr>23</nobr></td> <td> </td></tr> <tr> <td><nobr>24</nobr></td> <td style="background-color:#f7f7f7"></font><font style="color:blue">void</font><font style="font-size:11px"> yyerror(</font><font style="color:blue">char</font><font style="font-size:11px"> *s) {  </font></td></tr> <tr> <td><nobr>25</nobr></td> <td>    fprintf(stderr, </font><font style="color:blue">&quot;%s\n&quot;</font><font style="font-size:11px">, s);  </font></td></tr> <tr> <td><nobr>26</nobr></td> <td style="background-color:#f7f7f7">}  </td></tr> <tr> <td><nobr>27</nobr></td> <td> </td></tr> <tr> <td><nobr>28</nobr></td> <td style="background-color:#f7f7f7"></font><font style="color:blue">int</font><font style="font-size:11px"> main(</font><font style="color:blue">void</font><font style="font-size:11px">) {  </font></td></tr> <tr> <td><nobr>29</nobr></td> <td>    yyparse();  </td></tr> <tr> <td><nobr>30</nobr></td> <td style="background-color:#f7f7f7">    </font><font style="color:blue">return</font><font style="font-size:11px"> 0;  </font></td></tr> <tr> <td><nobr>31</nobr></td> <td>}  </td></tr> <tr> <td><nobr>32</nobr></td> <td style="background-color:#f7f7f7"> </td></tr></tbody></table></div><br><br>I am interested in how to handle <strong>yacc</strong>'s <strong>$$</strong> construct in MGrammar. As you can see in lines 18 and 19 above, the <strong>$$</strong> holds the result of an expression calculated in the &quot;codebehind&quot; (for lack of a better term) for the expression. This allows the result to be used in parse trees as if it were brought in as a literal.<br><br>How would I do the same in MGrammar? I am familiar with navigating parse trees in .NET, so, if you say &quot;you need C# for that&quot;, that's fine ... in fact, I almost expect that answer. I know how to calculate the result in the # code, but where do I return the result of the expression to be used in the parse tree?<br><br>Thank you,<br>Jeff Ferguson© 2009 Microsoft Corporation. All rights reserved.Wed, 01 Apr 2009 12:09:11 Z625ba3a1-df8c-416a-917d-a4a99a0a8aa5http://social.msdn.microsoft.com/Forums/en-US/oslo/thread/625ba3a1-df8c-416a-917d-a4a99a0a8aa5#625ba3a1-df8c-416a-917d-a4a99a0a8aa5http://social.msdn.microsoft.com/Forums/en-US/oslo/thread/625ba3a1-df8c-416a-917d-a4a99a0a8aa5#625ba3a1-df8c-416a-917d-a4a99a0a8aa5JeffFergusonhttp://social.msdn.microsoft.com/Profile/en-US/?user=JeffFergusonMGrammar Equivalent of Yacc's $$?Consider the following simple calculator implemeted in yacc:<br><br> <div style="border-bottom:#7f9db9 1px solid;border-left:#7f9db9 1px solid;line-height:100% !important;background-color:white;font-family:Courier New;font-size:11px;overflow:auto;border-top:#7f9db9 1px solid;border-right:#7f9db9 1px solid"> <table style="border-bottom:#eee 0px solid;border-right-width:0px;background-color:#fff;margin:2px 0px;width:99%;border-collapse:collapse;border-top-width:0px;border-left-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="text-align:right;background-color:#eee;padding-left:10px;width:5px;padding-right:5px;font-family:Courier New;color:gray;font-size:11px;vertical-align:top;border-right:gray 1px solid"> <col style="border-bottom:#f7f7f7 1px solid;padding-left:10px;font-family:Courier New;white-space:nowrap;font-size:11px"> <tbody> <tr> <td><nobr>1</nobr></td> <td><font style="font-size:11px">%{ </font><font style="color:gray"></font></td></tr> <tr> <td><nobr>2</nobr></td> <td style="background-color:#f7f7f7"><font style="color:gray">    #include &lt;stdio.h&gt; </font><font style="font-size:11px"> </font></td></tr> <tr> <td><nobr>3</nobr></td> <td>    </font><font style="color:blue">int</font><font style="font-size:11px"> yylex(</font><font style="color:blue">void</font><font style="font-size:11px">);  </font></td></tr> <tr> <td><nobr>4</nobr></td> <td style="background-color:#f7f7f7">    </font><font style="color:blue">void</font><font style="font-size:11px"> yyerror(</font><font style="color:blue">char</font><font style="font-size:11px"> *);  </font></td></tr> <tr> <td><nobr>5</nobr></td> <td>%}  </td></tr> <tr> <td><nobr>6</nobr></td> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td><nobr>7</nobr></td> <td>%token INTEGER  </td></tr> <tr> <td><nobr>8</nobr></td> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td><nobr>9</nobr></td> <td>%%  </td></tr> <tr> <td><nobr>10</nobr></td> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td><nobr>11</nobr></td> <td>program:  </td></tr> <tr> <td><nobr>12</nobr></td> <td style="background-color:#f7f7f7">        program expr '\n'         { printf(</font><font style="color:blue">&quot;%d\n&quot;</font><font style="font-size:11px">, $2); }  </font></td></tr> <tr> <td><nobr>13</nobr></td> <td>        |   </td></tr> <tr> <td><nobr>14</nobr></td> <td style="background-color:#f7f7f7">        ;  </td></tr> <tr> <td><nobr>15</nobr></td> <td> </td></tr> <tr> <td><nobr>16</nobr></td> <td style="background-color:#f7f7f7">expr:  </td></tr> <tr> <td><nobr>17</nobr></td> <td>        INTEGER  </td></tr> <tr> <td><nobr>18</nobr></td> <td style="background-color:#f7f7f7">        | expr '+' expr           { $$ = $1 + $3; }  </td></tr> <tr> <td><nobr>19</nobr></td> <td>        | expr '-' expr           { $$ = $1 - $3; }  </td></tr> <tr> <td><nobr>20</nobr></td> <td style="background-color:#f7f7f7">        ;  </td></tr> <tr> <td><nobr>21</nobr></td> <td> </td></tr> <tr> <td><nobr>22</nobr></td> <td style="background-color:#f7f7f7">%%  </td></tr> <tr> <td><nobr>23</nobr></td> <td> </td></tr> <tr> <td><nobr>24</nobr></td> <td style="background-color:#f7f7f7"></font><font style="color:blue">void</font><font style="font-size:11px"> yyerror(</font><font style="color:blue">char</font><font style="font-size:11px"> *s) {  </font></td></tr> <tr> <td><nobr>25</nobr></td> <td>    fprintf(stderr, </font><font style="color:blue">&quot;%s\n&quot;</font><font style="font-size:11px">, s);  </font></td></tr> <tr> <td><nobr>26</nobr></td> <td style="background-color:#f7f7f7">}  </td></tr> <tr> <td><nobr>27</nobr></td> <td> </td></tr> <tr> <td><nobr>28</nobr></td> <td style="background-color:#f7f7f7"></font><font style="color:blue">int</font><font style="font-size:11px"> main(</font><font style="color:blue">void</font><font style="font-size:11px">) {  </font></td></tr> <tr> <td><nobr>29</nobr></td> <td>    yyparse();  </td></tr> <tr> <td><nobr>30</nobr></td> <td style="background-color:#f7f7f7">    </font><font style="color:blue">return</font><font style="font-size:11px"> 0;  </font></td></tr> <tr> <td><nobr>31</nobr></td> <td>}  </td></tr> <tr> <td><nobr>32</nobr></td> <td style="background-color:#f7f7f7"> </td></tr></tbody></table></div><br><br>I am interested in how to handle <strong>yacc</strong>'s <strong>$$</strong> construct in MGrammar. As you can see in lines 18 and 19 above, the <strong>$$</strong> holds the result of an expression calculated in the &quot;codebehind&quot; (for lack of a better term) for the expression. This allows the result to be used in parse trees as if it were brought in as a literal.<br><br>How would I do the same in MGrammar? I am familiar with navigating parse trees in .NET, so, if you say &quot;you need C# for that&quot;, that's fine ... in fact, I almost expect that answer. I know how to calculate the result in the # code, but where do I return the result of the expression to be used in the parse tree?<br><br>Thank you,<br>Jeff FergusonSun, 15 Mar 2009 21:53:21 Z2009-03-15T21:53:21Zhttp://social.msdn.microsoft.com/Forums/en-US/oslo/thread/625ba3a1-df8c-416a-917d-a4a99a0a8aa5#43da7e27-8d34-4850-a0f1-3a07468f9155http://social.msdn.microsoft.com/Forums/en-US/oslo/thread/625ba3a1-df8c-416a-917d-a4a99a0a8aa5#43da7e27-8d34-4850-a0f1-3a07468f9155Lars Wilhelmsenhttp://social.msdn.microsoft.com/Profile/en-US/?user=Lars%20WilhelmsenMGrammar Equivalent of Yacc's $$?Hi Jeff,<br><br> There's no directequivalent in MGrammar for Yacc's $$ macro, but there isn't a 1:1 relationship between these two technologies<br> either. MGrammar does both the work that Lex and Yacc solve together - and you can easily output nodes to the AST that will<br> do the same thing for you, e.g.:<br><br> syntax SomeSyntax = a:tNumber op:tOperator b:tNumber =&gt; Add { Left { a }, Right { b} };<br><br> In the backend you will have to walk the AST and take actions accordingly.<br><br> HTH,<br><br> --larsw<br> <hr class="sig">Lars Wilhelmsen | Senior Consultant | <a href="http://www.miles.no/">Miles</a>, Norway | <a href="https://mvp.support.microsoft.com/profile/Lars">Connected Systems MVP</a> | <a href="http://larswilhelmsen.com/">http://larswilhelmsen.com/</a>Mon, 16 Mar 2009 16:52:29 Z2009-03-16T16:52:29Zhttp://social.msdn.microsoft.com/Forums/en-US/oslo/thread/625ba3a1-df8c-416a-917d-a4a99a0a8aa5#62e236a5-e4e1-42bd-847c-215668e77a9fhttp://social.msdn.microsoft.com/Forums/en-US/oslo/thread/625ba3a1-df8c-416a-917d-a4a99a0a8aa5#62e236a5-e4e1-42bd-847c-215668e77a9fJeffFergusonhttp://social.msdn.microsoft.com/Profile/en-US/?user=JeffFergusonMGrammar Equivalent of Yacc's $$? Thanks, Lars ... as I said in my original question, &quot;you need C# for that&quot; is an acceptable answer. I just thought that, if there were some equivalent that I was missing, I wanted to find out what it was.Mon, 16 Mar 2009 17:16:04 Z2009-03-16T17:16:04Zhttp://social.msdn.microsoft.com/Forums/en-US/oslo/thread/625ba3a1-df8c-416a-917d-a4a99a0a8aa5#5528008c-5cf5-4d7d-ae4a-9bb8139d331bhttp://social.msdn.microsoft.com/Forums/en-US/oslo/thread/625ba3a1-df8c-416a-917d-a4a99a0a8aa5#5528008c-5cf5-4d7d-ae4a-9bb8139d331bjchasehttp://social.msdn.microsoft.com/Profile/en-US/?user=jchaseMGrammar Equivalent of Yacc's $$? Hi jeff,<br>I have implemented a grammar that lets me parse various binary expressions in MGrammar I could share with you if you're interested. I found it pretty hard to use MGrammar to parse epxressions and maintain precedence but I managed to figure it out.<br><br>Where:<br>x + y =&gt; (x + y)<br>x + y * z =&gt; x + (y * z)<br>x + y and z | w =&gt; x + (y and (z | w))<br><br>For example. Very tricky.Mon, 16 Mar 2009 17:57:41 Z2009-03-16T17:57:41Zhttp://social.msdn.microsoft.com/Forums/en-US/oslo/thread/625ba3a1-df8c-416a-917d-a4a99a0a8aa5#d2bd1559-d98b-4792-8be8-f249059a434fhttp://social.msdn.microsoft.com/Forums/en-US/oslo/thread/625ba3a1-df8c-416a-917d-a4a99a0a8aa5#d2bd1559-d98b-4792-8be8-f249059a434fJeffFergusonhttp://social.msdn.microsoft.com/Profile/en-US/?user=JeffFergusonMGrammar Equivalent of Yacc's $$? Hey there! Sure, I'd love to se it. Thanks!Mon, 16 Mar 2009 18:09:20 Z2009-03-16T18:09:20Zhttp://social.msdn.microsoft.com/Forums/en-US/oslo/thread/625ba3a1-df8c-416a-917d-a4a99a0a8aa5#9a7bfa0e-1da8-4873-8bf0-8a7186b89706http://social.msdn.microsoft.com/Forums/en-US/oslo/thread/625ba3a1-df8c-416a-917d-a4a99a0a8aa5#9a7bfa0e-1da8-4873-8bf0-8a7186b89706Lars Wilhelmsenhttp://social.msdn.microsoft.com/Profile/en-US/?user=Lars%20WilhelmsenMGrammar Equivalent of Yacc's $$?Hi again,<br><br> I assume that the problem you had with the expressions in your post is related to associativity and precedence. In<br> MGrammar you can use <b>precedence</b>, <b>left(n)</b> and <b>right(n)</b> to solve this.<br><br> For the basic mathematical operators, multiply and divide have higher precedence than plus and minus.<br><br> A small example of the use of left &amp; right:<br><br><i> syntax tOperator = left(1) tPlus =&gt; &quot;Plus&quot;<br>                         | left(1) tMinus =&gt; &quot;Minus&quot;<br>                         | left(2) tDivide =&gt; &quot;Divide&quot;<br>                         | left(2) tMultiply =&gt; &quot;Multiply&quot;<br>                         | right(3) tExponentiation =&gt; &quot;Exp&quot;;<br>        token tPlus = &quot;+&quot;;<br>        token tMinus = &quot;-&quot;;<br>        token tDivide = &quot;/&quot;;<br>        token tMultiply = &quot;*&quot;;<br>        token tExponentiation = &quot;**&quot;;</i><br><br> The precedence keyword will help you solve the classical &quot;danging else&quot; problem. A more thorough explanation of this<br> is done in the MGrammar specification that is shipped with the Oslo SDK.<br><br> HTH,<br><br> --larsw<br> <hr class="sig">Lars Wilhelmsen | Senior Consultant | <a href="http://www.miles.no/">Miles</a>, Norway | <a href="https://mvp.support.microsoft.com/profile/Lars">Connected Systems MVP</a> | <a href="http://larswilhelmsen.com/">http://larswilhelmsen.com/</a>Mon, 16 Mar 2009 19:18:22 Z2009-03-16T19:18:22Zhttp://social.msdn.microsoft.com/Forums/en-US/oslo/thread/625ba3a1-df8c-416a-917d-a4a99a0a8aa5#c35c66a8-67d6-4f67-bfb4-4813bb93124bhttp://social.msdn.microsoft.com/Forums/en-US/oslo/thread/625ba3a1-df8c-416a-917d-a4a99a0a8aa5#c35c66a8-67d6-4f67-bfb4-4813bb93124bPaul Vickhttp://social.msdn.microsoft.com/Profile/en-US/?user=Paul%20VickMGrammar Equivalent of Yacc's $$?I'm just catching belatedly up on forum threads, but I thought I'd interject that we're considering ways to evaluate expressions directly in grammars. It's a pretty useful feature, and we'd love to provide the full M expression capabilities on the RHS of a grammar production. So stay tuned!<br/><br/>Paul [MSFT]<hr class="sig">Paul Vick MSFTTue, 31 Mar 2009 22:28:26 Z2009-03-31T22:28:26Zhttp://social.msdn.microsoft.com/Forums/en-US/oslo/thread/625ba3a1-df8c-416a-917d-a4a99a0a8aa5#43d790b6-4ec3-4fb7-825a-b6fed9c8406fhttp://social.msdn.microsoft.com/Forums/en-US/oslo/thread/625ba3a1-df8c-416a-917d-a4a99a0a8aa5#43d790b6-4ec3-4fb7-825a-b6fed9c8406fLars Wilhelmsenhttp://social.msdn.microsoft.com/Profile/en-US/?user=Lars%20WilhelmsenMGrammar Equivalent of Yacc's $$?Hi Paul,<br/><br/> That would be awesome! :-)<br/><br/> --larsw<hr class="sig">Lars Wilhelmsen | Senior Consultant | <a href="http://www.miles.no/">Miles</a>, Norway | <a href="https://mvp.support.microsoft.com/profile/Lars">Connected Systems MVP</a> | <a href="http://larswilhelmsen.com/">http://larswilhelmsen.com/</a>Wed, 01 Apr 2009 12:09:08 Z2009-04-01T12:09:08Z