|
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
specified by the declarations:
%root Expression %root Definitioninstead of 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 stand-alone application calling the generated partial parser methods parseDefinition and parseExpresion on given strings and printing the result of the parse for each string. Here is the generated output.
|
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 Jan 29 12:24:47 CET 2013 from file PartialCalculator.java
by the hlt.language.tools.Hilite Java tool written by Hassan Aït-Kaci