Asked By : Val
Answered By : jmad
Why not to learn semantics of C right away instead of inventing another language, for describing semantics of C?
Because in order to define the semantics of C you need some kind of language, for example English. English can be ambiguous, and especially is the C99 semantics. The computer science notion of semantics is generally a mathematical description of a program, and this description is not exactly a translation. There are several kind of semantics, that can be about different things:
- about which mathematical function the program computes (denotational semantics), $$ ⟦ verb!lambda x: x + x !⟧ = (x mapsto 2x)$$
- about properties that shall hold before and after the program (axiomatic semantics), $$ {verb.a. > 0} quad verb! x = a; y = 0; while(1 < x){ x = x/2; y++; } ! quad {verb.y. = lfloorlog(verb.a.)rfloor} $$
- about how the program will reduce (operational semantics). $$ (σ, verb! while(x){x–;y++;}!) ⇒ σ’ $$ where $σ, σ’$ represents your memory before and after. In this case $σ=(x↦a,y↦b)$ and $σ’=(x↦a+b,y↦0)$ if $x$ and $y$ are the memory cells for $verb.x.$ and $verb.y.$, and if $a∈ℕ$.
A little sadly for now, universally understanding a program through its semantics is neither easy nor the intention of it all. It is not the former because, well, those math can be messy. It is not the latter because semantics provide indeed provide a common and unambiguous description, but it is not used to understand a program. If reading the semantics is not that much meaningful, reading and agreeing on the semantics on the basic elements of the language is very important, for example to explain unambiguously how C’s $verb.continue.$ primitive works. In conclusion:
Do they tell us that they introduce another language to understand the first one?
Technically no, the language is math. Also it is basically the same for all programming languages, so even introducing a new language, if it is the same for all, would be pretty much OK.
Why do we understand it better than the original one?
Because it is math so it may be harder to understand, but it is way more precise and complete.
The same applies to the syntax.
There are works on verified parsers, but the question of what would mean a semantics of a parser is not trivial.
Best Answer from StackOverflow
Question Source : http://cs.stackexchange.com/questions/6517