Problem Regular Expression to analyse text file

조회 수: 3 (최근 30일)
Sagar Talaviya
Sagar Talaviya 2018년 1월 20일
댓글: Walter Roberson 2018년 1월 25일
Hello, I want to create given structure from any text file,
Which shows Function argument type, data type, list of variables used and defined, variable data type and whether they are local, global.
here is one .txt file, which contains C code into the file.
#include "add_lib11.h"
double PI_Regler_a(double a,double b, double c, double d, double e, double f)
{
double x;
x = (d*a) + (e*b) + (f*c);
return(x);
}
So, How can I use textscan and regexp to get structure like
functioncall.name= 'PI_Regler_a'
functioncall.type= 'double'
functioncall.returnvalueexists= '1'
variables(1).name = 'a'
variables(1).dtype = 'double'
variable(1).type = 'local'
like that,.....
please help me... I need to know that how I can separate and get what I need by using regexp and inputparsing class.
  댓글 수: 1
Walter Roberson
Walter Roberson 2018년 1월 23일
inputParser() class is not at all suited for this problem.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2018년 1월 23일
You need to build a C parser first. regexp() is not powerful enough for that task. You can find a formal grammar up to C11 at http://www.quut.com/c/ANSI-C-grammar-y.html
You would need to enhance this to support #include and all other preprocessing . It might be easier to invoke a C preprocessor to have it emit the expanded code, if doing so would not be considered cheating. Supporting #include and #define and so on is absolutely crucial in C since just about every "word" can be #define to be something other than it appears. And you cannot possibly get types correctly without processing typedef, which are often in #include files.
You might want to look at https://www.mathworks.com/matlabcentral/fileexchange/32769-mparser which is a parser for MATLAB written partly in MATLAB that uses the ANTLR parser (calling that with a mex interface.)
Getting all of this right is a non-trivial project.
Or you could do something half-cooked that would have trouble getting types correct and would have trouble determining whether a variable is local or global.
... Did you forget comments can have things that look like code?
  댓글 수: 3
Sagar Talaviya
Sagar Talaviya 2018년 1월 25일
HIIII.... again do you have some idea how should I write commands to get type correct and function name. If I get that then I will have some idea to write further command.
Walter Roberson
Walter Roberson 2018년 1월 25일
You need to build a parser routine, such as in yacc and lex, or in ANTLR, or re-invent such routines in MATLAB.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by