10 - Interpreter, Visitor

Stáhněte si a přidejte do projektu knihovnu Guava: jar

Přečtěte si o Immutable Collections

Úloha na cvičení

1. Do pripraveneho programu doimplementujte vyrazy (operatory):

  • IntList: konstantni seznam,
  • VarList: promenna,
  • Remove: odstrani vsechny vyskyty element v podvyrazu, pr: R([1, 2, 3, 2], 2) → [1, 3], zachovava puvodni poradi,
  • Concatenate: spoji dva seznamy, pr:C([1,2,3,4],[5,6,7]) → [1,2,3,4,5,6,7],
  • Unique: pro kazdy prvek vrati jen jeho prvni vyskyt, pr: U ([3, 2, 1, 2, 2, 1]) → [3, 2, 1], zachovava poradi,

  • Pouzijte vzor interpreter.
  • Vysledkem vyhodnoceni bude: ImmutableList<Integer>.

2. Pridejte podporu pro vzor visitor a naimplementujte tridu PrintListExpressionVisitor.

  • Pouzijte prefixovou notaci. Pro nazvy neterminalnich operatoru staci prvni pismeno jejich nazvu.
  • Pozor na zavorky. Pr: spojeni seznamu x a [1,2,3] vytisknete jako C (x, [1, 2, 3]).

3. Naimplementujte visitor (SimplifyListExpressionVisitor), ktery bude schopen symbolicky zjednodusit vyrazy podle nasledujiciho pravidla:

  • Pokud jsou vsichni potomci konstantnimi seznamy, nahradi rodicovsky uzel konstantnim seznamem s vysledkem vyhodnoceni.

Operator instanceof je pripustny. Nemodifikujte puvodni vyraz.

Minimalizujte opakovani kodu.

  Prevod ImmutableList -> ArrayList:
      List<Integer> list = new ArrayList<>(immutableList);
  Prevod List -> ImmutableList:
      ImmutableList<Integer> immutableList = ImmutableList.copyOf(list);
  Odstraneni vsech vyskytu prvku element ze seznamu:
      list.removeAll(Collections.singleton(element));
  Seznamy typu List<Integer> muzete tisknout primo pomoci System.out.print(). 

interface ListExpression {
 
}
 
class IntList implements ListExpression {
    protected final ImmutableList<Integer> list;
 
    private IntList(ImmutableList<Integer> list) {
        this.list = list;
    }
 
    public static IntList empty() {
        return new IntList(ImmutableList.<Integer>of());
    }
 
    public static IntList of(int value) {
        return new IntList(ImmutableList.of(value));
    }
 
    public static IntList of(ImmutableList<Integer> list) {
        return new IntList(list);
    }
}
 
 
class VarList implements ListExpression {
    protected final String name;
 
    VarList(String name) {
        this.name = name;
    }
}
 
class Remove implements ListExpression {
    protected final ListExpression sub;
    protected int element;
 
    Remove(ListExpression sub, int element) {
        this.sub = sub;
        this.element = element;
    }
}
 
class Concatenate implements ListExpression {
    protected final ListExpression left;
    protected final ListExpression right;
 
    Concatenate(ListExpression left, ListExpression right) {
        this.left = left;
        this.right = right;
    }
}
 
class Unique implements ListExpression {
    protected final ListExpression sub;
 
    Unique(ListExpression sub) {
        this.sub = sub;
    }
}
 
public class ExpressionExample {
    public static void main(String[] args) {
 
    }
} 

courses/b6b36omo/labs/lab10.txt · Last modified: 2018/10/03 11:13 (external edit)