
/**
 * Grammar as list of Productions (Grammar rules)
 * 
 * @author Nobo Komagata 
 * @version 5/30/03
 */
import java.util.*;

/*
   Grammar File Syntax

   // comment till EOL

   X {(Y Z)} -> Y Z
   U {sem} -> terminal

   Notes:
   1. Nonterminals on the RHS must be unique.  To distinguish them, use 
      "_#" where # is a numeral.
   2. The grammar written in grammarArea must be correctly formatted.  No 
      error checking is provided.  To make verify, use Java console.

*/
    
public class Grammar
{
    public Productions productions = new Productions();

    /**
     * Constructor for objects of class Grammar
     */
    public Grammar(String grammar) {
        StringTokenizer lines = new StringTokenizer(grammar, "\n");

        while (lines.countTokens() > 0) {
            String line = lines.nextToken();
 
            if (line.indexOf("//") == 0)
                continue;

            if (!(line.indexOf("{") >= 0 && line.indexOf("{") < line.indexOf("}")))
                continue;
      
            productions.add(new Production(line));
        }
        
        show();  // display in the Java console for grammar debugging
    }

    /**
     * Display the grammar text in the Java console
     * 
     * @param       None
     * @return      void 
     */
    public void show() {
        System.out.println("\n// Current grammar");

        for (int i = 0; i < productions.length(); ++i) {
            System.out.println(productions.at(i).toString());
        }
    }
}

