19

I am wondering if someone has compiled an EBNF for PHP somewhere. I found both this site and this site. Both seem like they are incomplete, though. This question is very similar, but it's a year old. I would like to know if there has been any progress made.

Community
  • 1
  • 1
Azmisov
  • 6,493
  • 7
  • 53
  • 70

4 Answers4

15

You may want to have a look at the yacc grammar PHP uses internally. I know, it's not EBNF, but maybe it's okay for your needs.

You may also want to look at the re2c lexer definition.

NikiC
  • 100,734
  • 37
  • 191
  • 225
  • The link to the grammar seems broken, but I think the file is also on [GitHub](https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y). – Fabian Lauer Dec 25 '16 at 08:50
  • This awkward moment when php language has a specification, while typescript [does not](https://github.com/Microsoft/TypeScript/issues/15711)... – Klesun Oct 21 '20 at 09:18
8

The site from which I've taken the following links is in italian but we have:

dierre
  • 7,140
  • 12
  • 75
  • 120
1

ANTLR 4 PHP grammar is also available: https://github.com/antlr/grammars-v4/tree/master/php.

Features

  • Heterogeneous code processing (islands):
    • HTML
    • Script
    • CSS
    • PHP
    • Heredoc
  • Alternative syntax.
  • Heredoc.
  • Interpolation strings (but not fully covered).
  • aspTags.
  • Improved lexer error processing with artificial string fragments (for example double closed quote at the end: <div attr='value'' />).

Testing

Grammar has been tested (parsing without errors) on the following projects:

Also, this parser has been tested on plenty number of PHP files from different CMS (~70000 files).

Ivan Kochurkin
  • 4,413
  • 8
  • 45
  • 80
0

An ANTLR3 grammar is available her:

  http://code.google.com/p/phpparser/

The above grammar isn't compatible with ANTLR4, the current ANTLR release.

SteAp
  • 11,853
  • 10
  • 53
  • 88