straightline/src/main/java/org/poormanscastle/studies/compilers/utils/grammartools/GrammarReader.java
/**
     * @param inputStream
     * @return Grammar for method call chaining, where applicable
     * @throws IOException
     */
@param inputStream
@return Grammar for method call chaining, where applicable
@throws IOException
@param inputStream @return Grammar for method call chaining where applicable @throws IOException
public static Grammar readGrammar(InputStream inputStream, Grammar grammar) throws IOException {
        int state = STATE_BEGIN;
        boolean firstProduction = true;
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        while ((line = reader.readLine()) != null) {
            switch (state) {
                case STATE_BEGIN:
                    if (TERMINALS_START.equalsIgnoreCase(line)) {
                        state = STATE_READ_TERMINAL_SYMBOLS;
                    }
                    break;
                case STATE_READ_TERMINAL_SYMBOLS:
                    if (TERMINALS_END.equalsIgnoreCase(line)) {
                        state = STATE_READ_TERMINAL_SYMBOLS_FINISHED;
                    } else if (!StringUtils.isBlank(line)) {
                        grammar.addTerminalSymbol(Symbol.createTerminalSymbol(line));
                    }
                    break;
                case STATE_READ_TERMINAL_SYMBOLS_FINISHED:
                    if (PRODUCTIONS_START.equalsIgnoreCase(line)) {
                        state = STATE_READ_PRODUCTIONS;
                    }
                    break;
                case STATE_READ_PRODUCTIONS:
                    if (PRODUCTIONS_END.equalsIgnoreCase(line)) {
                        state = STATE_READ_PRODUCTIONS_FINISHED;
                    } else if (!StringUtils.isBlank(line)) {
                        grammar.addProduction(new Production(line, firstProduction, grammar));
                    }
                    firstProduction = false;
                    break;
                case STATE_READ_PRODUCTIONS_FINISHED:
                    break;
                default:
                    throw new CompilerException("What the heck, what state is this machine in?");
            }
        }
        return grammar.initialize();
    }
public static Grammar readGrammar ( InputStream inputStream , Grammar grammar ) throws IOException { int state = STATE_BEGIN ; boolean firstProduction = true ; BufferedReader reader = new BufferedReader ( new InputStreamReader ( inputStream ) ) ; String line ; while ( ( line = reader . readLine ( ) ) != null ) { switch ( state ) { case STATE_BEGIN : if ( TERMINALS_START . equalsIgnoreCase ( line ) ) { state = STATE_READ_TERMINAL_SYMBOLS ; } break ; case STATE_READ_TERMINAL_SYMBOLS : if ( TERMINALS_END . equalsIgnoreCase ( line ) ) { state = STATE_READ_TERMINAL_SYMBOLS_FINISHED ; } else if ( ! StringUtils . isBlank ( line ) ) { grammar . addTerminalSymbol ( Symbol . createTerminalSymbol ( line ) ) ; } break ; case STATE_READ_TERMINAL_SYMBOLS_FINISHED : if ( PRODUCTIONS_START . equalsIgnoreCase ( line ) ) { state = STATE_READ_PRODUCTIONS ; } break ; case STATE_READ_PRODUCTIONS : if ( PRODUCTIONS_END . equalsIgnoreCase ( line ) ) { state = STATE_READ_PRODUCTIONS_FINISHED ; } else if ( ! StringUtils . isBlank ( line ) ) { grammar . addProduction ( new Production ( line , firstProduction , grammar ) ) ; } firstProduction = false ; break ; case STATE_READ_PRODUCTIONS_FINISHED : break ; default : throw new CompilerException ( "What the heck, what state is this machine in?" ) ; } } return grammar . initialize ( ) ; }
@param inputStream
 @return Grammar for method call chaining, where applicable
 @throws IOException
@param inputStream @return Grammar for method call chaining where applicable @throws IOException
// here some checking could be done whether the grammar makes sense // or something expected is missing.