当前位置:网站首页>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
边栏推荐
- What is the difference between a kill process and a close process- What are the differences between kill process and close process?
- The 29th day of force deduction (DP topic)
- Global and Chinese market of cyanuric acid 2022-2028: Research Report on technology, participants, trends, market size and share
- Today's work summary and plan: February 14, 2022
- 2.3 other data types
- WPF format datetime in TextBlock- WPF format DateTime in TextBlock?
- unittest框架基本使用
- 6. Data agent object Defineproperty method
- QT tutorial: signal and slot mechanism
- Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles
猜你喜欢

Vscode reports an error according to the go plug-in go get connectex: a connection attempt failed because the connected party did not pro

Chapitre 1: le roi de shehan a mal calculé

Nerfplusplus parameter format sorting
![Oak-d raspberry pie cloud project [with detailed code]](/img/34/76b461bf03fba373da5b5898c5204c.jpg)
Oak-d raspberry pie cloud project [with detailed code]

2.4 conversion of different data types

The 29th day of force deduction (DP topic)

NFT without IPFs and completely on the chain?

2022 Xinjiang latest construction eight members (standard members) simulated examination questions and answers

1.5 learn to find mistakes first

Ae/pr/fcpx super visual effects plug-in package fxfactory
随机推荐
Micro service knowledge sorting - cache technology
Chapter 1: find all factorial sums, Grand Prix site unified programming, three factorial sums, graphic point scanning, recursive factorial n of n!, Find the factorial n of n!, King Shehan miscalculate
FPGA 学习笔记:Vivado 2019.1 工程创建
Pat grade B 1009 is ironic (20 points)
IP address is such an important knowledge that it's useless to listen to a younger student?
Global and Chinese market of liquid antifreeze 2022-2028: Research Report on technology, participants, trends, market size and share
About callback function and hook function
Global and Chinese market of speed limiter 2022-2028: Research Report on technology, participants, trends, market size and share
Chapter 1: drinking soft drinks, step tariff calculation, step tariff calculation function, personal income tax, solving square root inequality, simplifying solving square root inequality, solving dem
BOC protected phenylalanine zinc porphyrin (Zn · TAPP Phe BOC) / iron porphyrin (Fe · TAPP Phe BOC) / nickel porphyrin (Ni · TAPP Phe BOC) / manganese porphyrin (Mn · TAPP Phe BOC) Qiyue Keke
Micro service knowledge sorting - asynchronous communication technology
AI enhanced safety monitoring project [with detailed code]
Find a line in a file and remove it
P5.js development - setting
Global and Chinese market of high purity copper foil 2022-2028: Research Report on technology, participants, trends, market size and share
Gym welcomes the first complete environmental document, which makes it easier to get started with intensive learning!
[raid] [simple DP] mine excavation
Difference between surface go1 and surface GO2 (non professional comparison)
unittest框架基本使用
Sword finger offer 30 Stack containing min function