Задайте вопросЗадайте вопрос
 

ОтвеченоMGrammar Equivalent of Yacc's $$?

  • 15 марта 2009 г. 21:53JeffFerguson Медали пользователяМедали пользователяМедали пользователяМедали пользователяМедали пользователя
     С кодом
    Consider the following simple calculator implemeted in yacc:

    1 %{
    2     #include <stdio.h>  
    3     int yylex(void);  
    4     void yyerror(char *);  
    5 %}  
    6  
    7 %token INTEGER  
    8  
    9 %%  
    10  
    11 program:  
    12         program expr '\n'         { printf("%d\n", $2); }  
    13         |   
    14         ;  
    15  
    16 expr:  
    17         INTEGER  
    18         | expr '+' expr           { $$ = $1 + $3; }  
    19         | expr '-' expr           { $$ = $1 - $3; }  
    20         ;  
    21  
    22 %%  
    23  
    24 void yyerror(char *s) {  
    25     fprintf(stderr, "%s\n", s);  
    26 }  
    27  
    28 int main(void) {  
    29     yyparse();  
    30     return 0;  
    31 }  
    32  


    I am interested in how to handle yacc's $$ construct in MGrammar. As you can see in lines 18 and 19 above, the $$ holds the result of an expression calculated in the "codebehind" (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.

    How would I do the same in MGrammar? I am familiar with navigating parse trees in .NET, so, if you say "you need C# for that", 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?

    Thank you,
    Jeff Ferguson

Ответы

Все ответы