4 ms·
> A classic example is that in C++ it is impossible to know whether << is brackets around template arguments, or the left bit shift operator, or the stream outp
by johannes1234321 7y ago
> A classic example is that in C++ it is impossible to know whether << is brackets around template arguments, or the left bit shift operator, or the stream output operator, without arbitrary lookahead--you need to parse the rest of the statement in order to decide which one it is in the current statement's context.
The classic example, I guess, is the most vexing parse: https://en.m.wikipedia.org/wiki/Most_vexing_parse https://en.m.wikipedia.org/wiki/Most_vexing_parse
"""
The line
TimeKeeper time_keeper(Timer());
is seemingly ambiguous, since it could be interpreted either as
a variable definition [...]
a function declaration [...]
"""
These things you can only resolve by having the symbol tables around and checking. The parser alone can't tell.
A language could avoid this i.e. by requiring specific keywords or having more distinct syntax.
Humans (programmers) however often can deal with ambiguity, we have it in humannlanaguage as well and context on most cases helps and I argue those aren't practical problems. (While that's only opinion)
- kyllo 7y agoYes and programming languages are human interfaces to machine instructions, so context sensitivity can be manageable and even desirable to human users of the language, even if it makes the implementation of the language interpreter more complex or less elegant. Programming language designers make this trade-off all the time.