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

📄 sentence.pas

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

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

(* global definitions: *)
{*
 * 基于单词识别的句子分析
   用法 lex Sentence
 *}

unit Sentence;

interface
uses
  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

function yylex: Integer;

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

      2:
        begin
          state := LookUP;
          return(0);
            {* 遇到句子结尾了,注意这里是在yyaction中,不能用result:=0来返回,只能用定义的return函数*}
        end;

      3:
        state := Verb;
      4:
        state := Adj;
      5:
        state := Adv;
      6:
        state := Noun;
      7:
        state := Prep;
      8:
        state := Pron;
      9:
        state := Conj;

      10:
        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;

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

    end;
  end (*yyaction*);

  (* DFA table: *)

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

const

  yynmarks = 41;
  yynmatches = 41;
  yyntrans = 55;
  yynstates = 30;

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

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

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

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

  yykh: array[0..yynstates - 1] of Integer = (
    { 0: } 0,
    { 1: } 0,
    { 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: } 23,
    { 19: } 25,
    { 20: } 27,
    { 21: } 28,
    { 22: } 29,
    { 23: } 30,
    { 24: } 31,
    { 25: } 33,
    { 26: } 35,
    { 27: } 37,
    { 28: } 39,
    { 29: } 41
    );

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

  yymh: array[0..yynstates - 1] of Integer = (
    { 0: } 0,
    { 1: } 0,
    { 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: } 23,
    { 19: } 25,
    { 20: } 27,
    { 21: } 28,
    { 22: } 29,
    { 23: } 30,
    { 24: } 31,
    { 25: } 33,
    { 26: } 35,
    { 27: } 37,
    { 28: } 39,
    { 29: } 41
    );

  yytl: array[0..yynstates - 1] of Integer = (
    { 0: } 1,
    { 1: } 5,
    { 2: } 14,
    { 3: } 14,
    { 4: } 15,
    { 5: } 16,
    { 6: } 16,
    { 7: } 18,
    { 8: } 20,
    { 9: } 22,
    { 10: } 24,
    { 11: } 26,
    { 12: } 26,
    { 13: } 27,
    { 14: } 29,
    { 15: } 32,
    { 16: } 34,
    { 17: } 37,
    { 18: } 39,
    { 19: } 41,
    { 20: } 42,
    { 21: } 43,
    { 22: } 45,
    { 23: } 47,
    { 24: } 49,
    { 25: } 51,
    { 26: } 52,
    { 27: } 53,
    { 28: } 54,
    { 29: } 55
    );

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

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*);

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 + -