http://blog.csdn.net/gengzhikui1992/article/details/50762309
In order to fully define the programming language, we need:
Grammar, which describes what the program looks like;
Semantics, describing the meaning of the program.
The definition of grammatical grammar
Each programming language has a set of rules that describe what strings are considered to be valid programs in that language. These rules define the syntax for this language. Through the grammatical rules of the language, we can separate the potentially valid programs like y = x + 1 from meaningless strings such as >/;x:[email protected].
Abstract syntax Tree
Of course, the intended use of a computer program is to be read by a computer, and a grammar parser is required to read the program: The parser is able to read a string representing the program, check whether it is valid according to the syntax rules, and then convert it into a structured representation that is suitable for further processing.
There are various tools that can automatically convert a language's grammar rules into a parser.
In general, a parser should read a string like y = x + 1 and then convert it into an abstract syntax tree (AST). Abstract syntax tree is a representation of the source code, removing extraneous details such as spaces, and focusing only on the hierarchical structure of the program.
Grammar summary
Grammar is concerned only with what the surface of the program is, not what it means. The program may be syntactically correct but without any practical significance. For example, program y = x + 1 itself may not make sense because it does not specify what X is, and program z = True + 1 May error at runtime because it attempts to add a number to a Boolean value.
Semantic definition of Operation semantics
The most practical way to think about the meaning of a program is to think about what it does: what do we expect to happen when we run the program? How do different structures behave in a run-time programming language? What is the effect of putting them together to make a larger program?
This is the basis of operational semantics (operational semantic), which defines rules for the execution of a program on a machine to capture the meaning of a programming language. This machine is often an abstract machine:
Small Step Protocol
So how do we design an abstract machine and use it to define the operational semantics of a programming language? One way is to imagine a machine, with this machine directly in accordance with the language of the syntax of the operation of a small step by step to the protocol, so that a program evaluation. No matter what the final result means, we can get the program closer to the end result at every step.
This small step protocol is similar to the way in which algebraic expressions are evaluated. For example, in order to evaluate (1x2) + (3x4), we know that we should:
- Perform the multiplication on the left (1x2 becomes 2) so that the expression is 2 + (3x4);
- Perform the multiplication on the right (3x4 becomes 12) so that the expression is 2 + 12
- Perform the addition (2 + 12 becomes 14) and finally get 14.
We can say that 14 is the result, because the above step is no longer a further specification; we think 14 is a special algebraic expression, it is a value, has its own meaning, no further effort is required.
Formal rules of small step specification
This non-formalized process can be translated into an operational semantics by writing a formal rule for the specification of each small step.
The rules themselves need to be written down in a language (meta-language), which is usually a mathematical notation.
Reference: The nature of computing: in-depth analysis of programs and computers
Syntax and semantics of programming languages