The JavaScript interpreter, also known as the "JavaScript engine", is a virtual machine that specializes in processing JavaScript scripts and is generally included with web browsers; the JavaScript interpreter has four components: lexical analyzer, syntax analyzer Parser, bytecode generator, bytecode interpreter.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
What is a javascript interpreter?
JavaScript is a scripting language that needs to be interpreted and executed by others. This other person is the JavaScript interpreter . It reads a JS statement, translates it, executes it, then reads the next JS statement, and the cycle begins again.
The JavaScript interpreter, also known as the JavaScript engine, is a virtual machine that specializes in processing JavaScript scripts and is generally included with web browsers. Its function is to execute JavaScript source code.
The JavaScript parsing engine is a program that can "read" JavaScript code and accurately provide the results of the code execution.
Composition of JavaScript interpreter
The JavaScript interpreter has four components: lexical parser, syntactic parser, and bytecode generation processor, bytecode interpreter.
Lexical parser
It can split lines of code source code into the smallest units of word meaning. The so-called semantic unit is the smallest single character or combination of characters that cannot be divided grammatically.
var number = 2; //解析成下面的json格式 [ { 'type':'keyword', 'value':'var' }, { 'type':'identifier', 'value':'number', }, { 'type':'Punctuator', 'value':'=', }, { 'type':'Numeric', 'value':'2', } { 'type':'Punctuator', 'value':';' } ]
Syntax analyzer
Convert the above analyzed array into an abstract syntax tree according to the grammar rules. You can use the Esprimas parsing tool to know and convert it into json format.
What is the use of this abstract syntax tree?
People talk about compressing and optimizing code, but I don’t understand this. Then they say that compilers, IDEs, etc. have many uses (there is no way to go into it now because the skills are not enough)
On Meituan Dianping I saw in technology that syntax trees can be used to reconstruct javaScript code
bytecode generator
Its function is to convert the syntax tree into javaScript Binary code that the engine can read and execute.
Each javascript engine has its own bytecode format. The simplest way is to translate the semantic units into binary
Bytecode interpreter
Of course, this link after link has binary code, then the next step is to execute the binary code, producing results that you can see with your eyes.
[Related recommendations: javascript learning tutorial]
The above is the detailed content of What is the interpreter of javascript. For more information, please follow other related articles on the PHP Chinese website!