/* * JFLAP - Formal Languages and Automata Package * * * Susan H. Rodger * Computer Science Department * Duke University * August 27, 2009 * Copyright (c) 2002-2009 * All rights reserved. * JFLAP is open source software. Please see the LICENSE for terms. * */ package gui.action; import grammar.CNFConverter; import grammar.Grammar; import grammar.Production; import grammar.ProductionChecker; import grammar.UnrestrictedGrammar; import gui.environment.EnvironmentFrame; import gui.environment.GrammarEnvironment; import gui.environment.Universe; import java.awt.event.ActionEvent; import javax.swing.JOptionPane; import debug.EDebug; /** * Action for testing grammar to see what type of grammar it is. * @author Kyung Min (Jason) Lee * */ public class GrammarTypeTestAction extends GrammarAction { /** The grammar environment. */ private GrammarEnvironment environment; /** The frame for the grammar environment. */ private EnvironmentFrame frame; /** * Instantiates a new GrammarTypeTestAction. * * @param environment * the grammar environment */ public GrammarTypeTestAction(GrammarEnvironment environment) { super("Test for Grammar Type", null); this.environment = environment; this.frame = Universe.frameForEnvironment(environment); } /** * Performs the action. */ public void actionPerformed(ActionEvent e) { Grammar g=environment.getGrammar(UnrestrictedGrammar.class); // EDebug.print(g == null); if (g == null) return; Production[] p=g.getProductions(); boolean isRegular=checkforLinearity(p); if (!isRegular) { if (!isContextFreeGrammar(p, g)) { checkForSpecialUnrestrictedGrammar(p); } } } /** * Check if the grammar is Unrestrcited Grammar from TM (through our conversion feature) * @param p Production to check for */ private void checkForSpecialUnrestrictedGrammar(Production[] p) { // primitive, but it's not that bad. //Check to see if production contains three special productions that indicates, it is converted from TM\ Production[] sp=new Production[3]; sp[0]=new Production("S", "V(==)S"); sp[1]=new Production("S", "SV(==)"); sp[2]=new Production("S", "T"); boolean[] count=new boolean[3]; for (int i=0; i