...how does compiler (parser) know that empty condition in while is error and in for as Infinite?
Because the language definition specifies it, in both the syntax (grammar) and semantics.
Here's the syntax for a while
loop:
while ( expression ) statement
and here's the syntax for a for
loop (as of C2011):
for ( expressionopt ; expressionopt ; expressionopt ) statement
for ( declaration expressionopt ; expressionopt ) statement
The subscript opt in each expressionopt
in the for
statement indicates that the corresponding expression is optional. This is reinforced by the text:
6.8.5.3 The for statement
...
2 Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a
nonzero constant.
By contrast, the controlling expression of the while
statement is not marked as optional, which is also reinforced in the text:
6.8.5.1 The while statement
1 The evaluation of the controlling expression takes place before each execution of the loop body.
Not much room there to interpret that the controlling expression may be omitted.