PartialCalculator.java

// FILE. . . . . /home/hak/hlt/src/hlt/language/jaccapps/calc/sources/PartialCalculator.java
// EDIT BY . . . Hassan Ait-Kaci
// ON MACHINE. . Hp-Dv7
// STARTED ON. . Sun Oct 14 20:56:42 2012



This illustrates how the parser generated from the Calculator.grm grammar allows parsing of partial subgrammars (in this case definitions and expressions) as sepcified by the declarations: %root Expression %root Definition instead of the just the full grammar rooted in the initial non-terminal symbol Session, which stands for an interactive session where several expressions or definitions may be successively parsed and interpreted.

This is a standalone application calling the generated partial parser methods parseDefinition and parseExpresion on given strings and printing the result of the parse for each string.

Author:  Hassan Aït-Kaci
Copyright:  © by the author
Version:  Last modified on Sun Oct 28 01:21:14 2012 by hak




import hlt.language.syntax.GenericParser;

public class PartialCalculator
{
  public static void main (String args[])
    {
      try
        {
          CalculatorTokenizer t;

          t = new CalculatorTokenizer("");

          try
            {
              CalculatorParser p = new CalculatorParser(t);

              String input;

              p.parseTreeType = p.COMPACT_TREE;
//            p.toggleTrace();
              
              System.out.println("\n---------------------------------------------");

              p.parseExpression(input="1+2");
              System.out.println("\nvalue() of "+input+" is "+p.currentNode().nvalue());
              System.out.println("\nPARSE TREE:\n");
              p.currentNode().show();

              System.out.println("\n---------------------------------------------");

              p.parseDefinition("pi = 22/7");
              System.out.println("\nDefining pi as "+p.currentNode().nvalue());
              System.out.println("\nPARSE TREE:\n");
              p.currentNode().show();

              System.out.println("\n---------------------------------------------");
              
              p.parseExpression("2 * pi");
              System.out.println("\nTwice this pi is "+p.currentNode().nvalue());
              System.out.println("\nPARSE TREE:\n");
              p.currentNode().show();

              System.out.println("\n---------------------------------------------");
            }
          catch (Exception e)
            {
              System.out.println("*** Parsing error: "+e);
            }
        }
      catch (Exception e)
        {
          System.out.println("*** Tokenizing error: "+e);
        }
    }
}


This file was generated on Tue Oct 30 12:38:30 PDT 2012 from file PartialCalculator.java
by the hlt.language.tools.Hilite Java tool written by Hassan Aït-Kaci