0

In C++ programming language

int a=2;
a++=5;
++a=6;

From above expressions Only 2nd expression is valid . How compiler translate these expressions , so that it's able to find error: lvalue required as left operand of assignment .

I have tried following translation scheme for these expressions but it's wrong .

...
unary-expression:
    postfix-expression{
    $<info.place>$ = $<info.place>1;
}
|PLUSPLUS unary-expression{
    $<info.place>$ = $<info.place>2;
    char *temp =newTemp(); 
    genCode(temp,$<info.place>2,"+","1");
    genCode($<info.place>2,temp);
}
...
postfix-expression:
    primary-expression{
    $<info.place>$ = $<info.place>1;
}
|postfix-expression PLUSPLUS{
    $<info.place>$ = newTemp(); //Generate temporary var
    genCode($<info.place>$,$<info.place>1); //Assign temp with a 
    genCode($<info.place>1,$<info.place>$,"+","1");//Increment a by 1 
}
...

When i use a++=5 then it generates

0000 : T0   =   a
0001 : a    =   T0  +   1
0002 : T1   =   5
0003 : T0   =   T1

What are the changes have to be made in action so that it works like C++ .

sonus21
  • 5,178
  • 2
  • 23
  • 48

1 Answers1

1

How compiler translate these expressions, so that it's able to find error: lvalue required as left operand of assignment .

It doesn't translate the first expression. It gives a semantic error instead. Showing us what code you've generated and how you did it is futile. There isn't any correct generated code for this.

And in fact these productions themselves are irrelevant. You need to add a semantic check in the assignment production to ensure the LHS is an lvalue, and print an error if it isn't.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    I can't see any relevant update. You're still showing code generation from the wrong productions. I'm wondering if you have understood one word of my answer. – user207421 Mar 27 '15 at 08:43