Write a scheme with scheme compiler (i)

Source: Internet
Author: User

In the blogger's college career, the most headache of a course is the compilation principle, after learning this course, although know the LL,LR algorithm, and a series of terminology related to the compilation principle, but the understanding of it has been stuck in the problem, Although bloggers have always wanted to be able to write a compiler to deepen the understanding of the principles of compiling, but in C language compiler is really a nightmare, a lot of time every day spent on debugging bugs, but no time and energy to think about the compiler principles of things @[email protected].

The theory of compiling is not easy to understand, but it is difficult to use C language to focus on the learning of compiling, it is hard to learn the knowledge about compiling.

Later, after touching the functional programming, especially after learning the scheme language, the blogger picked up the naïve idea and wrote a compiler with scheme.

Let me start by describing why you should use scheme to write a compiler.

1. Since the syntax of scheme is used in the form of S-expression, the program of scheme has a clear advantage in the parsing phase (that is, we can skip the headache in traditional language grammar analysis:-P)

2. In the traditional way of compiler writing, we have to define a lot of the syntax of the program, and then in the writing of the grammar analysis algorithm, the time to take all the syntax to take into account (this inhumane behavior will simply grind the patience of the people lost), and in our compiler, we can 1.1 point to expand the syntax, Instead of writing the compiler all at once, this is also given by the flexibility of the scheme language.

3. There are many advantages, I can't think of it for a moment ^_^

The blogger will complete this compiler with a series of articles presented, if there are any errors, comments can directly contact me (email:[email protected])

We start with the numbers today, this is very simple, mainly want you to familiarize yourself with the syntax of scheme. (The blogger uses the Ubuntu 14.04 operating system, the scheme compiler uses the DRRACKET,C compiler using GCC)

We have to prepare two things first, one is the run-time program of the compiled program, and the other is the compiler program we wrote with scheme.

1 C Run-time program

This run-time program is still very simple and requires only a printf function to display the compiled program:

intint main (intChar* * argv) {       printf ("%d\n  ", Scheme_entry ());        return 0 ;}

2 Scheme Compilation Program

(Define (compile-Program x) (Emit". Text") (Emit". Global _scheme_entry") (Emit". def _scheme_entry;. SCL 2;. type ;. endef") (Emit"_scheme_entry:") (Emit"LFB0:") (Emit". Cfi_startproc") (Emit"PUSHL%EBP") (Emit". Cfi_def_cfa_offset 8") (Emit". Cfi_offset 5,-8") (Emit"movl%esp,%EBP") (Emit". Cfi_def_cfa_register 5") (Emit"movl $~a,%eax"x) (Emit"popl%EBP") (Emit". Cfi_restore 5") (Emit". CFI_DEF_CFA 4, 4") (Emit"ret") (Emit". Cfi_endproc") (Emit"LFE0:"))

This is the compiler's main program, the emit function is to export the string to our Output-port, the program's string is composed of assembly, not familiar with assembly language friends will feel strange, most of which we do not need to understand, we need to note is (emit " MOVL $~a,%eax "x", X is our immediate number, put it in the EAX register, because the computer will put the result into the EAX register after executing a function.

(define (Emit. Args)  (apply fprintf (compile-port) args)  (newline (compile-port)))

This is the definition of the emit function, and the result is displayed in our specified port.

(Define compile-port  (Make-parameter   (current-output-port) (   Lambda (p)     ( Unless (output-port?) p)        ' compile-port (format "not an output port ~s" P)))      P)))

Current-output-port defined in the Rnrs/io/ports-6 module, we need (require rnrs/io/ports-6).

(define (Compile expr) (  run-compile expr)  (build) (  execute))

This function helps us perform the compilation, linking, and execution of functions.

(Define (run-compile expr)  (Let (p (open"program.s"Replace) ) (    parameterize ((Compile-Port P)) (      compile-Programexpr)) (Close- Output-port p)))

We set the output port to PROGRAM.S.

(define (build)  (unless (not (false"Gcc-m32-wall-o program ~a PROGRAM.S"                                   (Runtime-file)                                 'make' could not build target " ))

Connect us using GCC to connect with the C run-time program we prepared earlier.

(define (execute)  (unless (not (false)./stst > Stst.out"   'make' (produced program exited abnormally " )))

Execute the program.

Well, after performing the above steps, you get a compiler that compiles the numbers, although it is very simple, but this compiler is the basis for us to construct a more complex compiler later, we hope that interested friends seriously treat it.

Finally, we have a rest, we hope you have fun

Write a scheme with scheme compiler (i)

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.