Note that there are some explanatory texts on larger screens.

plurals
  1. POANTLR (or alternative): decoupling parsing from evaluation
    text
    copied!<p>I have a relatively simple DSL that I would like to handle more robustly than a bunch of manually-coded <code>java.util.regex.Pattern</code> statements + parsing logic.</p> <p>The most-quoted tool seems to be ANTLR. I'm not familiar with it and am willing to give it a try. However I get a little leery when I look at the examples (e.g. the ANTLR <a href="http://www.antlr.org/wiki/display/ANTLR3/Expression+evaluator" rel="nofollow noreferrer">expression evaluator example</a>, or Martin Fowler's <a href="http://martinfowler.com/bliki/HelloAntlr.html" rel="nofollow noreferrer">HelloAntlr</a>, or <a href="https://stackoverflow.com/questions/1931307/antlr-is-there-a-simple-example/1932664#1932664">this other Q on stackoverflow</a>). The reason for this is that the grammar files seem like they are a hodgepodge of grammar definitions interspersed with fragments of the implementation language (e.g. Java) that are imperative in nature.</p> <p>What I would really prefer is to separate out the imperative / evaluation part of the parser. Is there a way to use ANTLR (or some other tool) to define a grammar &amp; produce a set of Java source files so that it compiles into classes that I can use to parse input into a structure w/o acting upon that structure?</p> <p>for example, if I wanted to use expression evaluation with just the <code>+</code> and <code>*</code> and <code>()</code> operators, and I had the input</p> <blockquote> <p>3 * (4 + 7 * 6) * (3 + 7 * (4 + 2))</p> </blockquote> <p>then what I would like to do is write a grammar to convert that to a hierarchical structure like</p> <pre><code>Product Term(3) Sum Term(4) Product Term(7) Term(6) Sum Term(3) Product Term(7) Sum Term(4) Term(2) </code></pre> <p>where I can use classes like</p> <pre><code>interface Expression&lt;T&gt; { public T evaluate(); } class Term implements Expression&lt;Double&gt; { final private double value; @Override public Double evaluate() { return value; } } class Product implements Expression&lt;Double&gt; { final private List&lt;Expression&lt;Double&gt;&gt; terms; @Override public Double evaluate() { double result = 1; for (Expression&lt;Double&gt; ex : terms) result *= ex.evaluate(); return result; } } class Sum implements Expression&lt;Double&gt; { final private List&lt;Expression&lt;Double&gt;&gt; terms; @Override public Double evaluate() { double result = 0; for (Expression&lt;Double&gt; ex : terms) result += ex.evaluate(); return result; } } </code></pre> <p>and use ANTLR to construct the structure. Is there a way to do this? I would really rather pursue this approach, as it lets me (and other software engineers) edit and visualize complete Java classes without having to have those classes fragmented into weird pieces in ANTLR grammar files.</p> <p>Is there a way to do this?</p> <hr> <p><strong>clarification:</strong> I want to spend as much of my effort as possible in two ways: defining the grammar itself, and in ANTLR-independent Java (e.g. my Product/Sum/Term classes). I want to minimize the amount of time/experience I have to spend learning ANTLR syntax, quirks and API. I don't know how to create and manipulate an AST from ANTLR grammar. Because this is only a small part of a large Java project, it's not just me, it's anyone in my team that has to review or maintain my code.</p> <p>(I don't mean to sound impertinent: I'm willing to make the investment of time and energy to use a tool, but only if the tool becomes a useful tool and does not continue to become a stumbling block.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload