Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C language semantic specification

Wikipidea says that Perl has a dominant implementation that is used as a reference for its specification ,while C language is specified by the standard ANSI ISO.

I learnt C language without reading a single line of the standard, is that normal...?

I would like to know how the standard (i.e. a natural language document) is capable of describing a programming language without referring to any dominant implementation.

like image 532
user591931 Avatar asked Dec 04 '22 23:12

user591931


1 Answers

It's extremely rare to find people who learn a programming language from the specification. The spec is mostly targeted at compiler authors (who need to adhere to it word-for-word to guarantee correctness) and as a final arbiter of what's legal in the language. Most language specs are extremely dense and technical, and would not be a good way to learn to program in a language. Often, only very advanced users of a language actually read the spec.

Also, very few languages are defined in terms of a reference implementation. Most languages are defined relative to some abstract execution environment. For example, the C++ specification says that

The semantic descriptions in this International Standard define a parameterized nondeterministic abstract machine. This International Standard places no requirement on the structure of conforming implementations. In particular, they need not copy or emulate the structure of the abstract machine. Rather, conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below.

In other words, the C++ specification describes how C++ programs need to behave in a purely theoretical sense. This gives the spec authors great leeway with how they define the language. They can talk about "objects" and "pointers", for example, without saying how they actually have to be implemented. They don't even need to say how the physical machine on which C++ works, since they can just define the machine to behave however they want and then leave it up to actual compiler writers to translate that abstract machine onto the physical machine.

Some languages are defined with respect to a virtual machine (a great example of this is Java). They can talk about the behavior of Java programs with respect to that virtual machine by saying how Java programs interact with the virtual machine, and then leave the details of the VM implementation up to the VM implementers.

Some other languages, such as ML, have definitions that are purely mathematical. The semantics of the language are described as abstract mathematical transformations between states, meaning that it's possible to prove properties of ML that couldn't easily be shown if the language were defined relative to a reference compiler.

To summarize - language specifications are complex documents that few programmers actually go about reading. They're mostly for compiler authors and define the program usually in some abstract term that doesn't take the machine into account. That way, the language can be defined in a portable way, since you can make a conformant implementation on any machine by just translating the formal description into machine operations.

Hope this helps!

like image 63
templatetypedef Avatar answered Jan 01 '23 11:01

templatetypedef