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

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. 

主站蜘蛛池模板: 通山县| 高碑店市| 宜章县| 余姚市| 普格县| 本溪| 亳州市| 孟州市| 新干县| 曲松县| 兴仁县| 南澳县| 上思县| 伽师县| 阿瓦提县| 肇源县| 石台县| 霸州市| 屯门区| 汝州市| 大石桥市| 华坪县| 苏尼特右旗| 安塞县| 大渡口区| 宾川县| 崇州市| 锡林郭勒盟| 南投县| 新乡县| 灵台县| 浦江县| 中宁县| 余干县| 永安市| 眉山市| 司法| 灵璧县| 大埔区| 任丘市| 湖北省|