I am writing an llvm code generation demo for a certain language which includes if statement. Here are the rules and the actions corresponding to my question:
IfStatement : IF CondExpression THEN Statement {if_Stmt(string($2),string($4));} %prec LOWER_THAN_ELSE ;
| IF CondExpression THEN Statement ELSE Statement {if_else_Stmt(string($2),string($4),string($6));}
;
CondExpression : Expression Relop Expression { $$ = operation($2,string($1),string($3));printf("Relop value : %s \n",$2);}
| Expression {$$ = $1;}
;
Relop : EE {$$ = (char *)(string("icmp eq ").c_str());printf("%s\n",$$);}
| NE {$$ = (char *)(string("icmp ne ").c_str());}
| LT {$$ = (char *)(string("icmp slt ").c_str());}
| GT {$$ = (char *)(string("icmp sgt ").c_str());}
| LTE {$$ = (char *)(string("icmp sle ").c_str());}
| GTE {$$ = (char *)(string("icmp sge ").c_str());}
;
The CondExpression rule should parse the conditional expression. I am using print function to print the value of Relop token which is of type < char * >. The Relop should have the value of the conditional tokens inside the string function as shown above in the code. However, the result of the print function is 0
Relop value : 0
and the result of the second print inside Relop is correct,
Relop value : icmp eq
why the Relop value in the CondExpression is 0 and how to make it take the correct value returned from Relop rule.