Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaCC Problem - Generated code doesn't find all parse errors
    text
    copied!<p>Just started with JavaCC. But I have a strange behaviour with it. I want to verify input int the form of tokens (letters and numbers) wich are concatenated with signs (+, -, /) and wich can contain parenthesis. I hope that was understandable :)</p> <p>In the main method is a string, which should produce an error, because it has one opening but two closing parenthesis, but I do not get a parse exception --> Why?</p> <p>Does anybody have a clue why I don't get the exception?</p> <p>I was struggling with left recursion and choice conflicts with my initial try, but managed to get over them. Maybe there I introduced the problem?!</p> <p>Oh - and maybe my solution is not very good - ignore this fact... or better, give some advice ;-) </p> <p>File: CodeParser.jj</p> <pre><code> options { STATIC=false; } PARSER_BEGIN(CodeParser) package com.testing; import java.io.StringReader; import java.io.Reader; public class CodeParser { public CodeParser(String s) { this((Reader)(new StringReader(s))); } public static void main(String args[]) { try { /** String has one open, but two closing parenthesis --&gt; should produce parse error */ String s = "A+BC+-(2XXL+A/-B))"; CodeParser parser = new CodeParser(s); parser.expression(); } catch(Exception e) { e.printStackTrace(); } } } PARSER_END(CodeParser) TOKEN: { &lt;code : ("-")?(["A"-"Z", "0"-"9"])+ &gt; | &lt;op : ("+"|"/") &gt; | &lt;not : ("-") &gt; | &lt;lparenthesis : ("(") &gt; | &lt;rparenthesis : (")") &gt; } void expression() : { } { negated_expression() | parenthesis_expression() | LOOKAHEAD(2) operator_expression() | &lt;code&gt; } void negated_expression() : { } { &lt;not&gt;parenthesis_expression() } void parenthesis_expression() : { } { &lt;lparenthesis&gt;expression()&lt;rparenthesis&gt; } void operator_expression() : { } { &lt;code&gt;&lt;op&gt;expression() } </code></pre> <p><b>Edit - 11/16/2009</b></p> <p>Now I gave ANTLR a try.</p> <p>I changed some terms to better match my problem domain. I came up with the following code (using the answers on this site), which seems to do the work now:</p> <pre><code>grammar Code; CODE : ('A'..'Z'|'0'..'9')+; OP : '+'|'/'; start : terms EOF; terms : term (OP term)*; term : '-'? CODE | '-'? '(' terms ')'; </code></pre> <p>And by the way... ANTLRWORKS is a great tool for debugging/visualizing! Helped me a lot.</p> <p>Additional info<br/> Above code matches stuff like:</p> <pre><code>(-Z19+-Z07+((FV+((M005+(M272/M276))/((M278/M273/M642)+-M005)))/(FW+(M005+(M273/M278/M642)))))+(-Z19+-Z07+((FV+((M005+(M272/M276))/((M278/M273/M642/M651)+-M005)))/(FW+(M0)))) </code></pre>
 

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