/* -*- Mode: java -*- */ /* Input file to JJTree and JavaCC to generate Matrix Parser */ options { LOOKAHEAD=1; STATIC = true; NODE_USES_PARSER = true; } PARSER_BEGIN(MatrixParser) /* Parser for matrices written in matlab format. Copyright (c) 1998-2008 The Regents of the University of California. All rights reserved. Permission is hereby granted, without written agreement and without license or royalty fees, to use, copy, modify, and distribute this software and its documentation for any purpose, provided that the above copyright notice and the following two paragraphs appear in all copies of this software. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. PT_COPYRIGHT_VERSION_2 COPYRIGHTENDKEY */ package ptolemy.data.expr; import ptolemy.kernel.util.IllegalActionException; import java.util.Vector; import java.io.ByteArrayInputStream; import java.io.FileReader; ////////////////////////////////////////////////////////////////////////// //// MatrixParser /** This Class provides a parser for read matrices in matlab format. @author Bart Kienhuis @version $Id: MatrixParser.jjt 50350 2008-08-08 15:20:00Z cxh $ @since Ptolemy II 1.0 @Pt.ProposedRating Red @Pt.AcceptedRating Red (kienhuis) */ @SuppressWarnings("static-access") public class MatrixParser { /** Read a Matrix from File. @exception IllegalActionException If an error occurs during parsing. */ public Vector readMatrix() throws IllegalActionException { Vector m = null; try { m = matrix(); } catch (Exception e) { throw new IllegalActionException(e.getMessage()); } return m; } } PARSER_END(MatrixParser) SKIP : { " " | "\t" | "\n" | "\r" } TOKEN : /* numeric constants */ { < CONSTANT: | ( ["e","E"] ([ "-","+"])? )? > | < #FLOAT: | ( "." )? | "." > | < #INTEGER: ( )+ > | < #DIGIT: ["0" - "9"] > } TOKEN : { < NAME : ( ["a"-"z" , "A"-"Z" , "_"] )+ ( ["a"-"z" , "A"-"Z" , "_" , "'", "0"-"9"] )* > | < NATURAL : ( ["0"-"9"] )+ ( ["0"-"9"] )* > | < SIGN : ["-","+"] > } SPECIAL_TOKEN : { < COMMENT : "#" (~["\n","\r"])* ["\n","\r"] > } Vector matrix() : { Vector m = new Vector();; Vector row = null; //Matrix m; //Matrix row = null; } { ( "[" [row=row()] {m.add(row);} (";" row=row() {m.add(row);} )* "]" ) { return m; } } Vector row() : { Vector row = new Vector(); double element; } { ( element=element() {row.add(new Double(element));} ( "," element=element() {row.add(new Double(element));} )* ) { return row; } } double element() : { Token i; Token s; int sign = 1; double value; } { ( [s= {if ( (s.image).equals("-") ) sign=-1*sign; }] i= {value = sign*(new Double(i.image)).doubleValue();} ) { return value; } }