本文介绍了

问题描述

每个人都会遇到语法错误.即使是有经验的程序员也会犯错.对于新手来说,这只是学习过程的一部分.但是,通常很容易解释错误消息,例如:

意外的符号并不总是真正的罪魁祸首.但是行号给出了从哪里开始寻找的粗略想法.

始终查看代码上下文.语法错误通常隐藏在前面代码行中提到的中.将您的代码与手册中的语法示例进行比较.

虽然不是每个案例都匹配另一个.然而,有一些,带有语法高亮.这也有助于括号/括号平衡.

  • 阅读手册中的和示例.两次,以达到一定程度.

  • 如何解释解析器错误

    典型的语法错误信息如下:

    解析错误:语法错误,意外的T_STRING,在file.中需要';'strong>line 217

    其中列出了语法错误的可能位置.查看提到的文件名行号.

    ,例如 T_STRING 解释了解析器/令牌生成器无法识别的符号最后处理.然而,这不一定是语法错误的原因.

    查看之前的代码行也很重要.通常语法错误只是之前发生的不幸.错误行号正是解析器最终放弃处理它的地方.

    解决语法错误

    有很多方法可以缩小和修复语法问题.

    如果所有其他方法都失败了,您可以随时google您的错误消息.语法符号并不容易搜索(不过,堆栈溢出本身由 SymbolHound 索引).因此,您可能需要多浏览几页才能找到相关内容.

    更多指南:

    And:

    While Stack Overflow is also welcoming rookie coders, it's mostly targetted at professional programming questions.

    • Answering everyone's coding mistakes and narrow typos is considered mostly off-topic.
    • So please take the time to follow the basic steps, before posting syntax fixing requests.
    • If you still have to, please show your own solving initiative, attempted fixes, and your thought process on what looks or might be wrong.

    If your browser displays error messages such as "SyntaxError: illegal character", then it's not actually javascript-syntax error.


    Syntax errors raised on vendor code: Finally, consider that if the syntax error was not raised by editing your codebase, but after an external vendor package install or upgrade, it could be due to

    解决方案

    What are the syntax errors?

    C-style and imperative programming languages. It has rigid grammar rules, which it cannot recover from when encountering misplaced symbols or identifiers. It can't guess your coding intentions.

    Most important tips

    There are a few basic precautions you can always take:

    How to interpret parser errors

    A typical syntax error message reads:

    Which lists the possible location of a syntax mistake. See the mentioned file name and line number.

    A such as T_STRING explains which symbol the parser/tokenizer couldn't process finally. This isn't necessarily the cause of the syntax mistake, however.

    It's important to look into previous code lines as well. Often syntax errors are just mishaps that happened earlier. The error line number is just where the parser conclusively gave up to process it all.

    Solving syntax errors

    There are many approaches to narrow down and fix syntax hiccups.

    If all else fails, you can always google your error message. Syntax symbols aren't as easy to search for (Stack Overflow itself is indexed by SymbolHound though). Therefore it may take looking through a few more pages before you find something relevant.

    Further guides: