2

PHP 有一个 Bison 语法文件,这是否意味着 PHP 是一种完全无上下文的语言?

4

3 回答 3

2

如果您正在为 php 构建解析器,请查看现有的解析器:

https://github.com/nikic/PHP-Parser - 这是用 php 编写的,是一个独立的 php 解析器。

https://github.com/svalaskevicius/ionPulse/tree/master/ionParticles/ionPhp/phpParser - 这是 ionPulse IDE 的 php-support 插件的一部分,用 c++ 编写,功能测试在 <...>/ ionTests/phpparsertest.h [仍在进行中]

于 2012-05-13T12:49:15.620 回答
1

只是想我会提到这一点,以防你没有看到它,它可能会为你节省很多时间,除非这是为了纯粹的学习。

查看 PHP Tokenizer Functions,它将为您将源文件解析为令牌。然后,您可以跨过令牌以检查来源。

这个例子取自 PHP.net,它将源文件读入令牌,并在去掉注释的情况下重现它:

<?php
/*
* T_ML_COMMENT does not exist in PHP 5.
* The following three lines define it in order to
* preserve backwards compatibility.
*
* The next two lines define the PHP 5 only T_DOC_COMMENT,
* which we will mask as T_ML_COMMENT for PHP 4.
*/
if (!defined('T_ML_COMMENT')) {
   define('T_ML_COMMENT', T_COMMENT);
} else {
   define('T_DOC_COMMENT', T_ML_COMMENT);
}

$source = file_get_contents('example.php');
$tokens = token_get_all($source);

foreach ($tokens as $token) {
   if (is_string($token)) {
       // simple 1-character token
       echo $token;
   } else {
       // token array
       list($id, $text) = $token;

       switch ($id) { 
           case T_COMMENT: 
           case T_ML_COMMENT: // we've defined this
           case T_DOC_COMMENT: // and this
               // no action on comments
               break;

           default:
               // anything else -> output "as is"
               echo $text;
               break;
       }
   }
}
?>
于 2012-05-11T20:56:18.147 回答
-2

I think you are mixing math with interpreted parsing.

Have a look at structures and data then determine the rationale behind your question.

于 2012-05-11T20:43:53.370 回答