Calculator.grm

// FILE. . . . . /home/hak/ilt/src/ilog/language/jaccapps/calc/Calculator.grm
// EDIT BY . . . Hassan Ait-Kaci
// ON MACHINE. . 4j4zn71
// STARTED ON. . Sun Nov 16 06:32:25 2008

// Last modified on Mon Dec 15 07:42:00 2008 by hak



This is a grammar for a simple arithmetic calculator.



%import java.util.HashMap;

%access public

%start Session

%root  Expression
%root  Definition

%token NUMBER IDENTIFIER
%left '+' '-'
%left '*' '/'
%right UMINUS

%{
  HashMap defs = new HashMap();
%}

%nodeprefix "Calculator"
%nodesuffix ""

%nodeclass public Definition implements Cloneable
{
  private String message;

  public String getMessage()
    {
      return message;
    }

  public void setMessage(String msg)
    {
      message = msg;
    }
}

%%

Session
        : Actions Exit
        ;

Actions
  	: /* empty */
        | Actions Action
        ;



An action is either an expression, a definition, or an error, followed by a semicolon.


Action
  	: Expression
        {
          currentNode().show(); // show the current parse tree
          if (!Double.isNaN($1.nvalue())) System.out.println($1.nvalue());
          CalculatorTokenizer.prompt();
        }
          ';'
        | Definition
        {
          currentNode().show(); // show the current parse tree
          System.out.println($1.getMessage());
          CalculatorTokenizer.prompt();
        }
          ';'
	| error
	{
	  errorManager().reportErrors(true);
          CalculatorTokenizer.prompt();
	} ';'
        ;




An expression is one of the following forms:


Expression
        :
          

An identifier is an expression.


          IDENTIFIER
        {
          Double value = (Double)defs.get($1.svalue());
          if (value == null)
            System.out.println("*** Undefined identifier: "+$1.svalue());
          else
            $$.setNvalue(value.doubleValue());
        }
        |
         

A number is an expression.


          NUMBER
        |
	 

An addition is an expression.


          Expression '+' Expression
        {
          $$.setNvalue($1.nvalue() + $3.nvalue());
        }
        | 
	 

A subtraction is an expression.


          Expression '-' Expression
        {
          $$.setNvalue($1.nvalue() - $3.nvalue());
        }
        | 
	 

A multiplication is an expression.


          Expression '*' Expression
        {
          $$.setNvalue($1.nvalue() * $3.nvalue());
        }
        | 
	 

A division is an expression.


          Expression '/' Expression
        {
          $$.setNvalue($1.nvalue() / $3.nvalue());
        }
        | 
	 

A negative expression is an expression.


          '-' Expression        %prec UMINUS
        {
          $$.setNvalue(- $2.nvalue());
        }
        | 
	 

A parenthesized expression is an expression.


          '(' Expression ')'
        {
          $$.setNvalue($2.nvalue());
        }
        ;




A definition stores the value of an expression as an identifier.


Definition
        : IDENTIFIER '=' Expression
        {
          defs.put($1.svalue(),new Double($3.nvalue()));
          $$.setNvalue($3.nvalue());
          $$.setSvalue($1.svalue());
          $$.setMessage("*** Defined "+$$.svalue()+" ("+$$.nvalue()+")");
        }
        ;



Action for exiting the calculator.


Exit
  	: 'exit'
        {
	  System.out.println("*** Bye bye!...");
	  System.exit(0);
	} ';'
	;




This file was generated on Mon Dec 15 15:48:38 PST 2008 from file Calculator.grm
by the ilog.language.tools.Hilite Java tool written by Hassan Aït-Kaci