Advertisement
Guest User

summe.flex2summe.java

a guest
May 24th, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.20 KB | None | 0 0
  1. /* The following code was generated by JFlex 1.4.3 on 24.05.12 11:19 */
  2.  
  3. /*
  4.   Einfache Demonstration des Java-Lexers Flex.
  5. */
  6.  
  7. import java.io.*;
  8.  
  9.  
  10. /**
  11.  * This class is a scanner generated by
  12.  * <a href="http://www.jflex.de/">JFlex</a> 1.4.3
  13.  * on 24.05.12 11:19 from the specification file
  14.  * <tt>/home/dex/DevWorkspace/java/jflex-1.4.3/jflex-1.4.3/bin/daten/summe.flex</tt>
  15.  */
  16. class Summe {
  17.  
  18.   /** This character denotes the end of file */
  19.   public static final int YYEOF = -1;
  20.  
  21.   /** initial size of the lookahead buffer */
  22.   private static final int ZZ_BUFFERSIZE = 16384;
  23.  
  24.   /** lexical states */
  25.   public static final int YYINITIAL = 0;
  26.  
  27.   /**
  28.    * ZZ_LEXSTATE[l] is the state in the DFA for the lexical state l
  29.    * ZZ_LEXSTATE[l+1] is the state in the DFA for the lexical state l
  30.    *                  at the beginning of a line
  31.    * l is of the form l = 2*k, k a non negative integer
  32.    */
  33.   private static final int ZZ_LEXSTATE[] = {
  34.      0, 0
  35.   };
  36.  
  37.   /**
  38.    * Translates characters to character classes
  39.    */
  40.   private static final String ZZ_CMAP_PACKED =
  41.     "\11\0\1\10\1\7\1\0\1\10\1\6\22\0\1\10\11\0\1\5"+
  42.     "\1\1\1\0\1\1\1\3\1\4\12\2\uffc6\0";
  43.  
  44.   /**
  45.    * Translates characters to character classes
  46.    */
  47.   private static final char [] ZZ_CMAP = zzUnpackCMap(ZZ_CMAP_PACKED);
  48.  
  49.   /**
  50.    * Translates DFA states to action switch labels.
  51.    */
  52.   private static final int [] ZZ_ACTION = zzUnpackAction();
  53.  
  54.   private static final String ZZ_ACTION_PACKED_0 =
  55.     "\1\0\2\1\1\2\1\1\2\3\2\0\1\2\1\0";
  56.  
  57.   private static int [] zzUnpackAction() {
  58.     int [] result = new int[11];
  59.     int offset = 0;
  60.     offset = zzUnpackAction(ZZ_ACTION_PACKED_0, offset, result);
  61.     return result;
  62.   }
  63.  
  64.   private static int zzUnpackAction(String packed, int offset, int [] result) {
  65.     int i = 0;       /* index in packed string  */
  66.     int j = offset;  /* index in unpacked array */
  67.     int l = packed.length();
  68.     while (i < l) {
  69.       int count = packed.charAt(i++);
  70.       int value = packed.charAt(i++);
  71.       do result[j++] = value; while (--count > 0);
  72.     }
  73.     return j;
  74.   }
  75.  
  76.  
  77.   /**
  78.    * Translates a state to a row index in the transition table
  79.    */
  80.   private static final int [] ZZ_ROWMAP = zzUnpackRowMap();
  81.  
  82.   private static final String ZZ_ROWMAP_PACKED_0 =
  83.     "\0\0\0\11\0\22\0\33\0\44\0\55\0\11\0\66"+
  84.     "\0\77\0\66\0\110";
  85.  
  86.   private static int [] zzUnpackRowMap() {
  87.     int [] result = new int[11];
  88.     int offset = 0;
  89.     offset = zzUnpackRowMap(ZZ_ROWMAP_PACKED_0, offset, result);
  90.     return result;
  91.   }
  92.  
  93.   private static int zzUnpackRowMap(String packed, int offset, int [] result) {
  94.     int i = 0;  /* index in packed string  */
  95.     int j = offset;  /* index in unpacked array */
  96.     int l = packed.length();
  97.     while (i < l) {
  98.       int high = packed.charAt(i++) << 16;
  99.       result[j++] = high | packed.charAt(i++);
  100.     }
  101.     return j;
  102.   }
  103.  
  104.   /**
  105.    * The transition table of the DFA
  106.    */
  107.   private static final int [] ZZ_TRANS = zzUnpackTrans();
  108.  
  109.   private static final String ZZ_TRANS_PACKED_0 =
  110.     "\1\2\1\3\1\4\1\2\1\5\1\2\1\6\2\7"+
  111.     "\13\0\1\4\10\0\1\4\1\10\12\0\1\11\12\0"+
  112.     "\1\7\3\0\1\12\6\0\5\11\1\13\7\11\1\7"+
  113.     "\1\13\3\11";
  114.  
  115.   private static int [] zzUnpackTrans() {
  116.     int [] result = new int[81];
  117.     int offset = 0;
  118.     offset = zzUnpackTrans(ZZ_TRANS_PACKED_0, offset, result);
  119.     return result;
  120.   }
  121.  
  122.   private static int zzUnpackTrans(String packed, int offset, int [] result) {
  123.     int i = 0;       /* index in packed string  */
  124.     int j = offset;  /* index in unpacked array */
  125.     int l = packed.length();
  126.     while (i < l) {
  127.       int count = packed.charAt(i++);
  128.       int value = packed.charAt(i++);
  129.       value--;
  130.       do result[j++] = value; while (--count > 0);
  131.     }
  132.     return j;
  133.   }
  134.  
  135.  
  136.   /* error codes */
  137.   private static final int ZZ_UNKNOWN_ERROR = 0;
  138.   private static final int ZZ_NO_MATCH = 1;
  139.   private static final int ZZ_PUSHBACK_2BIG = 2;
  140.  
  141.   /* error messages for the codes above */
  142.   private static final String ZZ_ERROR_MSG[] = {
  143.     "Unkown internal scanner error",
  144.     "Error: could not match input",
  145.     "Error: pushback value was too large"
  146.   };
  147.  
  148.   /**
  149.    * ZZ_ATTRIBUTE[aState] contains the attributes of state <code>aState</code>
  150.    */
  151.   private static final int [] ZZ_ATTRIBUTE = zzUnpackAttribute();
  152.  
  153.   private static final String ZZ_ATTRIBUTE_PACKED_0 =
  154.     "\1\0\1\11\4\1\1\11\2\0\1\1\1\0";
  155.  
  156.   private static int [] zzUnpackAttribute() {
  157.     int [] result = new int[11];
  158.     int offset = 0;
  159.     offset = zzUnpackAttribute(ZZ_ATTRIBUTE_PACKED_0, offset, result);
  160.     return result;
  161.   }
  162.  
  163.   private static int zzUnpackAttribute(String packed, int offset, int [] result) {
  164.     int i = 0;       /* index in packed string  */
  165.     int j = offset;  /* index in unpacked array */
  166.     int l = packed.length();
  167.     while (i < l) {
  168.       int count = packed.charAt(i++);
  169.       int value = packed.charAt(i++);
  170.       do result[j++] = value; while (--count > 0);
  171.     }
  172.     return j;
  173.   }
  174.  
  175.   /** the input device */
  176.   private java.io.Reader zzReader;
  177.  
  178.   /** the current state of the DFA */
  179.   private int zzState;
  180.  
  181.   /** the current lexical state */
  182.   private int zzLexicalState = YYINITIAL;
  183.  
  184.   /** this buffer contains the current text to be matched and is
  185.       the source of the yytext() string */
  186.   private char zzBuffer[] = new char[ZZ_BUFFERSIZE];
  187.  
  188.   /** the textposition at the last accepting state */
  189.   private int zzMarkedPos;
  190.  
  191.   /** the current text position in the buffer */
  192.   private int zzCurrentPos;
  193.  
  194.   /** startRead marks the beginning of the yytext() string in the buffer */
  195.   private int zzStartRead;
  196.  
  197.   /** endRead marks the last character in the buffer, that has been read
  198.       from input */
  199.   private int zzEndRead;
  200.  
  201.   /** number of newlines encountered up to the start of the matched text */
  202.   private int yyline;
  203.  
  204.   /** the number of characters up to the start of the matched text */
  205.   private int yychar;
  206.  
  207.   /**
  208.    * the number of characters from the last newline up to the start of the
  209.    * matched text
  210.    */
  211.   private int yycolumn;
  212.  
  213.   /**
  214.    * zzAtBOL == true <=> the scanner is currently at the beginning of a line
  215.    */
  216.   private boolean zzAtBOL = true;
  217.  
  218.   /** zzAtEOF == true <=> the scanner is at the EOF */
  219.   private boolean zzAtEOF;
  220.  
  221.   /** denotes if the user-EOF-code has already been executed */
  222.   private boolean zzEOFDone;
  223.  
  224.   /* user code: */
  225.     double s = 0;
  226.  
  227.     public static void main(String args[]) throws IOException {
  228.         Summe scanner = new Summe(System.in);
  229.         scanner.yylex();
  230.         System.out.println("Summe = " + scanner.s);
  231.     }
  232.  
  233.  
  234.  
  235.   /**
  236.    * Creates a new scanner
  237.    * There is also a java.io.InputStream version of this constructor.
  238.    *
  239.    * @param   in  the java.io.Reader to read input from.
  240.    */
  241.   Summe(java.io.Reader in) {
  242.     this.zzReader = in;
  243.   }
  244.  
  245.   /**
  246.    * Creates a new scanner.
  247.    * There is also java.io.Reader version of this constructor.
  248.    *
  249.    * @param   in  the java.io.Inputstream to read input from.
  250.    */
  251.   Summe(java.io.InputStream in) {
  252.     this(new java.io.InputStreamReader(in));
  253.   }
  254.  
  255.   /**
  256.    * Unpacks the compressed character translation table.
  257.    *
  258.    * @param packed   the packed character translation table
  259.    * @return         the unpacked character translation table
  260.    */
  261.   private static char [] zzUnpackCMap(String packed) {
  262.     char [] map = new char[0x10000];
  263.     int i = 0;  /* index in packed string  */
  264.     int j = 0;  /* index in unpacked array */
  265.     while (i < 34) {
  266.       int  count = packed.charAt(i++);
  267.       char value = packed.charAt(i++);
  268.       do map[j++] = value; while (--count > 0);
  269.     }
  270.     return map;
  271.   }
  272.  
  273.  
  274.   /**
  275.    * Refills the input buffer.
  276.    *
  277.    * @return      <code>false</code>, iff there was new input.
  278.    *
  279.    * @exception   java.io.IOException  if any I/O-Error occurs
  280.    */
  281.   private boolean zzRefill() throws java.io.IOException {
  282.  
  283.     /* first: make room (if you can) */
  284.     if (zzStartRead > 0) {
  285.       System.arraycopy(zzBuffer, zzStartRead,
  286.                        zzBuffer, 0,
  287.                        zzEndRead-zzStartRead);
  288.  
  289.       /* translate stored positions */
  290.       zzEndRead-= zzStartRead;
  291.       zzCurrentPos-= zzStartRead;
  292.       zzMarkedPos-= zzStartRead;
  293.       zzStartRead = 0;
  294.     }
  295.  
  296.     /* is the buffer big enough? */
  297.     if (zzCurrentPos >= zzBuffer.length) {
  298.       /* if not: blow it up */
  299.       char newBuffer[] = new char[zzCurrentPos*2];
  300.       System.arraycopy(zzBuffer, 0, newBuffer, 0, zzBuffer.length);
  301.       zzBuffer = newBuffer;
  302.     }
  303.  
  304.     /* finally: fill the buffer with new input */
  305.     int numRead = zzReader.read(zzBuffer, zzEndRead,
  306.                                             zzBuffer.length-zzEndRead);
  307.  
  308.     if (numRead > 0) {
  309.       zzEndRead+= numRead;
  310.       return false;
  311.     }
  312.     // unlikely but not impossible: read 0 characters, but not at end of stream    
  313.     if (numRead == 0) {
  314.       int c = zzReader.read();
  315.       if (c == -1) {
  316.         return true;
  317.       } else {
  318.         zzBuffer[zzEndRead++] = (char) c;
  319.         return false;
  320.       }    
  321.     }
  322.  
  323.     // numRead < 0
  324.     return true;
  325.   }
  326.  
  327.    
  328.   /**
  329.    * Closes the input stream.
  330.    */
  331.   public final void yyclose() throws java.io.IOException {
  332.     zzAtEOF = true;            /* indicate end of file */
  333.     zzEndRead = zzStartRead;  /* invalidate buffer    */
  334.  
  335.     if (zzReader != null)
  336.       zzReader.close();
  337.   }
  338.  
  339.  
  340.   /**
  341.    * Resets the scanner to read from a new input stream.
  342.    * Does not close the old reader.
  343.    *
  344.    * All internal variables are reset, the old input stream
  345.    * <b>cannot</b> be reused (internal buffer is discarded and lost).
  346.    * Lexical state is set to <tt>ZZ_INITIAL</tt>.
  347.    *
  348.    * @param reader   the new input stream
  349.    */
  350.   public final void yyreset(java.io.Reader reader) {
  351.     zzReader = reader;
  352.     zzAtBOL  = true;
  353.     zzAtEOF  = false;
  354.     zzEOFDone = false;
  355.     zzEndRead = zzStartRead = 0;
  356.     zzCurrentPos = zzMarkedPos = 0;
  357.     yyline = yychar = yycolumn = 0;
  358.     zzLexicalState = YYINITIAL;
  359.   }
  360.  
  361.  
  362.   /**
  363.    * Returns the current lexical state.
  364.    */
  365.   public final int yystate() {
  366.     return zzLexicalState;
  367.   }
  368.  
  369.  
  370.   /**
  371.    * Enters a new lexical state
  372.    *
  373.    * @param newState the new lexical state
  374.    */
  375.   public final void yybegin(int newState) {
  376.     zzLexicalState = newState;
  377.   }
  378.  
  379.  
  380.   /**
  381.    * Returns the text matched by the current regular expression.
  382.    */
  383.   public final String yytext() {
  384.     return new String( zzBuffer, zzStartRead, zzMarkedPos-zzStartRead );
  385.   }
  386.  
  387.  
  388.   /**
  389.    * Returns the character at position <tt>pos</tt> from the
  390.    * matched text.
  391.    *
  392.    * It is equivalent to yytext().charAt(pos), but faster
  393.    *
  394.    * @param pos the position of the character to fetch.
  395.    *            A value from 0 to yylength()-1.
  396.    *
  397.    * @return the character at position pos
  398.    */
  399.   public final char yycharat(int pos) {
  400.     return zzBuffer[zzStartRead+pos];
  401.   }
  402.  
  403.  
  404.   /**
  405.    * Returns the length of the matched text region.
  406.    */
  407.   public final int yylength() {
  408.     return zzMarkedPos-zzStartRead;
  409.   }
  410.  
  411.  
  412.   /**
  413.    * Reports an error that occured while scanning.
  414.    *
  415.    * In a wellformed scanner (no or only correct usage of
  416.    * yypushback(int) and a match-all fallback rule) this method
  417.    * will only be called with things that "Can't Possibly Happen".
  418.    * If this method is called, something is seriously wrong
  419.    * (e.g. a JFlex bug producing a faulty scanner etc.).
  420.    *
  421.    * Usual syntax/scanner level error handling should be done
  422.    * in error fallback rules.
  423.    *
  424.    * @param   errorCode  the code of the errormessage to display
  425.    */
  426.   private void zzScanError(int errorCode) {
  427.     String message;
  428.     try {
  429.       message = ZZ_ERROR_MSG[errorCode];
  430.     }
  431.     catch (ArrayIndexOutOfBoundsException e) {
  432.       message = ZZ_ERROR_MSG[ZZ_UNKNOWN_ERROR];
  433.     }
  434.  
  435.     throw new Error(message);
  436.   }
  437.  
  438.  
  439.   /**
  440.    * Pushes the specified amount of characters back into the input stream.
  441.    *
  442.    * They will be read again by then next call of the scanning method
  443.    *
  444.    * @param number  the number of characters to be read again.
  445.    *                This number must not be greater than yylength()!
  446.    */
  447.   public void yypushback(int number)  {
  448.     if ( number > yylength() )
  449.       zzScanError(ZZ_PUSHBACK_2BIG);
  450.  
  451.     zzMarkedPos -= number;
  452.   }
  453.  
  454.  
  455.   /**
  456.    * Resumes scanning until the next regular expression is matched,
  457.    * the end of input is encountered or an I/O-Error occurs.
  458.    *
  459.    * @return      the next token
  460.    * @exception   java.io.IOException  if any I/O-Error occurs
  461.    */
  462.   public Double yylex() throws java.io.IOException {
  463.     int zzInput;
  464.     int zzAction;
  465.  
  466.     // cached fields:
  467.     int zzCurrentPosL;
  468.     int zzMarkedPosL;
  469.     int zzEndReadL = zzEndRead;
  470.     char [] zzBufferL = zzBuffer;
  471.     char [] zzCMapL = ZZ_CMAP;
  472.  
  473.     int [] zzTransL = ZZ_TRANS;
  474.     int [] zzRowMapL = ZZ_ROWMAP;
  475.     int [] zzAttrL = ZZ_ATTRIBUTE;
  476.  
  477.     while (true) {
  478.       zzMarkedPosL = zzMarkedPos;
  479.  
  480.       zzAction = -1;
  481.  
  482.       zzCurrentPosL = zzCurrentPos = zzStartRead = zzMarkedPosL;
  483.  
  484.       zzState = ZZ_LEXSTATE[zzLexicalState];
  485.  
  486.  
  487.       zzForAction: {
  488.         while (true) {
  489.    
  490.           if (zzCurrentPosL < zzEndReadL)
  491.             zzInput = zzBufferL[zzCurrentPosL++];
  492.           else if (zzAtEOF) {
  493.             zzInput = YYEOF;
  494.             break zzForAction;
  495.           }
  496.           else {
  497.             // store back cached positions
  498.             zzCurrentPos  = zzCurrentPosL;
  499.             zzMarkedPos   = zzMarkedPosL;
  500.             boolean eof = zzRefill();
  501.             // get translated positions and possibly new buffer
  502.             zzCurrentPosL  = zzCurrentPos;
  503.             zzMarkedPosL   = zzMarkedPos;
  504.             zzBufferL      = zzBuffer;
  505.             zzEndReadL     = zzEndRead;
  506.             if (eof) {
  507.               zzInput = YYEOF;
  508.               break zzForAction;
  509.             }
  510.             else {
  511.               zzInput = zzBufferL[zzCurrentPosL++];
  512.             }
  513.           }
  514.           int zzNext = zzTransL[ zzRowMapL[zzState] + zzCMapL[zzInput] ];
  515.           if (zzNext == -1) break zzForAction;
  516.           zzState = zzNext;
  517.  
  518.           int zzAttributes = zzAttrL[zzState];
  519.           if ( (zzAttributes & 1) == 1 ) {
  520.             zzAction = zzState;
  521.             zzMarkedPosL = zzCurrentPosL;
  522.             if ( (zzAttributes & 8) == 8 ) break zzForAction;
  523.           }
  524.  
  525.         }
  526.       }
  527.  
  528.       // store back cached position
  529.       zzMarkedPos = zzMarkedPosL;
  530.  
  531.       switch (zzAction < 0 ? zzAction : ZZ_ACTION[zzAction]) {
  532.         case 3:
  533.           { /* Ignorieren */
  534.           }
  535.         case 4: break;
  536.         case 2:
  537.           { System.out.println("Zahl " + yytext());
  538.           s = s + Double.parseDouble(yytext());
  539.           }
  540.         case 5: break;
  541.         case 1:
  542.           { throw new RuntimeException("Unerlaubtes Zeichen \"" + yytext() + "\".");
  543.           }
  544.         case 6: break;
  545.         default:
  546.           if (zzInput == YYEOF && zzStartRead == zzCurrentPos) {
  547.             zzAtEOF = true;
  548.             return null;
  549.           }
  550.           else {
  551.             zzScanError(ZZ_NO_MATCH);
  552.           }
  553.       }
  554.     }
  555.   }
  556.  
  557.  
  558. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement