官术网_书友最值得收藏!

Syntax analysis

When speaking about programming language compilation, we usually differentiate two terms: syntax and semantics. The syntax is the structure of the code; it defines the rules by which tokens combined make structural sense. For example, day nice is a syntactically correct phrase in English because it doesn't contain errors in either of the tokens. Semantics, on the other hand, concerns the actual meaning of the code. That is, day nice is semantically incorrect and should be corrected as a nice day.

Syntax analysis is a crucial part of source analysis, because tokens will be analyzed syntactically and semantically, that is, as to whether they bear any meaning that conforms to the general grammar rules. Take the following, for example:  

int b = a + 0;

It may not make sense for us, because adding zero to the variable won't change its value, but the compiler doesn't look on logical meaning here—it looks for the syntactic correctness of the code (a missing semicolon, a missing closing parenthesis, and more). Checking the syntactic correctness of the code is done in the syntax analysis phase of compilation. The lexical analysis divides the code into tokens; syntax analysis checks for syntactic correctness, which means that the aforementioned expression will produce a syntax error if we have missed a semicolon: 

int b = a + 0

g++ will complain with the expected ';' at end of declaration error. 

主站蜘蛛池模板: 洛阳市| 乐清市| 鄂托克前旗| 红桥区| 寿光市| 梅河口市| 兴城市| 墨脱县| 山东省| 陆河县| 喀什市| 章丘市| 罗定市| 通河县| 铜梁县| 徐水县| 遵义县| 岳西县| 胶州市| 西乡县| 吐鲁番市| 南宫市| 乌兰察布市| 佳木斯市| 原平市| 永胜县| 革吉县| 静安区| 安陆市| 托克托县| 北辰区| 新河县| 天祝| 安图县| 大英县| 都匀市| 峡江县| 襄汾县| 兴业县| 武平县| 全椒县|