I want to write a PHP interpreter in C (simple on the line) where to start, what steps to go through?

Source: Internet
Author: User
Can explain the execution of PHP programs, such as:

  
  Echo "Hello world." ; ?>

Reply content:

1: Lexical analyzer, parser, get syntax tree.

2: Semantic parser, used to check the code is not a problem, for example, 1+null is not a legitimate program.

3:php has table,php by reference, in order to solve the circular reference, you need to develop a garbage collector.

4: Design a set of instructions for a dynamic type language, design a stack-based VM (software to simulate this set of instruction-level CPUs), which is the simplest way to quickly simulate a running process.


And then you get the results.

Do you want to use the PHP scripting language in your program or do you really want to make an interpreter?
If it is the former, reuse the existing interpreter.
Otherwise, go to the @vczh answer. Lexical analysis obtained token-> syntax analysis obtained ast->ast conversion to IR intermediate instruction set, and then run the IR intermediate directive on the VM.
Most of the explanatory scripting languages are similar to these steps. In fact, if you are just running, generating an AST, then recursively traversing the AST is also possible. I was so early in the ALEX1 version is so dry, hehe. In doing so, however, the performance is poor and the GC is difficult to do. The better way is to do the IR with a stack-based VM, as @vczh says. Stack-based VMS are better than register-based VMS. The simplest use of regular to violent match lexical units, and then constructs the abstract syntax tree, then the traversal node translated into C language syntax 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.