Source Code Cross Referenced for Emitter.java in  » Parser » JFlex » JFlex » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Parser » JFlex » JFlex 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
0002:         * JFlex 1.4.1                                                             *
0003:         * Copyright (C) 1998-2004  Gerwin Klein <lsf@jflex.de>                    *
0004:         * All rights reserved.                                                    *
0005:         *                                                                         *
0006:         * This program is free software; you can redistribute it and/or modify    *
0007:         * it under the terms of the GNU General Public License. See the file      *
0008:         * COPYRIGHT for more information.                                         *
0009:         *                                                                         *
0010:         * This program is distributed in the hope that it will be useful,         *
0011:         * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
0012:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
0013:         * GNU General Public License for more details.                            *
0014:         *                                                                         *
0015:         * You should have received a copy of the GNU General Public License along *
0016:         * with this program; if not, write to the Free Software Foundation, Inc., *
0017:         * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                 *
0018:         *                                                                         *
0019:         * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
0020:
0021:        package JFlex;
0022:
0023:        import java.io.*;
0024:        import java.util.*;
0025:        import java.text.*;
0026:
0027:        /**
0028:         * This class manages the actual code generation, putting
0029:         * the scanner together, filling in skeleton sections etc.
0030:         *
0031:         * Table compression, String packing etc. is also done here.
0032:         *
0033:         * @author Gerwin Klein
0034:         * @version JFlex 1.4.1, $Revision: 2.26 $, $Date: 2004/11/07 03:12:46 $
0035:         */
0036:        final public class Emitter {
0037:
0038:            // bit masks for state attributes
0039:            static final private int FINAL = 1;
0040:            static final private int PUSHBACK = 2;
0041:            static final private int LOOKEND = 4;
0042:            static final private int NOLOOK = 8;
0043:
0044:            static final private String date = (new SimpleDateFormat())
0045:                    .format(new Date());
0046:
0047:            private File inputFile;
0048:
0049:            private PrintWriter out;
0050:            private Skeleton skel;
0051:            private LexScan scanner;
0052:            private LexParse parser;
0053:            private DFA dfa;
0054:
0055:            // for switch statement:
0056:            // table[i][j] is the set of input characters that leads from state i to state j
0057:            private CharSet table[][];
0058:
0059:            private boolean isTransition[];
0060:
0061:            // noTarget[i] is the set of input characters that have no target state in state i
0062:            private CharSet noTarget[];
0063:
0064:            // for row killing:
0065:            private int numRows;
0066:            private int[] rowMap;
0067:            private boolean[] rowKilled;
0068:
0069:            // for col killing:
0070:            private int numCols;
0071:            private int[] colMap;
0072:            private boolean[] colKilled;
0073:
0074:            /** maps actions to their switch label */
0075:            private Hashtable actionTable = new Hashtable();
0076:
0077:            private CharClassInterval[] intervalls;
0078:
0079:            private String visibility = "public";
0080:
0081:            public Emitter(File inputFile, LexParse parser, DFA dfa)
0082:                    throws IOException {
0083:
0084:                String name = parser.scanner.className + ".java";
0085:
0086:                File outputFile = normalize(name, inputFile);
0087:
0088:                Out.println("Writing code to \"" + outputFile + "\"");
0089:
0090:                this .out = new PrintWriter(new BufferedWriter(new FileWriter(
0091:                        outputFile)));
0092:                this .parser = parser;
0093:                this .scanner = parser.scanner;
0094:                this .visibility = scanner.visibility;
0095:                this .inputFile = inputFile;
0096:                this .dfa = dfa;
0097:                this .skel = new Skeleton(out);
0098:            }
0099:
0100:            /**
0101:             * Constructs a file in Options.getDir() or in the same directory as
0102:             * another file. Makes a backup if the file already exists.
0103:             *
0104:             * @param name  the name (without path) of the file
0105:             * @param path  the path where to construct the file
0106:             * @param input fallback location if path = <tt>null</tt>
0107:             *              (expected to be a file in the directory to write to)   
0108:             */
0109:            public static File normalize(String name, File input) {
0110:                File outputFile;
0111:
0112:                if (Options.getDir() == null)
0113:                    if (input == null || input.getParent() == null)
0114:                        outputFile = new File(name);
0115:                    else
0116:                        outputFile = new File(input.getParent(), name);
0117:                else
0118:                    outputFile = new File(Options.getDir(), name);
0119:
0120:                if (outputFile.exists() && !Options.no_backup) {
0121:                    File backup = new File(outputFile.toString() + "~");
0122:
0123:                    if (backup.exists())
0124:                        backup.delete();
0125:
0126:                    if (outputFile.renameTo(backup))
0127:                        Out.println("Old file \"" + outputFile
0128:                                + "\" saved as \"" + backup + "\"");
0129:                    else
0130:                        Out.println("Couldn't save old file \"" + outputFile
0131:                                + "\", overwriting!");
0132:                }
0133:
0134:                return outputFile;
0135:            }
0136:
0137:            private void println() {
0138:                out.println();
0139:            }
0140:
0141:            private void println(String line) {
0142:                out.println(line);
0143:            }
0144:
0145:            private void println(int i) {
0146:                out.println(i);
0147:            }
0148:
0149:            private void print(String line) {
0150:                out.print(line);
0151:            }
0152:
0153:            private void print(int i) {
0154:                out.print(i);
0155:            }
0156:
0157:            private void print(int i, int tab) {
0158:                int exp;
0159:
0160:                if (i < 0)
0161:                    exp = 1;
0162:                else
0163:                    exp = 10;
0164:
0165:                while (tab-- > 1) {
0166:                    if (Math.abs(i) < exp)
0167:                        print(" ");
0168:                    exp *= 10;
0169:                }
0170:
0171:                print(i);
0172:            }
0173:
0174:            private void emitScanError() {
0175:                print("  private void zzScanError(int errorCode)");
0176:
0177:                if (scanner.scanErrorException != null)
0178:                    print(" throws " + scanner.scanErrorException);
0179:
0180:                println(" {");
0181:
0182:                skel.emitNext();
0183:
0184:                if (scanner.scanErrorException == null)
0185:                    println("    throw new Error(message);");
0186:                else
0187:                    println("    throw new " + scanner.scanErrorException
0188:                            + "(message);");
0189:
0190:                skel.emitNext();
0191:
0192:                print("  " + visibility + " void yypushback(int number) ");
0193:
0194:                if (scanner.scanErrorException == null)
0195:                    println(" {");
0196:                else
0197:                    println(" throws " + scanner.scanErrorException + " {");
0198:            }
0199:
0200:            private void emitMain() {
0201:                if (!(scanner.standalone || scanner.debugOption || scanner.cupDebug))
0202:                    return;
0203:
0204:                if (scanner.cupDebug) {
0205:                    println("  /**");
0206:                    println("   * Converts an int token code into the name of the");
0207:                    println("   * token by reflection on the cup symbol class/interface "
0208:                            + scanner.cupSymbol);
0209:                    println("   *");
0210:                    println("   * This code was contributed by Karl Meissner <meissnersd@yahoo.com>");
0211:                    println("   */");
0212:                    println("  private String getTokenName(int token) {");
0213:                    println("    try {");
0214:                    println("      java.lang.reflect.Field [] classFields = "
0215:                            + scanner.cupSymbol + ".class.getFields();");
0216:                    println("      for (int i = 0; i < classFields.length; i++) {");
0217:                    println("        if (classFields[i].getInt(null) == token) {");
0218:                    println("          return classFields[i].getName();");
0219:                    println("        }");
0220:                    println("      }");
0221:                    println("    } catch (Exception e) {");
0222:                    println("      e.printStackTrace(System.err);");
0223:                    println("    }");
0224:                    println("");
0225:                    println("    return \"UNKNOWN TOKEN\";");
0226:                    println("  }");
0227:                    println("");
0228:                    println("  /**");
0229:                    println("   * Same as " + scanner.functionName
0230:                            + " but also prints the token to standard out");
0231:                    println("   * for debugging.");
0232:                    println("   *");
0233:                    println("   * This code was contributed by Karl Meissner <meissnersd@yahoo.com>");
0234:                    println("   */");
0235:
0236:                    print("  " + visibility + " ");
0237:                    if (scanner.tokenType == null) {
0238:                        if (scanner.isInteger)
0239:                            print("int");
0240:                        else if (scanner.isIntWrap)
0241:                            print("Integer");
0242:                        else
0243:                            print("Yytoken");
0244:                    } else
0245:                        print(scanner.tokenType);
0246:
0247:                    print(" debug_");
0248:
0249:                    print(scanner.functionName);
0250:
0251:                    print("() throws java.io.IOException");
0252:
0253:                    if (scanner.lexThrow != null) {
0254:                        print(", ");
0255:                        print(scanner.lexThrow);
0256:                    }
0257:
0258:                    if (scanner.scanErrorException != null) {
0259:                        print(", ");
0260:                        print(scanner.scanErrorException);
0261:                    }
0262:
0263:                    println(" {");
0264:
0265:                    println("    java_cup.runtime.Symbol s = "
0266:                            + scanner.functionName + "();");
0267:                    print("    System.out.println( ");
0268:                    if (scanner.lineCount)
0269:                        print("\"line:\" + (yyline+1) + ");
0270:                    if (scanner.columnCount)
0271:                        print("\" col:\" + (yycolumn+1) + ");
0272:                    println("\" --\"+ yytext() + \"--\" + getTokenName(s.sym) + \"--\");");
0273:                    println("    return s;");
0274:                    println("  }");
0275:                    println("");
0276:                }
0277:
0278:                if (scanner.standalone) {
0279:                    println("  /**");
0280:                    println("   * Runs the scanner on input files.");
0281:                    println("   *");
0282:                    println("   * This is a standalone scanner, it will print any unmatched");
0283:                    println("   * text to System.out unchanged.");
0284:                    println("   *");
0285:                    println("   * @param argv   the command line, contains the filenames to run");
0286:                    println("   *               the scanner on.");
0287:                    println("   */");
0288:                } else {
0289:                    println("  /**");
0290:                    println("   * Runs the scanner on input files.");
0291:                    println("   *");
0292:                    println("   * This main method is the debugging routine for the scanner.");
0293:                    println("   * It prints debugging information about each returned token to");
0294:                    println("   * System.out until the end of file is reached, or an error occured.");
0295:                    println("   *");
0296:                    println("   * @param argv   the command line, contains the filenames to run");
0297:                    println("   *               the scanner on.");
0298:                    println("   */");
0299:                }
0300:
0301:                println("  public static void main(String argv[]) {");
0302:                println("    if (argv.length == 0) {");
0303:                println("      System.out.println(\"Usage : java "
0304:                        + scanner.className + " <inputfile>\");");
0305:                println("    }");
0306:                println("    else {");
0307:                println("      for (int i = 0; i < argv.length; i++) {");
0308:                println("        " + scanner.className + " scanner = null;");
0309:                println("        try {");
0310:                println("          scanner = new " + scanner.className
0311:                        + "( new java.io.FileReader(argv[i]) );");
0312:
0313:                if (scanner.standalone) {
0314:                    println("          while ( !scanner.zzAtEOF ) scanner."
0315:                            + scanner.functionName + "();");
0316:                } else if (scanner.cupDebug) {
0317:                    println("          while ( !scanner.zzAtEOF ) scanner.debug_"
0318:                            + scanner.functionName + "();");
0319:                } else {
0320:                    println("          do {");
0321:                    println("            System.out.println(scanner."
0322:                            + scanner.functionName + "());");
0323:                    println("          } while (!scanner.zzAtEOF);");
0324:                    println("");
0325:                }
0326:
0327:                println("        }");
0328:                println("        catch (java.io.FileNotFoundException e) {");
0329:                println("          System.out.println(\"File not found : \\\"\"+argv[i]+\"\\\"\");");
0330:                println("        }");
0331:                println("        catch (java.io.IOException e) {");
0332:                println("          System.out.println(\"IO error scanning file \\\"\"+argv[i]+\"\\\"\");");
0333:                println("          System.out.println(e);");
0334:                println("        }");
0335:                println("        catch (Exception e) {");
0336:                println("          System.out.println(\"Unexpected exception:\");");
0337:                println("          e.printStackTrace();");
0338:                println("        }");
0339:                println("      }");
0340:                println("    }");
0341:                println("  }");
0342:                println("");
0343:            }
0344:
0345:            private void emitNoMatch() {
0346:                println("            zzScanError(ZZ_NO_MATCH);");
0347:            }
0348:
0349:            private void emitNextInput() {
0350:                println("          if (zzCurrentPosL < zzEndReadL)");
0351:                println("            zzInput = zzBufferL[zzCurrentPosL++];");
0352:                println("          else if (zzAtEOF) {");
0353:                println("            zzInput = YYEOF;");
0354:                println("            break zzForAction;");
0355:                println("          }");
0356:                println("          else {");
0357:                println("            // store back cached positions");
0358:                println("            zzCurrentPos  = zzCurrentPosL;");
0359:                println("            zzMarkedPos   = zzMarkedPosL;");
0360:                if (scanner.lookAheadUsed)
0361:                    println("            zzPushbackPos = zzPushbackPosL;");
0362:                println("            boolean eof = zzRefill();");
0363:                println("            // get translated positions and possibly new buffer");
0364:                println("            zzCurrentPosL  = zzCurrentPos;");
0365:                println("            zzMarkedPosL   = zzMarkedPos;");
0366:                println("            zzBufferL      = zzBuffer;");
0367:                println("            zzEndReadL     = zzEndRead;");
0368:                if (scanner.lookAheadUsed)
0369:                    println("            zzPushbackPosL = zzPushbackPos;");
0370:                println("            if (eof) {");
0371:                println("              zzInput = YYEOF;");
0372:                println("              break zzForAction;");
0373:                println("            }");
0374:                println("            else {");
0375:                println("              zzInput = zzBufferL[zzCurrentPosL++];");
0376:                println("            }");
0377:                println("          }");
0378:            }
0379:
0380:            private void emitHeader() {
0381:                println("/* The following code was generated by JFlex "
0382:                        + Main.version + " on " + date + " */");
0383:                println("");
0384:            }
0385:
0386:            private void emitUserCode() {
0387:                if (scanner.userCode.length() > 0)
0388:                    println(scanner.userCode.toString());
0389:            }
0390:
0391:            private void emitClassName() {
0392:                if (!endsWithJavadoc(scanner.userCode)) {
0393:                    String path = inputFile.toString();
0394:                    // slashify path (avoid backslash u sequence = unicode escape)
0395:                    if (File.separatorChar != '/') {
0396:                        path = path.replace(File.separatorChar, '/');
0397:                    }
0398:
0399:                    println("/**");
0400:                    println(" * This class is a scanner generated by ");
0401:                    println(" * <a href=\"http://www.jflex.de/\">JFlex</a> "
0402:                            + Main.version);
0403:                    println(" * on " + date + " from the specification file");
0404:                    println(" * <tt>" + path + "</tt>");
0405:                    println(" */");
0406:                }
0407:
0408:                if (scanner.isPublic)
0409:                    print("public ");
0410:
0411:                if (scanner.isAbstract)
0412:                    print("abstract ");
0413:
0414:                if (scanner.isFinal)
0415:                    print("final ");
0416:
0417:                print("class ");
0418:                print(scanner.className);
0419:
0420:                if (scanner.isExtending != null) {
0421:                    print(" extends ");
0422:                    print(scanner.isExtending);
0423:                }
0424:
0425:                if (scanner.isImplementing != null) {
0426:                    print(" implements ");
0427:                    print(scanner.isImplementing);
0428:                }
0429:
0430:                println(" {");
0431:            }
0432:
0433:            /**
0434:             * Try to find out if user code ends with a javadoc comment 
0435:             * 
0436:             * @param buffer   the user code
0437:             * @return true    if it ends with a javadoc comment
0438:             */
0439:            public static boolean endsWithJavadoc(StringBuffer usercode) {
0440:                String s = usercode.toString().trim();
0441:
0442:                if (!s.endsWith("*/"))
0443:                    return false;
0444:
0445:                // find beginning of javadoc comment   
0446:                int i = s.lastIndexOf("/**");
0447:                if (i < 0)
0448:                    return false;
0449:
0450:                // javadoc comment shouldn't contain a comment end
0451:                return s.substring(i, s.length() - 2).indexOf("*/") < 0;
0452:            }
0453:
0454:            private void emitLexicalStates() {
0455:                Enumeration stateNames = scanner.states.names();
0456:
0457:                while (stateNames.hasMoreElements()) {
0458:                    String name = (String) stateNames.nextElement();
0459:
0460:                    int num = scanner.states.getNumber(name).intValue();
0461:
0462:                    if (scanner.bolUsed)
0463:                        println("  " + visibility + " static final int " + name
0464:                                + " = " + 2 * num + ";");
0465:                    else
0466:                        println("  " + visibility + " static final int " + name
0467:                                + " = " + dfa.lexState[2 * num] + ";");
0468:                }
0469:
0470:                if (scanner.bolUsed) {
0471:                    println("");
0472:                    println("  /**");
0473:                    println("   * ZZ_LEXSTATE[l] is the state in the DFA for the lexical state l");
0474:                    println("   * ZZ_LEXSTATE[l+1] is the state in the DFA for the lexical state l");
0475:                    println("   *                  at the beginning of a line");
0476:                    println("   * l is of the form l = 2*k, k a non negative integer");
0477:                    println("   */");
0478:                    println("  private static final int ZZ_LEXSTATE[] = { ");
0479:
0480:                    int i, j = 0;
0481:                    print("    ");
0482:
0483:                    for (i = 0; i < dfa.lexState.length - 1; i++) {
0484:                        print(dfa.lexState[i], 2);
0485:
0486:                        print(", ");
0487:
0488:                        if (++j >= 16) {
0489:                            println();
0490:                            print("    ");
0491:                            j = 0;
0492:                        }
0493:                    }
0494:
0495:                    println(dfa.lexState[i]);
0496:                    println("  };");
0497:
0498:                }
0499:            }
0500:
0501:            private void emitDynamicInit() {
0502:                int count = 0;
0503:                int value = dfa.table[0][0];
0504:
0505:                println("  /** ");
0506:                println("   * The transition table of the DFA");
0507:                println("   */");
0508:
0509:                CountEmitter e = new CountEmitter("Trans");
0510:                e.setValTranslation(+1); // allow vals in [-1, 0xFFFE]
0511:                e.emitInit();
0512:
0513:                for (int i = 0; i < dfa.numStates; i++) {
0514:                    if (!rowKilled[i]) {
0515:                        for (int c = 0; c < dfa.numInput; c++) {
0516:                            if (!colKilled[c]) {
0517:                                if (dfa.table[i][c] == value) {
0518:                                    count++;
0519:                                } else {
0520:                                    e.emit(count, value);
0521:
0522:                                    count = 1;
0523:                                    value = dfa.table[i][c];
0524:                                }
0525:                            }
0526:                        }
0527:                    }
0528:                }
0529:
0530:                e.emit(count, value);
0531:                e.emitUnpack();
0532:
0533:                println(e.toString());
0534:            }
0535:
0536:            private void emitCharMapInitFunction() {
0537:
0538:                CharClasses cl = parser.getCharClasses();
0539:
0540:                if (cl.getMaxCharCode() < 256)
0541:                    return;
0542:
0543:                println("");
0544:                println("  /** ");
0545:                println("   * Unpacks the compressed character translation table.");
0546:                println("   *");
0547:                println("   * @param packed   the packed character translation table");
0548:                println("   * @return         the unpacked character translation table");
0549:                println("   */");
0550:                println("  private static char [] zzUnpackCMap(String packed) {");
0551:                println("    char [] map = new char[0x10000];");
0552:                println("    int i = 0;  /* index in packed string  */");
0553:                println("    int j = 0;  /* index in unpacked array */");
0554:                println("    while (i < " + 2 * intervalls.length + ") {");
0555:                println("      int  count = packed.charAt(i++);");
0556:                println("      char value = packed.charAt(i++);");
0557:                println("      do map[j++] = value; while (--count > 0);");
0558:                println("    }");
0559:                println("    return map;");
0560:                println("  }");
0561:            }
0562:
0563:            private void emitZZTrans() {
0564:
0565:                int i, c;
0566:                int n = 0;
0567:
0568:                println("  /** ");
0569:                println("   * The transition table of the DFA");
0570:                println("   */");
0571:                println("  private static final int ZZ_TRANS [] = {"); //XXX
0572:
0573:                print("    ");
0574:                for (i = 0; i < dfa.numStates; i++) {
0575:
0576:                    if (!rowKilled[i]) {
0577:                        for (c = 0; c < dfa.numInput; c++) {
0578:                            if (!colKilled[c]) {
0579:                                if (n >= 10) {
0580:                                    println();
0581:                                    print("    ");
0582:                                    n = 0;
0583:                                }
0584:                                print(dfa.table[i][c]);
0585:                                if (i != dfa.numStates - 1
0586:                                        || c != dfa.numInput - 1)
0587:                                    print(", ");
0588:                                n++;
0589:                            }
0590:                        }
0591:                    }
0592:                }
0593:
0594:                println();
0595:                println("  };");
0596:            }
0597:
0598:            private void emitCharMapArrayUnPacked() {
0599:
0600:                CharClasses cl = parser.getCharClasses();
0601:                intervalls = cl.getIntervalls();
0602:
0603:                println("");
0604:                println("  /** ");
0605:                println("   * Translates characters to character classes");
0606:                println("   */");
0607:                println("  private static final char [] ZZ_CMAP = {");
0608:
0609:                int n = 0; // numbers of entries in current line    
0610:                print("    ");
0611:
0612:                int max = cl.getMaxCharCode();
0613:                int i = 0;
0614:                while (i < intervalls.length && intervalls[i].start <= max) {
0615:
0616:                    int end = Math.min(intervalls[i].end, max);
0617:                    for (int c = intervalls[i].start; c <= end; c++) {
0618:
0619:                        print(colMap[intervalls[i].charClass], 2);
0620:
0621:                        if (c < max) {
0622:                            print(", ");
0623:                            if (++n >= 16) {
0624:                                println();
0625:                                print("    ");
0626:                                n = 0;
0627:                            }
0628:                        }
0629:                    }
0630:
0631:                    i++;
0632:                }
0633:
0634:                println();
0635:                println("  };");
0636:                println();
0637:            }
0638:
0639:            private void emitCharMapArray() {
0640:                CharClasses cl = parser.getCharClasses();
0641:
0642:                if (cl.getMaxCharCode() < 256) {
0643:                    emitCharMapArrayUnPacked();
0644:                    return;
0645:                }
0646:
0647:                // ignores cl.getMaxCharCode(), emits all intervalls instead
0648:
0649:                intervalls = cl.getIntervalls();
0650:
0651:                println("");
0652:                println("  /** ");
0653:                println("   * Translates characters to character classes");
0654:                println("   */");
0655:                println("  private static final String ZZ_CMAP_PACKED = ");
0656:
0657:                int n = 0; // numbers of entries in current line    
0658:                print("    \"");
0659:
0660:                int i = 0;
0661:                while (i < intervalls.length - 1) {
0662:                    int count = intervalls[i].end - intervalls[i].start + 1;
0663:                    int value = colMap[intervalls[i].charClass];
0664:
0665:                    printUC(count);
0666:                    printUC(value);
0667:
0668:                    if (++n >= 10) {
0669:                        println("\"+");
0670:                        print("    \"");
0671:                        n = 0;
0672:                    }
0673:
0674:                    i++;
0675:                }
0676:
0677:                printUC(intervalls[i].end - intervalls[i].start + 1);
0678:                printUC(colMap[intervalls[i].charClass]);
0679:
0680:                println("\";");
0681:                println();
0682:
0683:                println("  /** ");
0684:                println("   * Translates characters to character classes");
0685:                println("   */");
0686:                println("  private static final char [] ZZ_CMAP = zzUnpackCMap(ZZ_CMAP_PACKED);");
0687:                println();
0688:            }
0689:
0690:            /**
0691:             * Print number as octal/unicode escaped string character.
0692:             * 
0693:             * @param c   the value to print
0694:             * @prec  0 <= c <= 0xFFFF 
0695:             */
0696:            private void printUC(int c) {
0697:                if (c > 255) {
0698:                    out.print("\\u");
0699:                    if (c < 0x1000)
0700:                        out.print("0");
0701:                    out.print(Integer.toHexString(c));
0702:                } else {
0703:                    out.print("\\");
0704:                    out.print(Integer.toOctalString(c));
0705:                }
0706:            }
0707:
0708:            private void emitRowMapArray() {
0709:                println("");
0710:                println("  /** ");
0711:                println("   * Translates a state to a row index in the transition table");
0712:                println("   */");
0713:
0714:                HiLowEmitter e = new HiLowEmitter("RowMap");
0715:                e.emitInit();
0716:                for (int i = 0; i < dfa.numStates; i++) {
0717:                    e.emit(rowMap[i] * numCols);
0718:                }
0719:                e.emitUnpack();
0720:                println(e.toString());
0721:            }
0722:
0723:            private void emitAttributes() {
0724:                println("  /**");
0725:                println("   * ZZ_ATTRIBUTE[aState] contains the attributes of state <code>aState</code>");
0726:                println("   */");
0727:
0728:                CountEmitter e = new CountEmitter("Attribute");
0729:                e.emitInit();
0730:
0731:                int count = 1;
0732:                int value = 0;
0733:                if (dfa.isFinal[0])
0734:                    value = FINAL;
0735:                if (dfa.isPushback[0])
0736:                    value |= PUSHBACK;
0737:                if (dfa.isLookEnd[0])
0738:                    value |= LOOKEND;
0739:                if (!isTransition[0])
0740:                    value |= NOLOOK;
0741:
0742:                for (int i = 1; i < dfa.numStates; i++) {
0743:                    int attribute = 0;
0744:                    if (dfa.isFinal[i])
0745:                        attribute = FINAL;
0746:                    if (dfa.isPushback[i])
0747:                        attribute |= PUSHBACK;
0748:                    if (dfa.isLookEnd[i])
0749:                        attribute |= LOOKEND;
0750:                    if (!isTransition[i])
0751:                        attribute |= NOLOOK;
0752:
0753:                    if (value == attribute) {
0754:                        count++;
0755:                    } else {
0756:                        e.emit(count, value);
0757:                        count = 1;
0758:                        value = attribute;
0759:                    }
0760:                }
0761:
0762:                e.emit(count, value);
0763:                e.emitUnpack();
0764:
0765:                println(e.toString());
0766:            }
0767:
0768:            private void emitClassCode() {
0769:                if (scanner.eofCode != null) {
0770:                    println("  /** denotes if the user-EOF-code has already been executed */");
0771:                    println("  private boolean zzEOFDone;");
0772:                    println("");
0773:                }
0774:
0775:                if (scanner.classCode != null) {
0776:                    println("  /* user code: */");
0777:                    println(scanner.classCode);
0778:                }
0779:            }
0780:
0781:            private void emitConstructorDecl() {
0782:
0783:                print("  ");
0784:
0785:                if (scanner.isPublic)
0786:                    print("public ");
0787:                print(scanner.className);
0788:                print("(java.io.Reader in)");
0789:
0790:                if (scanner.initThrow != null) {
0791:                    print(" throws ");
0792:                    print(scanner.initThrow);
0793:                }
0794:
0795:                println(" {");
0796:
0797:                if (scanner.initCode != null) {
0798:                    print("  ");
0799:                    print(scanner.initCode);
0800:                }
0801:
0802:                println("    this.zzReader = in;");
0803:
0804:                println("  }");
0805:                println();
0806:
0807:                println("  /**");
0808:                println("   * Creates a new scanner.");
0809:                println("   * There is also java.io.Reader version of this constructor.");
0810:                println("   *");
0811:                println("   * @param   in  the java.io.Inputstream to read input from.");
0812:                println("   */");
0813:
0814:                print("  ");
0815:                if (scanner.isPublic)
0816:                    print("public ");
0817:                print(scanner.className);
0818:                print("(java.io.InputStream in)");
0819:
0820:                if (scanner.initThrow != null) {
0821:                    print(" throws ");
0822:                    print(scanner.initThrow);
0823:                }
0824:
0825:                println(" {");
0826:                println("    this(new java.io.InputStreamReader(in));");
0827:                println("  }");
0828:            }
0829:
0830:            private void emitDoEOF() {
0831:                if (scanner.eofCode == null)
0832:                    return;
0833:
0834:                println("  /**");
0835:                println("   * Contains user EOF-code, which will be executed exactly once,");
0836:                println("   * when the end of file is reached");
0837:                println("   */");
0838:
0839:                print("  private void zzDoEOF()");
0840:
0841:                if (scanner.eofThrow != null) {
0842:                    print(" throws ");
0843:                    print(scanner.eofThrow);
0844:                }
0845:
0846:                println(" {");
0847:
0848:                println("    if (!zzEOFDone) {");
0849:                println("      zzEOFDone = true;");
0850:                println("    " + scanner.eofCode);
0851:                println("    }");
0852:                println("  }");
0853:                println("");
0854:                println("");
0855:            }
0856:
0857:            private void emitLexFunctHeader() {
0858:
0859:                if (scanner.cupCompatible) {
0860:                    // force public, because we have to implement java_cup.runtime.Symbol
0861:                    print("  public ");
0862:                } else {
0863:                    print("  " + visibility + " ");
0864:                }
0865:
0866:                if (scanner.tokenType == null) {
0867:                    if (scanner.isInteger)
0868:                        print("int");
0869:                    else if (scanner.isIntWrap)
0870:                        print("Integer");
0871:                    else
0872:                        print("Yytoken");
0873:                } else
0874:                    print(scanner.tokenType);
0875:
0876:                print(" ");
0877:
0878:                print(scanner.functionName);
0879:
0880:                print("() throws java.io.IOException");
0881:
0882:                if (scanner.lexThrow != null) {
0883:                    print(", ");
0884:                    print(scanner.lexThrow);
0885:                }
0886:
0887:                if (scanner.scanErrorException != null) {
0888:                    print(", ");
0889:                    print(scanner.scanErrorException);
0890:                }
0891:
0892:                println(" {");
0893:
0894:                skel.emitNext();
0895:
0896:                if (scanner.useRowMap) {
0897:                    println("    int [] zzTransL = ZZ_TRANS;");
0898:                    println("    int [] zzRowMapL = ZZ_ROWMAP;");
0899:                    println("    int [] zzAttrL = ZZ_ATTRIBUTE;");
0900:
0901:                }
0902:
0903:                if (scanner.lookAheadUsed) {
0904:                    println("    int zzPushbackPosL = zzPushbackPos = -1;");
0905:                    println("    boolean zzWasPushback;");
0906:                }
0907:
0908:                skel.emitNext();
0909:
0910:                if (scanner.charCount) {
0911:                    println("      yychar+= zzMarkedPosL-zzStartRead;");
0912:                    println("");
0913:                }
0914:
0915:                if (scanner.lineCount || scanner.columnCount) {
0916:                    println("      boolean zzR = false;");
0917:                    println("      for (zzCurrentPosL = zzStartRead; zzCurrentPosL < zzMarkedPosL;");
0918:                    println("                                                             zzCurrentPosL++) {");
0919:                    println("        switch (zzBufferL[zzCurrentPosL]) {");
0920:                    println("        case '\\u000B':");
0921:                    println("        case '\\u000C':");
0922:                    println("        case '\\u0085':");
0923:                    println("        case '\\u2028':");
0924:                    println("        case '\\u2029':");
0925:                    if (scanner.lineCount)
0926:                        println("          yyline++;");
0927:                    if (scanner.columnCount)
0928:                        println("          yycolumn = 0;");
0929:                    println("          zzR = false;");
0930:                    println("          break;");
0931:                    println("        case '\\r':");
0932:                    if (scanner.lineCount)
0933:                        println("          yyline++;");
0934:                    if (scanner.columnCount)
0935:                        println("          yycolumn = 0;");
0936:                    println("          zzR = true;");
0937:                    println("          break;");
0938:                    println("        case '\\n':");
0939:                    println("          if (zzR)");
0940:                    println("            zzR = false;");
0941:                    println("          else {");
0942:                    if (scanner.lineCount)
0943:                        println("            yyline++;");
0944:                    if (scanner.columnCount)
0945:                        println("            yycolumn = 0;");
0946:                    println("          }");
0947:                    println("          break;");
0948:                    println("        default:");
0949:                    println("          zzR = false;");
0950:                    if (scanner.columnCount)
0951:                        println("          yycolumn++;");
0952:                    println("        }");
0953:                    println("      }");
0954:                    println();
0955:
0956:                    if (scanner.lineCount) {
0957:                        println("      if (zzR) {");
0958:                        println("        // peek one character ahead if it is \\n (if we have counted one line too much)");
0959:                        println("        boolean zzPeek;");
0960:                        println("        if (zzMarkedPosL < zzEndReadL)");
0961:                        println("          zzPeek = zzBufferL[zzMarkedPosL] == '\\n';");
0962:                        println("        else if (zzAtEOF)");
0963:                        println("          zzPeek = false;");
0964:                        println("        else {");
0965:                        println("          boolean eof = zzRefill();");
0966:                        println("          zzEndReadL = zzEndRead;");
0967:                        println("          zzMarkedPosL = zzMarkedPos;");
0968:                        println("          zzBufferL = zzBuffer;");
0969:                        println("          if (eof) ");
0970:                        println("            zzPeek = false;");
0971:                        println("          else ");
0972:                        println("            zzPeek = zzBufferL[zzMarkedPosL] == '\\n';");
0973:                        println("        }");
0974:                        println("        if (zzPeek) yyline--;");
0975:                        println("      }");
0976:                    }
0977:                }
0978:
0979:                if (scanner.bolUsed) {
0980:                    // zzMarkedPos > zzStartRead <=> last match was not empty
0981:                    // if match was empty, last value of zzAtBOL can be used
0982:                    // zzStartRead is always >= 0
0983:                    println("      if (zzMarkedPosL > zzStartRead) {");
0984:                    println("        switch (zzBufferL[zzMarkedPosL-1]) {");
0985:                    println("        case '\\n':");
0986:                    println("        case '\\u000B':");
0987:                    println("        case '\\u000C':");
0988:                    println("        case '\\u0085':");
0989:                    println("        case '\\u2028':");
0990:                    println("        case '\\u2029':");
0991:                    println("          zzAtBOL = true;");
0992:                    println("          break;");
0993:                    println("        case '\\r': ");
0994:                    println("          if (zzMarkedPosL < zzEndReadL)");
0995:                    println("            zzAtBOL = zzBufferL[zzMarkedPosL] != '\\n';");
0996:                    println("          else if (zzAtEOF)");
0997:                    println("            zzAtBOL = false;");
0998:                    println("          else {");
0999:                    println("            boolean eof = zzRefill();");
1000:                    println("            zzMarkedPosL = zzMarkedPos;");
1001:                    println("            zzEndReadL = zzEndRead;");
1002:                    println("            zzBufferL = zzBuffer;");
1003:                    println("            if (eof) ");
1004:                    println("              zzAtBOL = false;");
1005:                    println("            else ");
1006:                    println("              zzAtBOL = zzBufferL[zzMarkedPosL] != '\\n';");
1007:                    println("          }");
1008:                    println("          break;");
1009:                    println("        default:");
1010:                    println("          zzAtBOL = false;");
1011:                    println("        }");
1012:                    println("      }");
1013:                }
1014:
1015:                skel.emitNext();
1016:
1017:                if (scanner.bolUsed) {
1018:                    println("      if (zzAtBOL)");
1019:                    println("        zzState = ZZ_LEXSTATE[zzLexicalState+1];");
1020:                    println("      else");
1021:                    println("        zzState = ZZ_LEXSTATE[zzLexicalState];");
1022:                    println();
1023:                } else {
1024:                    println("      zzState = zzLexicalState;");
1025:                    println();
1026:                }
1027:
1028:                if (scanner.lookAheadUsed)
1029:                    println("      zzWasPushback = false;");
1030:
1031:                skel.emitNext();
1032:            }
1033:
1034:            private void emitGetRowMapNext() {
1035:                println("          int zzNext = zzTransL[ zzRowMapL[zzState] + zzCMapL[zzInput] ];");
1036:                println("          if (zzNext == " + DFA.NO_TARGET
1037:                        + ") break zzForAction;");
1038:                println("          zzState = zzNext;");
1039:                println();
1040:
1041:                println("          int zzAttributes = zzAttrL[zzState];");
1042:
1043:                if (scanner.lookAheadUsed) {
1044:                    println("          if ( (zzAttributes & " + PUSHBACK
1045:                            + ") == " + PUSHBACK + " )");
1046:                    println("            zzPushbackPosL = zzCurrentPosL;");
1047:                    println();
1048:                }
1049:
1050:                println("          if ( (zzAttributes & " + FINAL + ") == "
1051:                        + FINAL + " ) {");
1052:                if (scanner.lookAheadUsed)
1053:                    println("            zzWasPushback = (zzAttributes & "
1054:                            + LOOKEND + ") == " + LOOKEND + ";");
1055:
1056:                skel.emitNext();
1057:
1058:                println("            if ( (zzAttributes & " + NOLOOK + ") == "
1059:                        + NOLOOK + " ) break zzForAction;");
1060:
1061:                skel.emitNext();
1062:            }
1063:
1064:            private void emitTransitionTable() {
1065:                transformTransitionTable();
1066:
1067:                println("          zzInput = zzCMapL[zzInput];");
1068:                println();
1069:
1070:                if (scanner.lookAheadUsed)
1071:                    println("          boolean zzPushback = false;");
1072:
1073:                println("          boolean zzIsFinal = false;");
1074:                println("          boolean zzNoLookAhead = false;");
1075:                println();
1076:
1077:                println("          zzForNext: { switch (zzState) {");
1078:
1079:                for (int state = 0; state < dfa.numStates; state++)
1080:                    if (isTransition[state])
1081:                        emitState(state);
1082:
1083:                println("            default:");
1084:                println("              // if this is ever reached, there is a serious bug in JFlex");
1085:                println("              zzScanError(ZZ_UNKNOWN_ERROR);");
1086:                println("              break;");
1087:                println("          } }");
1088:                println();
1089:
1090:                println("          if ( zzIsFinal ) {");
1091:
1092:                if (scanner.lookAheadUsed)
1093:                    println("            zzWasPushback = zzPushback;");
1094:
1095:                skel.emitNext();
1096:
1097:                println("            if ( zzNoLookAhead ) break zzForAction;");
1098:
1099:                skel.emitNext();
1100:            }
1101:
1102:            /**
1103:             * Escapes all " ' \ tabs and newlines
1104:             */
1105:            private String escapify(String s) {
1106:                StringBuffer result = new StringBuffer(s.length() * 2);
1107:
1108:                for (int i = 0; i < s.length(); i++) {
1109:                    char c = s.charAt(i);
1110:                    switch (c) {
1111:                    case '\'':
1112:                        result.append("\\\'");
1113:                        break;
1114:                    case '\"':
1115:                        result.append("\\\"");
1116:                        break;
1117:                    case '\\':
1118:                        result.append("\\\\");
1119:                        break;
1120:                    case '\t':
1121:                        result.append("\\t");
1122:                        break;
1123:                    case '\r':
1124:                        if (i + 1 == s.length() || s.charAt(i + 1) != '\n')
1125:                            result.append("\"+ZZ_NL+\"");
1126:                        break;
1127:                    case '\n':
1128:                        result.append("\"+ZZ_NL+\"");
1129:                        break;
1130:                    default:
1131:                        result.append(c);
1132:                    }
1133:                }
1134:
1135:                return result.toString();
1136:            }
1137:
1138:            public void emitActionTable() {
1139:                int lastAction = 1;
1140:                int count = 0;
1141:                int value = 0;
1142:
1143:                println("  /** ");
1144:                println("   * Translates DFA states to action switch labels.");
1145:                println("   */");
1146:                CountEmitter e = new CountEmitter("Action");
1147:                e.emitInit();
1148:
1149:                for (int i = 0; i < dfa.numStates; i++) {
1150:                    int newVal;
1151:                    if (dfa.isFinal[i]) {
1152:                        Action action = dfa.action[i];
1153:                        Integer stored = (Integer) actionTable.get(action);
1154:                        if (stored == null) {
1155:                            stored = new Integer(lastAction++);
1156:                            actionTable.put(action, stored);
1157:                        }
1158:                        newVal = stored.intValue();
1159:                    } else {
1160:                        newVal = 0;
1161:                    }
1162:
1163:                    if (value == newVal) {
1164:                        count++;
1165:                    } else {
1166:                        if (count > 0)
1167:                            e.emit(count, value);
1168:                        count = 1;
1169:                        value = newVal;
1170:                    }
1171:                }
1172:
1173:                if (count > 0)
1174:                    e.emit(count, value);
1175:
1176:                e.emitUnpack();
1177:                println(e.toString());
1178:            }
1179:
1180:            private void emitActions() {
1181:                println("      switch (zzAction < 0 ? zzAction : ZZ_ACTION[zzAction]) {");
1182:
1183:                int i = actionTable.size() + 1;
1184:                Enumeration actions = actionTable.keys();
1185:                while (actions.hasMoreElements()) {
1186:                    Action action = (Action) actions.nextElement();
1187:                    int label = ((Integer) actionTable.get(action)).intValue();
1188:
1189:                    println("        case " + label + ": ");
1190:
1191:                    if (scanner.debugOption) {
1192:                        print("          System.out.println(");
1193:                        if (scanner.lineCount)
1194:                            print("\"line: \"+(yyline+1)+\" \"+");
1195:                        if (scanner.columnCount)
1196:                            print("\"col: \"+(yycolumn+1)+\" \"+");
1197:                        println("\"match: --\"+yytext()+\"--\");");
1198:                        print("          System.out.println(\"action ["
1199:                                + action.priority + "] { ");
1200:                        print(escapify(action.content));
1201:                        println(" }\");");
1202:                    }
1203:
1204:                    println("          { " + action.content);
1205:                    println("          }");
1206:                    println("        case " + (i++) + ": break;");
1207:                }
1208:            }
1209:
1210:            private void emitEOFVal() {
1211:                EOFActions eofActions = parser.getEOFActions();
1212:
1213:                if (scanner.eofCode != null)
1214:                    println("            zzDoEOF();");
1215:
1216:                if (eofActions.numActions() > 0) {
1217:                    println("            switch (zzLexicalState) {");
1218:
1219:                    Enumeration stateNames = scanner.states.names();
1220:
1221:                    // record lex states already emitted:
1222:                    Hashtable used = new Hashtable();
1223:
1224:                    // pick a start value for break case labels. 
1225:                    // must be larger than any value of a lex state:
1226:                    int last = dfa.numStates;
1227:
1228:                    while (stateNames.hasMoreElements()) {
1229:                        String name = (String) stateNames.nextElement();
1230:                        int num = scanner.states.getNumber(name).intValue();
1231:                        Action action = eofActions.getAction(num);
1232:
1233:                        // only emit code if the lex state is not redundant, so
1234:                        // that case labels don't overlap
1235:                        // (redundant = points to the same dfa state as another one).
1236:                        // applies only to scanners that don't use BOL, because
1237:                        // in BOL scanners lex states get mapped at runtime, so
1238:                        // case labels will always be unique.
1239:                        boolean unused = true;
1240:                        if (!scanner.bolUsed) {
1241:                            Integer key = new Integer(dfa.lexState[2 * num]);
1242:                            unused = used.get(key) == null;
1243:
1244:                            if (!unused)
1245:                                Out.warning("Lexical states <" + name
1246:                                        + "> and <" + used.get(key)
1247:                                        + "> are equivalent.");
1248:                            else
1249:                                used.put(key, name);
1250:                        }
1251:
1252:                        if (action != null && unused) {
1253:                            println("            case " + name + ": {");
1254:                            if (scanner.debugOption) {
1255:                                print("              System.out.println(");
1256:                                if (scanner.lineCount)
1257:                                    print("\"line: \"+(yyline+1)+\" \"+");
1258:                                if (scanner.columnCount)
1259:                                    print("\"col: \"+(yycolumn+1)+\" \"+");
1260:                                println("\"match: <<EOF>>\");");
1261:                                print("              System.out.println(\"action ["
1262:                                        + action.priority + "] { ");
1263:                                print(escapify(action.content));
1264:                                println(" }\");");
1265:                            }
1266:                            println("              " + action.content);
1267:                            println("            }");
1268:                            println("            case " + (++last) + ": break;");
1269:                        }
1270:                    }
1271:
1272:                    println("            default:");
1273:                }
1274:
1275:                Action defaultAction = eofActions.getDefault();
1276:
1277:                if (defaultAction != null) {
1278:                    println("              {");
1279:                    if (scanner.debugOption) {
1280:                        print("                System.out.println(");
1281:                        if (scanner.lineCount)
1282:                            print("\"line: \"+(yyline+1)+\" \"+");
1283:                        if (scanner.columnCount)
1284:                            print("\"col: \"+(yycolumn+1)+\" \"+");
1285:                        println("\"match: <<EOF>>\");");
1286:                        print("                System.out.println(\"action ["
1287:                                + defaultAction.priority + "] { ");
1288:                        print(escapify(defaultAction.content));
1289:                        println(" }\");");
1290:                    }
1291:                    println("                " + defaultAction.content);
1292:                    println("              }");
1293:                } else if (scanner.eofVal != null)
1294:                    println("              { " + scanner.eofVal + " }");
1295:                else if (scanner.isInteger)
1296:                    println("            return YYEOF;");
1297:                else
1298:                    println("            return null;");
1299:
1300:                if (eofActions.numActions() > 0)
1301:                    println("            }");
1302:            }
1303:
1304:            private void emitState(int state) {
1305:
1306:                println("            case " + state + ":");
1307:                println("              switch (zzInput) {");
1308:
1309:                int defaultTransition = getDefaultTransition(state);
1310:
1311:                for (int next = 0; next < dfa.numStates; next++) {
1312:
1313:                    if (next != defaultTransition && table[state][next] != null) {
1314:                        emitTransition(state, next);
1315:                    }
1316:                }
1317:
1318:                if (defaultTransition != DFA.NO_TARGET
1319:                        && noTarget[state] != null) {
1320:                    emitTransition(state, DFA.NO_TARGET);
1321:                }
1322:
1323:                emitDefaultTransition(state, defaultTransition);
1324:
1325:                println("              }");
1326:                println("");
1327:            }
1328:
1329:            private void emitTransition(int state, int nextState) {
1330:
1331:                CharSetEnumerator chars;
1332:
1333:                if (nextState != DFA.NO_TARGET)
1334:                    chars = table[state][nextState].characters();
1335:                else
1336:                    chars = noTarget[state].characters();
1337:
1338:                print("                case ");
1339:                print((int) chars.nextElement());
1340:                print(": ");
1341:
1342:                while (chars.hasMoreElements()) {
1343:                    println();
1344:                    print("                case ");
1345:                    print((int) chars.nextElement());
1346:                    print(": ");
1347:                }
1348:
1349:                if (nextState != DFA.NO_TARGET) {
1350:                    if (dfa.isFinal[nextState])
1351:                        print("zzIsFinal = true; ");
1352:
1353:                    if (dfa.isPushback[nextState])
1354:                        print("zzPushbackPosL = zzCurrentPosL; ");
1355:
1356:                    if (dfa.isLookEnd[nextState])
1357:                        print("zzPushback = true; ");
1358:
1359:                    if (!isTransition[nextState])
1360:                        print("zzNoLookAhead = true; ");
1361:
1362:                    if (nextState == state)
1363:                        println("break zzForNext;");
1364:                    else
1365:                        println("zzState = " + nextState + "; break zzForNext;");
1366:                } else
1367:                    println("break zzForAction;");
1368:            }
1369:
1370:            private void emitDefaultTransition(int state, int nextState) {
1371:                print("                default: ");
1372:
1373:                if (nextState != DFA.NO_TARGET) {
1374:                    if (dfa.isFinal[nextState])
1375:                        print("zzIsFinal = true; ");
1376:
1377:                    if (dfa.isPushback[nextState])
1378:                        print("zzPushbackPosL = zzCurrentPosL; ");
1379:
1380:                    if (dfa.isLookEnd[nextState])
1381:                        print("zzPushback = true; ");
1382:
1383:                    if (!isTransition[nextState])
1384:                        print("zzNoLookAhead = true; ");
1385:
1386:                    if (nextState == state)
1387:                        println("break zzForNext;");
1388:                    else
1389:                        println("zzState = " + nextState + "; break zzForNext;");
1390:                } else
1391:                    println("break zzForAction;");
1392:            }
1393:
1394:            private void emitPushback() {
1395:                println("      if (zzWasPushback)");
1396:                println("        zzMarkedPos = zzPushbackPosL;");
1397:            }
1398:
1399:            private int getDefaultTransition(int state) {
1400:                int max = 0;
1401:
1402:                for (int i = 0; i < dfa.numStates; i++) {
1403:                    if (table[state][max] == null)
1404:                        max = i;
1405:                    else if (table[state][i] != null
1406:                            && table[state][max].size() < table[state][i]
1407:                                    .size())
1408:                        max = i;
1409:                }
1410:
1411:                if (table[state][max] == null)
1412:                    return DFA.NO_TARGET;
1413:                if (noTarget[state] == null)
1414:                    return max;
1415:
1416:                if (table[state][max].size() < noTarget[state].size())
1417:                    max = DFA.NO_TARGET;
1418:
1419:                return max;
1420:            }
1421:
1422:            // for switch statement:
1423:            private void transformTransitionTable() {
1424:
1425:                int numInput = parser.getCharClasses().getNumClasses() + 1;
1426:
1427:                int i;
1428:                char j;
1429:
1430:                table = new CharSet[dfa.numStates][dfa.numStates];
1431:                noTarget = new CharSet[dfa.numStates];
1432:
1433:                for (i = 0; i < dfa.numStates; i++)
1434:                    for (j = 0; j < dfa.numInput; j++) {
1435:
1436:                        int nextState = dfa.table[i][j];
1437:
1438:                        if (nextState == DFA.NO_TARGET) {
1439:                            if (noTarget[i] == null)
1440:                                noTarget[i] = new CharSet(numInput, colMap[j]);
1441:                            else
1442:                                noTarget[i].add(colMap[j]);
1443:                        } else {
1444:                            if (table[i][nextState] == null)
1445:                                table[i][nextState] = new CharSet(numInput,
1446:                                        colMap[j]);
1447:                            else
1448:                                table[i][nextState].add(colMap[j]);
1449:                        }
1450:                    }
1451:            }
1452:
1453:            private void findActionStates() {
1454:                isTransition = new boolean[dfa.numStates];
1455:
1456:                for (int i = 0; i < dfa.numStates; i++) {
1457:                    char j = 0;
1458:                    while (!isTransition[i] && j < dfa.numInput)
1459:                        isTransition[i] = dfa.table[i][j++] != DFA.NO_TARGET;
1460:                }
1461:            }
1462:
1463:            private void reduceColumns() {
1464:                colMap = new int[dfa.numInput];
1465:                colKilled = new boolean[dfa.numInput];
1466:
1467:                int i, j, k;
1468:                int translate = 0;
1469:                boolean equal;
1470:
1471:                numCols = dfa.numInput;
1472:
1473:                for (i = 0; i < dfa.numInput; i++) {
1474:
1475:                    colMap[i] = i - translate;
1476:
1477:                    for (j = 0; j < i; j++) {
1478:
1479:                        // test for equality:
1480:                        k = -1;
1481:                        equal = true;
1482:                        while (equal && ++k < dfa.numStates)
1483:                            equal = dfa.table[k][i] == dfa.table[k][j];
1484:
1485:                        if (equal) {
1486:                            translate++;
1487:                            colMap[i] = colMap[j];
1488:                            colKilled[i] = true;
1489:                            numCols--;
1490:                            break;
1491:                        } // if
1492:                    } // for j
1493:                } // for i
1494:            }
1495:
1496:            private void reduceRows() {
1497:                rowMap = new int[dfa.numStates];
1498:                rowKilled = new boolean[dfa.numStates];
1499:
1500:                int i, j, k;
1501:                int translate = 0;
1502:                boolean equal;
1503:
1504:                numRows = dfa.numStates;
1505:
1506:                // i is the state to add to the new table
1507:                for (i = 0; i < dfa.numStates; i++) {
1508:
1509:                    rowMap[i] = i - translate;
1510:
1511:                    // check if state i can be removed (i.e. already
1512:                    // exists in entries 0..i-1)
1513:                    for (j = 0; j < i; j++) {
1514:
1515:                        // test for equality:
1516:                        k = -1;
1517:                        equal = true;
1518:                        while (equal && ++k < dfa.numInput)
1519:                            equal = dfa.table[i][k] == dfa.table[j][k];
1520:
1521:                        if (equal) {
1522:                            translate++;
1523:                            rowMap[i] = rowMap[j];
1524:                            rowKilled[i] = true;
1525:                            numRows--;
1526:                            break;
1527:                        } // if
1528:                    } // for j
1529:                } // for i
1530:
1531:            }
1532:
1533:            /**
1534:             * Set up EOF code sectioin according to scanner.eofcode 
1535:             */
1536:            private void setupEOFCode() {
1537:                if (scanner.eofclose) {
1538:                    scanner.eofCode = LexScan.conc(scanner.eofCode,
1539:                            "  yyclose();");
1540:                    scanner.eofThrow = LexScan.concExc(scanner.eofThrow,
1541:                            "java.io.IOException");
1542:                }
1543:            }
1544:
1545:            /**
1546:             * Main Emitter method.  
1547:             */
1548:            public void emit() {
1549:
1550:                setupEOFCode();
1551:
1552:                if (scanner.functionName == null)
1553:                    scanner.functionName = "yylex";
1554:
1555:                reduceColumns();
1556:                findActionStates();
1557:
1558:                emitHeader();
1559:                emitUserCode();
1560:                emitClassName();
1561:
1562:                skel.emitNext();
1563:
1564:                println("  private static final int ZZ_BUFFERSIZE = "
1565:                        + scanner.bufferSize + ";");
1566:
1567:                if (scanner.debugOption) {
1568:                    println("  private static final String ZZ_NL = System.getProperty(\"line.separator\");");
1569:                }
1570:
1571:                skel.emitNext();
1572:
1573:                emitLexicalStates();
1574:
1575:                emitCharMapArray();
1576:
1577:                emitActionTable();
1578:
1579:                if (scanner.useRowMap) {
1580:                    reduceRows();
1581:
1582:                    emitRowMapArray();
1583:
1584:                    if (scanner.packed)
1585:                        emitDynamicInit();
1586:                    else
1587:                        emitZZTrans();
1588:                }
1589:
1590:                skel.emitNext();
1591:
1592:                if (scanner.useRowMap)
1593:                    emitAttributes();
1594:
1595:                skel.emitNext();
1596:
1597:                emitClassCode();
1598:
1599:                skel.emitNext();
1600:
1601:                emitConstructorDecl();
1602:
1603:                emitCharMapInitFunction();
1604:
1605:                skel.emitNext();
1606:
1607:                emitScanError();
1608:
1609:                skel.emitNext();
1610:
1611:                emitDoEOF();
1612:
1613:                skel.emitNext();
1614:
1615:                emitLexFunctHeader();
1616:
1617:                emitNextInput();
1618:
1619:                if (scanner.useRowMap)
1620:                    emitGetRowMapNext();
1621:                else
1622:                    emitTransitionTable();
1623:
1624:                if (scanner.lookAheadUsed)
1625:                    emitPushback();
1626:
1627:                skel.emitNext();
1628:
1629:                emitActions();
1630:
1631:                skel.emitNext();
1632:
1633:                emitEOFVal();
1634:
1635:                skel.emitNext();
1636:
1637:                emitNoMatch();
1638:
1639:                skel.emitNext();
1640:
1641:                emitMain();
1642:
1643:                skel.emitNext();
1644:
1645:                out.close();
1646:            }
1647:
1648:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.