Giter Site home page Giter Site logo

maamardli / good-for-nothing-compiler Goto Github PK

View Code? Open in Web Editor NEW

This project forked from johandanforth/good-for-nothing-compiler

0.0 2.0 0.0 148 KB

This is a continuation of the old Good for Nothing Compiler in C# presented by Joel Pobar and Joe Duffy at PDC back in 2005

C# 100.00%

good-for-nothing-compiler's Introduction

Good for Nothing Compiler in C#

This is a continuation of the old Good for Nothing Compiler presented by Joel Pobar and Joe Duffy at PDC back in 2005! The presentation was liked and the article and code posted at https://msdn.microsoft.com/en-us/magazine/cc136756.aspx has been read and used by many programmers who wanted to learn more about Scanners, Parsers and how to write a somple compiler in C#.

The code compiles a simple c-like language called "Good for nothing" and has support for variables, simple inputs/outputs and a for-loop.

The article by the gentlemen mentioned above contains a Language Definition using a metasyntax called EBNF (Extended Backus-Naur Form), that should support parsing and compiling of arithmetic expressions, but the code doesn't, so I thought I would try and add support for that and learn something along the way.

Feel free to spice the code up!

##Language Specification## This is the language specification defined in a simple EBNF style:

<stmt> := var <ident> = <expr>
	| <ident> = <expr>
	| for <ident> = <expr> to <expr> do <stmt> end
	| read_int <ident>
	| print <expr>
	| <stmt> ; <stmt>

<expr> := <string>
	| <int>
	| <arith_expr>
	| <ident>

<arith_expr> := <expr> <arith_op> <expr>
<arith_op> := + | - | * | /

<ident> := <char> <ident_rest>*
<ident_rest> := <char> | <digit>

<int> := <digit>+
<digit> := 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

<string> := " <string_elem>* "
<string_elem> := <any char other than ">

##Sample Programs## A simple program could look like this:

var x = 2;
var y = 4;
var z = y / x;
print z;
print "that's it folks!";

A program with a loop could look like this:

var ntimes = 0;
print "How much do you love this company? (1-10) ";
read_int ntimes;
var x = 0;
for x = 0 to ntimes do
   print "Developers!";
end;
print "Who said sit down?!!!!!";

good-for-nothing-compiler's People

Contributors

johandanforth avatar

Watchers

 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.