Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 457079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:32:05+00:00 2026-05-12T22:32:05+00:00

Just started with JavaCC. But I have a strange behaviour with it. I want

  • 0

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 🙂

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?

Does anybody have a clue why I don’t get the exception?

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?!

Oh – and maybe my solution is not very good – ignore this fact… or better, give some advice 😉

File: CodeParser.jj

 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 --> 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:
 {
  <code : ("-")?(["A"-"Z", "0"-"9"])+ >
  | <op : ("+"|"/") >
  | <not : ("-") >
  | <lparenthesis : ("(") >
  | <rparenthesis : (")") >
 }

 void expression() :
 {
 }
 {
  negated_expression() | parenthesis_expression() | LOOKAHEAD(2) operator_expression() | <code>
 }

 void negated_expression() :
 {
 }
 {
       <not>parenthesis_expression()
 }

 void parenthesis_expression() :
 {
 }
 {
        <lparenthesis>expression()<rparenthesis>
 }

 void operator_expression() :
 {
 }
 {
       <code><op>expression()
 }

Edit – 11/16/2009

Now I gave ANTLR a try.

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:

grammar Code;

CODE    :   ('A'..'Z'|'0'..'9')+;
OP  :   '+'|'/';

start   :   terms EOF;
terms   :   term (OP term)*;
term    :   '-'? CODE
    |   '-'? '(' terms ')';

And by the way… ANTLRWORKS is a great tool for debugging/visualizing! Helped me a lot.

Additional info
Above code matches stuff like:

(-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))))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T22:32:06+00:00Added an answer on May 12, 2026 at 10:32 pm

    What kgregory says is the right answer. You can see this if you build the grammar with the DEBUG_PARSER option and then run it:

    $ javacc -debug_parser -output_directory=com/testing/ CodeParser.jj && javac com/testing/*.java && java -cp . com.testing.CodeParser
    Java Compiler Compiler Version 5.0 (Parser Generator)
    (type "javacc" with no arguments for help)
    Reading from file CodeParser.jj . . .
    File "TokenMgrError.java" is being rebuilt.
    File "ParseException.java" is being rebuilt.
    File "Token.java" is being rebuilt.
    File "SimpleCharStream.java" is being rebuilt.
    Parser generated successfully.
    Call:   expression
      Call:   operator_expression
        Consumed token: <<code>: "A" at line 1 column 1>
        Consumed token: <<op>: "+" at line 1 column 2>
        Call:   expression
          Call:   operator_expression
            Consumed token: <<code>: "BC" at line 1 column 3>
            Consumed token: <<op>: "+" at line 1 column 5>
            Call:   expression
              Call:   negated_expression
                Consumed token: <"-" at line 1 column 6>
                Call:   parenthesis_expression
                  Consumed token: <"(" at line 1 column 7>
                  Call:   expression
                    Call:   operator_expression
                      Consumed token: <<code>: "2XXL" at line 1 column 8>
                      Consumed token: <<op>: "+" at line 1 column 12>
                      Call:   expression
                        Call:   operator_expression
                          Consumed token: <<code>: "A" at line 1 column 13>
                          Consumed token: <<op>: "/" at line 1 column 14>
                          Call:   expression
                            Consumed token: <<code>: "-B" at line 1 column 15>
                          Return: expression
                        Return: operator_expression
                      Return: expression
                    Return: operator_expression
                  Return: expression
                  Consumed token: <")" at line 1 column 17>
                Return: parenthesis_expression
              Return: negated_expression
            Return: expression
          Return: operator_expression
        Return: expression
      Return: operator_expression
    Return: expression
    

    See that? The last token consumed is the second to last character – the second to last right parenthesis.

    If you want the exception, again, like kgregory said, you could add a new top-level production called “file” or “data” or something and end it with an token. That way any dangling parens like this would cause an error. Here’s an grammar that does that:

    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 --> should produce parse error */
                  String s = "A+BC+-(2XXL+A/-B))";
                  CodeParser parser = new CodeParser(s);
                  parser.file();
            }
            catch(Exception e)
            {
                  e.printStackTrace();
            }
        }
    }
    PARSER_END(CodeParser)
    
    TOKEN:
    {
            <code : ("-")?(["A"-"Z", "0"-"9"])+ >
            | <op : ("+"|"/") >
            | <not : ("-") >
            | <lparenthesis : ("(") >
            | <rparenthesis : (")") >
    }
    
    void file() : {} {
      expression() <EOF>
    }
    void expression() :
    {
    }
    {
            negated_expression() | parenthesis_expression() | LOOKAHEAD(2) operator_expression() | <code>
    }
    
    void negated_expression() :
    {
    }
    {
          <not>parenthesis_expression()
    }
    
    void parenthesis_expression() :
    {
    }
    {
           <lparenthesis>expression()<rparenthesis>
    }
    
    void operator_expression() :
    {
    }
    {
          <code><op>expression()
    }
    

    And a sample run:

    $ javacc -debug_parser -output_directory=com/testing/ CodeParser.jj && javac com/testing/*.java && java -cp . com.testing.CodeParser
    Java Compiler Compiler Version 5.0 (Parser Generator)
    (type "javacc" with no arguments for help)
    Reading from file CodeParser.jj . . .
    File "TokenMgrError.java" is being rebuilt.
    File "ParseException.java" is being rebuilt.
    File "Token.java" is being rebuilt.
    File "SimpleCharStream.java" is being rebuilt.
    Parser generated successfully.
    Call:   file
      Call:   expression
        Call:   operator_expression
          Consumed token: <<code>: "A" at line 1 column 1>
          Consumed token: <<op>: "+" at line 1 column 2>
          Call:   expression
            Call:   operator_expression
              Consumed token: <<code>: "BC" at line 1 column 3>
              Consumed token: <<op>: "+" at line 1 column 5>
              Call:   expression
                Call:   negated_expression
                  Consumed token: <"-" at line 1 column 6>
                  Call:   parenthesis_expression
                    Consumed token: <"(" at line 1 column 7>
                    Call:   expression
                      Call:   operator_expression
                        Consumed token: <<code>: "2XXL" at line 1 column 8>
                        Consumed token: <<op>: "+" at line 1 column 12>
                        Call:   expression
                          Call:   operator_expression
                            Consumed token: <<code>: "A" at line 1 column 13>
                            Consumed token: <<op>: "/" at line 1 column 14>
                            Call:   expression
                              Consumed token: <<code>: "-B" at line 1 column 15>
                            Return: expression
                          Return: operator_expression
                        Return: expression
                      Return: operator_expression
                    Return: expression
                    Consumed token: <")" at line 1 column 17>
                  Return: parenthesis_expression
                Return: negated_expression
              Return: expression
            Return: operator_expression
          Return: expression
        Return: operator_expression
      Return: expression
    Return: file
    com.testing.ParseException: Encountered " ")" ") "" at line 1, column 18.
    Was expecting:
        <EOF> 
    
      at com.testing.CodeParser.generateParseException(CodeParser.java:354)
      at com.testing.CodeParser.jj_consume_token(CodeParser.java:238)
      at com.testing.CodeParser.file(CodeParser.java:34)
      at com.testing.CodeParser.main(CodeParser.java:22)
    

    Voila! An exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just started using Blueprint CSS and now playing with the grids but have a
Just started using fadein/fadeout - it's functioning but with problems. I have a set
Just started mongo and started having issue with querying already. i have a collection
Just started using Java (for Android, if that matters) and all I want is
Just started learning Haskell. I have an empty source file with this inside: pe
Just started learning PySide and is having problem with QTimer I have this #!/usr/bin/python
Just started working with android and ran into a small problem. I am have
Just started a site and I have an /img directory on the main domain.
Just started a site and I have an /img directory on the main domain.
Just started doing some code porting from .Net CF to Blackberry JDE 4.6.1. But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.