Git Product home page Git Product logo

simplecompiler's Introduction

A SIMPLE COMPILER

Author: Tam N Nguyen
Github: https://github.com/genterist/simpleCompiler

Forewords

This is a simple compiler demonstrating the basic concepts commonly found in most compilers. Usually, a compiler consists of a scanner, a parser and a generator each of which will be described in further details below. There are some bugs in the codes and the final generated assembly codes are just for illustration purposes. If you are my potential employer and want to check out the full version of this program, please email your name, company name, position, contact info to genterist [at] live.com Students! You are free to check the codes so you can understand the concepts but please do not copy the codes for your assignments.

Execution

make : compile main program
make clean : clean up all compiled files
comp [file name] : compile a file (not that file ext of .fs16 is implicit)
comp : input from keyboard or file redirection stream

Compiler will generate:
1- Onscreen feedbacks
2- *.tree files for parsed tree
3- *.asm files for generated asm codes

Program will quit when EOF reached or there is an error
When in manual keyboard input mode, CTRL-Z (EOF signal) will terminate the program
EOF is recommended to be on a new line in programming text (a blank line at the end of each programming text, after "End" keyword)

The Context-Free Grammar to be used

<program>  ->     <vars> <block>
<block>    ->      Begin <vars> <stats> End
<vars>     ->      empty | Var Identifier <mvars> 
<mvars>    ->      empty | : : Identifier <mvars>
<expr>     ->      <M> + <expr> | <M>
<M>        ->      <T> - <M> | <T>
<T>        ->      <F> * <T> | <F> / <T> | <F>
<F>        ->      - <F> | <R>
<R>        ->      [ <expr> ] | Identifier | Number   
<stats>    ->      <stat>  <mStat>
<mStat>    ->      empty | <stat>  <mStat>
<stat>     ->      <in> | <out> | <block> | <if> | <loop> | <assign>
<in>       ->      Scan : Identifier .
<out>      ->      Print [ <expr>  ] .
<if>       ->      [ <expr> <RO> <expr> ]  Iff <block>             
<loop>     ->      Loop [ <expr> <RO> <expr> ] <block>
<assign>   ->      Identifier == <expr> .                    // == is one token here
<RO>       ->      >=> | <=< | = |  > | <  |  =!=           // each is one token here

Lexical Definitions

  • All case sensitive
  • Each scanner error should display the error message after "Scanner Error:"
  1. Alphabet
  • All English letters (upper and lower), digits, plus the extra characters as seen below, plus WS
  • No other characters allowed and they should generate lexical errors
  1. Identifiers
  • Begin with a letter and continue with any number of letters and digits
  • no ID is longer than 8
  1. Keywords (reserved, suggested individual tokens)
  • Begin End Start Stop Iff Loop Void Var Int Call Return Scan Print Program
  1. Relational operators
  • = < > =!= >=> <=<
  1. Other operators
  • == : + - * / & %
  1. Delimiters . ( ) , { } ; [ ]
  2. Integers
  • Any sequence of decimal digits, no sign
  • No number longer than 8 digits
  1. Comments
  • Start with @ and end with WS

Scanner :: Automaton

![parser automaton] (https://github.com/genterist/simpleParser/blob/master/Parser-2.png)

![driver table] (https://github.com/genterist/simpleParser/blob/master/Screen%20Shot%202016-10-16%20at%2011.24.06%20AM.png)

![mapped driver table] (https://github.com/genterist/simpleParser/blob/master/Screen%20Shot%202016-10-16%20at%2011.24.31%20AM.png)

Parser :: Recursive descent parsing

  • This is a top-down parser
  • Every non-terminal has a function
  • Parsed results will be added into a binary tree
  • Parsing is done left to right
  • Parsed tree will be a Binary Tree

Parser :: The parsed tree of test case (test2 file)

![parsed bnTree] (https://github.com/genterist/simpleParser/blob/master/ParserBNTree-2.png)

Compiller

  • Compiler will traverse the parsed tree top down
  • Compiler will generate ASM codes and save to .asm file
  • Reserved variables name are T[scopenumber] and Ttemp
  • Compiler uses global storage with local scope separation (no stack was used to maintain local scope variables - full implementation of this program will use stacks to manage local variables)
  • Even when there are errors in programming syntax, an ASM file will still be generated .

simplecompiler's People

Contributors

genterist avatar

Watchers

James Cloos avatar  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.