Home Backend Development PHP Tutorial PHP syntax analyzer: RE2C && BISON summary_PHP tutorial

PHP syntax analyzer: RE2C && BISON summary_PHP tutorial

Jul 13, 2016 pm 05:54 PM
amp php Analyzer Summarize I us of grammar project


Before this, I have tried a project that automatically generates so extensions for our PHP code,

Compiled into PHP, I call it phptoc.

However, due to various reasons, this project was suspended.

I wrote this article firstly because there is too little information in this area, and secondly to summarize what I have learned for future reference. If you can understand PHP syntax analysis

The research on PHP source code will go to a higher level ^.^…

I try to write it as plainly and understandably as possible.

This project idea originated from Facebook’s open source project HipHop.

Actually, I am skeptical about the 50%-60% performance improvement of this project. Fundamentally speaking, if PHP uses APC cache, will its performance be low?

As for HipHop, I haven’t tested it yet, so I can’t say for sure.

PHPtoc, I just want to liberate C programmers, hoping to achieve the goal of allowing PHPer to use PHP code to write an extension close to PHP extension performance,

The process is as follows: read the PHP file, parse the PHP code, perform a syntax analyzer on it, generate the corresponding ZendAPI, and compile it into an extension.

Get to the point

The most difficult thing here is the syntax analyzer. Everyone should know that PHP also has its own syntax analyzer. The current version uses re2c and Bison.

So, naturally I also used this combination.

It is not practical to use PHP's syntax analyzer, because you need to modify zend_language_parser.y and zend_language_scanner.l and recompile, which is not only difficult, but may also affect PHP itself.

So I decided to rewrite a set of my own syntax analysis rules. This function is equivalent to rewriting PHP's syntax analyzer, and of course some uncommon ones will be discarded.

re2c && yacc/bison, by referencing its corresponding files, then compile them into a *.c file, and finally compile it with gcc to generate

Into our own program. Therefore, they are not fundamentally a syntax analysis program. They just generate an independent C text based on our rules

file, this c file is the real syntax analysis program we need. I prefer to call it a syntax generator. As shown below:

Note: a.c in the picture is the final code generated by the scanner. .

re2c scanner, if the scanning rule file we write is called scanner.l, it will scan the content of the PHP file we wrote, and then scan it according to

The rules we wrote generate different tokens and pass them to parse.

The (f)lex grammar rule we wrote, for example, we call it Parse.y

It will be compiled into a parse.tab.h, parse.tab.c file through yacc/bison. Parse performs different operations according to different tokens

For example, our PHP code is “echo 1″;

Scan there is a rule:

"echo" {

return T_ECHO;
}
The scanner function scan will get the "echo 1" string, and it will loop through this piece of code. If it finds an echo string, it will return the token as a keyword: T_ECHO,

parse.y and scanner.l will generate two c files, scanner.c and parse.tab.c respectively, and compile them together with gcc.

I will talk about it in detail below

If you are interested, you can check it out. I have also translated a Chinese version,

It’s not over yet, I will post it later.

re2c provides some macro interfaces, which we use. I simply translated them. My English is not good and there may be errors. If you need the original text, you can go to the address above to view it.

Interface code:
Unlike other scanner programs, re2c does not generate a complete scanner: the user must provide some interface code. Users must define the following macros or other corresponding configurations.
YYCONDTYPE
With -c mode you can use the -to parameter to generate a file: use conditions containing enumeration types. Each value will be used as a condition in the rule set.
YYCTYPE
Used to maintain an input symbol. Usually char or unsigned char.
YYCTXMARKER
*For expressions of type YYCTYPE, the context of the generated code traceback information will be saved in YYCTXMARKER. The user needs to define this macro if the scanner rule requires the use of one or more regular expressions in the context.
YYCURSOR
The expression pointer of type *YYCTYPE points to the currently input symbol, and the generated code is matched as a symbol. At the beginning, YYCURSOR is assumed to point to the first character of the current token. At the end, YYCURSOR will point to the first character of the next token.
YYDEBUG(state,current)
This is only required when the -d flag is specified. It is very easy to debug the generated code when calling user-defined functions.
This function should have the following signature: void YYDEBUG(int state,char current). The first parameter accepts state , with a default value of -1 The second parameter accepts the current position of the input.
YYFILL(n)
When the buffer needs to be filled, the generated code will call YYFILL(n): providing at least n characters. YYFILL(n) will adjust YYCURSOR, YYLIMIT, YYMARKER and YYCTXMARKER as needed. Note that in typical programming languages, n is equal to the length of the longest keyword plus one. Users can define YYMAXFILL once in /*!max:re2c*/ to specify the maximum length. If -1 is used, YYMAXFILL will block once after /*!re2c*/.
YYGETCONDITION()
If -c mode is used, this definition will obtain the condition set before the scanner code. This value must be initialized to the type of the enumeration YYCONDTYPE.
YYGETSTATE()
If -f mode is specified, the user needs to define this macro. If so, in order to get the saved state at the beginning of the scanner, the generated code will call YYGETSTATE(). YYGETSTATE() must return a signed integer. If this value is -1, it tells the scanner that this is the first time. Execution, otherwise this value is equal to the state saved by previous YYSETSTATE(s). Otherwise, the scanner will call YYFILL(n) immediately after resuming operation.
YYLIMIT
The type of expression *YYCTYPE marks the end of the buffer (YYLIMIT(-1) is the last character of the buffer). The generated code will continuously compare YYCORSUR and YYLIMIT to determine when to fill the buffer.
YYSETCONDITION(c)
This macro is used to set conditions in conversion rules. It is only useful when -c mode is specified and conversion rules are used.
YYSETSTATE(s)
The user only needs to define this macro when specifying -f mode. If so, the generated code will call YYSETSTATE(s) before YYFILL(n). The parameter of YYSETSTATE is a signed integer called a unique identifier. A specific YYFILL(n) instance.
YYMARKER
An expression of type *YYCTYPE, the generated code saves the traceback information to YYMARKER. Some simple scanners may not be useful.
The scanner, as the name suggests, scans files to find key codes.

Scanner file structure:

/* #include file*/
/*Macro definition*/
//Scan function
int scan(char *p){
/*Scanner rules area*/
}
//Execute the scan function and return the token to yacc/bison.
int yylex(){
         int token;
​​​​ char *p=YYCURSOR;//YYCURSOR is a pointer pointing to our PHP text content
​​​​​while(token=scan(p)){//The pointer p will be moved here to determine whether it is the scanner we defined above...
                    return token;
}
}
int main(int argc,char**argv){
BEGIN(INITIAL);//
YYCURSOR=argv[1];//YYCURSOR is a pointer pointing to our PHP text content,
yyparse();
}
BEGIN is a defined macro

#define YYCTYPE char //Type of input symbol
#define STATE(name) yyc##name
#define BEGIN(n) YYSETCONDITION(STATE(n))
#define LANG_SCNG(v) (sc_globals.v)
#define SCNG LANG_SCNG
#define YYGETCONDITION() SCNG(yy_state)
#define YYSETCONDITION(s) SCNG(yy_state)=s
The yyparse function is defined in yacc,

There is a key macro in it: YYLEX

#define YYLEX yylex()

It will execute the scanner's yylex

It may be a little twisted, so re-tie it:

In scanner.l, by calling parse.y parser function yyparse, this function calls yylex of scanner.l to generate the key code token, yylex

Return the scanner's

The token is returned to parse.y, and parse executes different codes based on different tokens.

Example:

scanner.l
#include "scanner.h"
#include "parse.tab.h"
int scan(char *p){
/*!re2c
" BEGIN(ST_IN_SCRIPTING);
                    return T_OPEN_TAG;
}
"echo" {

return T_ECHO;
}
[0-9]+ {
                     return T_LNUMBER;
}
*/
}
int yylex(){
           int c;

//      return T_STRING;
         int token;
char *p=YYCURSOR;
​​​​while(token=scan(p)){
                    return token;
}
}

int main (int argc,char ** argv){
​​​​BEGIN(INITIAL);//Initialization
YYCURSOR=argv[1];//Put the string entered by the user into YYCURSOR
      yyparse();//yyparse() -》yylex()-》yyparse()
         return 0;
}
Such a simple scanner is made,

What about the parser?

The parsers I use are flex and bison. . .

About the file structure of flex:

%{
/*
The C code segment will be copied verbatim into the C source file generated after lex compilation
You can define some global variables, arrays, function routines, etc...
*/
#include
#include "scanner.h"
extern int yylex();//It is defined in scanner.l. .
void yyerror(char *);
# define YYPARSE_PARAM tsrm_ls
# define YYLEX_PARAM tsrm_ls
%}
{Definition section, where token is defined}
//This is the key. The token program does the switch based on this.
%token T_OPEN_TAG
%token T_ECHO
%token T_LNUMBER
%%
{Rule Section}
start:
T_OPEN_TAG{printf("startn"); }
|start statement
;
statement:
T_ECHO expr {printf("echo :%sn",$3)}
;
expr:
T_LNUMBER {$$=$1;}
%%
{User code snippet}
void yyerror(char *msg){
​​​​ printf("error:%sn",msg);
}
In the rule section, start is the beginning. If scan recognizes the PHP start tag, it will return T_OPEN_TAG, then execute the code in the brackets and output start.

In scanner.l, the call to scan is a while loop, so it will check to the end of the php code,

yyparse will switch based on the tag returned by scan, and then goto to the corresponding code. For example, yyparse.y finds that the current token is T_OPEN_TAG,

It will be mapped to line 21 corresponding to parse.y through the macro #line, the position of T_OPEN_TAG, and then executed


So, what did TOKEN do after it was returned to yyparse?

In order to be more intuitive, I use gdb tracking:

PHP syntax analyzer: RE2C && BISON summary_PHP tutorial

At this time yychar is 258, what is 258?

PHP syntax analyzer: RE2C && BISON summary_PHP tutorial

258 is the enumeration type data automatically generated by bison.

Continue

YYTRANSLATE macro accepts yychar and returns the corresponding value

#define YYTRANSLATE(YYX)                                                                      ((unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK)

/* YYTRANSLATE[YYLEX] -- Bison symbol number corresponding to YYLEX.  */
static const yytype_uint8 yytranslate[] =
{
       0,     2,     2,     2,     2,     2,     2,     2,     2,     2,
       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
       2,     2,     2,     2,     2,     2,     2,     2,    27,     2,
      22,    23,     2,     2,    28,     2,     2,     2,     2,     2,
       2,     2,     2,     2,     2,     2,     2,     2,     2,    21,
       2,    26,     2,     2,     2,     2,     2,     2,     2,     2,
       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
       2,     2,     2,    24,     2,    25,     2,     2,     2,     2,
       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
       2,     2,     2,     2,     2,     2,     1,     2,     3,     4,
       5,     6,     7,     8,     9,    10,    11,    12,    13,    14,
      15,    16,    17,    18,    19,    20
};
yyparse拿到这个值,不断地translate,

 PHP syntax analyzer: RE2C && BISON summary_PHP tutorial

bison会生成很多用来映射的数组,将最终的translate保存到yyn,

这样bison就能找到token所对应的代码

  switch (yyn)
    {
        case 2:

/* Line 1455 of yacc.c  */
#line 30 "parse.y"
    {printf("startn"); ;}
    break;
这样,不断循环,生成token逐条执行,然后解析成所对应的zend 函数等,生成对应的op保存在哈希表中,这些不是本文的重点,

 

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/477941.htmlTechArticle在这之前,我曾经尝试过一个项目,就是将我们的PHP代码自动生成so扩展, 编译到PHP中,我叫它 phptoc。 但是由于各种原因,暂停了此项目...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles