public static Grammar forProductions(String ... prods) {
        StringArrayList symbolTable;
        Grammar grammar;
        String s;
        String[] stringProd;
        int[] prod;
        int idx;

        grammar = new Grammar();
        symbolTable = grammar.symbolTable;
        idx = 0; // definite assignment
        for (int p = 0; p < prods.length; p++) {
            stringProd = Strings.toArray(Separator.SPACE.split(prods[p]));
            if (stringProd.length == 0) {
                throw new IllegalArgumentException();
            }
            prod = new int[stringProd.length];
            // count backwards to have the left-hand-side in idx when the loop has finished
            for (int ofs = prod.length - 1; ofs >= 0; ofs--) {
                s = stringProd[ofs];
                idx = symbolTable.indexOf(s);
                if (idx == -1) {
                    idx = symbolTable.size();
                    symbolTable.add(s);
                }
                prod[ofs] = idx;
            }
            if (p == 0) {
                grammar.start = idx;
            }
            grammar.addProduction(prod);
        }
        return grammar;
    }
public static Grammar forProductions ( String ... prods ) { StringArrayList symbolTable ; Grammar grammar ; String s ; String [ ] stringProd ; int [ ] prod ; int idx ; grammar = new Grammar ( ) ; symbolTable = grammar . symbolTable ; idx = 0 ; for ( int p = 0 ; p < prods . length ; p ++ ) { stringProd = Strings . toArray ( Separator . SPACE . split ( prods [ p ] ) ) ; if ( stringProd . length == 0 ) { throw new IllegalArgumentException ( ) ; } prod = new int [ stringProd . length ] ; for ( int ofs = prod . length - 1 ; ofs >= 0 ; ofs -- ) { s = stringProd [ ofs ] ; idx = symbolTable . indexOf ( s ) ; if ( idx == - 1 ) { idx = symbolTable . size ( ) ; symbolTable . add ( s ) ; } prod [ ofs ] = idx ; } if ( p == 0 ) { grammar . start = idx ; } grammar . addProduction ( prod ) ; } return grammar ; }
start symbol is set to the subject of the first production.