What does "a---b" mean?
Adam Kao
adamk at mit-amt.MEDIA.MIT.EDU
Wed Jan 3 18:01:00 AEST 1990
I've been thinking about a C interpreter and ran into this problem.
Harbison & Steele says the lexical scanner always makes the largest
token, so I assume the example from the subject line would be tokenized
as "(a--) - b", and in general "a---...---b" would come out as
"(((a--)--)...--)-b" or something (ignoring what that might mean).
But it's not clear to me that this is consistent with the precedence
and associativity rules (mainly because I have trouble understanding
them!). Is it actually true that the lexical scanner is defined to be
"dumb" and MUST ignore syntax while tokenizing? Then "a--b" would
always generate a compile error, right? (Waitaminute! This explains
that problem my friend was having . . .)
This is further complicated by the requirement that compound
assignment operators actually be considered as two separate tokens
during the scanning phase ("a - = b" is "a -= b" . . .) but the tokens
are combined into one token before being sent off to the syntax
analyzer.
More generally I'm not sure I understand how C handles ambiguity with
multiple-character tokens. To summarize, my understanding is:
1. The lexical analyzer scans left to right making the biggest token possible.
2. Compound assignment tokens (ONLY) can be read as two consecutive
tokens (ie separated by whitespace ONLY) that are IMMEDIATELY
joined into one token.
3. Remaining ambiguity is resolved by simple (hah!) precedence and
associativity rules. All remaining ambiguities can be resolved by
these rules, which essentially answer the questions "When do
parentheses enclose this operator?" (precedence) and "Which
operator does this operand go with?" (associativity). In
particular, the questions "Which operand does this operator go
with?" and "What operator(s) is this?" never arise or are irrelevant.
Is this correct? Is it complete?
Any other suggestions or "gotcha's" about C interpreters are welcome.
Thank you for your time.
Adam
More information about the Comp.lang.c
mailing list