UP | HOME

Toy Compiler Projects

Table of Contents

Overview

Hand-code both toy compilers (hand-written parser and generated parser). These projects are solely to practice using make, gcc, bison, flex, and git to develop, build, and run software. Since the goal of this is not to design and implement the compilation algorithms for yourself, toy compilers may not be turned in late, since they only require hand-entering and running the code.

You may use the code developed live in class. You must, however, enter the code yourself by hand, without any digital copying from student or others' versions of the code. This will help ensure you have comfort with the style of C and programming tools used for the SimpleC Compiler programming project that you will design and implement individually.

To submit these projects, accept the GitHub assignment for "Toy Compiler 1" and "Toy Compiler 2" from the link in Webcourses, and submit your answers to GitHub. Please do not create or copy the repository to your personal GitHub page, since this violates UCF's Golden Rule policies.

Grading scheme for both toy compiler projects:

  • 1pt for any submission
  • 3pt for any submission that builds
  • 4pt for any submission that builds and runs with some minor mistakes in the output
  • 5pt for any submission that builds and runs correctly

Toy compiler 1, hand-written parser

Read over the lecture notes covering the hand-written toy compiler.

  1. Be sure that your virtual machine is setup.
  2. Enter your virtual machine vagrant ssh.
  3. Create the git repo using the Toy Compiler 1 assignment link in webcourses, clone it in your virtual machine and enter the newly created directory.

    git clone git@github.com:cop3402/toy-compiler-1-USERNAME
    cd toy-compiler-1-USERNAME
    
  4. Hand-write all the source code for the toy compiler using the images in the starter repository.
  5. Double-check that it is working correctly using the instructions below.
  6. Submit the project to your toy compiler repository.
  7. Double-check that all the files are in your GitHub repository by visiting its link or running git clone YOUR_REPO_URL to verify that graders will have access to it.

Toy compiler 2, generated parser

Read over the lecture notes about using the parser generator to replace the hand-written parser in the toy compiler.

  1. Create the git repo using the Toy Compiler 2 assignment link in webcourses, clone it in your virtual machine and enter the newly created directory.

    git clone git@github.com:cop3402/toy-compiler-2-USERNAME
    cd toy-compiler-2-USERNAME
    
  2. Hand-write all the source code for the toy compiler using the images in the starter repository.
  3. Double-check that it is working correctly using the instructions below.
  4. Submit the project to your toy compiler repository.
  5. Double-check that all the files are in your GitHub repository by visiting its link or running git clone YOUR_REPO_URL to verify that graders will have access to it.

Checking your work

It's important to validate that your toy compiler both builds and runs as expected. From inside of your source code repository directory, run the following:

make  # build the toy compiler
cat template_start.s <(cat example.toy | ./toy) template_end.s > example.s  # run the toy compiler, will echo the input in toy compiler 2
diff -w example.s.expected example.s  # compare the output, will not print anything if the files math
gcc -o example example.s  # assemble the output
./example  # execute the output program, this will output the results of the calculations in the example.toy program

Submitting your work

Submit your project using git to add source, commit changes to them, and push your local repository to the GitHub repo as described in class. Put the Makefile and all source files in the root of the source code repository; there is no need to create a subfolder.

It is important to validate that your source code repository has the necessary files to build and run your project. You can check this by going to a new directory (somewhere besides your current toy compiler source directory) and recloning your repository, i.e.,

# in a directory other than your toy compiler directory
git clone git@github.com:cop3402/toy-compiler-1-USERNAME toy-compiler-1-test-repo
cd toy-compiler-test-repo

(Be sure to substitute USERNAME with your actual GitHub user name.)

Then try to build and run your project as described above.

Complete example

example.toy

1+5;
7-4;
8*3;
9/4;

Output of cat example.toy | ./toy

movl $1, %eax
movl $5, %ebx
addl %ebx, %eax
movl  %eax, %esi
leaq    .LC0(%rip), %rdi
movl    $0, %eax
call    printf@PLT

movl $7, %eax
movl $4, %ebx
subl %ebx, %eax
movl  %eax, %esi
leaq    .LC0(%rip), %rdi
movl    $0, %eax
call    printf@PLT

movl $8, %eax
movl $3, %ebx
imull %ebx, %eax
movl  %eax, %esi
leaq    .LC0(%rip), %rdi
movl    $0, %eax
call    printf@PLT

movl $9, %eax
movl $4, %ebx
cdq
idiv %ebx
movl  %eax, %esi
leaq    .LC0(%rip), %rdi
movl    $0, %eax
call    printf@PLT

Output of cat template_start.s <(cat example.toy | ./toy) template_end.s > example.s

        .file	"toy"
        .text
        .section	.rodata
.LC0:
        .string	"%d\n"
        .text
        .globl	main
        .type	main, @function


main:
.LFB0:
        .cfi_startproc
        endbr64
        pushq	%rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq	%rsp, %rbp
        .cfi_def_cfa_register 6

        movl $1, %eax
        movl $5, %ebx
        addl %ebx, %eax
        movl  %eax, %esi
        leaq	.LC0(%rip), %rdi
        movl	$0, %eax
        call	printf@PLT

        movl $7, %eax
        movl $4, %ebx
        subl %ebx, %eax
        movl  %eax, %esi
        leaq	.LC0(%rip), %rdi
        movl	$0, %eax
        call	printf@PLT

        movl $8, %eax
        movl $3, %ebx
        imull %ebx, %eax
        movl  %eax, %esi
        leaq	.LC0(%rip), %rdi
        movl	$0, %eax
        call	printf@PLT

        movl $9, %eax
        movl $4, %ebx
        cdq
        idiv %ebx
        movl  %eax, %esi
        leaq	.LC0(%rip), %rdi
        movl	$0, %eax
        call	printf@PLT
        movl	$0, %eax
        popq	%rbp
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc


.LFE0:
        .size	main, .-main
        .ident	"GCC: (Ubuntu 9.2.1-9ubuntu2) 9.2.1 20191008"
        .section	.note.GNU-stack,"",@progbits
        .section	.note.gnu.property,"a"
        .align 8
        .long	 1f - 0f
        .long	 4f - 1f
        .long	 5
0:
        .string	 "GNU"
1:
        .align 8
        .long	 0xc0000002
        .long	 3f - 2f
2:
        .long	 0x3
3:
        .align 8
4:

Author: Paul Gazzillo

Created: 2024-02-19 Mon 14:12

Validate