在delphi中怎么编写一个解析器,,输入一段delphi程序,就能解出,
头文件:。,
文件名:。
类变量:。,类型;值
方法:
  方法名,
  方法代码,
  方法中变量的名称,值 急需要啊!希望各位大侠不吝赐教。回者别酬,,小鸟先多谢各位了!!!
谢谢谢谢,谢谢谢谢,谢谢谢谢,谢谢谢谢,谢谢谢谢,谢谢谢谢,

解决方案 »

  1.   

    常用的方法采用下面的步骤:
      (1)定义ObjectPascal语言的各种标记(Token);
      (2)词法分析,获得当前代码的Token;
      (3)语法分析:在语法分析过程中,调用词法分析子程序
           语法分析可以采用 递归下降的分析方法。
           根据ObjectPascal语言语言的文法规则,分别写出每个非终结符相应的子程序;
           同时,在分析过程中,将需要的各种数据记录下来。
    我是使用了该方法做了针对Object Pascal语言的软件质量度量工具,分析程序的源代码,获得
    代码中类的信息、方法的信息等等,对软件的质量进行评价。
    下面贴出我的程序中的一段代码希望对你有帮助:
    词法分析过程
    1.定义标记:
    type
      TTokenKind = (tkAbsolute, tkAbstract, tkAnd, tkAnsiComment, tkArray, tkAs,
        。将近80个
     TmPasParser = class(TObject)
      private
        FToken: TmPasToken;function GetIsJunk: Boolean;
        function IdentKind: TTokenKind;
        procedure SetOrigin(value: PChar);
        procedure SetRunPos(NewPos: LongInt);
        procedure HandleComments;
      protected
      public
        constructor Create;
        destructor Destroy; override;
        function GetSubString(StartPos, EndPos: LongInt): String;
        procedure NextClassLine;
        procedure NextObjectLine;
        procedure NextID(ID: TToken
        procedure NextNonComment;
        procedure NextComment;
        procedure NextNonJunk;
        procedure NextNonSpace;    procedure ToLineStart;
        function GetMethodImpLine(ClassName: String; MethodName: String): LongInt;
        property Comments: TCommentState read FComment write FComment;
        property EndCount: Integer read FEndCount write FEndCount;
        property ImplementationsPos: LongInt read FImplementationsPos;
        property IsJunk: Boolean read GetIsJunk;
      . .....
        property Origin: PChar read fOrigin write SetOrigin;
        property RunPos: LongInt read Run write SetRunPos;
      published
      end;语法分析过程:类的分析过程,隐式构造语法树 type
      TClass_MethodRec = record      //类中的方法记录
        MethodName: String[30];      //方法名
        Flag: Integer;
        //方法类型:1--procedure 2--Function  3--Constructor  4--Destructor
        Visibility: Integer;
        //方法的可视性1: private 2:protected 3: public 4:published
        Directive: String[6];
        //方法的指示字属性; override,overload,dynamic ,virtual,abstract,reintroduce
        // 0: virtual ,1:dynamic 2:abstract  3 :reintroduce 4: oveerride 5: overload;
        //ParamLst  :TStringList; //参数数据字符串 ,奇数下标是参数名,偶数下标是参数类型;
        ParamStr: String[250];
        RetType: String[20];   //返回类型;
      end;
      TClass_PropertyRec = record    //类中的属性记录
        PropertyName: String[30];    //方法名
        Visibility: Integer;
        //方法的可视性1: private 2:protected 3: public 4:published
        PropertyType: String[30];
      end;  TClass_FieldRec = record       //类中的字段记录
        FieldName: String[30];       //字段名
        FieldType: String[30];       //字段类型;
        Visibility: Integer;
        //方法的可视性1: private 2:protected 3: public 4:published
      end;  ///////////////////////类的具体定义//////////////////////////  TClassInfoCls = class         //存放数据的类
      public
        ClassName: String[30];      //类名;
        ParentClsName: String[30];  //父类名;
        lstMethod: TList;           //方法Lst 1--procedure 2--Function  3--Constructor  4--Destructor
        lstProperty: TList;         //属性Lst;
        lstField: TList;            //成员变量Lst;
        constructor Create;
      end;type
      TClassCls = class(TmPasParser)
      private
        FClassInfoCls: TClassInfoCls;
        procedure ClearLst(lst: TList; size: Integer);          //释放内存
        function isHasClass: Boolean;                           //是否还有需要分析的类
        function Match(AToken: ttokenkind): Boolean;            //匹配一个关键字
        procedure MatchClassType;                               //分析的主控程序;
        procedure MatchClassHeritage;                           //匹配类的继承    procedure MatchClassMethodLst;
        procedure MatchClassPropertyLst;
        procedure MatchClassFieldLst;    procedure MatchObjFieldList;    procedure MatchPropertyList;
        procedure MatchPropertySpecifiers;
        procedure MatchPropertyInterface;
        procedure MatchPropertyParameterList;    procedure MatchMethodList;
        procedure MatchProcedureHeading;       //匹配过程头
        procedure MatchFunctionHeading;        //匹配函数头
        procedure MatchConstructorHeading;     //匹配构造头
        procedure MatchDestructorHeading;      //匹配析构头    function MatchClassVisibility: Boolean;//匹配可视性    function MatchDirective: String;       //匹配指示字
      protected  public
        procedure NextClass;                    //分析下一个类的信息;
        property ClassInfo: TClassInfoCls read FClassInfoCls;
        property HasClass: Boolean read isHasClass;
        constructor Create;
        destructor Destroy; override;
      end;

    这些编译原理树上都有,仔细看看书把,我写的分析程序花了一个多月,不难主要是ObjectPascal语法规则太多,太复杂了。