This article address: http://www.cnblogs.com/archimedes/p/pl0-compiler1.html, reprint please indicate source address.
pl/0 Introduction
The following is excerpted from wiki:
pl/0, is similar to but much simpler than the general-purpose programming language Pascal, intended as an educatio NAL programming language. It serves as an example of what to construct a compiler. It is originally introduced in the book, algorithms + Data structures = Programs, by Niklaus Wirth in 1975. It features quite limited language constructs:there is no real numbers, very few basic arithmetic operations and no cont Rol-flow constructs other than the "if" and "while" blocks. While these limitations make writing real applications in this language impractical, it helps the compiler remain compact and simple.
Translate as follows:
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 by Niklaus Wirth in 1975 in the book " algorithm + data structure = Program ". it has very limited language constructs: There is no real number, only a very small amount of basic arithmetic operations, and there is no control flow other than the "if" and "while" statement blocks. while These limitations make the language Limited in practice , It helps the compiler keep it compact and simple .
pl/0 language is a subset of Pascal language
With Pascal
Scope rule (the inner layer can refer to an identifier enclosing it's outer definition), context constraints, procedures can be nested definitions, recursive calls
Subset
Data type, only integral type
Data structures, only simple changes and constants
Numbers up to 14 bits
The valid length of the identifier is 10
Statement type
Process can be nested up to three layers
As can be seen from the above narrative, familiar with pl/0 directives, and can be in other languages (I choose C language) to write a simple interpreter, to some extent, master some of the principles of the compiler, improve their own programming ability
pl/0 Program Examples
To give a few examples more intuitive:
1. Calculation Greatest common divisor
varm, N, R, Q;{calculate the greatest common divisor of M and N}procedureGCD; begin whiler#0 Do beginQ:= m/N; R:= M-q *N; M:=N; N:=R; End End;beginread (m); Read (n); {for convenience, specify M >= n} ifM < n Then beginr:=m; M:=N; N:=R; End; beginr:=1; Call GCD; Write (m); End;End.
2, Calculate sum = 1! + 2! + ... + n! (n read in from console)
varN, M, fact, sum;{recursive Calculation fact = m!}procedurefactorial; begin ifM >0 Then beginfact:= Fact *m; M:= M-1; Call factorial; End; End;begin {read in n}read (n); Sum:=0; whilen >0 Do beginm:=N; Fact:=1; Call factorial; Sum:= Sum +fact; N:= N-1; End; {Output n!}write (sum);End.
For the above code, familiar with the Pascal language students should be very familiar with, yes, pl/0 is a simplified version of Pascal, in order to reduce the difficulty of the design and compilation program deliberately for the purpose of teaching.
pl/0 Compiling the program
pl/0 compilation system
Structure of class Pcode code directives
pl/0 compiler generated by the target code is an imaginary stack of computer assembly language, can be called Class pcode directive code, it does not depend on the specific computer, its instruction set is very simple, the instruction format is also simple, its format is as follows:
Where f is the function code, L represents the level difference, that is, the variable or process is referred to the sub-program and the description of the variable or process of the difference between the sub-program. The meaning of a is different from the instruction, the amount of displacement is expressed on the access instruction, and the other instructions have different meanings, see the explanation of each instruction.
The target directive has 8 article:
①lit: Takes a constant value to the top of the run stack. A field is a constant value.
②lod: Place the variable on top of the stack. The A field is the relative position of the variable in the indicated layer, and L is the layer difference between the call layer and the description layer.
③sto: The contents of the top of the stack are fed into a variable cell. The meaning of the a,l domain is the same as LOD directives.
④cal: The instruction that invokes the procedure. A is the target program entry address for the called procedure, and L is the layer difference.
⑤int: A data area is opened for the invoked procedure (or main program) in the run stack. The number of units opened for a field.
⑥JMP: Unconditional transfer instruction, A is the turn address.
⑦JPC: Conditional transfer instruction, when the Boolean value of the top of the stack is non-true, turn to the address of domain A, otherwise the order is executed.
⑧OPR: Relational operations and arithmetic operations directives. The stack top and the top of the sub-stack operation, the results are stored at the top of the second stack, but also can be read and write special functions such as instructions, the specific operation by the value of a field given. (See explanation of the execution procedure).
class Pcode detailed explanation of code instructions (Instruction menu)
LIT 0 A |
Takes a constant value to the top of the stack, a is a constant value |
LOD L A |
Take the value of the variable to the top of the stack, a is the offset, and L is the layer difference |
STO L A |
The top content of the stack is fed into a variable cell, a is offset and L is a layer difference |
CAL L a |
Call procedure, A is the process address, L is the layer difference |
INT 0 A |
Open the data area of a unit in the run stack for the called procedure |
JMP 0 A |
Skip to a address unconditionally |
JPC 0 A |
Conditional jump, when the stack top Boolean value is not true jump to a address, otherwise the order executes |
OPR 0 0 |
After the procedure call is finished, return to the call point and stack back |
OPR 0 1 |
Stack top element Inverse |
OPR 0 2 |
The top of the stack and the top of the stack, back two stack elements, the result value into the stack |
OPR 0 3 |
The top of the stack minus the top of the stack, back two stack elements, the result value into the stack |
OPR 0 4 |
Times the top of the stack multiplied by the top, back two stack elements, the result value into the stack |
OPR 0 5 |
The top of the stack divided by the top of the stack, back two stack elements, the result value into the stack |
OPR 0 6 |
The parity of the top element of the stack, the result value at the top of the stack |
OPR 0 7 |
|
OPR 0 8 |
The top of the stack is equal to the top of the stack, two stack elements are returned, and the result value is stacked |
OPR 0 9 |
The top of the stack and the top of the stack are not equal, back two stack elements, the result value into the stack |
OPR 0 10 |
The top of the stack is less than the top of the stack, back two stack elements, the result value into the stack |
OPR 0 11 |
The top of the stack is greater than or equal to the top of the stack, back two stack elements, the result value into the stack |
OPR 0 12 |
The top of the stack is greater than the top of the stack, back two stack elements, the result value into the stack |
OPR 0 13 |
The top of the stack is less than or equal to the top of the stack, back two stack elements, the result value into the stack |
OPR 0 14 |
Stack top value output to screen |
OPR 0 15 |
Screen output line break |
OPR 0 16 |
Reads an input from the command line onto the top of the stack |
An example of mapping a pl/0 program to a target code class pcode directive:
pl/0 compiler overall structure pl/0 compiler organization: A syntax-based, semantic parser-centric, single-pass compilation program
Resources
"Compiling principle"--Tsinghua University Press
pl/0 Language Compilation Programming 1 (C language)