source: other-projects/rsyntax-textarea/devel-packages/jflex-1.4.3/examples/byaccj/calc.y@ 25584

Last change on this file since 25584 was 25584, checked in by davidb, 12 years ago

Initial cut an a text edit area for GLI that supports color syntax highlighting

File size: 3.7 KB
Line 
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Copyright (C) 2001 Gerwin Klein <[email protected]> *
3 * All rights reserved. *
4 * *
5 * This is a modified version of the example from *
6 * http://www.lincom-asg.com/~rjamison/byacc/ *
7 * *
8 * Thanks to Larry Bell and Bob Jamison for suggestions and comments. *
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License. See the file *
12 * COPYRIGHT for more information. *
13 * *
14 * This program is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU General Public License for more details. *
18 * *
19 * You should have received a copy of the GNU General Public License along *
20 * with this program; if not, write to the Free Software Foundation, Inc., *
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
22 * *
23 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24
25%{
26 import java.io.*;
27%}
28
29%token NL /* newline */
30%token <dval> NUM /* a number */
31
32%type <dval> exp
33
34%left '-' '+'
35%left '*' '/'
36%left NEG /* negation--unary minus */
37%right '^' /* exponentiation */
38
39%%
40
41input: /* empty string */
42 | input line
43 ;
44
45line: NL { if (interactive) System.out.print("Expression: "); }
46 | exp NL { System.out.println(" = " + $1);
47 if (interactive) System.out.print("Expression: "); }
48 ;
49
50exp: NUM { $$ = $1; }
51 | exp '+' exp { $$ = $1 + $3; }
52 | exp '-' exp { $$ = $1 - $3; }
53 | exp '*' exp { $$ = $1 * $3; }
54 | exp '/' exp { $$ = $1 / $3; }
55 | '-' exp %prec NEG { $$ = -$2; }
56 | exp '^' exp { $$ = Math.pow($1, $3); }
57 | '(' exp ')' { $$ = $2; }
58 ;
59
60%%
61
62 private Yylex lexer;
63
64
65 private int yylex () {
66 int yyl_return = -1;
67 try {
68 yylval = new ParserVal(0);
69 yyl_return = lexer.yylex();
70 }
71 catch (IOException e) {
72 System.err.println("IO error :"+e);
73 }
74 return yyl_return;
75 }
76
77
78 public void yyerror (String error) {
79 System.err.println ("Error: " + error);
80 }
81
82
83 public Parser(Reader r) {
84 lexer = new Yylex(r, this);
85 }
86
87
88 static boolean interactive;
89
90 public static void main(String args[]) throws IOException {
91 System.out.println("BYACC/Java with JFlex Calculator Demo");
92
93 Parser yyparser;
94 if ( args.length > 0 ) {
95 // parse a file
96 yyparser = new Parser(new FileReader(args[0]));
97 }
98 else {
99 // interactive mode
100 System.out.println("[Quit with CTRL-D]");
101 System.out.print("Expression: ");
102 interactive = true;
103 yyparser = new Parser(new InputStreamReader(System.in));
104 }
105
106 yyparser.yyparse();
107
108 if (interactive) {
109 System.out.println();
110 System.out.println("Have a nice day");
111 }
112 }
Note: See TracBrowser for help on using the repository browser.