0

I am making a translator with bison / flex, but I have a problem because when implementing the rules I cannot print in only one and I have not been able to send the text to print it in the main rule, this is the code of the rules is the following:

%%

initial:            RES TEXT EOL RES RES TEXT BOP main BCL      {printf("%s", $8);}


main:               RES RES RES TEXT POP VAR RES TEXT PCL BOP content BCL   {$$ = ("%s", $11);}

content:            declaration                     {$$ = ("%s", $1);}
            |   operations                      {$$ = ("%s", $1);}
            |   print                           {$$ = ("%s", $1);}
            |   content content
            ;

declaration:            VAR TEXT ASG TEXT EOL                   {aux = ("%s = %s\n", $2, $4); $$ = aux;}

operations:         TEXT ASG TEXT OPE TEXT EOL              {$$ = ("%s = %s %s %s\n", $1, $3, $4, $5);}

print:          SOP POP TEXT PCL EOL                    {$$ = ("print(%s)", $3);}

%%
  • What do you expect `$$ = ("%s", $1);` does? Aside from changing `$n` to a reference to an entry on the parser's semantic value stack, bison does not add any superpowers (or syntax) to C. – rici Sep 20 '20 at 05:52
  • I was using it to save it so that I could then print it by calling it in another rule – Yeferson Gallo Sep 20 '20 at 15:20
  • That's not how C works. `("%s", x)` is not a C expression. It might be the arguments to the `printf` function, but that doesn't make it a value in itself. – rici Sep 20 '20 at 15:49
  • Perhaps I misunderstood your reply. But in general, we ask here that you post [mre], with an emphasis on the word "minimal". Commented-out code and syntax errors are not minimal, nor are the reproducible. They only serve to confuse the reader. I'll try to answer you question on [es.so]. – rici Sep 20 '20 at 15:51
  • I want to assign a value when validating a rule, for example when validating the rule SOP POP TEXT PCL EOL that the text print ("") is assigned and within the parentheses it shows what was in the rule as TEXT and then when calling the rule in initial that value is printed – Yeferson Gallo Sep 20 '20 at 17:22
  • Yeah, I get it. But you are programming in C, not some other language which lets you create strings out of templates. If you need to create a string, you need to do the heavy lifting yourself. That part has nothing to do with bison. (Of course, you could use `asprintf` to help you with the templates, since you're working in a Gnu environment.) – rici Sep 20 '20 at 17:36

0 Answers0