⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sentence.l

📁 这是一个和lex功能差不多的parser编写的程序
💻 L
字号:
%{{* * 基于单词识别的句子分析   用法 lex Sentence  *}Unit Sentence;interfaceuses  SysUtils, LexLib, Classes;const  Noun=257;  Pron=258;  Verb=259;  Adv= 260;  Adj= 261;  Prep=262;  Conj=263;  LookUp=0;type   TWord=class(TObject)  public    word:string;    State:integer;//TWordState;  end;var  state:integer;//TWordState;   rstate:integer;  WordList:TStringList;function LookUpWord(word:string):integer;//TWordState;function AddWord(State:integer{TWordState}; Word:string):integer;function yylex: Integer;implementation%}%%\n	state := LookUp;\.\n	begin          state := LookUP;	  return(0); {* 遇到句子结尾了,注意这里是在yyaction中,不能用result:=0来返回,只能用定义的return函数*}	end;^verb	state := Verb;^adj	state := Adj;^adv	state := Adv;^noun	state := Noun;^prep	state := Prep;^pron	state := Pron;^conj	state := Conj;[a-zA-Z]+  begin	     if(state <> LookUp) then 	     	AddWord(state, yytext)	     else	     begin                rstate:=LookUpWord(yytext);		if rstate<>LookUp then		begin		  writeln(format('查找到了%s,返回给yacc', [yytext]));                  return(rstate);		end		else                  Writeln(format('%s 无法识别', [yytext]));            end;          end;.	{* 忽略剩下的所有的字符串 *} ;%%function LookUpWord(word:string):integer;//TWordState;var  I:integer;begin  I:=WordList.IndexOf(Word);  if I>-1 then  begin    result:=TWord(WordList.Objects[I]).State;  end  else    result:=LookUp;end;function AddWord(State:integer{TWordState}; Word:string):integer;var  WordObj:TWord;begin  if (LookUpWord(Word)<>LookUp) then  begin    Writeln(format('!!! %s 已经被定义了', [word]));    result:=0;    exit;  end;	  WordObj:= TWord.Create;  WordObj.Word:=Word;  WordObj.State:=State;  WordList.AddObject(Word, WordObj);  result:=1;end;initialization  WordList:=TStringList.Create;finalization  // Todo:释放列表中的WordObj对象  WordList.Free;end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -