Search in sources :

Example 6 with Grammar

use of suite.ebnf.Grammar in project suite by stupidsing.

the class ReduceHeadRecursion method getHeadRecursionForm.

private HeadRecursionForm getHeadRecursionForm(Grammar en0, String entity) {
    List<Grammar> empty = List.of();
    Grammar en = expand(en0);
    HeadRecursionForm hrf;
    if (en.type == GrammarType.AND___ && en.children.isEmpty())
        hrf = new HeadRecursionForm(empty, empty);
    else if (en.type == GrammarType.AND___) {
        HeadRecursionForm hrf0 = getHeadRecursionForm(en.children.get(0), entity);
        List<Grammar> tail = List_.right(en.children, 1);
        Fun<List<Grammar>, List<Grammar>> fun = list -> Read.from(list).map(en_ -> {
            List<Grammar> ens1 = new ArrayList<>();
            ens1.add(en_);
            ens1.addAll(tail);
            return new Grammar(GrammarType.AND___, ens1);
        }).toList();
        hrf = new HeadRecursionForm(fun.apply(hrf0.listb), fun.apply(hrf0.listc));
    } else if (en.type == GrammarType.NAMED_ && String_.equals(en.content, entity))
        hrf = new HeadRecursionForm(empty, List.of(new Grammar(GrammarType.AND___)));
    else if (en.type == GrammarType.OR____) {
        List<HeadRecursionForm> hrfs = Read.from(en.children).map(en_ -> getHeadRecursionForm(en_, entity)).toList();
        List<Grammar> listb = Read.from(hrfs).flatMap(hrf_ -> hrf_.listb).toList();
        List<Grammar> listc = Read.from(hrfs).flatMap(hrf_ -> hrf_.listc).toList();
        hrf = new HeadRecursionForm(listb, listc);
    } else
        hrf = new HeadRecursionForm(List.of(en), empty);
    return hrf;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Grammar(suite.ebnf.Grammar) Fun(suite.util.FunUtil.Fun)

Aggregations

Grammar (suite.ebnf.Grammar)6 List (java.util.List)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Ast (suite.ebnf.Ebnf.Ast)2 GrammarType (suite.ebnf.Grammar.GrammarType)2 IntIntPair (suite.primitive.adt.pair.IntIntPair)2 Read (suite.streamlet.Read)2 Fail (suite.util.Fail)2 Fun (suite.util.FunUtil.Fun)2 ArrayDeque (java.util.ArrayDeque)1 Deque (java.util.Deque)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Pair (suite.adt.pair.Pair)1 ExpectFun (suite.ebnf.topdown.Expect.ExpectFun)1 IList (suite.immutable.IList)1 LogUtil (suite.os.LogUtil)1