8 Replies Latest reply: Jun 4, 2008 11:08 AM by 807799 RSS

    File Parsing

    807591
      Hello friends,

      I want to ask about the problem here :

      There are four binary operators called B1 , B2 , B3 , B4 .
      There are two unary operators called U1 , U2 .
      There are two keywords K1 , K2 .

      There can be any formula generated from above operators and keywords,
      which is written in a text file.

      Formula has basic form :

      [ U1 expression ]
      [ U2 expression ]
      [ expression B1 expression ]
      [ expression B2 expression ]
      [ expression B3 expression ]
      [ expression B4 expression ]

      NOTE : Keywords are used in building expression as below :

      (K1 anyname anyname (x (y (z (...)))))
      (K2 anyname anyname (x (y (z (......)))))

      For example there is a formula below written in the sample.txt

      [  (K1 anyname anyname (x (y (z (...))))) B3 [ (K2 anyname anyname (x (y (z (......))))) B4 [ U1 (K1 anyname1 anyname2 (x1 (y1 (z1 (...)))))   ] ] ]

      For every expression in the above formula, I want to replace a unique integer there,
      for example here,

      [ 1 B3 [ 2 B4 [ U1 3 ] ] ]

      Can some one suggest how to get this form ?

      Thank you. Sachin.
      // input file.
      [  (K1 anyname anyname (x (y (z (...))))) B3 [ (K2 anyname anyname (x (y (z (......))))) B4 [ U1 (K1 anyname1 anyname2 (x1 (y1 (z1 (...)))))   ] ] ]
      // output file.
      
      [ 1 B3 [ 2 B4 [ U1 3 ] ] ]
        • 1. Re: File Parsing
          807591
          Huh?

          Seems like you are trying to create a simple lexer & parser. This is really non-trivial business.

          Search the web for GNU lex + yacc. This is C stuff... you probably need the java version
          • 2. Re: File Parsing
            807591
            Hello friends,

            is there any other way can I transform it into another form ?

            Thanks. Sachin
            • 3. Re: File Parsing
              807591
              http://www.javaworld.com/javaworld/jw-12-1996/jw-12-jack.html

              http://catalog.compilertools.net/java.html
              • 4. Re: File Parsing
                800282
                tjacobs01 wrote:
                ... you probably need the java version
                I recommend ANTLR: fairly easy to use.

                [http://www.antlr.org/]
                • 5. Re: File Parsing
                  807799
                  sachin.annadate wrote:
                  is there any other way can I transform it into another form ?
                  There are many ways... if I understand your spec correctly you could do something like
                  public static void main(String[] args)
                  {
                      System.out.println(parseFormula("<your input string>"));
                  }
                  
                  private static String parseFormula(String input)
                  {
                      StringReader sr = new StringReader(input);
                      StringBuilder sb = new StringBuilder();
                      int expressions, parentheses;
                      char c;
                      
                      while ((c = (char)sr.read()) != -1)
                      {
                          // start of expression
                          if (c == '(' && parentheses++ == 0)
                          {
                              sb.append(++expressions);
                          }
                          else if (c == ')')
                          {
                              --parentheses;
                          }        
                          else if (parentheses == 0)
                          {
                              sb.append(c);
                          }
                      }
                      return sb.toString();
                  }
                  This is just an idea that came to me, not tested and not quality approved (duh!)... with some testing, adjusting and cleaning up it should work. However, I too recommend you take a look at the links provided by the others.

                  Regards!

                  Edited by: lindsten on 2008-jun-03 11:33
                  • 6. Re: File Parsing
                    807591
                    Thank you friend.

                    Sachin.
                    • 7. Re: File Parsing
                      807591
                      // input string
                      [  (K1 anyname anyname (x (y (z (...))))) B3 [ (K2 anyname anyname (x (y (z (......))))) B4 [ U1 (K1 anyname1 anyname2 (x1 (y1 (z1 (...)))))   ] ] ]
                      // output string
                      [ 1 B3 [ 2 B4 [ U1 3 ] ] 
                      In the above problem I also want to get mapping (HashMap<Integer , String>)
                      1    ------> (K1 anyname anyname (x (y (z (...)))))
                      2    ------> (K2 anyname anyname (x (y (z (......)))))
                      3    ------> (K1 anyname1 anyname2 (x1 (y1 (z1 (...)))))
                      Can you please suggest that is it possible to that such a change in above program given
                      or I any other suggestion ?

                      Thank you . Sachin.
                      • 8. Re: File Parsing
                        807799
                        sachin.annadate wrote:
                        Can you please suggest that is it possible to that such a change in above program given
                        or I any other suggestion ?
                        Well, if you're using my example it should be quite easy! I'll give you a hint...
                        public static void main(String[] args)
                        {
                            System.out.println(parseFormula("<your input string>"));
                        }
                         
                        private static String parseFormula(String input)
                        {
                            StringReader sr = new StringReader(input);
                            StringBuilder sb = new StringBuilder();
                            StringBuilder expr;
                            int expressions, parentheses;
                            char c;
                            
                            while ((c = (char)sr.read()) != -1)
                            {
                                // start of expression
                                if (c == '(' && parentheses++ == 0)
                                {
                                    sb.append(++expressions);
                                    expr = new StringBuilder("(");
                                }
                                // end of expression
                                else if (c == ')' && --parentheses == 0)
                                {
                                    expr.append(')');
                                    // store your mapping here: expressions --> expr.toString()
                                    expr = null;
                                }
                                else if (parentheses == 0)
                                {
                                    sb.append(c);
                                }
                                else
                                {
                                    expr.append(c);
                                }
                            }
                            return sb.toString();
                        }
                        Good luck!