"매 source code 의 tree shape, syntax noise 의 strip". AST = parser 의 output, 매 token sequence 의 hierarchical node tree (FunctionDecl, BinaryExpr, ...) 로 변환. 매 compiler/linter/formatter/codemod/LLM-codegen 의 foundation — 매 2026 LLM agentic coding 의 매 ground truth structural layer.
매 핵심
매 vs Concrete Syntax Tree (CST)
CST (parse tree): 매 every token (paren, semicolon, whitespace) 의 retain.
AST: 매 semantically meaningful node only — 매 cosmetic 의 drop.
매 modern formatter (Prettier, rustfmt) 의 CST-like (lossless) 의 use, 매 compiler/codemod 의 AST.
매 node anatomy
type: BinaryExpression, FunctionDeclaration, ...
children: structured field (left, right, body, params).
Codemod (jscodeshift, ts-morph, libcst) — 매 large refactor.
LLM agentic coding (Claude Opus 4.7 의 tree-sitter grounding).
Static analysis / SAST (Semgrep, CodeQL).
IDE (LSP, syntax highlight, jump-to-def).
💻 패턴
Python ast — visit + transform
importastsrc="x = 1 + 2 * 3"tree=ast.parse(src)classConstFold(ast.NodeTransformer):defvisit_BinOp(self,node:ast.BinOp):self.generic_visit(node)ifisinstance(node.left,ast.Constant)andisinstance(node.right,ast.Constant):try:returnast.copy_location(ast.Constant(value=eval(compile(ast.Expression(node),"","eval"))),node)exceptException:passreturnnodenew=ast.fix_missing_locations(ConstFold().visit(tree))print(ast.unparse(new))# x = 7
tree-sitter (multi-language, incremental)
fromtree_sitterimportLanguage,Parserimporttree_sitter_pythonastspyPY=Language(tspy.language())parser=Parser(PY)tree=parser.parse(b"def add(a, b):\n return a + b\n")root=tree.root_nodeforninroot.children:print(n.type,n.start_point,n.end_point)
Babel codemod (JS/TS)
import*astfrom"@babel/types";importgeneratefrom"@babel/generator";import{parse}from"@babel/parser";importtraversefrom"@babel/traverse";constast=parse(`var x = 1;`,{sourceType:"module"});traverse(ast,{VariableDeclaration(path){if(path.node.kind==="var")path.node.kind="const";},});console.log(generate(ast).code);// const x = 1;
# 매 LLM 의 line-range edit instead of free-form rewrite — AST 의 anchoredit={"file":"app.py","node_path":"Module/FunctionDef[name=handler]/body[2]","replace_with":"return JSONResponse({'ok': True})"}apply_ast_edit(edit)# 매 syntactic safety guaranteed