// Program: 4033 // Author: Manuel J. A. Eugster ( e0126312@student.tuwien.ac.at ) // FileId: $Id: Parser.java,v 1.1 2002/01/14 17:28:09 heder Exp $ package Wider; import java.lang.*; // CLASS: // Implements contextfree grammar. // // : | + | / // : () | v // public class Parser implements Token { private Scanner scanner; // Scanner object with the resistor network description // METHOD: // Constructor. // public Parser( Scanner scanner ) { this.scanner = scanner; } // METHOD: // Starts the parsing run. // public float start() throws Exception { return( expression() ); } // METHOD: // Implements first rule of the contextfree grammar. // Recursive, and returns the result. // private float expression() throws Exception { float result = term(); if ( scanner.valueTok == T_SER ) { scanner.nextToken(); result = CalcResistor.ser( result, expression() ); } else if ( scanner.valueTok == T_PAR ) { scanner.nextToken(); result = CalcResistor.par( result, term() ); } return( result ); } // METHOD: // Implements second rule of the contextfree grammar. // Recursive, and returns the result. // private float term() throws Exception { float result = 0f; if ( scanner.valueTok == T_BRO ) { scanner.nextToken(); result = expression(); if ( scanner.valueTok != T_BRC ) { throw new ParserException( "Bracket closed" ); } scanner.nextToken(); return( result ); } else if ( scanner.valueTok == T_NUM ) { result = scanner.valueNum; scanner.nextToken(); return( result ); } throw new ParserException( "Missing term" ); } }