/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright (C) 2001 Gerwin Klein * * All rights reserved. * * * * This is a modified version of the example from * * http://www.lincom-asg.com/~rjamison/byacc/ * * * * Thanks to Larry Bell and Bob Jamison for suggestions and comments. * * * * License: BSD * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ %{ import java.io.*; %} %token NL /* newline */ %token NUM /* a number */ %token PLUS MINUS TIMES DIV /* operators */ %token LPAR RPAR /* parethesis */ %type exp %left MINUS PLUS %left TIMES DIV %left NEG /* negation--unary minus */ %% input: /* empty string */ | input line ; line: NL { if (interactive) System.out.print("Expression: "); } | exp NL { System.out.println(" = " + $1); if (interactive) System.out.print("Expression: "); } ; exp: NUM { $$ = $1; } | exp PLUS exp { $$ = $1 + $3; } | exp MINUS exp { $$ = $1 - $3; } | exp TIMES exp { $$ = $1 * $3; } | exp DIV exp { $$ = $1 / $3; } | MINUS exp %prec NEG { $$ = -$2; } | LPAR exp RPAR { $$ = $2; } ; %% private Yylex lexer; private int yylex () { int yyl_return = -1; try { yylval = new ParserVal(0); yyl_return = lexer.yylex(); } catch (IOException e) { System.err.println("IO error :"+e); } return yyl_return; } public void yyerror (String error) { System.err.println ("Error: " + error); } public Parser(Reader r) { lexer = new Yylex(r, this); } static boolean interactive; public static void main(String args[]) throws IOException { System.out.println("BYACC/Java with JFlex Calculator Demo"); Parser yyparser; if ( args.length > 0 ) { // parse a file yyparser = new Parser(new FileReader(args[0])); } else { // interactive mode System.out.println("[Quit with CTRL-D]"); System.out.print("Expression: "); interactive = true; yyparser = new Parser(new InputStreamReader(System.in)); } yyparser.yyparse(); if (interactive) { System.out.println(); System.out.println("Have a nice day"); } }