4 ms·
I love the explanation: It starts by tokenizing the PHP code into tokens, which it then uses to build an AST tree. Once the tree has been constructed, the scri
by DanWaterworth 14y ago
I love the explanation:
It starts by tokenizing the PHP code into tokens, which it then uses to build an AST tree. Once the tree has been constructed, the script compiles it into JavaScript that can be interpreted by the VM and then executes it.
What you've done there is you've explained the frontend processing that every compiler does, (tokenizing, building an AST) and missed out the part that is entirely specific to this particular one.
- vidarh 14y agoI mostly agree with the spirit of your comment. But to nitpick a bit: While most compilers today build an AST, it's by no means true that all do. Some very influential compilers through history explicitly does not. E.g. Niklaus Wirth's languages, such as Pascal and Oberon are explicitly designed so that they can easily be compiled with single pass compilers that call the code generator to emit code from the parser instead of going via an AST, because he as more concerned with small easily understandable compilers than complex optimizations. It's not nearly as common as it was, but it was a fairly widespread practice in compilers for languages where it is possible back when memory and disk space was still in short supply. It's also not true that all compilers tokenize by any meaningful interpretation. While e.g. Wirth's Pascal-P compiler arguably tokenize, compact recursive descent compilers for languages with as small grammar as Wirth's languages can and often do blur the lines significantly, or have no tokenization as a separate step from the parser at all.
- DanWaterworth 14y agoYou're technically right, but I was exaggerating for effect.
- mahmud 14y agoAlso, the 'T' in "AST" stands for tree. Not all intermediate-represenatations are trees: some are linear, like stack and register operations, others are graphs, etc. But you were right, that description is .. PHPish.
- DanWaterworth 14y agoThe AST is typically the first generated intermediate representation and by definition it relates to the textual form of the language. All textual forms can be viewed as trees.
- napillo 14y agoIs a tree without branches still a tree?