Package net.sourceforge.chaperon.test

Source Code of net.sourceforge.chaperon.test.AutomatonTestCase

/*
*  Copyright (C) Chaperon. All rights reserved.
*  -------------------------------------------------------------------------
*  This software is published under the terms of the Apache Software License
*  version 1.1, a copy of which has been included  with this distribution in
*  the LICENSE file.
*/

package net.sourceforge.chaperon.test;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import net.sourceforge.chaperon.build.Automaton;
import net.sourceforge.chaperon.model.grammar.Grammar;
import net.sourceforge.chaperon.model.grammar.GrammarFactory;
import net.sourceforge.chaperon.model.grammar.Production;
import net.sourceforge.chaperon.model.symbol.Nonterminal;
import net.sourceforge.chaperon.model.symbol.Terminal;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import javax.xml.parsers.SAXParserFactory;

public class AutomatonTestCase extends TestCase
{
  private Terminal plus;
  private Terminal mult;
  private Terminal bopen;
  private Terminal bclose;
  private Terminal id;
  private Nonterminal E;
  private Nonterminal T;
  private Nonterminal F;
  private Grammar grammar;

  public AutomatonTestCase(String name)
  {
    super(name);
  }

  public void setUp()
  {
    plus = new Terminal("+");
    mult = new Terminal("*");
    bopen = new Terminal("(");
    bclose = new Terminal(")");
    id = new Terminal("id");

    E = new Nonterminal("E");
    T = new Nonterminal("T");
    F = new Nonterminal("F");

    grammar = new Grammar();

    // E -> E + T
    Production production = new Production(E);
    production.getDefinition().addSymbol(E);
    production.getDefinition().addSymbol(plus);
    production.getDefinition().addSymbol(T);
    grammar.addProduction(production);

    // E -> T
    production = new Production(E);
    production.getDefinition().addSymbol(T);
    grammar.addProduction(production);

    // T -> T * F
    production = new Production(T);
    production.getDefinition().addSymbol(T);
    production.getDefinition().addSymbol(mult);
    production.getDefinition().addSymbol(F);
    grammar.addProduction(production);

    // T -> F
    production = new Production(T);
    production.getDefinition().addSymbol(F);
    grammar.addProduction(production);

    // F -> ( E )
    production = new Production(F);
    production.getDefinition().addSymbol(bopen);
    production.getDefinition().addSymbol(E);
    production.getDefinition().addSymbol(bclose);
    grammar.addProduction(production);

    production = new Production(F);
    production.getDefinition().addSymbol(id);
    grammar.addProduction(production);

    grammar.setStartSymbol(E);
  }

  public void testConstruction()
  {
    System.out.println(grammar);

    //FirstSetCollection firstsets = new FirstSetCollection(grammar);
    Automaton collection = new Automaton(grammar,  /*firstsets,*/
                                         null);

    System.out.println(collection);
  }

  public void testBigGrammar() throws Exception
  {
    SAXParserFactory factory = SAXParserFactory.newInstance();

    factory.setNamespaceAware(true);

    XMLReader parser = factory.newSAXParser().getXMLReader();

    GrammarFactory handler = new GrammarFactory();
    parser.setContentHandler(handler);
    parser.parse(new InputSource(getClass().getResourceAsStream("java.xgrm")));

    Grammar grammar = handler.getGrammar();

    Automaton collection = new Automaton(grammar,  /*firstsets,*/
                                         null);

    System.out.println(collection);
  }

  public static Test suite()
  {
    return new TestSuite(AutomatonTestCase.class);
  }
}
TOP

Related Classes of net.sourceforge.chaperon.test.AutomatonTestCase

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.