/mongaComp

Compiler for monga programming language

Primary LanguageCMIT LicenseMIT

MongaComp is a compiler for the Monga language, a small C-like programming language designed by Roberto Ierusalimschy at PUC-Rio. More information of its design can be found here.

Requirements

  • C compiler
  • make
  • flex
  • bison
  • bash
  • clang/llc (llvm compiler)

Obs.: rules.lex was developed using flex. It should also work on lex, but it was never tested. Obs.2.: grammar.y was developed using bison. It should also work on yacc, but bison's output should be taken over yacc's, in case they differ.

Getting Started

  • First, use make to compile the project:
make

make will generate a executable file named comp. comp reads from standard input and outputs a file named a.ll and only then calls clang to generate a binary named a.out.

Usage examples

cat test/helloWorld.monga | ./comp
./a.out
cat test/test6.monga | ./comp
./a.out -c 

Usage Description

<string input> | ./comp [options] [clangOptions]
clangOptions

clangOptions will be redirected to clang in compile time as-is.

options
noBin

noBin flag does not emit a binary file a.out.

./comp -noBin 
noTree

noTree flag does not emit the program's AST.

./comp -noTree
noCode

noCode outputs only the program's AST.

./comp -noCode

Tests

To run the tests run:

make test

For binary compilation only, run:

make testbin

The tests aim to check syntax, types and variables. It should output the messages "Syntax OK" and Types OK before printing the AST and compiling your program into the files a.ll and a.out. The file a.out can be executed by running ./a.out.

For checking the file only, run:

make testchecks

For testing only the lexical scope, run:

make testlexical

For testing only the syntactical scope, run:

make testsyntax

Clean binary files

To clean builds, use:

make clean

Folder structure

src/ contains the sources:

  • rules.lex
  • lex.h
  • grammar.y
  • grammar.c
  • grammar.h
  • main.c
  • ...

lex.c is generated by compiling rules.lex with flex. Likewise grammar.c and grammar.h by compiling grammar.y with bison.

test/ contains MongaComp test suites. Each folder is a collection of test cases. Each test has a name and consists of two files: name.monga and name.answer name.monga is the input the compiler receives and name.answer, the expected output. The result and the answer are compared textually. If they are equal, the test is said to be OK. If they are different, a problem is found and the difference is outputted.

Each test suite has its own script.

temp/ is a folder for temporary files during a given compilation.