What is the difference between syntax and semantics in programming languages?

后端 未结 10 1919
长情又很酷
长情又很酷 2020-12-04 04:51

What is the difference between syntax and semantics in programming languages (like C, C++)?

相关标签:
10条回答
  • 2020-12-04 05:23

    Syntax is the structure or form of expressions, statements, and program units but Semantics is the meaning of those expressions, statements, and program units. Semantics follow directly from syntax. Syntax refers to the structure/form of the code that a specific programming language specifies but Semantics deal with the meaning assigned to the symbols, characters and words.

    0 讨论(0)
  • 2020-12-04 05:28

    Understanding how the compiler sees the code

    Usually, syntax and semantics analysis of the code is done in the 'frontend' part of the compiler.

    • Syntax: Compiler generates tokens for each keyword and symbols: the token contains the information- type of keyword and its location in the code. Using these tokens, an AST(short for Abstract Syntax Tree) is created and analysed. What compiler actually checks here is whether the code is lexically meaningful i.e. does the 'sequence of keywords' comply with the language rules? As suggested in previous answers, you can see it as the grammar of the language(not the sense/meaning of the code). Side note: Syntax errors are reported in this phase.(returns tokens with the error type to the system)

    • Semantics: Now, the compiler will check whether your code operations 'makes sense'. e.g. If the language supports Type Inference, sematic error will be reported if you're trying to assign a string to a float. OR declaring the same variable twice. These are errors that are 'grammatically'/ syntaxially correct, but makes no sense during the operation. Side note: For checking whether the same variable is declared twice, compiler manages a symbol table

    So, the output of these 2 frontend phases is an annotated AST(with data types) and symbol table.

    Understanding it in a less technical way

    Considering the normal language we use; here, English:

    e.g. He go to the school. - Incorrect grammar/syntax, though he wanted to convey a correct sense/semantic.

    e.g. He goes to the cold. - cold is an adjective. In English, we might say this doesn't comply with grammar, but it actually is the closest example to incorrect semantic with correct syntax I could think of.

    0 讨论(0)
  • 2020-12-04 05:28

    He drinks rice (wrong semantic- meaningless, right syntax- grammar)

    Hi drink water (right semantic- has meaning, wrong syntax- grammar)

    0 讨论(0)
  • 2020-12-04 05:29

    Syntax is about the structure or the grammar of the language. It answers the question: how do I construct a valid sentence? All languages, even English and other human (aka "natural") languages have grammars, that is, rules that define whether or not the sentence is properly constructed.

    Here are some C language syntax rules:

    • separate statements with a semi-colon
    • enclose the conditional expression of an IF statement inside parentheses
    • group multiple statements into a single statement by enclosing in curly braces
    • data types and variables must be declared before the first executable statement (this feature has been dropped in C99. C99 and latter allow mixed type declarations.)

    Semantics is about the meaning of the sentence. It answers the questions: is this sentence valid? If so, what does the sentence mean? For example:

    x++;                  // increment
    foo(xyz, --b, &qrs);  // call foo
    

    are syntactically valid C statements. But what do they mean? Is it even valid to attempt to transform these statements into an executable sequence of instructions? These questions are at the heart of semantics.

    Consider the ++ operator in the first statement. First of all, is it even valid to attempt this?

    • If x is a float data type, this statement has no meaning (according to the C language rules) and thus it is an error even though the statement is syntactically correct.
    • If x is a pointer to some data type, the meaning of the statement is to "add sizeof(some data type) to the value at address x and store the result into the location at address x".
    • If x is a scalar, the meaning of the statement is "add one to the value at address x and store the result into the location at address x".

    Finally, note that some semantics cannot be determined at compile-time and must therefore must be evaluated at run-time. In the ++ operator example, if x is already at the maximum value for its data type, what happens when you try to add 1 to it? Another example: what happens if your program attempts to dereference a pointer whose value is NULL?

    In summary, syntax is the concept that concerns itself only whether or not the sentence is valid for the grammar of the language . Semantics is about whether or not the sentence has a valid meaning.

    0 讨论(0)
提交回复
热议问题