Where can I find a formal grammar for the Perl programming language?

前端 未结 3 1284
春和景丽
春和景丽 2021-02-12 21:23

I understand that the Perl syntax is ambiguous and that its disambiguation is non-trivial (sometimes involving execution of code during the compile phase). Regardless, does Perl

3条回答
  •  孤城傲影
    2021-02-12 21:42

    Other people have posted this link before on similar questions, but I think it is fun and has a great case example: Perl Cannot Be Parsed (A Formal Proof).

    From that link:

    [Consider] the following devilish snippet of code, concocted by Randal Schwartz, and determine the correct parse for it:

    whatever / 25 ; # / ; die "this dies!";

    Schwartz's Snippet can parse two different ways: if whatever is nullary (that is, takes no arguments), the first statement is a division in void context, and the rest of the line is a comment. If whatever takes an argument, Schwartz's Snippet parses as a call to the whatever function with the result of a match operator, then a call to the die() function.

    This means that, in order to statically parse Perl, it must be possible to determine from a string of Perl 5 code whether it establishes a nullary prototype for the whatever subroutine.

    I just post this part to show that it gets really hard really quickly.

    Alternatively, many code/text editors can do a decent (though never great) job of syntax highlighting so you may start at those specs to see what they do. In fact you have inspired me, I think I will post a related question asking what editor best highlights Perl.

提交回复
热议问题