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 ~