I want to use C language to write a php interpreter (simple). What steps should I take to start?

Source: Internet
Author: User
Can explain the execution of php programs, such as: & lt ;? Phpechohelloworld ..;? & Gt; can explain the execution of php programs, such:

 
Reply content:

1: lexical analyzer and syntax analyzer to obtain the syntax tree.

2: semantic analyzer is used to check whether the code is faulty. for example, 1 + null is not a legal program.

3: php has a table, and php is referenced by php. to solve the circular reference, you need to develop a garbage collector.

4: design a dynamic language instruction set and a stack-based VM (software simulation of this set of command-level CPU). This is the simplest practice and can quickly simulate the running process.


Then you get the result.

Do you want to use the PHP script language in your program or do you really want to be an interpreter?
If it is the former, reuse the existing interpreter.
Otherwise, go to @ vczh's answer. Obtain the token through lexical analysis-> obtain the AST through syntax analysis-> convert the AST to the IR intermediate instruction set-> and then run the IR intermediate instruction set on the vm.
Most interpreted scripting languages follow these steps. In fact, if you just run it, you can generate an AST and recursively traverse the AST. I did this a long time ago in alex1. However, in this case, the performance is poor, and GC is hard to do. A better way is to execute IR on a stack-based vm, as @ vczh said. Stack-based VMs are better than register-based VMs. The simplest method is to use regular expressions to perform brute force matching on the lexical unit, and then construct the abstract syntax tree. then, traverse the node and translate it into the C language syntax. For more information, see Zend Engine source code ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.