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

📄 wordpro.dpr

📁 语法分析 语法分析
💻 DPR
字号:

(* lexical analyzer template (TP Lex V3.0), V1.0 3-2-91 AG *)

(* global definitions: *)
{*
 * 基于符号列表定制的单词识别
 *}

program IsVerb;

{$APPTYPE CONSOLE}

uses
  SysUtils, LexLib, Classes;

type
  TWordState = (wsLookUp, wsVerb, wsAdj, wsAdv, wsNoun, wsPrep, wsPron, wsConj);
  TWord = class(TObject)
  public
    word: string;
    State: TWordState;
  end;

var
  state: TWordState;
  WordList: TStringList;

function LookUpWord(word: string): TWordState;
var
  I: integer;
begin
  I := WordList.IndexOf(Word);
  if I > -1 then
  begin
    result := TWord(WordList.Objects[I]).State;
  end
  else
    result := wsLookUp;
end;

function AddWord(State: TWordState; Word: string): integer;
var
  WordObj: TWord;
begin
  //struct word *wp;
  if (LookUpWord(Word) <> wsLookUp) 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;

function yylex: Integer;

  procedure yyaction(yyruleno: Integer);
    (* local definitions: *)
  begin
    (* actions: *)
    case yyruleno of
      1:
        state := wsLookUp;

      2:
        state := wsVerb;
      3:
        state := wsAdj;
      4:
        state := wsAdv;
      5:
        state := wsNoun;
      6:
        state := wsPrep;
      7:
        state := wsPron;
      8:
        state := wsConj;

      9:
        begin
          if (state <> wsLookUp) then
            AddWord(state, yytext)
          else
          begin
            case LookUpWord(yytext) of
              wsVerb: Writeln(format('%s 是动词', [yytext]));
              wsAdj: Writeln(format('%s 是形容词', [yytext]));
              wsAdv: Writeln(format('%s 是副词', [yytext]));
              wsNoun: Writeln(format('%s 是名词', [yytext]));
              wsPrep: Writeln(format('%s 是介词', [yytext]));
              wsPron: Writeln(format('%s 是代词', [yytext]));
              wsConj: Writeln(format('%s 是连词', [yytext]));
            else
              Writeln(format('%s 无法识别', [yytext]));
            end;
          end;
        end;

      10:
        {* 忽略剩下的所有的字符串 *};

    end;
  end (*yyaction*);

  (* DFA table: *)

type
  YYTRec = record
    cc: set of Char;
    s: Integer;
  end;

const

  yynmarks = 39;
  yynmatches = 39;
  yyntrans = 52;
  yynstates = 28;

  yyk: array[1..yynmarks] of Integer = (
    { 0: }
    { 1: }
    { 2: }
    1,
    { 3: }
    9,
    10,
    { 4: }
    10,
    { 5: }
    9,
    10,
    { 6: }
    9,
    10,
    { 7: }
    9,
    10,
    { 8: }
    9,
    10,
    { 9: }
    9,
    10,
    { 10: }
    9,
    { 11: }
    9,
    { 12: }
    9,
    { 13: }
    9,
    { 14: }
    9,
    { 15: }
    9,
    { 16: }
    9,
    { 17: }
    3,
    9,
    { 18: }
    4,
    9,
    { 19: }
    9,
    { 20: }
    9,
    { 21: }
    9,
    { 22: }
    9,
    { 23: }
    2,
    9,
    { 24: }
    5,
    9,
    { 25: }
    6,
    9,
    { 26: }
    7,
    9,
    { 27: }
    8,
    9
    );

  yym: array[1..yynmatches] of Integer = (
    { 0: }
    { 1: }
    { 2: }
    1,
    { 3: }
    9,
    10,
    { 4: }
    10,
    { 5: }
    9,
    10,
    { 6: }
    9,
    10,
    { 7: }
    9,
    10,
    { 8: }
    9,
    10,
    { 9: }
    9,
    10,
    { 10: }
    9,
    { 11: }
    9,
    { 12: }
    9,
    { 13: }
    9,
    { 14: }
    9,
    { 15: }
    9,
    { 16: }
    9,
    { 17: }
    3,
    9,
    { 18: }
    4,
    9,
    { 19: }
    9,
    { 20: }
    9,
    { 21: }
    9,
    { 22: }
    9,
    { 23: }
    2,
    9,
    { 24: }
    5,
    9,
    { 25: }
    6,
    9,
    { 26: }
    7,
    9,
    { 27: }
    8,
    9
    );

  yyt: array[1..yyntrans] of YYTrec = (
    { 0: }
    (cc: [#1..#9, #11..'@', '['..'`', '{'..#255]; s: 4),
    (cc: [#10]; s: 2),
    (cc: ['A'..'Z', 'a'..'z']; s: 3),
    { 1: }
    (cc: [#1..#9, #11..'@', '['..'`', '{'..#255]; s: 4),
    (cc: [#10]; s: 2),
    (cc: ['A'..'Z', 'b', 'd'..'m', 'o', 'q'..'u', 'w'..'z']; s: 3),
    (cc: ['a']; s: 6),
    (cc: ['c']; s: 9),
    (cc: ['n']; s: 7),
    (cc: ['p']; s: 8),
    (cc: ['v']; s: 5),
    { 2: }
    { 3: }
    (cc: ['A'..'Z', 'a'..'z']; s: 10),
    { 4: }
    { 5: }
    (cc: ['A'..'Z', 'a'..'d', 'f'..'z']; s: 10),
    (cc: ['e']; s: 11),
    { 6: }
    (cc: ['A'..'Z', 'a'..'c', 'e'..'z']; s: 10),
    (cc: ['d']; s: 12),
    { 7: }
    (cc: ['A'..'Z', 'a'..'n', 'p'..'z']; s: 10),
    (cc: ['o']; s: 13),
    { 8: }
    (cc: ['A'..'Z', 'a'..'q', 's'..'z']; s: 10),
    (cc: ['r']; s: 14),
    { 9: }
    (cc: ['A'..'Z', 'a'..'n', 'p'..'z']; s: 10),
    (cc: ['o']; s: 15),
    { 10: }
    (cc: ['A'..'Z', 'a'..'z']; s: 10),
    { 11: }
    (cc: ['A'..'Z', 'a'..'q', 's'..'z']; s: 10),
    (cc: ['r']; s: 16),
    { 12: }
    (cc: ['A'..'Z', 'a'..'i', 'k'..'u', 'w'..'z']; s: 10),
    (cc: ['j']; s: 17),
    (cc: ['v']; s: 18),
    { 13: }
    (cc: ['A'..'Z', 'a'..'t', 'v'..'z']; s: 10),
    (cc: ['u']; s: 19),
    { 14: }
    (cc: ['A'..'Z', 'a'..'d', 'f'..'n', 'p'..'z']; s: 10),
    (cc: ['e']; s: 20),
    (cc: ['o']; s: 21),
    { 15: }
    (cc: ['A'..'Z', 'a'..'m', 'o'..'z']; s: 10),
    (cc: ['n']; s: 22),
    { 16: }
    (cc: ['A'..'Z', 'a', 'c'..'z']; s: 10),
    (cc: ['b']; s: 23),
    { 17: }
    (cc: ['A'..'Z', 'a'..'z']; s: 10),
    { 18: }
    (cc: ['A'..'Z', 'a'..'z']; s: 10),
    { 19: }
    (cc: ['A'..'Z', 'a'..'m', 'o'..'z']; s: 10),
    (cc: ['n']; s: 24),
    { 20: }
    (cc: ['A'..'Z', 'a'..'o', 'q'..'z']; s: 10),
    (cc: ['p']; s: 25),
    { 21: }
    (cc: ['A'..'Z', 'a'..'m', 'o'..'z']; s: 10),
    (cc: ['n']; s: 26),
    { 22: }
    (cc: ['A'..'Z', 'a'..'i', 'k'..'z']; s: 10),
    (cc: ['j']; s: 27),
    { 23: }
    (cc: ['A'..'Z', 'a'..'z']; s: 10),
    { 24: }
    (cc: ['A'..'Z', 'a'..'z']; s: 10),
    { 25: }
    (cc: ['A'..'Z', 'a'..'z']; s: 10),
    { 26: }
    (cc: ['A'..'Z', 'a'..'z']; s: 10),
    { 27: }
    (cc: ['A'..'Z', 'a'..'z']; s: 10)
    );

  yykl: array[0..yynstates - 1] of Integer = (
    { 0: } 1,
    { 1: } 1,
    { 2: } 1,
    { 3: } 2,
    { 4: } 4,
    { 5: } 5,
    { 6: } 7,
    { 7: } 9,
    { 8: } 11,
    { 9: } 13,
    { 10: } 15,
    { 11: } 16,
    { 12: } 17,
    { 13: } 18,
    { 14: } 19,
    { 15: } 20,
    { 16: } 21,
    { 17: } 22,
    { 18: } 24,
    { 19: } 26,
    { 20: } 27,
    { 21: } 28,
    { 22: } 29,
    { 23: } 30,
    { 24: } 32,
    { 25: } 34,
    { 26: } 36,
    { 27: } 38
    );

  yykh: array[0..yynstates - 1] of Integer = (
    { 0: } 0,
    { 1: } 0,
    { 2: } 1,
    { 3: } 3,
    { 4: } 4,
    { 5: } 6,
    { 6: } 8,
    { 7: } 10,
    { 8: } 12,
    { 9: } 14,
    { 10: } 15,
    { 11: } 16,
    { 12: } 17,
    { 13: } 18,
    { 14: } 19,
    { 15: } 20,
    { 16: } 21,
    { 17: } 23,
    { 18: } 25,
    { 19: } 26,
    { 20: } 27,
    { 21: } 28,
    { 22: } 29,
    { 23: } 31,
    { 24: } 33,
    { 25: } 35,
    { 26: } 37,
    { 27: } 39
    );

  yyml: array[0..yynstates - 1] of Integer = (
    { 0: } 1,
    { 1: } 1,
    { 2: } 1,
    { 3: } 2,
    { 4: } 4,
    { 5: } 5,
    { 6: } 7,
    { 7: } 9,
    { 8: } 11,
    { 9: } 13,
    { 10: } 15,
    { 11: } 16,
    { 12: } 17,
    { 13: } 18,
    { 14: } 19,
    { 15: } 20,
    { 16: } 21,
    { 17: } 22,
    { 18: } 24,
    { 19: } 26,
    { 20: } 27,
    { 21: } 28,
    { 22: } 29,
    { 23: } 30,
    { 24: } 32,
    { 25: } 34,
    { 26: } 36,
    { 27: } 38
    );

  yymh: array[0..yynstates - 1] of Integer = (
    { 0: } 0,
    { 1: } 0,
    { 2: } 1,
    { 3: } 3,
    { 4: } 4,
    { 5: } 6,
    { 6: } 8,
    { 7: } 10,
    { 8: } 12,
    { 9: } 14,
    { 10: } 15,
    { 11: } 16,
    { 12: } 17,
    { 13: } 18,
    { 14: } 19,
    { 15: } 20,
    { 16: } 21,
    { 17: } 23,
    { 18: } 25,
    { 19: } 26,
    { 20: } 27,
    { 21: } 28,
    { 22: } 29,
    { 23: } 31,
    { 24: } 33,
    { 25: } 35,
    { 26: } 37,
    { 27: } 39
    );

  yytl: array[0..yynstates - 1] of Integer = (
    { 0: } 1,
    { 1: } 4,
    { 2: } 12,
    { 3: } 12,
    { 4: } 13,
    { 5: } 13,
    { 6: } 15,
    { 7: } 17,
    { 8: } 19,
    { 9: } 21,
    { 10: } 23,
    { 11: } 24,
    { 12: } 26,
    { 13: } 29,
    { 14: } 31,
    { 15: } 34,
    { 16: } 36,
    { 17: } 38,
    { 18: } 39,
    { 19: } 40,
    { 20: } 42,
    { 21: } 44,
    { 22: } 46,
    { 23: } 48,
    { 24: } 49,
    { 25: } 50,
    { 26: } 51,
    { 27: } 52
    );

  yyth: array[0..yynstates - 1] of Integer = (
    { 0: } 3,
    { 1: } 11,
    { 2: } 11,
    { 3: } 12,
    { 4: } 12,
    { 5: } 14,
    { 6: } 16,
    { 7: } 18,
    { 8: } 20,
    { 9: } 22,
    { 10: } 23,
    { 11: } 25,
    { 12: } 28,
    { 13: } 30,
    { 14: } 33,
    { 15: } 35,
    { 16: } 37,
    { 17: } 38,
    { 18: } 39,
    { 19: } 41,
    { 20: } 43,
    { 21: } 45,
    { 22: } 47,
    { 23: } 48,
    { 24: } 49,
    { 25: } 50,
    { 26: } 51,
    { 27: } 52
    );

var
  yyn: Integer;

label
  start, scan, action;

begin

  start:

  (* initialize: *)

  yynew;

  scan:

  (* mark positions and matches: *)

  for yyn := yykl[yystate] to yykh[yystate] do
    yymark(yyk[yyn]);
  for yyn := yymh[yystate] downto yyml[yystate] do
    yymatch(yym[yyn]);

  if yytl[yystate] > yyth[yystate] then
    goto action; (* dead state *)

  (* get next character: *)

  yyscan;

  (* determine action: *)

  yyn := yytl[yystate];
  while (yyn <= yyth[yystate]) and not (yyactchar in yyt[yyn].cc) do
    inc(yyn);
  if yyn > yyth[yystate] then
    goto action;
  (* no transition on yyactchar in this state *)

(* switch to new state: *)

  yystate := yyt[yyn].s;

  goto scan;

  action:

  (* execute action: *)

  if yyfind(yyrule) then
  begin
    yyaction(yyrule);
    if yyreject then
      goto action;
  end
  else if not yydefault and yywrap then
  begin
    yyclear;
    return(0);
  end;

  if not yydone then
    goto start;

  yylex := yyretval;

end (*yylex*);

begin
  WordList := TStringList.Create;
  try
    yylex;
  finally
    WordList.Free;
  end;
end.

⌨️ 快捷键说明

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