/** This class tests the Grammar and Symbol classes of Assignment 1, CS 152 */ import java.io.*; import java.util.*; public class A1 { public static final Symbol s = new Symbol("s"); public static final Symbol np = new Symbol("np"); public static final Symbol vp = new Symbol("vp"); public static final Symbol pp = new Symbol("pp"); public static final Symbol det = new Symbol("det"); public static final Symbol n = new Symbol("n"); public static final Symbol v = new Symbol("v"); public static final Symbol p = new Symbol("p"); public static final Symbol x = new Symbol("x"); public static final Symbol y = new Symbol("y"); public static final Symbol e = new Symbol("e"); public static final Symbol t = new Symbol("t"); public static final Symbol f = new Symbol("f"); public static final Symbol plus = new Symbol("+"); public static final Symbol times = new Symbol("*"); public static final Symbol lt = new Symbol("<"); public static final Symbol gt = new Symbol(">"); public static void main(String[] args) { // This is a grammar for a fragment of English // The rules are: // S -> NP VP // NP -> Det N | Det N PP // PP -> P NP // VP -> V | V NP | V PP // // Note that the lexical categories P, V, N, and Det // are treated as terminal symbols Grammar g0 = new Grammar(s); g0.addNonterminal(np); g0.addNonterminal(vp); g0.addNonterminal(pp); g0.addRule(s,np,vp); g0.addRule(np,det,n); g0.addRule(np,det,n,pp); g0.addRule(vp,v); g0.addRule(vp,v,np); g0.addRule(vp,v,pp); g0.addRule(vp,v,np,pp); g0.addRule(pp,p,np); List l0 = g0.getYields(s,3); System.out.println(l0); System.out.println(); l0=g0.getYields(s,4); System.out.println(l0); System.out.println(); l0=g0.getYields(s,5); System.out.println(l0); System.out.println(); l0=g0.getYields(s,6); System.out.println(l0); System.out.println(); l0=g0.getYields(s,7); System.out.println(l0); System.out.println(); l0=g0.getYields(vp,6); System.out.println(l0); System.out.println(); l0=g0.getYields(s,8); System.out.println(l0); System.out.println(); // This is a simple grammar with rules // S -> x | x S S // Note that the symbol s is already defined Grammar g1 = new Grammar(s); g1.addRule(s,x,s,s); g1.addRule(s,x); l0=g1.getYields(s,3); System.out.println(l0); System.out.println(); l0=g1.getYields(s,5); System.out.println(l0); System.out.println(); l0=g1.getYields(s,6); System.out.println(l0); System.out.println(); l0=g1.getYields(s,7); System.out.println(l0); System.out.println(); l0=g1.getYields(vp,6); System.out.println(l0); System.out.println(); // This is a grammar for simple algebraic expressions. // Its rules are // E -> T | T + E // T -> F | F * T // F -> x | y | < E > // Note that the symbol x is already defined. Grammar g2 = new Grammar(e); g2.addNonterminal(t); g2.addNonterminal(f); g2.addRule(e,t); g2.addRule(e,t,plus,e); g2.addRule(t,f); g2.addRule(t,f,times,t); g2.addRule(f,x); g2.addRule(f,y); g2.addRule(f,lt,e,gt); l0=g2.getYields(e,1); System.out.println(l0); System.out.println(); l0=g2.getYields(e,3); System.out.println(l0); System.out.println(); l0=g2.getYields(e,4); System.out.println(l0); System.out.println(); l0=g2.getYields(e,5); System.out.println(l0); System.out.println(); System.out.println(l0.size()); System.out.println(); } }