in reply to parens question

There's a deeper answer that has to do in part with how far the parser has to "look ahead" to disambiguate a statement. A lookahead of one (token) is easy for a parser to deal with. When the grammar calls for a lookahead of two or more, the mechanics of parsing -- particularly the mechanics of diagnosing an error and "recovering" to continue parsing -- get much more complicated. (Back when parsing was a hot topic, there where many dozen articles on parser error recovery.)

This is one reason why some languages use THEN with IF statements. As soon as the parser looks ahead one token and sees THEN, it knows that it's done parsing the IF condition. In Perl, the parenthesis serve the same function. When the parser looks ahead one token and sees ')' (having already accounted for nested parenthesis), it knows it has the condition parsed.

Consider a silly (and probably incorrect) example:

if $a == undef $b = 1; ^
If the parser, having recognized that it's dealing with an IF statement and is parsing the condition, is looking ahead at the single token '$b', how can it tell whether it has finished parsing the condition and is now looking at the following statement? The answer is, it can't.

Parsers are fun. Playing with one for a while can give one a much deeper appreciation for language design.

(And now someone who really knows the Perl parser will tell my why I'm full of it. ;-)

Replies are listed 'Best First'.
(tye)Re: parens question
by tye (Sage) on Jan 28, 2001 at 04:58 UTC

    Saying the same thing in a different way: An if after a statement ends quite simply at the ; token. If you left the parens off of the full if statement, the expression would end at the { that starts the block. But a { token, unlike a ; token, can also appear in an expression.

    So, the parens are required in the full if statement to make finding the end of the Boolean expression easy (easy enough that not a lot of look-ahead is required).

            - tye (but my friends call me "Tye")