Skip to content

Compiler for an invented programming language using ANTLR and a custom stack machine.

License

Notifications You must be signed in to change notification settings

hanzopgp/CalcANTLR

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 

Repository files navigation

CalcANTLR

Table of contents

  1. Presentation
  2. How does it work
  3. Project architecture
  4. Commands
  5. Details
  6. Links

Presentation

This is a calculator language compiler done with ANTLR and a custom stack machine.
3rd year bachelor degree in computer science, grade : 20/20.

How does it work

I'm using ANTLR to generate a java lexer and parser with a grammar file. The "calculator language" is made of numbers, operators, spaces, brackets... The purpose of the grammar I've built is to translate the calculator language into stack machine language. We can then run this stack machine language (which looks like assembly but without register) using the stack machine in src/stackmachine folder.

Project architecture


CalcANTLR/
├── Project/
│   	├── src/
│	│	├── Calculette.g4 (Grammar file of the project)
│   	│	├── do_all.sh     (Script that does basicaly everything from building to testing)
│	│	├── tests/        (Contains the test file runned in do_all.sh script)
│   	│	├── scripts/      (Contains all the scripts)
│	│	├── build/        (Contains the class files after compiling)
│   	│	├── util/         (Contains the java useful java files used in the grammar file)
│	│ 	├── mvap_output/  (Contains the mvap code which is the output of our compiler)
│	│	└── antlr_output/ (Contains all the files generated by antlr4 like token files)
│   	└── stackmachine/         (Contains the stack machine which can run our mvap file)
├── README.md		          
├── Who.txt
└── Licence  

Commands

General commands :

  • Export classpath : export CLASSPATH=".:/usr/local/lib/antlr-4.9-complete.jar:$CLASSPATH"
  • Use ANTRL to generate lexer and parser java files : java org.antlr.v4.Tool <your_grammar>.g4
  • Build all the java files : javac \*.java
  • Run java program if you use a main instead of TestRig or grun : java <your_main_class>
  • Test your grammar quickly : java org.antlr.v4.runtime.misc.TestRig <your_grammar> <start_rule>
  • Test it and show tree : java org.antlr.v4.runtime.misc.TestRig <your_grammar> <start_rule> -gui
  • Test it and show tokens : java org.antlr.v4.runtime.misc.TestRig <your_grammar> "tokens" -tokens
  • Test the grammar and put the output in a file : java org.antlr.v4.runtime.misc.TestRig <your_grammar> <start_rule> > <file_name>.mvap
  • Signal you are done writing your test : CTRL+D
  • Show binary code : od -t x4 --endian=big <file>.mvap.cbap od -t d4 --endian=big <file>.mvap.cbap

Stack machine commands :

  • Build stack machine code : java MVAPAssembler -d <your_stackmachine_file>.mvap
  • Run stack machine code : java CBaP <your_stackmachine_file>.mvap.cbap

Scripts :

  • Does everything in one script : (the input file is in tests folder) bash do_all.sh
  • Does basicaly everything from building to test : bash build_all_and_test.sh
  • Reset everything : bash reset.sh
  • Build new grammar : bash build_grammar.sh
  • Build all util java files : bash build_util.sh
  • Run a test file, display the result and put the output in a mvap file : (the test file is in tests folder) bash test_grammar.sh
  • Run .mvap file : bash test_mvap.sh

Details

  • ANTLR basic use
  • Basic calcul grammar
  • Calcul priority
  • Negative numbers
  • Negative expressions
  • Parenthesis support
  • Integur variables
  • Skips all comments
  • Write / Read
  • While loop
  • For loop
  • Repeat until loop
  • Blocks (sequence of instructions)
  • If statement
  • Else statement
  • Relational operators
  • Logical operators
  • Full float/bool support
  • Functions
  • Global/Local variables
  • Multiple args functions
  • Implicit cast
  • Auto cast if needed
  • Freeing stack memory at the end
  • General tests
  • Errors and warnings
  • Increments ++ --

Links

About

Compiler for an invented programming language using ANTLR and a custom stack machine.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages