/** * Refills the input buffer. * * @return <code>false</code>, iff there was new input. * @exception IOException if any I/O-Error occurs */ private boolean yy_refill() throws java.io.IOException { /* first: make room (if you can) */ if (yy_startRead > 0) { System.arraycopy(yy_buffer, yy_startRead, yy_buffer, 0, yy_endRead - yy_startRead); /* translate stored positions */ yy_endRead -= yy_startRead; yy_currentPos -= yy_startRead; yy_markedPos -= yy_startRead; yy_pushbackPos -= yy_startRead; yy_startRead = 0; } /* is the buffer big enough? */ if (yy_currentPos >= yy_buffer.length) { /* if not: blow it up */ char newBuffer[] = new char[yy_currentPos * 2]; System.arraycopy(yy_buffer, 0, newBuffer, 0, yy_buffer.length); yy_buffer = newBuffer; } /* finally: fill the buffer with new input */ int numRead = yy_reader.read(yy_buffer, yy_endRead, yy_buffer.length - yy_endRead); if (numRead < 0) { return true; } else { yy_endRead += numRead; return false; } }
/** * Refills the input buffer. * * @return <code>false</code>, iff there was new input. * @exception java.io.IOException if any I/O-Error occurs */ private boolean zzRefill() throws java.io.IOException { /* first: make room (if you can) */ if (zzStartRead > 0) { System.arraycopy(zzBuffer, zzStartRead, zzBuffer, 0, zzEndRead - zzStartRead); /* translate stored positions */ zzEndRead -= zzStartRead; zzCurrentPos -= zzStartRead; zzMarkedPos -= zzStartRead; zzPushbackPos -= zzStartRead; zzStartRead = 0; } /* is the buffer big enough? */ if (zzCurrentPos >= zzBuffer.length) { /* if not: blow it up */ char newBuffer[] = new char[zzCurrentPos * 2]; System.arraycopy(zzBuffer, 0, newBuffer, 0, zzBuffer.length); zzBuffer = newBuffer; } /* finally: fill the buffer with new input */ int numRead = zzReader.read(zzBuffer, zzEndRead, zzBuffer.length - zzEndRead); if (numRead < 0) { return true; } else { zzEndRead += numRead; return false; } }
/** * Refills the input buffer. * * @return <code>false</code>, iff there was new input. * @exception java.io.IOException if any I/O-Error occurs */ private boolean zzRefill() throws java.io.IOException { /* first: make room (if you can) */ if (zzStartRead > 0) { zzEndRead += zzFinalHighSurrogate; zzFinalHighSurrogate = 0; System.arraycopy(zzBuffer, zzStartRead, zzBuffer, 0, zzEndRead - zzStartRead); /* translate stored positions */ zzEndRead -= zzStartRead; zzCurrentPos -= zzStartRead; zzMarkedPos -= zzStartRead; zzStartRead = 0; } /* is the buffer big enough? */ if (zzCurrentPos >= zzBuffer.length - zzFinalHighSurrogate) { /* if not: blow it up */ char newBuffer[] = new char[zzBuffer.length * 2]; System.arraycopy(zzBuffer, 0, newBuffer, 0, zzBuffer.length); zzBuffer = newBuffer; zzEndRead += zzFinalHighSurrogate; zzFinalHighSurrogate = 0; } /* fill the buffer with new input */ int requested = zzBuffer.length - zzEndRead; int totalRead = 0; while (totalRead < requested) { int numRead = zzReader.read(zzBuffer, zzEndRead + totalRead, requested - totalRead); if (numRead == -1) { break; } totalRead += numRead; } if (totalRead > 0) { zzEndRead += totalRead; if (totalRead == requested) { /* possibly more input available */ if (Character.isHighSurrogate(zzBuffer[zzEndRead - 1])) { --zzEndRead; zzFinalHighSurrogate = 1; } } return false; } // totalRead = 0: End of stream return true; }
/** * Refills the input buffer. * * @return <code>false</code>, iff there was new input. * @exception java.io.IOException if any I/O-Error occurs */ private boolean zzRefill() throws java.io.IOException { /* first: make room (if you can) */ if (zzStartRead > 0) { System.arraycopy(zzBuffer, zzStartRead, zzBuffer, 0, zzEndRead - zzStartRead); /* translate stored positions */ zzEndRead -= zzStartRead; zzCurrentPos -= zzStartRead; zzMarkedPos -= zzStartRead; zzStartRead = 0; } /* is the buffer big enough? */ if (zzCurrentPos >= zzBuffer.length) { /* if not: blow it up */ char newBuffer[] = new char[zzCurrentPos * 2]; System.arraycopy(zzBuffer, 0, newBuffer, 0, zzBuffer.length); zzBuffer = newBuffer; } /* finally: fill the buffer with new input */ int numRead = zzReader.read(zzBuffer, zzEndRead, zzBuffer.length - zzEndRead); if (numRead > 0) { zzEndRead += numRead; return false; } // unlikely but not impossible: read 0 characters, but not at end of stream if (numRead == 0) { int c = zzReader.read(); if (c == -1) { return true; } else { zzBuffer[zzEndRead++] = (char) c; return false; } } // numRead < 0 return true; }
public static void main(String args[]) throws Exception { Yylex l = null; parser p; Interpreter I; try { if (args.length == 0) l = new Yylex(System.in); else l = new Yylex(new FileReader(args[0])); } catch (FileNotFoundException e) { System.err.println("Error: File not found: " + args[0]); System.exit(1); } p = new parser(l); /* The default parser is the first-defined entry point. */ /* You may want to change this. Other options are: */ /* */ try { calc.Absyn.Exp parse_tree = p.pExp(); I = new Interpreter(); // I.eval(parse_tree) ; System.out.println(I.eval(parse_tree)); /*System.out.println(); System.out.println("Parse Succesful!"); System.out.println(); System.out.println("[Abstract Syntax]"); System.out.println(); System.out.println(PrettyPrinter.show(parse_tree)); System.out.println(); System.out.println("[Linearized Tree]"); System.out.println(); System.out.println(PrettyPrinter.print(parse_tree)); */ } catch (Throwable e) { System.err.println( "At line " + String.valueOf(l.line_num()) + ", near \"" + l.buff() + "\" :"); System.err.println(" " + e.getMessage()); System.exit(1); } }
private void chars_expand() { char temp[] = new char[chars_size * 2]; System.arraycopy(chars, 0, temp, 0, chars_size); chars_size *= 2; chars = temp; }
public static void main(String[] args) throws IOException // may be thrown by the scanner { // check for command-line args if (args.length != 2) { System.err.println( "please supply name of file to be parsed and name of file for unparsed version."); System.exit(-1); } try { Codegen.p = new PrintWriter("/home/ben/Scripts/github/Academic/cs 536/p6/gencode.asm"); } catch (FileNotFoundException e) { System.err.println("File " + args[0] + " not found."); System.exit(-1); } // open input file FileReader inFile = null; try { inFile = new FileReader(args[0]); } catch (FileNotFoundException ex) { System.err.println("File " + args[0] + " not found."); System.exit(-1); } // open output file PrintWriter outFile = null; try { outFile = new PrintWriter(args[1]); } catch (FileNotFoundException ex) { System.err.println("File " + args[1] + " could not be opened for writing."); System.exit(-1); } parser P = new parser(new Yylex(inFile)); Symbol root = null; // the parser will return a Symbol whose value // field is the translation of the root // nonterminal (i.e., of the nonterminal // "program") try { root = P.parse(); // do the parse System.out.println("Little program parsed correctly."); } catch (Exception ex) { System.err.println("Exception occured during parse: " + ex); ex.printStackTrace(); System.exit(-1); } try { // ((SymTabNode)root.value).provideSymTab(new SymTab()); ((ProgramNode) root.value).processNames(); } catch (Exception ex) { System.err.println("Exception occured during parse: " + ex); ex.printStackTrace(); System.exit(-1); } if (!Errors.errors) { ((ASTnode) root.value).unparse(outFile, 0); try { ((ProgramNode) root.value).typeCheck(); } catch (Exception ex) { System.err.println("Exception occured during typecheck: " + ex); ex.printStackTrace(); System.exit(-1); } } if (!Errors.errors) { try { ((ProgramNode) root.value).generate(); Codegen.p.close(); } catch (Exception ex) { System.err.println("Exception occured during codegen: " + ex); ex.printStackTrace(); System.exit(-1); } } outFile.close(); return; }