Coder Social home page Coder Social logo

pminhtam / compiler Goto Github PK

View Code? Open in Web Editor NEW
1.0 0.0 0.0 8.46 MB

Implement a compiler for PL\0 language

C++ 86.06% C 13.94%
complier toy-compiler ll1-grammar assembly toy-language compiler programming-language interpreter virtual-machine pl0-compiler

compiler's Introduction

PL\0 complier

This project is a toy compiler which compile from PL\0 language to assembly language

PL/0 is a programming language, intended as an educational programming language, that is similar to but much simpler than Pascal, a general-purpose programming language.

PL\0 is LL (1) language which just need to lookahead 1 tokens to decide what to do with the current token.

Compile and run project

g++ *.cpp -o main || g++ main.cpp -o main

Run project

./main <path to code file>

The compiler will check syntax. If the code does not have the error, PL\0 code will comply to the assembly code

Example :

./main asm8

PL\0 code is:

program asd;
const b = 10;
var x,i,k;
begin 
	i:=0;
	x:=1;
	call readi(k);
	for i:=1 to k do
	begin
		x:=x*i;
	end;
	call writei(x);
end.

will be complied become:

0    J   1
1    INT   7
2    LA   0,   5
3    LC   0
4    ST
5    LA   0,   4
6    LC   1
7    ST
8    LA   0,   6
9    RI
10    LA   0,   5
11    CV
12    LC   1
13    ST
14    CV
15    LI
16    LV   0,   6
17    LE
18    FJ   31
19    LA   0,   4
20    LV   0,   4
21    LV   0,   5
22    MUL
23    ST
24    CV
25    CV
26    LI
27    LC   1
28    ADD
29    ST
30    J   14
31    DCT   1
32    LV   0,   4
33    WRI
34    BP
35    

Analysis Phase

Lexical

begin, end, if, then, while, do, call, odd, to const, var, procedure, program, else, for

alt text

Syntax

Syntax of PL\0 language

alt text alt text alt text alt text

Complie need parse code to each statement.

Check error

alt text Check error of identical

alt text Check error of type of expression

alt text Check error of type of command

alt text Check error of type of function

Assembly Code Generator

Use Stack-based CPU Organization

Struct follow by
op + p + q with op is operator

alt text alt text alt text alt text alt text

Virtual machine

File may_ao.py is a virtual machine which can run assembly code from compiler.

compiler's People

Contributors

pminhtam avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.