File Exchange

image thumbnail

mparser

version 1.0.0.0 (174 KB) by David Wingate
A matlab lexer and parser written with ANTLR, with a mex interface.

8 Downloads

Updated 01 Sep 2011

View License

This is a MATLAB lexer and parser written using ANTLR. The goal of
this package is to provide a way to parse MATLAB code from within
MATLAB, and to be able to do source code translations on the resulting
AST.

The main workhorse of this package is "mparser", a MEX file which
accepts either a string or a filename and parses it into a deeply
nested set of MATLAB structures representing an AST. These can
examined and manipulated from within MATLAB.

Includes a variety of other tools to parse and examine matlab code.

While I have made every effort to be as exact as I can, there are
still a few differences between this grammar and the "true" MATLAB
grammar. Most of these you will never notice, but there are a few
that are significant:

* The transpose operator ' is not supported (due to lexing confusion
with the single quotes used in string constants).

* Constant matrices and cells must have individual elements separated
by a comma.

* Block comments are not supported.

For others, please see the comments in matlab.g
Hopefully, future versions will remedy this.

Cite As

David Wingate (2020). mparser (https://www.mathworks.com/matlabcentral/fileexchange/32769-mparser), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (7)

bas jacobs

Very nice piece of work, it turned out that it was exactly what I needed. However, it didn't work out of the box on Fedora 31, GCC9.2. I managed to fix it, and here is how:

in mparser.cpp, in the function mexFunction
if ( nrhs == 2 ) {
// input = antlr3AsciiFileStreamNew( (pANTLR3_UINT8)buf );
input = antlr3FileStreamNew( (pANTLR3_UINT8)buf , 4);
if ( input == NULL ) {
mexErrMsgTxt( "unable to open file!\n" );
}
} else {
//input = antlr3NewAsciiStringInPlaceStream( (pANTLR3_UINT8)buf, strlen(buf), NULL );
//if ( input == NULL ) {
// mexErrMsgTxt( "unable to create input stream from string!\n" );
//}
}

And the last line:
//free( buf );
mxFree( buf );

This looks like exactly what I need right now! Unfortunately, I don't know how to use it. I installed it in R2015b using Add-On Explorer. I then trying to call show_tree, mparser, and matlab_ast_print, but each one says `Attempt to execute SCRIPT ______ as a function`.

Abel

is this parser capable of parsing classdef files?

Donn Shull

There may be several reasons why a rating of lower than five should be given to this submission, however, David has clearly stated that this is a preliminary version of the parser. He has given us a commented grammar with notes on how to produce either the mex parser that he uses or a java parser which can be debugged using the tools from the antlr website.

I would encourage the use of H1 help lines in the m files to be in line with MATLAB normal coding standards, and facilitate the use of lookfor, etc. There is a lot of information on making good help text for m files. You can find it in the MATLAB documentation, in the style guidelines by Richard Johnson posted on the file exchange, or better yet in Richard Johnsons book "The Elements of MATLAB style.

Again a good effort which will be useful for someone developing MATLAB tools.

MATLAB Release Compatibility
Created with R2010b
Compatible with any release
Platform Compatibility
Windows macOS Linux