首页  编辑  

何使用Delphi中的TParser语法分析器类

Tags: /超级猛料/Language.Object Pascal/在Delphi编程中使用C语言代码/   Date Created:

何使用Delphi中的TParser语法分析器类

如何进行词法分析?

Newsgroups: comp.lang.pascal.delphi.misc

Subject: Re: How do I use Tparser?

From: jbui@scd.hp.com (Joseph Bui)

Date: 31 Jul 1995 22:15:29 GMT

To do what you want with Tparser, do:

procedure ParseFile(const AFileName: string);

var FS: TFileStream; AP: Tparser;

begin

FS:=TFileStream.Create(AFileName, fmOpenRead);

AP:=Tparser.Create(FS);

with AP do

repeat

case Token of

#1 : {Token is a pascal identifier}

{retreive with TokenString}

#2 : {Token is a pascal string ie. 'blah' or #<number>}

{retreive with TokenString}

#3 : {Token is a pascal integer in decimal or hex}

{Retreive with TokenInt}

#4 : {Token is a pascal floating point}

{retreive with TokenFloat}

else

{Token is a single character token, such as a comma}

{retreive with Token or TokenString}

until NextToken = #0; {#0 mean End Of File}

AP.Free;

FS.Free;

end;

The TokenInt and TokenFloat routines cause errors when you try

and use them on non-compatible tokens. Also, the detection of

integers/floats is flawed, so ".e" comes back as a floating

point, and "$$$$$" comes back as an integer (a hex number).

You can't go backwards, and if some sort of error occurs, you are

pretty much screwed. Thus I suggest you find and use my

objects. If you can't find them, email me, and I'll send you

a copy.