|
View:
New views
2 Messages
—
Rating Filter:
Alert me
|
|
|
Parse java string to Matlab data typeHi javacc users,
I had posted the following message to the Matlab users mailing list but it doesn't look like i'm going to get a response. Brief background: I'm using Matlab Builder JA which compiles Matlab code to java callable classes, methods, etc. A column vector is represented in Matlab as [a; b], a 2x2 matrix as [a b; c d] and the two can be stored together in the "cell" data type as {[a; b], [a b; c d]} Hi all, I'm building a java swing based GUI front-end for my Matlab application. As such, user input is available as text strings, for example "{[0; 0], [3.5 0; 0 2]}" (mean vector and covariance matrix of a 2D Gaussian). The user input will be passed to the compiled matlab method as a MWCellArray. Problem is how do a convert (parse) the above string into a MWCellArray? From its javadoc http://www.mathworks.com/access/helpdesk/help/toolbox/javabuilder/MWArrayAPI/index.html the following method is available: java.lang.String toString() Returns a string representation of this array, but the reverse method is not. Evidently, parsing a string like the one above is a non trivial task and I'm loath to attempt writing one by myself. I would be very thankful if someone can point me to the required parser implementation. Matlab itself is carrying out this task all the time, perhaps it is possible to access Matlab's own parser. Help much appreciated, Ritesh Ritesh Sood Office: Post Doc Researcher 2147 Math Sci Building Department of Mathematics University of California, Davis University of California, Davis Davis CA 95616 Ph. (530) 574 0272 --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@... For additional commands, e-mail: users-help@... |
|
|
Re: Parse java string to Matlab data typeOn Oct 7, 2009, at 8:23 PM, Ritesh Sood wrote:
> Hi javacc users, > > I had posted the following message to the Matlab users mailing list > but it doesn't look like i'm going to get a response. Brief > background: I'm using Matlab Builder JA which compiles Matlab code > to java callable classes, methods, etc. A column vector is > represented in Matlab as [a; b], a 2x2 matrix as [a b; c d] and the > two can be stored together in the "cell" data type as {[a; b], [a b; > c d]} > > Hi all, > > I'm building a java swing based GUI front-end for my Matlab > application. As such, user input is available as text strings, for > example "{[0; 0], [3.5 0; 0 2]}" (mean vector and covariance matrix > of a 2D Gaussian). > > The user input will be passed to the compiled matlab method as a > MWCellArray. Problem is how do a convert (parse) the above string > into a MWCellArray? > From its javadoc > http://www.mathworks.com/access/helpdesk/help/toolbox/javabuilder/MWArrayAPI/index.html > the following method is available: > java.lang.String toString() > Returns a string representation of this array, > but the reverse method is not. > > Evidently, parsing a string like the one above is a non trivial task > and I'm loath to attempt writing one by myself. I would be very > thankful if someone can point me to the required parser > implementation. Matlab itself is carrying out this task all the > time, perhaps it is possible to access Matlab's own parser. Hi Ritesh - Hm, that looks very doable with JavaCC. Here's a sample grammar; this is a little less flexible than you'll want, probably, but it'll give you the idea and a place to start: ================ $ cat matlab.jj && javacc matlab.jj && javac *.java && java Matlab "{[0; 0], [3.5 0; 0 2]}" PARSER_BEGIN(Matlab) import java.io.*; public class Matlab { public static void main(String[] args) throws Exception { Matlab p = new Matlab(new StringReader(args[0])); p.Cell(); System.out.println("Parsed successfully!"); } } PARSER_END(Matlab) SKIP: { " " } TOKEN : { <LBRACE : "{"> | <RBRACE : "}"> | <LBRACKET : "["> | <RBRACKET : "]"> | <SEMICOLON : ";"> | <COMMA : ","> | <DOT : "."> | <INTEGER_NUMBER : ["0"-"9"](["0"-"9"])*> | <FLOATING_POINT_NUMBER : (["0"-"9"])+ "." (["0"-"9"])+> } void Cell() : {} { "{" ColumnVector() "," Matrix() "}" } void ColumnVector() : {} { "[" <INTEGER_NUMBER> ";" <INTEGER_NUMBER> "]" } void Matrix() : {} { "[" <FLOATING_POINT_NUMBER> <INTEGER_NUMBER> ";" <INTEGER_NUMBER> <INTEGER_NUMBER> "]" } Java Compiler Compiler Version 5.0 (Parser Generator) (type "javacc" with no arguments for help) Reading from file matlab.jj . . . File "TokenMgrError.java" is being rebuilt. File "ParseException.java" is being rebuilt. File "Token.java" is being rebuilt. File "SimpleCharStream.java" is being rebuilt. Parser generated successfully. Parsed successfully! ================ You can add syntactic actions to do things when a particular item is parsed, or you can use JJTree to generate an abstract syntax tree that you can traverse to do whatever you need. Good stuff! Yours, Tom http://generatingparserswithjavacc.com/ --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@... For additional commands, e-mail: users-help@... |
| Free embeddable forum powered by Nabble | Forum Help |