当前位置:网站首页>AST (Abstract Syntax Tree)
AST (Abstract Syntax Tree)
2022-07-03 20:04:00 【Front end baymax】
AST (Abstract Syntax Tree)
| title | Content |
|---|---|
| AST | AST Definition , Usage mode , principle |
| AST | AST Example |
| AST | AST application |
AST Definition
- AST(Abstract Syntax Tree) Abstract syntax tree , abbreviation AST, It is source code ( That is to say, it is not only applied to JavaScript, It also applies to other languages , for example : Python,Rust etc. ) An abstract representation of a grammatical structure .
- It represents the syntax structure of programming language in the form of tree , Each node on the tree represents a structure in the source code .
- grammar 【 abstract 】: It means that the grammar here does not express every detail that appears in the real grammar .
AST Usage mode
- Our common lexical parser and parser ( It can be JavaScript Convert the source code into AST), For example, in the following example esprima, Of course, there are other , for example : acorn、shift、traceur etc.
purpose
AST Abstract syntax trees are widely used , such as :
vscode、atom、sublime Medium Code highlighting 、 Code formatting 、 Error message 、 Code auto completion ;
babeltranslationES6ToES5
…So if you want to optimize JavaScript Compilation and running speed ,AST It is essential to understand .
AST principle
Compilation process
- JavaScript The process of execution starts with reading JavaScript In the document Character stream , Secondly through Lexical analyzer generates
token, After that parsers (Parser) Generate AST Trees , And then finally convert to Machine code perform .
var name = "jackdan";
- The first step is to read the above character stream
'var name = "jackdan"'; - Step 2 word segmentation : take
'var name = "jackdan"'The whole code string is divided into the smallest array of syntax units ; as follows :
[
{
type: 'Keyword', value: 'var' },
{
type: 'Identifier', value: 'name' },
{
type: 'Punctuator', value: '=' },
{
type: 'String', value: '"jackdan"' }
]
- The third step is grammatical analysis : On the basis of word segmentation, establish and analyze the relationship between grammatical units ; as follows :
- Program
type: "Program"
- body: [
- VariableDeclaration {
type: "VariableDeclaration"
- declarations: [
- VariableDeclarator {
type: "VariableDeclarator"
- id: Identifier {
type: "Identifier"
name: "name"
}
- init: Literal {
type: "Literal"
value: "jackdan"
raw: "jackdan"
}
}
]
kind: "var"
}
]
- Let's focus on combing the principles Lexical analysis and Syntax analysis , Those who have been exposed to these two should not be too strange .
Lexical analysis
- Lexical analysis : Also known as scanning (scanner), Simply call
next() Method , Read characters one letter at a time , And then with the defined JavaScript Key characters compare , Generate corresponding Token.Token It's a The smallest indivisible unit . for example :
var name = "jackdan";
// Read first v, Then continue to read a, Last read r Form a var
// var These three characters are recognized as JavaScript Keywords in , As a whole , Semantically, it can no longer be decomposed , Therefore, we also regard it as a Token
- From the above code example, it is not difficult to get , Lexical analyzer , Every The keyword is a Token, Every The identifier is a Token, Every The operator is a Token, Every Punctuation is also a Token. besides , Will Filter out comments and white space characters in the program ( A newline 、 Space 、 Box drawings, etc ).
- Final , We see that the whole code is divided into one tokens list ( Or a one-dimensional array ).
[
{
type: 'Keyword', value: 'var' },
{
type: 'Identifier', value: 'name' },
{
type: 'Punctuator', value: '=' },
{
type: 'String', value: '"jackdan"' }
]
Syntax analysis
- Grammatical analysis is to put Lexical analysis tokns The list is transformed into an abstract syntax tree structure with grammatical meaning . Go at the same time Validation Syntax ( The highlighted 、 Automatic completion, etc ), If there is a mistake in grammar , Throw a syntax error .
- Program
type: "Program"
- body: [
- VariableDeclaration {
type: "VariableDeclaration"
- declarations: [
- VariableDeclarator {
type: "VariableDeclarator"
- id: Identifier {
type: "Identifier"
name: "name"
}
- init: Literal {
type: "Literal"
value: "jackdan"
raw: "jackdan"
}
}
]
kind: "var"
}
]
AST example
// Source code
var name = "jackdan";
// AST Abstract syntax tree
/** * * +-----------+ * | assign(=) | * +-----------+ * / \ * / \ * +----+ +---------+ * |name| |"jackdan"| * +----+ +---------+ * */
// Run the output structure code
- Program
type: "Program"
- body: [
- VariableDeclaration {
type: "VariableDeclaration"
- declarations: [
- VariableDeclarator {
type: "VariableDeclarator"
- id: Identifier {
type: "Identifier"
name: "name"
}
- init: Literal {
type: "Literal"
value: "jackdan"
raw: "jackdan"
}
}
]
kind: "var"
}
]
> var esprima = require('esprima');
> var program = 'var name = "jackdan"';
> esprima.tokenize(program);
[
{
type: 'Keyword', value: 'var' },
{
type: 'Identifier', value: 'name' },
{
type: 'Punctuator', value: '=' },
{
type: 'String', value: '"jackdan"' }
]
> esprima.parse(program);
Script {
type: 'Program',
body: [
VariableDeclaration {
type: 'VariableDeclaration',
declarations: [Array],
kind: 'var'
}
],
sourceType: 'script'
}
Thinking in JackDan
边栏推荐
- Microsoft: the 12th generation core processor needs to be upgraded to win11 to give full play to its maximum performance
- Microservice framework - frequently asked questions
- BOC protected tryptophan porphyrin compound (TAPP Trp BOC) Pink Solid 162.8mg supply - Qiyue supply
- An old programmer gave it to college students
- 2022 Xinjiang latest construction eight members (standard members) simulated examination questions and answers
- 2.5 conversion of different data types (2)
- About unregistered transfer login page
- Use of aggregate functions
- Utilisation de base du cadre unitest
- 2.7 format output of values
猜你喜欢
![Meso tetra [P - (p-n-carbazole benzylidene imino)] phenylporphyrin (tcipp) /eu (tcipp) [pc( α- 2-oc8h17) 4] and euh (tcipp) [pc (a-2-oc8h17) 4] supplied by Qiyue](/img/5b/fc776a1982e24b82984d82be6a016f.jpg)
Meso tetra [P - (p-n-carbazole benzylidene imino)] phenylporphyrin (tcipp) /eu (tcipp) [pc( α- 2-oc8h17) 4] and euh (tcipp) [pc (a-2-oc8h17) 4] supplied by Qiyue

2022-06-25 网工进阶(十一)IS-IS-三大表(邻居表、路由表、链路状态数据库表)、LSP、CSNP、PSNP、LSP的同步过程

Gym welcomes the first complete environmental document, which makes it easier to get started with intensive learning!

Professional interpretation | how to become an SQL developer

Ae/pr/fcpx super visual effects plug-in package fxfactory

2.2 integer

2.6 formula calculation

Nerfplusplus parameter format sorting

NFT without IPFs and completely on the chain?

Chapter 2: find the classical solution of the maximum Convention and the least common multiple of a and B, find the conventional solution of the maximum Convention and the least common multiple of a a
随机推荐
Day10 -- forced login, token refresh and JWT disable
Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles
Ae/pr/fcpx super visual effects plug-in package fxfactory
6. Data agent object Defineproperty method
Cross compile opencv with contrib
MPLS configuration
Global and Chinese market of speed limiter 2022-2028: Research Report on technology, participants, trends, market size and share
Use of aggregate functions
AI enhanced safety monitoring project [with detailed code]
The simplicity of laravel
Chapter 1: find the algebraic sum of odd factors, find the same decimal sum s (D, n), simplify the same code decimal sum s (D, n), expand the same code decimal sum s (D, n)
Global and Chinese market of cyanuric acid 2022-2028: Research Report on technology, participants, trends, market size and share
Derivation of decision tree theory
What is the difference between a kill process and a close process- What are the differences between kill process and close process?
[raid] [simple DP] mine excavation
2.4 conversion of different data types
Sparse matrix (triple) creation, transpose, traversal, addition, subtraction, multiplication. C implementation
Microsoft: the 12th generation core processor needs to be upgraded to win11 to give full play to its maximum performance
Nacos usage of micro services
Day11 - my page, user information acquisition, modification and channel interface