image thumbnail

fparser: a pedestrian m-file parser and tokenizer

by

 

31 Aug 2003 (Updated )

lists ML-tokens and constructs (function, var, struct assignment) in a m-file or from a command line

fpardemo
fpardemo

Contents

command line decoding

   com='f=pi;dddd=''d'';a.bb=ccc.(dddd).e=inf';
   par=fparser(com,'-c','-d');
 
functions/keywords          2 SOURCE:
-----------------------------                                       
inf                   >     1 G:\usr\r14\toolbox\matlab\elmat\inf.bi
pi                    >     1 G:\usr\r14\toolbox\matlab\elmat\pi.bi 
-----------------------------                                       
variables/constants         4 <= NR OF TOKENS
-----------------------------                                       
a                     >     1 <= NR OF OCCURRENCES
ccc                   >     1                                       
dddd                  >     1                                       
f                     >     1                                       
-----------------------------                                       
struct.field assmnt         2 <= NR OF TOKENS
-----------------------------                                       
a.bb                  >     1 <= LINE NR
ccc.(dddd).e          >     1                                       
-----------------------------                                       

M-file decoding

show contents of m-file fpartest.m

   type fpartest
 
      foo='a';
      foo1=1;
      cc{foo1}.case = pi;
      cc{2} .        case = pi;
      dd ( foo1 ) . foo = pi;dd( 2 ).(foo).foo = pi;
      hoo=[];
      datenum.foo.(foo)=pi;
      hoo.foo1.foo2=10;
      ioo.(foo).(foo)=10;
      strcmp(ioo.(foo).(foo),'foo');
      find(ioo.(foo).(foo)=='f');
      hoo.foo1.foo2 ...
      .* hoo.foo1.foo2
      exist('hoo.foo1.foo2') 
      dd=datenum(aei.data{i}.header_info.stop_time);
 

decode m-file fpartest.m

   par=fparser('fpartest.m','-d');
   par
 
functions/keywords                       6 SOURCE:
------------------------------------------                                            
datenum                            >     1 G:\usr\r14\toolbox\matlab\timefun\datenum.m
exist                              >     1 G:\usr\r14\toolbox\matlab\lang\exist.m     
find                               >     1 G:\usr\r14\toolbox\matlab\elmat\find.m     
foo                                >     1 F:\usr\matlab\tmp\fpar\foo.m               
pi                                 >     5 G:\usr\r14\toolbox\matlab\elmat\pi.bi      
strcmp                             >     1 G:\usr\r14\toolbox\matlab\strfun\strcmp.m  
------------------------------------------                                            
variables/constants                      7 <= NR OF TOKENS
------------------------------------------                                            
aei                                >     1 <= NR OF OCCURRENCES
cc                                 >     2                                            
datenum                            >     1 <= SHOWS UP TWICE (unsafe to use in code!)
dd                                 >     3                                            
foo1                               >     3                                            
hoo                                >     3                                            
ioo                                >     2                                            
------------------------------------------                                            
struct.field assmnt                     12                                            
------------------------------------------                                            
aei.data{i}.header_info.stop_time  >    15 <= LINE NR
cc{2}.case                         >     4                                            
cc{foo1}.case                      >     3                                            
datenum.foo.(foo)                  >     7                                            
dd(foo1).foo                       >     5                                            
dd(2).(foo).foo                    >     5                                            
hoo.foo1.foo2                      >     8                                            
hoo.foo1.foo2                      >    12                                            
hoo.foo1.foo2                      >    13                                            
ioo.(foo).(foo)                    >     9                                            
ioo.(foo).(foo)                    >    10                                            
ioo.(foo).(foo)                    >    11                                            
------------------------------------------                                            
 
par = 
 
    magic: 'FPARSER'
      ver: '12-Dec-2005 09:26:17'
     time: '12-Dec-2005 09:27:45'
      par: [1x1 struct]
     tmpl: [1x1 struct]
     file: 'fpartest.m'
     type: 'file'
     buff: [1x352 char]
     skel: [1x352 char]
      res: [34x86 char]
    nline: 15
     linx: [2x16 double]
     nent: 25
    nmtok: 6
     mtok: {6x6 cell}
     mtxt: [9x86 char]
    nvtok: 7
     vtok: {7x6 cell}
     vtxt: [10x42 char]
    nstok: 12
     stok: {12x6 cell}
     stxt: [15x43 char]
 

Contact us