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

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. 

主站蜘蛛池模板: 柏乡县| 简阳市| 奉贤区| 陆川县| 如东县| 克东县| 冕宁县| 湖北省| 巴林左旗| 和田市| 建阳市| 合作市| 枞阳县| 陆丰市| 察哈| 封丘县| 九龙城区| 即墨市| 大荔县| 宁武县| 怀化市| 云梦县| 进贤县| 枝江市| 淮阳县| 武穴市| 偃师市| 丰顺县| 丹江口市| 腾冲县| 福贡县| 汉中市| 黄梅县| 东海县| 内丘县| 株洲市| 贺州市| 安远县| 宣武区| 喜德县| 定兴县|