In terms of programming, what do semantics mean?

前端 未结 7 707
被撕碎了的回忆
被撕碎了的回忆 2021-02-01 05:27

This is a sentence from Eric Lippert\'s blog:

Given that unfortunate situation, it makes sense to emphasize the storage mechanism first, and then the

相关标签:
7条回答
  • 2021-02-01 05:40

    but what does it mean in terms of computer jargon?

    Essentially the same thing. Example:

    x = 5;
    

    The above is the syntax (representation). The meaning (i.e. the semantics) of this term is to assign the value 5 to a symbol (variable, whatever) called x. Different languages offer different syntaxes to provide the same semantics. For example, the above assignment would be written as

    x := 5;
    

    in Pascal, and as

    x <- 5
    

    in several other languages. In all cases, the meaning is essentially the same. But sometimes, the same syntaxes can also have different meanings, depending on the language and/or context. VB for example redefines the equals operator to mean two different things. First, an assignment, just as above.

    Secondly, in the following code sippet, rather than assigning, it takes the meaning of comparing two values:

    If x = 5 Then Console.WriteLine("x is 5")
    
    0 讨论(0)
  • 2021-02-01 05:41

    A little more context from the blog:

    Therefore in C you put the storage metadata first (static int customerCount;) rather than the semantics first (it could have been var customerCount: static int;).

    He's saying that "static int" appears before "customerCount". Calling "static int" storage metadata and "customerCount" semantics. The storage metadata is information about what the variable holds - implicit in it is how many bits it occupies, what values it may hold, whether it's shared among instances, and its volatility.

    The semantics, the variable name, is information for readers of the code about what should be stored in the variable. What the variable means. You could call it "x" and the program would work just as well, but it would be hard for a programmer to understand it. Calling it "customerCount" provides the variable with meaning, and that's semantics.

    0 讨论(0)
  • 2021-02-01 05:44

    As I understand it, semantics is "what it all means to a human". It's the what it does part, not the how it does it.

    0 讨论(0)
  • 2021-02-01 05:45

    In terms of programming-language jargon, there are several notions of semantics:

    • Static semantics tells you which programs that are grammatical are also well formed. Many languages either have no static semantics (Scheme, Ruby, Python, Icon, Lua, Perl) or have a static semantics that is primarily about implementing a type system (Java, C, C#, Haskell). "Declaration required before use" is another possible static semantics. Static semantics answers the question "is this program meaningful?" and does so at compile time.

    • Dynamic semantics tells you one of two things:

      • Given that a program is meaningful, what is its meaning? Meaning has been defined mathematically in many, many different ways. A classic dynamic semantics might define a function (or a relation) between a program's inputs and the program's outputs. Meaning functions ("denotational semantics") were pioneered by Dana Scott and Christopher Strachey; meaning relations ("axiomatic semantics") were pioneered by Tony Hoare. Scott and Hoare won Turing awards; Strachey probably would have, but he died young. A good way to get introduced would be to read Tony Hoare's book Essays in Computing Science.
      • Given that a program is meaningful, how will it behave when executed?. This kind of semantics is usually called an "operational semantics" and describes the execution of the program on some kind of abstract machine. Again, there are many, many varieties. Today operational semantics is the tool of choice because there are powerful proof techniques, so for example, using operational semantics you can prove that there is never a memory error in managed code. Robin Milner got his Turing award in part for different operational techniques used to describe concurrent or multithreaded programs (CCS and the pi calculus). His 1999 book on Communicating and Mobile Systems is also a very good read if you skip the proofs :-)

    If you read the word "semantics" in a manual or article, and the context is informal English rather than precise mathematical description, the author is probably referring to the dynamic operational behavior—if you will, an operational semantics informally described. This kind of informal description can be quite helpful to compiler writers and programmers.

    0 讨论(0)
  • 2021-02-01 05:46

    In other words, given the context of the article, he is saying that it would have been better for the developers of C# to concentrate on how they would store the data internally when a variable was declared and not care so much about specifying the precise method of declaring the variable.

    In other words by doing this in C#

    static int customerCount
    

    you are telling the compiler to prepare storage for a statically accessable integer and then telling it to label that storage as customerCount

    whereas in VB you would use this line

    dim shared customerCount as Integer
    

    telling the compiler, in theory, that you have a variable called customerCount that it should store and make statically available, and oh by the way, it happens to be an Integer.

    It all really a fine line distinction kinda thing.

    0 讨论(0)
  • 2021-02-01 05:58

    Semantics is WHAT we mean the program should do. Sytax is language-specific constraint on how we express the semantics.

    In theory, as long as a program's semantics are correct, it doesn't matter what language was used to write it.

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