GithubHelp home page GithubHelp logo

kaby76 / antlr4templates Goto Github PK

View Code? Open in Web Editor NEW
9.0 3.0 2.0 82 KB

Third-party "dotnet new" templates for Antlr.

License: MIT License

C# 10.27% Shell 18.13% ANTLR 10.66% Makefile 2.67% Java 5.03% CMake 47.76% C++ 5.49%

antlr4templates's Introduction

Antlr4Templates

This package is a third-party set of templates for generating Antlr4 programs using the Arithmetic.g4 grammar. A generated program contains:

  • a grammar for expressions.
  • a simple console program for parsing input.

To use this package, you will need to install NET 6. Older versions are supported if you modify the <TargetFramework> element in the generated .csproj file.

To install:

dotnet new -i Antlr4Templates

To create an Antlr C# program:

mkdir Foo
cd Foo
dotnet new csharp-combine
dotnet run

Available templates

  • csharp-combine: unified Arithmetic grammar for C#.
  • csharp-split: split Arithmetic grammar for C#.
  • antlr4cs-combine: unified Arithmetic grammar for C# using the Antlr4cs fork.
  • antlr4cs-split: split Arithmetic grammar for C# using the Antlr4cs fork.
  • cpp-combine: unified Arithmetic grammar for C++.
  • cpp-split: split Arithmetic grammar for C++.
  • java-combine: unified Arithmetic grammar for Java.
  • java-split: split Arithmetic grammar for Java.

To see the complete list of templates, type dotnet new --list | grep '[-]combine\|[-]split' in Bash.

These templates have been tested and work with MSYS2 for Windows 11 using NET6, and Linux. All apps are console programs that take no program arguments and output a parse for various strings, some that are not correct in order to demonstrate parse errors.

NB: Antlr4cs (in nuget here, here, and here; source here) is an obsolete fork of the "official" Antlr4 tool and runtime for CSharp. The Antlr4cs fork has many problems, and it is not being maintained:

  • Build rule problems
    • CSC : warning CS2002: Source file 'C:\msys64\home\Kenne\te\obj\Debug\net6.0\ArithmeticBaseVisitor.cs' specified multiple times [C:\msys64\home\Kenne\te\te.csproj]
    • <CustomToolNamespace> element in .csproj is not displayed correctly in VS IDE settings.
  • There is no lexer error listener override because it is not supported by the runtime. The source code was updated to be netstandard2.0 in Dec 2020, but it was not compiled and released since Dec 2018!

It is supported here only as a convenience.

To uninstall:

dotnet new -u Antlr4Templates

Alternative generator

If you are interested in generating a parser from a grammar, or for a target other than CSharp, use trgen.

antlr4templates's People

Contributors

kaby76 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

antlr4templates's Issues

Got Antlr4.Runtime.InputMismatchException while adapting template example

Hi,

I was able to generate a template using dotnet new csharp-combine. I was able to build and run that example, and got the expected results from parsing the simple calculator language.

Now I have modified my own project using the template code to walk through the same sequence: read file, lex, parse, check errors.

I am getting an exception during the first call to the parser now:
Exception thrown: 'Antlr4.Runtime.InputMismatchException' in Antlr4.Runtime.Standard.dll

Looking at the documentation for this exception doesn't clarify much, it just says that the current input doesn't match the expected token.

If this was just my grammar resulting in a parse-error, I would think this would present as the flag had_error being true. Therefore, I assume this is a case of me misusing ANTLR, rather than anything being wrong in my grammar or my program source text being parsed.

Here is the function doing the work:

public Program Compile(string path)
        {
            var prog = new Program();

            String input = File.ReadAllText(path);
            var str = new AntlrInputStream(input);
            var lexer = new ML2Lexer(str);
            var tokens = new CommonTokenStream(lexer);
            var parser = new ML2Parser(tokens);
            var listener_lexer = new ML2ErrorListener<int>();
            var listener_parser = new ML2ErrorListener<IToken>();
            lexer.RemoveErrorListeners();
            parser.RemoveErrorListeners();
            lexer.AddErrorListener(listener_lexer);
            parser.AddErrorListener(listener_parser);
            
            parser.BuildParseTree = true;
            var tree = parser.program();

            if (listener_lexer.had_error || listener_parser.had_error)
            {
                Trace.WriteLine("Error in parse.");
            }
            else
            {
                Trace.WriteLine("Parse completed.");
            }

            Trace.WriteLine(tree.ToStringTree(parser));

            return prog;
        }

You can ignore the object Program since it's unused for now. The rest of the code, as far as I can see, follows the template example exactly, except that I am parsing a different grammar.

The exception happens at the line:
var tree = parser.program();

Do you have any suggestions for debugging this exception?

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.