develooper Front page | perl.perl5.porters | Postings from March 2014

Re: [perl #121501] Empty code block {} causes syntax error (misparsed as anonymous hash constructor?) before 'sub'

Thread Previous | Thread Next
From:
Ed Avis
Date:
March 25, 2014 16:59
Subject:
Re: [perl #121501] Empty code block {} causes syntax error (misparsed as anonymous hash constructor?) before 'sub'
Message ID:
loom.20140325T175337-29@post.gmane.org
Eric Brine <ikegami <at> adaelis.com> writes:

>>where {} is currently parsed as a hash constructor but that
>>gives a syntax error, perl should try instead to parse it as a block.

>Perl uses an LR parser and resolves these conflicts by looking ahead.
> 
>"{" is determined to be a hash constructor by looking ahead and seeing "}".
>It would have to look even ahead further to determine a syntax error follows

Hmm, I see.  Suppose for the moment that making the parser backtrack is not
practical.  Would there be some new heuristic that could be used instead?
For example, suppose you declare that the empty anonymous hash {} can never
appear in an lvalue position.  Might that work?

At the moment you get

% perl -E '{} ++'
Can't modify anonymous hash ({}) in postincrement (++)

but with the changed heuristics, {} would never even get as far as being
parsed as an lvalue for the ++ operator to apply to it, so you'd instead see
a plain 'syntax error'.

-- 
Ed Avis <eda@waniasset.com>


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About