0

I have a grammar:

B :B1 OR B2
  |E R E
  ;
E : id
  ;

Here B1 and B2 are same as B. Now i want to use the attributes of B1 while applying semantic actions for B2 i.e I want to pass the attributes of B1 to B2 in BISON/YACC.

For eg. to parse the given string:

a<b || c<d

I want the desired output to be:

L1:
     if a<b goto L2
     goto L3
L3 : if c<d goto L2
     goto L4

L2: //True Section//

L4: 

I will be really thankful if someone helps me to solve this.

  • backpatching is simpler. Building a complete AST and then generating code is even simpler. But there is a hack: the semantic value of `B1` is `$-2` inside `B2`, except that you'll have to explicitly write the type tag (`$-2`) since bison can't know what the type is, or even if the usage is typesafe. Also, make sure that the usage *is* typesafe. There's a section in the manual about this. But personally, I'd strongly recommend using one of the simpler solutions until you have some more experience – rici Apr 20 '20 at 20:38
  • Can You elaborate me the way you got the semantic value of B1 as $-2 inside B2. Also I would request you to kindly let me know how I can perform backpathing or anyplace from where I can learn it. – Anonymoustez Apr 21 '20 at 04:37
  • Here's an answer I wrote several years ago about backpatching https://stackoverflow.com/a/21297535/1566221. Here's a slide from lecture notes using attribute grammars: http://www.csd.uwo.ca/~moreno//CS447/Lectures/IntermediateCode.html/node7.html (based on a section in the Dragon Book). These and other references available with an internet search. – rici Apr 21 '20 at 15:18

0 Answers0