view tests/agcl/ffcalc/ffcm-a.syn @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children
line wrap: on
line source

{/*   FOUR FUNCTION CALCULATOR: FFCALC.SYN           */}

// -- CONFIGURATION SECTION ----------------------------
[
  default token type = double
  disregard white space
  lexeme { real}
  traditional engine
]

// -- FOUR FUNCTION CALCULATOR -------------------------
(void) calculator $
 -> [statement, '\n']..., eof

(void) calculation
 -> expression:x                      =printf("%g\n",x);
 -> name:n, '=', expression:x                         ={
                  printf("%c = %g\n",n+'A',value[n]=x);}
 -> error

expression
 -> term
 -> expression:x, '+', term:t                     = x+t;
 -> expression:x, '-', term:t                     = x-t;

term
 -> factor:f
 -> term:t, '*', factor:f                         = t*f;
 -> term:t, '/', factor:f                         = t/f;

factor
 -> identifier:n                             = value[n];
 -> real
 -> '(', expression:x, ')'                          = x;
 -> '-', factor:f                                  = -f;

identifier
 ->

// -- LEXICAL UNITS ------------------------------------
digit   = '0-9'
eof     = -1

(void) white space
 -> ' ' + '\t' + '\r' + '\f' + '\v'
 -> "/*", ~eof?..., "*/"

(int) name
 -> 'a-z' + 'A-Z':c                             = c-'A';

real
 -> integer part:i, '.', fraction part:f          = i+f;
 -> integer part, '.'?
 -> '.', fraction part:f                            = f;

integer part
 -> digit:d                                     = d-'0';
 -> integer part:x, digit:d              = 10*x + d-'0';

fraction part
 -> digit:d                                =(d-'0')/10.;
 -> digit:d, fraction part:f           =(d-'0' + f)/10.;

name
 -> letter: c           =n_chars = 0, name[n_chars++]=c;
 -> name, letter+digit: c            =name[n_chars++]=c;

{ /* -- EMBEDDED C ---------------------------------- */
  double value[64];                      /* registers */
  void main(void) {
    ffcalc();
  }
} // -- END OF EMBEDDED C ------------------------------