PL/0 language compilation interpreter Implementation 1 (C language), pl
URL: http://www.cnblogs.com/archimedes/p/pl0-compiler1.html.
PL/0 Overview
The following content is taken from Wikipedia:
PL/0, Is similar to but much simpler than the general-purpose programming language Pascal, intended as an educational programming language. it serves as an example of how to construct a compiler. it was originally introduced in the book,Algorithms + Data Structures = Programs, By Niklaus Wirth in 1975. it features quite limited language constructs: there are no real numbers, very few basic arithmetic operations and no control-flow constructs other than "if" and "while" blocks. while these limitations make writing real applications in this language impractical, it helps the compiler remain compact and simple.
Translation:
PL/0 language, as an educational programming language, is somewhat similar to the general programming language Pascal but much simpler. As an example of how to build a compiler. It was originally written in algorithm + Data Structure = program by Niklaus Wirth in 1975. It has very limited language structures: there are no real numbers, and there are only a small number of basic arithmetic operations, except for the "if" and "while" Statement blocks, there is no other control flow. Although these restrictions limit the language in practical applications, they help the compiler stay compact and simple.
PL/0 is a subset of PASCAL.
Same as PASCAL
Scope rules (the identifier of the outer definition that can be referenced in the inner layer), context constraints, nested procedures, and recursive calls
Subset
Data Type, only integer
Data structure, only simple changes and constants
The maximum number is 14 digits.
The valid length of the identifier is 10.
Statement type
A process can be nested with up to three layers
From the above description, we can see that we are familiar with PL/0 commands and can use other languages (I chose C Language) to write a simple interpreter, to some extent, master the principles of Some compilers and improve their programming capabilities.
PL/0 program example
Several instances are more intuitive:
1. Calculate the maximum public approx.
Var m, n, r, q; {calculate the maximum common approx. of m and n} procedure gcd; begin while r #0 do begin q: = m/n; r: = m-q * n; m: = n; n: = r; end; begin read (m); read (n); {for convenience, required m> = n} if m <n then begin r: = m; m: = n; n: = r; end; begin r: = 1; call gcd; write (m); end.
2. calculate sum = 1! + 2! +... + N! (N read from the console)
Var n, m, fact, sum; {recursive calculation fact = m! } Procedure factorial; begin if m> 0 then begin fact: = fact * m; m: = m-1; call factorial; end; begin {read n} read (n); sum: = 0; while n> 0 do begin m: = n; fact: = 1; call factorial; sum: = sum + fact; n: = n-1; end; {output n! } Write (sum); end.
Those familiar with Pascal should be familiar with the above code. That's right. pl/0 is the simplified version of pascal, in order to reduce the difficulty of designing and compiling the program, we deliberately offer it exclusively for teaching.
PL/0 Compiler
PL/0 compilation system
Pcode-like instruction Structure
The target code produced by PL/0 compilation programs is an assembly language of a hypothetical stack-type computer, which can be called PCODE-like instruction code. It does not depend on a specific computer, and its instruction set is extremely simple, the command format is also simple. The format is as follows:
F indicates the function code, and l indicates the level difference, that is, the Level Difference Between the program referenced by the variable or process and the program that describes the variable or process. The meaning of a is different for different commands. It indicates the amount of displacement for access commands, and has different meanings for other commands. For details, see the description of each command.
Target Commands include8Items:
① Success: Get the constant value to the top of the running stack. The a field is a constant value.
2. levels of detail: place the variable to the top of the stack. The a field is the relative position of the variable in the description layer, and the l field is the difference between the call layer and the description layer.
③ STO: Send the content at the top of the stack to a variable unit. The meaning of the and l fields is the same as that of the level-4 instruction.
④ CAL: indicates the call process. A is the entry address of the target program to be called, and l is the layer difference.
⑤ INT: opens a data zone in the running stack for the called process (or main program. A domain is the number of units opened up.
⑥ JMP: unconditional transfer instruction. a is the redirection address.
7. JPC: conditional transfer command. If the Boolean value on the top of the stack is not true, the command is switched to the address of Domain a. Otherwise, the command is executed sequentially.
⑧ OPR: relational operation and arithmetic operation commands. Perform operations on the content at the top of the stack and the content at the top of the secondary stack, and store the results at the top of the secondary stack. In addition, it can be commands for reading/writing and other special functions. Specific operations are given by the value of Domain. (For details, see explain execution program ).
ClassPcodeDetailed explanation of Code commands (Instruction menu)
Limit 0 |
Obtain the constant value to the top of the stack. a is the constant value. |
Levels |
Get the variable value to the top of the stack. a is the offset, and l is the layer difference. |
STO l |
Send the content at the top of the stack to a variable unit. a is the offset and l is the layer difference. |
CALL |
Call process. process a is the process address and layer deviation. |
INT 0 |
In the running stack, a data zone is opened for the called process. |
JMP 0 |
Jump to address a unconditionally |
JPC 0 |
Conditional jump: When the Boolean value on the top of the stack is not true, It is redirected to address a. Otherwise, it is executed sequentially. |
OPR 0 0 |
After the process is called, return the call point and return to the stack. |
OPR 0 1 |
Reverse element on top of stack |
OPR 0 2 |
The top of the secondary stack and the top of the stack are added. Two stack elements are removed and the result value is added to the stack. |
OPR 0 3 |
The top of the secondary stack minus the top of the stack. Two stack elements are returned and the result is returned to the stack. |
OPR 0 4 |
The top of the stack is multiplied by the top of the stack. Two stack elements are returned and the result is pushed to the stack. |
OPR 0 5 |
The top of the secondary stack is divided by the top of the stack. Two stack elements are returned and the result value is pushed to the stack. |
OPR 0 6 |
The result of the parity judgment on the top element of the stack is at the top of the stack. |
OPR 0 7 |
|
OPR 0 8 |
Whether the top of the secondary stack is equal to the top of the stack. Two elements are returned and the result is returned to the stack. |
OPR 0 9 |
Whether the top of the secondary stack is different from the top of the stack. Two elements are returned and the result is returned to the stack. |
OPR 0 10 |
Whether the top of the secondary stack is smaller than the top of the stack. Two stack elements are returned and the result is returned to the stack. |
OPR 0 11 |
Whether the top of the secondary stack is equal to or greater than the top of the stack. Two elements are returned and the result is returned to the stack. |
OPR 0 12 |
Whether the top of the secondary stack is greater than the top of the stack. Two elements are returned and the result is returned to the stack. |
OPR 0 13 |
Whether the top of the secondary stack is less than or equal to the top of the stack. Two elements are returned and the result is returned to the stack. |
OPR 0 14 |
Stack top value output to screen |
OPR 0 15 |
Screen output line feed |
OPR 0 16 |
Read an input from the command line and place it at the top of the stack. |
An example of ing between a PL/0 program and a pcode command of the target code class:
PL/0 Compilation Program overall structure PL/0 Compilation Program organization: A single-pass compilation program centered on a syntax and Semantic Analysis Program
References
Compilation principles-Tsinghua University Press