Parser
Parser
Section titled “Parser”Introduction
Section titled “Introduction”The parser converts JavaScript source code into an Abstract Syntax Tree (AST) — a tree representation of the code’s structure.
Parsing Process
Section titled “Parsing Process”flowchart LR A["Source: const x = 5;"] --> B["Lexer: Tokenizer"] B --> C["Tokens: [const, x, =, 5, ;]"] C --> D["Parser: Build AST"] D --> E["AST: VariableDeclaration"]Tokenization (Lexing)
Section titled “Tokenization (Lexing)”// Source codeconst x = 5;
// Tokens produced// [Keyword: const] [Identifier: x] [Operator: =] [Number: 5] [Punctuator: ;]AST Structure
Section titled “AST Structure”Program ├── VariableDeclaration (const) │ ├── VariableDeclarator │ │ ├── Identifier (x) │ │ └── NumericLiteral (5)Eager vs Lazy Parsing
Section titled “Eager vs Lazy Parsing”V8 uses two parsing modes:
- Eager: Parses functions immediately (slower start)
- Lazy: Skips function bodies until called (faster start, used by default)
// Lazily parsed — body skipped until calledfunction greet() { console.log('Hello');}
// Eagerly parsed with parensconst greet = (function() { console.log('Hello');});Summary
Section titled “Summary”- Parser converts source → AST
- Lexer tokenizes, Parser builds tree
- V8 uses lazy parsing for faster startup
- AST is input for the interpreter