Giter VIP home page Giter VIP logo

tiny-language-antlr4's Introduction

#Tiny Language for ANTLR 4

In some blog posts I wrote a while ago, I demonstrated how to create a small dynamically typed programming language called Tiny Language using ANTLR 3.

However, ANTLR 4 is now the leaner (and meaner) version of the popular parser generator. Since the changes from v3 to v4 are significant, making Tiny Language work using ANTLR 4 is non trivial. Most notably, ANTLR 4 does not have any tree rewriting anymore. The new version generates listeners (and/or visitors) that can be used to walk the plain parse tree.

This Github project provides a start on how to get Tiny Language working with ANTLR 4. Not all functionality is implemented in the class EvalVisitor , but by looking at what is implemented one can try the missing functionality.

Get up and running

First, clone this repository:

git clone https://github.com/bkiers/tiny-language-antlr4.git
cd tiny-language-antlr4

Then generate the lexer, parser and visitor classes using the antlr4 Maven plugin:

mvn antlr4:antlr4

Compile all classes:

mvn install

and finally run the Main class which executes the test.tl fil which contains:

a = 10;
b = 32;
c = 42;

if a + b == 42.00001 do
  println(a + " + " + b + " == 42.00001");
else if a + b == c do
  println(a + " + " + b + " == " + c);
else do
  println("nothing was true");
end
mvn -q exec:java

Or, combine all the previous commands in a single liner:

mvn -q antlr4:antlr4 install exec:java

which should print:

10.0 + 32.0 == 42.0

No Maven?

If you're unfamiliar with Maven, and are reluctant to install it, here's how to perform all the steps from the (*nix) command line (assuming you're in the root folder of the project tiny-language-antlr4):

Download ANTLR 4:

wget http://www.antlr.org/download/antlr-4.1-complete.jar

Generate the lexer, parser and visitor classes and move them to the other .java project sources:

java -cp antlr-4.1-complete.jar \
  org.antlr.v4.Tool src/main/antlr4/tl/antlr4/TL.g4 \
  -package tl.antlr4 \
  -visitor
  
mv src/main/antlr4/tl/antlr4/*.java src/main/java/tl/antlr4

Compile all .java source files:

javac -cp antlr-4.1-complete.jar src/main/java/tl/antlr4/*.java

Run the Main class:

java -cp src/main/java:antlr-4.1-complete.jar tl.antlr4.Main

tiny-language-antlr4's People

Contributors

bkiers avatar

Watchers

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