It is very difficult to describe a real compiler. The "real" compiler-which is what you want to do in everyday programming-is too complex and difficult to master in this textbook. On the other hand, the compilation of a very small language (whose list includes 1, 0 pages of text) is also unlikely to accurately describe all the features required by the "real" compiler.
In order to solve these problems, people in (A N S I) c for a small language to provide a complete source code, once you can understand this technology, it is easy to understand the small language compiler. This language, called T I N Y, is used in every chapter of the example, and its compiled code is soon mentioned. The complete compilation code is aggregated in Appendix B.
The program structure of tiny is very simple, and it is syntactically similar to the syntax of a d A or p a s c a l: Only a sequence of statements separated by semicolons. In addition, it has no process and no declaration. All variables are integer variables, which can be easily declared by assigning values (similar to F O RT R A n or b a S I C). It has only two control statements: the I-F statement and the R-e P-e a T statement, which can also contain a sequence of statements themselves. The I F statement has an optional E L S E part and must be terminated by the keyword e n d. In addition, the r e a D statement and the W r i T e statement complete the input/output. You can have comments in curly braces, but annotations cannot be nested.
Tiny expressions are also limited to Boolean expressions and integer arithmetic expressions. A Boolean expression consists of a comparison of two arithmetic expressions that uses the < and = comparison operators. An arithmetic expression can include integer constants, variables, parameters, and 4 integer operators +,-, *,/, and also general mathematical attributes. A Boolean expression may appear only as a test in a control statement--without a Boolean variable, assignment, or I/O.
This example is a simple programming example of a factorial function in the language. This example will be used throughout the book.
{Sample program in TINY language- computes factorial}read x; {input An integer}if 0 < x then {don ' t compute if x <= 0} fact: = 1; Repeat fact: = fact * x; x: = X-1 until x = 0; Write fact {output factorial of x}end
Compiler principle: Tiny language