Page 1 of 3 123 LastLast
Results 1 to 20 of 41
Like Tree1Likes

Thread: im studing compilers ,principles an techniques & tools 2nd edition and having errors

  1. #1
    flyhigh is offline Member
    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    0

    Default im studing compilers ,principles an techniques & tools 2nd edition and having errors

    i don't know where to start ive built the project run the jar file from the command prompt, then hit enter again that's when the errors appear.


    Java Code:
    errors are =exception in thread "main" java.lang.error:near line 1:syntex error
                             at parser.Parser.error(Parser.java:15)
                             at parser.Parser.match(Parser.java:19)
                             at parser.Parser.block(Parser.java:29)
                             at parser.Parser.program(Parser.java:23)
                             at main.Main.main(Main.java:9)

    ive tried tracing and cant find a reason why it want work?
    thanks
    Last edited by flyhigh; 06-27-2014 at 10:02 PM.

  2. #2
    flyhigh is offline Member
    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    0

    Default Re: im studing compilers ,principles an techniques & tools 2nd edition and having er

    I forgot to include the files ,sorry.
    want I do is go to command prompt ,go to the right dir
    I type in this >java -jar main.jar billstest.txt
    it runs but pressing enter a second time brings errors.
    thanks
    Attached Files Attached Files

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,341
    Rep Power
    25

    Default Re: im studing compilers ,principles an techniques & tools 2nd edition and having er

    Please post the code with the errors here (no link).
    Be sure to wrap your code with code tags:
    [code]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    exception in thread "main" java.lang.error:near line 1:syntex error
    Is that your exception's text? There is a spelling error: syntax

    pressing enter a second time brings errors.
    Please copy the full contents of the console showing the errors and paste it here.
    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    Last edited by Norm; 06-27-2014 at 08:01 PM.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    flyhigh is offline Member
    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    0

    Default Re: im studing compilers ,principles an techniques & tools 2nd edition and having er

    Java Code:
    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
    
    C:\Users\www>java -jar "C:\Users\www\Documents\NetBeansProjects\main\dist\main.j
    ar"
    
    Exception in thread "main" java.lang.Error: near line 1: syntax error
            at parser.Parser.error(Parser.java:15)
            at parser.Parser.match(Parser.java:19)
            at parser.Parser.block(Parser.java:29)
            at parser.Parser.program(Parser.java:23)
            at main.Main.main(Main.java:9)
    
    C:\Users\www>

    Java Code:
    package main;
    import java.io.*; import lexer.*; import parser.*;
    
    public class Main {
    
    	public static void main(String[] args) throws IOException {
    		Lexer lex = new Lexer();
    		Parser parse = new Parser(lex);
    		parse.program();
    		System.out.write('\n');
    	}
    }
    Java Code:
    package parser;
    import java.io.*; import lexer.*; import symbols.*; import inter.*;
    
    public class Parser {
    
       private Lexer lex;    // lexical analyzer for this parser
       private Token look;   // lookahead tagen
       Env top = null;       // current or top symbol table
       int used = 0;         // storage used for declarations
    
       public Parser(Lexer l) throws IOException { lex = l; move(); }
    
       void move() throws IOException { look = lex.scan(); }
    
       void error(String s) { throw new Error("near line "+lex.line+": "+s); }
    
       void match(int t) throws IOException {
          if( look.tag == t ) move();
          else error("syntax error");
       }
    
       public void program() throws IOException {  // program -> block
          Stmt s = block();
          int begin = s.newlabel();  int after = s.newlabel();
          s.emitlabel(begin);  s.gen(begin, after);  s.emitlabel(after);
       }
    
       Stmt block() throws IOException {  // block -> { decls stmts }
          match('{');  Env savedEnv = top;  top = new Env(top);
          decls(); Stmt s = stmts();
          match('}');  top = savedEnv;
          return s;
       }
    
       void decls() throws IOException {
    
          while( look.tag == Tag.BASIC ) {   // D -> type ID ;
             Type p = type(); Token tok = look; match(Tag.ID); match(';');
             Id id = new Id((Word)tok, p, used);
             top.put( tok, id );
             used = used + p.width;
          }
       }
    
       Type type() throws IOException {
    
          Type p = (Type)look;            // expect look.tag == Tag.BASIC 
          match(Tag.BASIC);
          if( look.tag != '[' ) return p; // T -> basic
          else return dims(p);            // return array type
       }
    
       Type dims(Type p) throws IOException {
          match('[');  Token tok = look;  match(Tag.NUM);  match(']');
          if( look.tag == '[' )
          p = dims(p);
          return new Array(((Num)tok).value, p);
       }
    
       Stmt stmts() throws IOException {
          if ( look.tag == '}' ) return Stmt.Null;
          else return new Seq(stmt(), stmts());
       }
    
       Stmt stmt() throws IOException {
          Expr x;  Stmt s, s1, s2;
          Stmt savedStmt;         // save enclosing loop for breaks
    
          switch( look.tag ) {
    
          case ';':
             move();
             return Stmt.Null;
    
          case Tag.IF:
             match(Tag.IF); match('('); x = bool(); match(')');
             s1 = stmt();
             if( look.tag != Tag.ELSE ) return new If(x, s1);
             match(Tag.ELSE);
             s2 = stmt();
             return new Else(x, s1, s2);
    
          case Tag.WHILE:
             While whilenode = new While();
             savedStmt = Stmt.Enclosing; Stmt.Enclosing = whilenode;
             match(Tag.WHILE); match('('); x = bool(); match(')');
             s1 = stmt();
             whilenode.init(x, s1);
             Stmt.Enclosing = savedStmt;  // reset Stmt.Enclosing
             return whilenode;
    
          case Tag.DO:
             Do donode = new Do();
             savedStmt = Stmt.Enclosing; Stmt.Enclosing = donode;
             match(Tag.DO);
             s1 = stmt();
             match(Tag.WHILE); match('('); x = bool(); match(')'); match(';');
             donode.init(s1, x);
             Stmt.Enclosing = savedStmt;  // reset Stmt.Enclosing
             return donode;
    
          case Tag.BREAK:
             match(Tag.BREAK); match(';');
             return new Break();
    
          case '{':
             return block();
    
          default:
             return assign();
          }
       }
    
       Stmt assign() throws IOException {
          Stmt stmt;  Token t = look;
          match(Tag.ID);
          Id id = top.get(t);
          if( id == null ) error(t.toString() + " undeclared");
    
          if( look.tag == '=' ) {       // S -> id = E ;
             move();  stmt = new Set(id, bool());
          }
          else {                        // S -> L = E ;
             Access x = offset(id);
             match('=');  stmt = new SetElem(x, bool());
          }
          match(';');
          return stmt;
       }
    
       Expr bool() throws IOException {
          Expr x = join();
          while( look.tag == Tag.OR ) {
             Token tok = look;  move();  x = new Or(tok, x, join());
          }
          return x;
       }
    
       Expr join() throws IOException {
          Expr x = equality();
          while( look.tag == Tag.AND ) {
             Token tok = look;  move();  x = new And(tok, x, equality());
          }
          return x;
       }
    
       Expr equality() throws IOException {
          Expr x = rel();
          while( look.tag == Tag.EQ || look.tag == Tag.NE ) {
             Token tok = look;  move();  x = new Rel(tok, x, rel());
          }
          return x;
       }
    
       Expr rel() throws IOException {
          Expr x = expr();
          switch( look.tag ) {
          case '<': case Tag.LE: case Tag.GE: case '>':
             Token tok = look;  move();  return new Rel(tok, x, expr());
          default:
             return x;
          }
       }
    
       Expr expr() throws IOException {
          Expr x = term();
          while( look.tag == '+' || look.tag == '-' ) {
             Token tok = look;  move();  x = new Arith(tok, x, term());
          }
          return x;
       }
    
       Expr term() throws IOException {
          Expr x = unary();
          while(look.tag == '*' || look.tag == '/' ) {
             Token tok = look;  move();   x = new Arith(tok, x, unary());
          }
          return x;
       }
    
       Expr unary() throws IOException {
          if( look.tag == '-' ) {
             move();  return new Unary(Word.minus, unary());
          }
          else if( look.tag == '!' ) {
             Token tok = look;  move();  return new Not(tok, unary());
          }
          else return factor();
       }
    
       Expr factor() throws IOException {
          Expr x = null;
          switch( look.tag ) {
          case '(':
             move(); x = bool(); match(')');
             return x;
          case Tag.NUM:
             x = new Constant(look, Type.Int);    move(); return x;
          case Tag.REAL:
             x = new Constant(look, Type.Float);  move(); return x;
          case Tag.TRUE:
             x = Constant.True;                   move(); return x;
          case Tag.FALSE:
             x = Constant.False;                  move(); return x;
          default:
             error("syntax error");
             return x;
          case Tag.ID:
             String s = look.toString();
             Id id = top.get(look);
             if( id == null ) error(look.toString() + " undeclared");
             move();
             if( look.tag != '[' ) return id;
             else return offset(id);
          }
       }
    
       Access offset(Id a) throws IOException {   // I -> [E] | [E] I
          Expr i; Expr w; Expr t1, t2; Expr loc;  // inherit id
    
          Type type = a.type;
          match('['); i = bool(); match(']');     // first index, I -> [ E ]
          type = ((Array)type).of;
          w = new Constant(type.width);
          t1 = new Arith(new Token('*'), i, w);
          loc = t1;
          while( look.tag == '[' ) {      // multi-dimensional I -> [ E ] I
             match('['); i = bool(); match(']');
             type = ((Array)type).of;
             w = new Constant(type.width);
             t1 = new Arith(new Token('*'), i, w);
             t2 = new Arith(new Token('+'), loc, t1);
             loc = t2;
          }
    
          return new Access(a, loc, type);
       }
    }

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,341
    Rep Power
    25

    Default Re: im studing compilers ,principles an techniques & tools 2nd edition and having er

    Why does the code at line 15 throw the exception that you have posted?

    The formatting of the code with hidden statements following if statements makes the code hard to read:
    Java Code:
    if( id == null ) error(look.toString() + " undeclared");
    //  vs
    if( id == null ) {
       error(look.toString() + " undeclared");
    }
    There should NOT be more than ONE statement per line.


    You need lots more debug information in the error() method messages and via println() statements so you can see what the code is doing when it executes.
    Last edited by Norm; 06-27-2014 at 10:15 PM.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    flyhigh is offline Member
    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    0

    Default Re: im studing compilers ,principles an techniques & tools 2nd edition and having er

    it says accessing static field line .
    all have yellow warnings on them.


    down here line 1 is the right way then?
    Java Code:
     
    (1)    if( id == null ) error(look.toString() + " undeclared");
    //  vs
    (2)    if( id == null ) {
    (3)    error(look.toString() + " undeclared");
    (4)    }

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,341
    Rep Power
    25

    Default Re: im studing compilers ,principles an techniques & tools 2nd edition and having er

    it says accessing static field line .
    all have yellow warnings on them.
    What is the "it"? Your IDE?
    static fields can be a problem if misused.

    Your code throws the exception. I'm not sure what your problem is? Can you explain?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,543
    Rep Power
    5

    Default Re: im studing compilers ,principles an techniques & tools 2nd edition and having er

    Probably a warning that the OP is accessing a static field from an instance reference instead of the class name.

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

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: im studing compilers ,principles an techniques & tools 2nd edition and having er

    My guess is that the OP is trying to run code that doesn't even compile. The OP should throw away all of the .class files and recompile everything. The compiler issues better diagnostics than the JVM when it has to run faulty code.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    flyhigh is offline Member
    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    0

    Default Re: im studing compilers ,principles an techniques & tools 2nd edition and having er

    its old stuff java 1.2 .
    it does compile

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: im studing compilers ,principles an techniques & tools 2nd edition and having er

    Quote Originally Posted by flyhigh View Post
    its old stuff java 1.2 .
    it does compile
    That is quite old indeed; I had a peek at your code and you are trying to use the code as a black box: it's your parser that throws the exception: i.e. it is trying to match a '{' from the input while it isn't there (if there is any input at all); learn how to use that tool.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    flyhigh is offline Member
    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    0

    Default Re: im studing compilers ,principles an techniques & tools 2nd edition and having er

    thanks
    back to reading .
    thanks all

  13. #13
    flyhigh is offline Member
    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    0

    Default Re: im studing compilers ,principles an techniques & tools 2nd edition and having er

    jos
    yes the lexical analyzer is should have read a file line by line in to a hashtable then match '{' at the begaining and end of file ,
    why im still not sure yet.
    thanks

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,341
    Rep Power
    25

    Default Re: im studing compilers ,principles an techniques & tools 2nd edition and having er

    why im still not sure yet.
    Try more debugging by adding lots of println statements to print out the values of variables as their values change and as they are used. The print out will show you what the code sees when it executes.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: im studing compilers ,principles an techniques & tools 2nd edition and having er

    Quote Originally Posted by flyhigh View Post
    jos
    yes the lexical analyzer is should have read a file line by line in to a hashtable then match '{' at the begaining and end of file ,
    why im still not sure yet.
    thanks
    It's the lexical analyzer that reads characters from some source (and produces tokens on demand); most likely the analyzer needs an input source, so you should supply one ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: im studing compilers ,principles an techniques & tools 2nd edition and having er

    p.s. I peeked at your source code again and saw that the lexical analyzer reads from System.in, so you should type in source code for your language to make your parser run ... (you could've found this out yourself).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,543
    Rep Power
    5

    Default Re: im studing compilers ,principles an techniques & tools 2nd edition and having er

    Quote Originally Posted by flyhigh View Post
    its old stuff java 1.2 .
    it does compile
    At some point you may want to upgrade to at least Java 1.5. That version introduces generics which can be very useful and can potentially reduce a lot of casting in your code. By catching type related errors at compile time, it makes the code easier to debug, imho. The present version of java is 1.8 and was officially released in March 2014.

    But I would not worry about that now. You seem to have your hands full with other issues.

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

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: im studing compilers ,principles an techniques & tools 2nd edition and having er

    Quote Originally Posted by jim829 View Post
    At some point you may want to upgrade to at least Java 1.5. That version introduces generics which can be very useful and can potentially reduce a lot of casting in your code. By catching type related errors at compile time, it makes the code easier to debug, imho. The present version of java is 1.8 and was officially released in March 2014.

    But I would not worry about that now. You seem to have your hands full with other issues.
    The code is fine as it is, but it's not the OP's code, I'm sure, otherwise the OP could've and would've known that the lexical analyzer reads (hard coded) from Sytem.in. I wonder what 'studying compilers, principles and techniques' means ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,341
    Rep Power
    25

    Default Re: im studing compilers ,principles an techniques & tools 2nd edition and having er

    I wonder what 'studying compilers, principles and techniques' means ...
    Is there a requirement to know java programming for this course?

    If the OP downloaded this code from some site, would that site have some doc on how to use it?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: im studing compilers ,principles an techniques & tools 2nd edition and having er

    Quote Originally Posted by Norm View Post
    Is there a requirement to know java programming for this course?
    I think it is a prerequisite because the entire compiler frontend (the lexical analyzer and the parser) is written in java ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 3 123 LastLast

Similar Threads

  1. Am I using correct programming principles?
    By beefy in forum New To Java
    Replies: 12
    Last Post: 04-28-2010, 07:19 PM
  2. Help me please in studing rich:tree
    By ruby in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 07-28-2009, 08:13 PM
  3. What techniques would be used to do this...
    By rico16135 in forum New To Java
    Replies: 0
    Last Post: 04-22-2008, 07:47 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
  •