Compiler principle: Tiny language

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.