lexical analyzer (for parsing #defines and enums) written in matlab?

Discussion in 'MATLAB' started by gavin haentjens, Jul 25, 2005.

  1. Hello,

    Has anyone heard of a lexical analyzer that was written in Matlab (in
    the "M" language, i.e. in an .m file)?

    I would like to write a matlab script that opens a C header file (.h
    file), reads some #define and "enum" statements, and creates
    corresponding Matlab variables in the workspace.

    For example, if I had a header file with the following single line:

    #define PI 3.14

    I would like the script to create a variable with the name "PI" and
    value 3.14 in the matlab workspace.

    I have found the "textscan" function to be helpful for this task, but
    only for header files that have simple #define statements. For more
    sophisticated #defines, (e.g., "#define DOUBLE_PI (2 * PI)") I think I
    would need something like a lexical analyzer (such as the program
    "lex").

    Thanks a lot for any tips,
    Gavin
     
    gavin haentjens, Jul 25, 2005
    #1
    1. Advertisements

  2. I've not come across one (although I've not been looking either), but you
    could analyse pretty much any pattern using regular expressions if you had
    the inclination to do so. Alternatively, you could try setting up an M-file
    to actually call the 'lex' program and operate on the C header file first,
    if it already does what you want.
     
    Simon Durrant, Jul 25, 2005
    #2
    1. Advertisements

  3. It'd be easy to do but why bother? Just put your definitions in a
    script and call them:

    <start foo.m>
    function a = foo(b);
    fooheader;
    a = D*b;
    <end foo.m>

    <start fooheader.m>
    D = 3;
    <end fooheader.m>
     
    Michael Robbins, Jul 25, 2005
    #3
  4. Thank you both for the suggestions.

    An approach I am now considering is to write a matlab script that does
    the following:
    Call h2ph.bat (script included with perl distributions) to convert C
    header (.h file) to a perl header (.ph file).
    Call a simple perl script that I wrote which converts a .ph file to
    a .m file.
    Read in the newly created .m file.

    Thanks again,
    Gavin
     
    gavin haentjens, Jul 27, 2005
    #4
  5. Thank you both for the suggestions.

    The approach I am now considering is to write a matlab script that does
    the following:
    Call h2ph.bat (script included with perl distributions) to convert C
    header (.h file) to a perl header (.ph file).
    Run a simple, custom perl script that converts a .ph file to a .m
    file.
    Read in the newly created .m file.

    This approach actually incorporates both of the suggestions made by
    Simon:
    1) Calling an external program from matlab (in this case, h2ph.bat and
    perl.exe)
    2) Using regular expressions (in this case, my simple perl script)

    Thanks again,
    Gavin
     
    gavin haentjens, Jul 28, 2005
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.