Divick wrote:
Hi all,
can anyone tell me what kind of parser is used to parse C++
language i.e. LALR or LL (k?) ? Or is it implementation dependent?
The C and the C++ grammars are not context-free, so they cannot be
handled by these parsing techniques alone.
The lexical category of tokens depends on their semantics (how they are
declared).
In order to decide how to categorize a lexeme, the lexical analyzer
must sometimes make use of semantic information gathered up to that
point.
The reason for this question is that I was trying to figure out what
the output of the following program be:
int main()
{
int a = 10;
printf("%d %d %d\n",++a,a++,a);
return 0;
}
and I am getting two different results on two different compilers.
This is because of semantics, not grammar. In C, the grammar for a
function call is:
postfix-expression:
...
postfix-expression ( argument-expression-list_opt )
...
argument-expression-list:
assignment-expression
argument-expression-list , assignment-expression
As you can see, this is left recursion which generates a nice, linear
parse tree sloping to the left.
The shape of that tree has precisely nothing to do with the evaluation
order of those arguments, which is unspecified.
one definitely answer based on how the laguage is parsed (lef to right
or right to left) to tell the output of the above program?
What does /parsing/ have to do with /evaluation/ semantics?
You are overlooking the basic fact that control flow can jump around
arbitrarily within a program, in spite of that program being produced
from source code that is read from top to bottom.
If parsing determined evaluation order, then programs would execute
from top to bottom, and wouldn't have any loops or function calls.