Initial Commit
[packages] / xemacs-packages / semantic / wisent / wisent-calc.wy
1 ;;; wisent-calc.wy --- Grammar of the infix notation calculator
2
3 ;; Copyright (C) 2002, 2003 David Ponce
4
5 ;; Author: David Ponce <david@dponce.com>
6 ;; Maintainer: David Ponce <david@dponce.com>
7 ;; Created: 19 Feb 2002
8 ;; Keywords: syntax
9 ;; X-RCS: $Id: wisent-calc.wy,v 1.1 2007-11-26 15:12:29 michaels Exp $
10
11 ;; This file is not part of GNU Emacs.
12
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
17
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 ;; General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program; see the file COPYING.  If not, write to
25 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27
28 ;;; Commentary:
29 ;;
30 ;; This is a port of the Bison 1.28d Infix Calc sample program to the
31 ;; Elisp LALR parser Wisent.  It illustrates usage of operator
32 ;; precedence and contextual precedence.  Implementation is in the
33 ;; file wisent-calc.el.
34
35 ;;; History:
36 ;; 
37
38 ;;%package wisent-calc-wy
39
40 %token <number> NUM
41
42 %nonassoc '=' ;; comparison
43 %left '-' '+'
44 %left '*' '/'
45 %left NEG     ;; negation--unary minus
46 %right '^'    ;; exponentiation
47
48 %%
49
50 input:
51     line
52   | input line
53     (format "%s %s" $1 $2)
54   ;
55
56 line:
57     ';'
58     {";"}
59   | exp ';'
60     (format "%s;" $1)
61   | error ';'
62     {"Error;"}
63   ;
64
65 exp:
66     NUM
67     (string-to-number $1)
68   | exp '=' exp
69     (= $1 $3)
70   | exp '+' exp
71     (+ $1 $3)
72   | exp '-' exp
73     (- $1 $3)
74   | exp '*' exp
75     (* $1 $3)
76   | exp '/' exp
77     (/ $1 $3)
78   | '-' exp %prec NEG
79     (- $2)
80   | exp '^' exp
81     (expt $1 $3)
82   | '(' exp ')'
83     {$2}
84   ;
85
86 %%
87
88 (defun wisent-calc-setup-parser ()
89   "Setup buffer for parse."
90   (wisent-calc-wy--install-parser)
91   (setq semantic-number-expression
92         (concat "\\([0-9]+\\([.][0-9]*\\)?\\([eE][-+]?[0-9]+\\)?"
93                 "\\|[.][0-9]+\\([eE][-+]?[0-9]+\\)?\\)")
94         semantic-lex-analyzer 'wisent-calc-lexer
95         semantic-lex-depth nil
96         semantic-lex-syntax-modifications
97         '((?\; ".") (?\= ".") (?\+ ".")
98           (?\- ".") (?\* ".") (?\/ ".")
99           (?\^ ".") (?\( ".") (?\) ".")
100           )
101         )
102   )
103
104 ;;; wisent-calc.wy ends here