Results 1 to 15 of 15
  1. #1
    rrl
    rrl is offline Member
    Join Date
    May 2013
    Posts
    8
    Rep Power
    0

    Default How to handle error in parser javacc

    in attachment, my parser generated with javacc in eclipse. I don't know as to handle errors of sintax in my parser.
    how I can to do? what ways there are?
    I prefer if you can posting your code that show it.
    Thank you very much in advanced.
    Attached Files Attached Files

  2. #2
    rrl
    rrl is offline Member
    Join Date
    May 2013
    Posts
    8
    Rep Power
    0

    Default Re: How to handle error in parser javacc

    I have an important exam of this project and I don't know to do.
    Help me! Thank you very much

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: How to handle error in parser javacc

    For reference, this is the content of the attachment.
    Java Code:
    /**
     * JavaCC template file created by SF JavaCC plugin 1.5.17+ wizard for JavaCC 1.5.0+
     */
    options
    {
      JDK_VERSION = "1.5";
      IGNORE_CASE = true;
      LOOKAHEAD = 2;
      static = true;
    }
    
    PARSER_BEGIN(MyParser)
    import java.io.* ;
    
    public class MyParser
    {  
      public static void main(String args []) throws ParseException
      {
        Token t;
    		MyParserTokenManager tm;
    		int numToken = 0;
    
    		if (args.length<1) {
    			System.out.println("Sorgente non specificato");
    			System.exit(1);
    		}
    			
    		try {
    			java.io.InputStream infile;
    			infile = new java.io.FileInputStream(args[0]);
    			tm = new MyParserTokenManager(new SimpleCharStream(infile));
    		}
    		catch (java.io.FileNotFoundException e) {
    			System.out.println("File "+args[0]+" non trovato");
    			return; 
    		} // catch
    
    		t = tm.getNextToken();
    		numToken++;
    		while(t.kind != MyParserConstants.EOF){
    			t = tm.getNextToken();
    			System.out.printf("%s\t %s\n",tokenImage[t.kind],t.image);
    			numToken++;
    		}// while
      }
    
      static void error_skipto(int kind) {
      ParseException e = generateParseException();  // generate the exception object.
      System.out.println(e.toString());  // print the error message
      Token t;
      do {
        t = getNextToken();
      } while (t.kind != kind);
    }
    }
    
    PARSER_END(MyParser)
    
    void Stm() :
    {}
    {
      try {
        (
          SE()
        |
          MENTRE()
        |
       	  DO()
       	|
       	  PER()
       	|
       	  condizione()
       	|
       	  espressione()
       	|
       	  expres()
        )
      }catch (ParseException e) {
        error_skipto(PV);
      }
    }
    
    SKIP :
    {
      " "
    | "\r"
    | "\t"
    | "\n"
    }
    
    TOKEN : /* OPERATORS */
    {
    	<PIU: "+">
    |	<MENO: "-">
    |	<PER: "*">
    |	<DIV: "/">
    }
    
    TOKEN :
    {
    	<PAR_SIN: "(">
    |	<PAR_DES: ")">
    |	<QUAD_SIN: "[">
    |	<QUAD_DES: "]">
    |	<UGUALE: "==">
    |   <VIRGOLA: ","> 
    |	<ASS: "=">
    |	<NEG: "?=">
    |	<PV: ";">
    |	<FOR: "da">
    | 	<TO: "a">
    |	<IF: "se">
    | 	<ELSE: "altrimenti">
    |   <END_IF: "fine">
    |	<WHILE: "mentre">
    | 	<DO: "esegui">
    |	<COST_INT: (["0"-"9"])+>
    |	<ID: ["a"-"z","A"-"Z"]>
    
    }
    
    
    void SE() :
    {}
    {
      (<IF> <QUAD_SIN> condizione() <QUAD_DES> <PAR_SIN> espressione() <PV> <PAR_DES> <END_IF>)
    |  (<IF> <QUAD_SIN> condizione() <QUAD_DES> <PAR_SIN> espressione() <PV> <PAR_DES> <ELSE> <PAR_SIN>
    	espressione() <PV> <PAR_DES> <END_IF>)
    }
    
    void MENTRE() :
    {}
    {
      <WHILE> <QUAD_SIN> condizione() <QUAD_DES> <PAR_SIN> espressione() <PV> <PAR_DES>
    }
    
    void DO() :
    {}
    {
      <DO> <PAR_SIN> espressione() <PV> <PAR_DES> <WHILE> <QUAD_SIN> condizione() <QUAD_DES> <PV>
    }
    
    void PER() :
    {}
    {
      <FOR> (<ID>)+ <ASS> <COST_INT> <TO> (<COST_INT>)+ <PAR_SIN> espressione() <PV> <PAR_DES>
    }
    
    void condizione():
    { }
    {
      (<ID>)+ (<NEG> | <UGUALE>) ((<ID>)+ | (<COST_INT>)+)
    }
    
    void espressione ():
    {}
    {
      (<ID>)+ <ASS> ((<ID>)+ | (<COST_INT>)+ | expres() )
    }
    
    void expres() :
    {}
    {
      ((<ID>)+ <PIU> ((<ID>)+ | (<COST_INT>)+))
    | ((<ID>)+ <MENO> ((<ID>)+ | (<COST_INT>)+))
    | ((<ID>)+ <PER> ((<ID>)+ | (<COST_INT>)+))
    | ((<ID>)+ <DIV> ((<ID>)+ | (<COST_INT>)+))
    
    }
    In future please post the code in the forum. See Guide For New Members and BB Code List - Java Programming Forum - Learn Java Programming for formatting tips.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    rrl
    rrl is offline Member
    Join Date
    May 2013
    Posts
    8
    Rep Power
    0

    Default Re: How to handle error in parser javacc

    I understand, now I hope that someone help me

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to handle error in parser javacc

    I don't know JavaCC but I do know that it is quite an established parser generator and I can't believe that it doesn't offer the nuts and bolds for error detection and possibly error recovery. Because those parse errors are grammar defined, the actual handling needs to be user defined. Also, I can't believe that JavaCC doesn't come with a manual that explains those issues.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,512
    Rep Power
    5

    Default Re: How to handle error in parser javacc

    Quote Originally Posted by JosAH View Post
    Also, I can't believe that JavaCC doesn't come with a manual that explains those issues.
    I took the bait (and I was curious). https://javacc.java.net/ Amazing what simple web searches can find these days.

    Disclaimer: I did not scour the site to see if it explained the OP's issue.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    rrl
    rrl is offline Member
    Join Date
    May 2013
    Posts
    8
    Rep Power
    0

    Default Re: How to handle error in parser javacc

    I have already read this documents but I don't understand as I can implemented in my parser.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to handle error in parser javacc

    After a long and tedious search (3 minutes) on that page, I found: https://javacc.java.net/doc/errorrecovery.html

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    rrl
    rrl is offline Member
    Join Date
    May 2013
    Posts
    8
    Rep Power
    0

    Default Re: How to handle error in parser javacc

    I have already seen this page, infact in my parser I have implemented this procedure, but don't function because does not produce any result.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to handle error in parser javacc

    Which version of JavaCC are you using?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    rrl
    rrl is offline Member
    Join Date
    May 2013
    Posts
    8
    Rep Power
    0

    Default Re: How to handle error in parser javacc

    javacc version 5.0

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to handle error in parser javacc

    Have you asked your question on their mailing list? I bet you can get much better help there; I don't use JavaCC (I use ANTLR) so I can't help you much further. Maybe somebody else shows up here ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    rrl
    rrl is offline Member
    Join Date
    May 2013
    Posts
    8
    Rep Power
    0

    Default Re: How to handle error in parser javacc

    I understand, where should I ask? where is their mailing list?

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to handle error in parser javacc

    It is on the page supplied by Jim: https://java.net/projects/javacc/lists

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    rrl
    rrl is offline Member
    Join Date
    May 2013
    Posts
    8
    Rep Power
    0

    Default Re: How to handle error in parser javacc

    Thanks, I have sent email at users@javacc.java.net, I hope that their can help me.

Similar Threads

  1. Replies: 1
    Last Post: 10-29-2012, 09:49 AM
  2. error for empty element in sax parser
    By alexandra12 in forum XML
    Replies: 9
    Last Post: 08-01-2012, 12:59 AM
  3. Single Line Comments in JavaCC
    By mjdousti in forum Advanced Java
    Replies: 8
    Last Post: 06-05-2011, 08:36 PM
  4. JavaCC parser generator.
    By Danpanda in forum New To Java
    Replies: 0
    Last Post: 04-19-2010, 12:07 AM
  5. How to handle ArrayIndexOutOfBoundsException
    By 3xpr1ment in forum New To Java
    Replies: 6
    Last Post: 03-03-2010, 03:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •