Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 have a formal grammar (albeit ambiguous and/or context-sensitive)?

like image 547
Adam Paynter Avatar asked Jan 07 '11 12:01

Adam Paynter


People also ask

Does Perl still use 2022?

Why it is still relevant in 2022. Perl is not going away even if it tends to be less trendy than other modern languages. It is used in production codebases of many companies, for tasks as diverse as web development, databases access, log analysis or web crawling. It is a core component of most unix-like systems.

What language is Perl written in?

Perl is implemented as a core interpreter, written in C, together with a large collection of modules, written in Perl and C.

How long does it take to learn Perl?

As a rough guide, expect to spend up to two months learning the basics of Perl, with an hour of study per day. If you want to use Perl professionally, you need to study for up to six months to build the understanding you would need to use Perl to the extent required in the workplace.

What programming language is Perl most similar to?

Perl is a lot similar to C syntactically and is easy for the users who have knowledge of C, C++. Python is a widely used general-purpose, high level programming language. It was initially designed by Guido van Rossum in 1991 and developed by Python Software Foundation.


2 Answers

From perlfaq7

Can I get a BNF/yacc/RE for the Perl language?

There is no BNF, but you can paw your way through the yacc grammar in perly.y in the source distribution if you're particularly brave. The grammar relies on very smart tokenizing code, so be prepared to venture into toke.c as well.

In the words of Chaim Frenkel: "Perl's grammar can not be reduced to BNF. The work of parsing perl is distributed between yacc, the lexer, smoke and mirrors."

To see the wonderful set of examples of WHY it's pretty much near impossible to parse Perl due to context influences, please look into Randal Schwartz's post: On Parsing Perl

In addition, please see the discussion in "Perl 5 Internals (Chapter 5. The Lexer and the Parser)" by Simon Cozens.


Please note that the answer is different for Perl6:

  • There exists a grammar for Perl6

  • Rakudo Perl has its own version of the grammar

like image 197
DVK Avatar answered Oct 04 '22 20:10

DVK


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.

like image 38
Joel Berger Avatar answered Oct 04 '22 21:10

Joel Berger