Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're allowed to use a parser generator tool like ANTLR, here's how you could get started. The grammar for a simple logic-language could look like this:</p> <pre><code>grammar Logic; parse : expression EOF ; expression : implication ; implication : or ('-&gt;' or)* ; or : and ('||' and)* ; and : not ('&amp;&amp;' not)* ; not : '~' atom | atom ; atom : ID | '(' expression ')' ; ID : ('a'..'z' | 'A'..'Z')+; Space : (' ' | '\t' | '\r' | '\n')+ {$channel=HIDDEN;}; </code></pre> <p>However, if you'd parse input like <code>(P || Q || R) &amp;&amp; ((P -&gt; R) -&gt; Q)</code> with a parser generated from the grammar above, the parse tree would contain the parenthesis (something you're not interested in after parsing the expression) and the operators would not be the root of each sub-trees, which doesn't make your life any easier if you're interested in evaluating the expression.</p> <p>You'll need to tell ANTLR to omit certain tokens from the AST (this can be done by placing a <code>!</code> <em>after</em> the token/rule) and make certain tokens/rules the root of their (sub) tree (this can be done by placing a <code>^</code> after it). Finally, you need to indicate in the <code>options</code> section of your grammar that you want a proper AST to be created instead of a simple parse tree.</p> <p>So, the grammar above would look like this:</p> <pre><code>// save it in a file called Logic.g grammar Logic; options { output=AST; } // parser/production rules start with a lower case letter parse : expression EOF! // omit the EOF token ; expression : implication ; implication : or ('-&gt;'^ or)* // make `-&gt;` the root ; or : and ('||'^ and)* // make `||` the root ; and : not ('&amp;&amp;'^ not)* // make `&amp;&amp;` the root ; not : '~'^ atom // make `~` the root | atom ; atom : ID | '('! expression ')'! // omit both `(` and `)` ; // lexer/terminal rules start with an upper case letter ID : ('a'..'z' | 'A'..'Z')+; Space : (' ' | '\t' | '\r' | '\n')+ {$channel=HIDDEN;}; </code></pre> <p>You can test the parser with the following class:</p> <pre><code>import org.antlr.runtime.*; import org.antlr.runtime.tree.*; import org.antlr.stringtemplate.*; public class Main { public static void main(String[] args) throws Exception { // the expression String src = "(P || Q || R) &amp;&amp; ((P -&gt; R) -&gt; Q)"; // create a lexer &amp; parser LogicLexer lexer = new LogicLexer(new ANTLRStringStream(src)); LogicParser parser = new LogicParser(new CommonTokenStream(lexer)); // invoke the entry point of the parser (the parse() method) and get the AST CommonTree tree = (CommonTree)parser.parse().getTree(); // print the DOT representation of the AST DOTTreeGenerator gen = new DOTTreeGenerator(); StringTemplate st = gen.toDOT(tree); System.out.println(st); } } </code></pre> <p>Now to run the <code>Main</code> class, do:</p> <h2>*nix/MacOS</h2> <pre>java -cp antlr-3.3.jar org.antlr.Tool Logic.g javac -cp antlr-3.3.jar *.java java -cp .:antlr-3.3.jar Main</pre> <h2>Windows</h2> <pre>java -cp antlr-3.3.jar org.antlr.Tool Logic.g javac -cp antlr-3.3.jar *.java java -cp .;antlr-3.3.jar Main</pre> <p>which will print a <a href="http://www.graphviz.org/content/dot-language" rel="nofollow noreferrer">DOT source</a> of the following AST:</p> <p><img src="https://i.stack.imgur.com/2bF3E.png" alt="enter image description here"></p> <p><em>(image produced with <a href="http://graphviz-dev.appspot.com/" rel="nofollow noreferrer">graphviz-dev.appspot.com</a>)</em></p> <p>Now <em>all</em> you need to do is evaluate this AST! :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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