语法
一句话解释
语法是 设置的 grammar 规则那个定义正确结构的 code in programming language.
用大白话说
Syntax 是the grammar of a programming language。 Just as English has rules for sentence structure (subject-verb-object), programming languages have rules for code structure — where to put parentheses, semicolons, keywords, and operators.
A syntax error means your code violates these rules — like writing 函数 ({} instead of 函数 () {}. The computer cannot parse the code, so it refuses to run. Syntax errors are usually caught by the interpreter or compiler before execution.
Each language has its own syntax: Python uses indentation for blocks, JavaScript uses curly braces, Lisp uses parentheses. Learning a language is largely learning its syntax. Good editors help with syntax highlighting, autocomplete, and linting to catch syntax errors as you type.
示例
// Correct syntax
if (x > 0) {
console.log("positive");
}
// Syntax error — missing parenthesis
// if x > 0) {
// console.log("positive");
// }正确 version 有 matching parentheses and braces. broken version 是 missing 打开 parenthesis, causing 语法错误.
常见混淆
容易混淆的对象: 语义
关键区别: 语法是 关于结构 (是 code well-formed?); 语义是 关于 meaning (做 code 做什么你 intend?). 语法错误是 caught 之前运行; 语义错误 (logic bugs) 是不.
相关术语
相关资源
相关课程
- 如何 Programs 工作
学习关于语法 and 语义.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。