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

📄 expmain.pas

📁 编译原理源程序扫描
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit ExpMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, ComCtrls, Buttons, ToolWin, StdCtrls, ImgList, StrUtils,
  ExtCtrls, Gauges;

type
  TfrmMain = class(TForm)
    StatusBar1: TStatusBar;
    MainMenu1: TMainMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    N6: TMenuItem;
    N7: TMenuItem;
    N8: TMenuItem;
    N9: TMenuItem;
    N10: TMenuItem;
    N11: TMenuItem;
    N12: TMenuItem;
    N13: TMenuItem;
    N14: TMenuItem;
    N15: TMenuItem;
    N16: TMenuItem;
    N17: TMenuItem;
    N18: TMenuItem;
    N19: TMenuItem;
    N20: TMenuItem;
    ToolBar1: TToolBar;
    memText: TMemo;
    btnOne: TToolButton;
    btnTwo: TToolButton;
    btnThree: TToolButton;
    ToolButton13: TToolButton;
    btnExit: TToolButton;
    ImageList1: TImageList;
    panText: TPanel;
    memClass: TMemo;
    Gauge1: TGauge;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    btnEdit: TToolButton;
    ToolButton2: TToolButton;
    ToolButton1: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    procedure N7Click(Sender: TObject);
    procedure btnOneClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnTwoClick(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure btnThreeClick(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure btnEditClick(Sender: TObject);
  private
    { Private declarations }

    function WhatType(strWord: String;intExp: Integer): Integer;
    function GetChar(strSource: String; intI: Integer): String;
    function ShowLabel(intL,intT,intC: Integer; strCaption: String): Integer;
    function WordProcess(intType,intPos,intExp: Integer;strAWord,strChar: String): Integer;

    procedure InitPro;
    procedure FreeLabel;
    procedure SetGauge(intP,intT: Integer);
    procedure ShowWord(strWord: String;intC,intExp: Integer);
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;
  //定义显示的颜色
  AColor: Array[0..6] of TColor;
  //定义字符缓冲区
  strText: string;
  //定义空格
  SPACE: String;
  //定义关键字
//  keywords: Array[1..8] of String;
  //定义运算符
  operator: Array[1..10] of String;
  //定义分隔符
  separator: Array[1..8] of String;
  //定义保留字
  reservedwords: Array[1..12] of String;

  //用来存储当前位置,以确定在什么地方来显示字符
  intX,intY: Integer;
  //指定字符高度
  intH: Integer;

implementation

{$R *.dfm}

//模拟DFA,判断当前数是否为实数
//分四种状态,初始为0,只有在1状态下才可识别“.”号
//该函数能识别常规正实数
//参数: strWord为要判断的单词
//返回值:Integer
//    4:表示strWord合法,是实数
//   -1:表示strWord非法,不是实数
function funDFA(strWord: String): Integer;
var
  intLen,intPos,intState: Integer;
  strChar: String;
begin
  intLen  := Length(strWord);
  intState := 0;
  funDFA := 4;
  for intPos := 1 to intLen do
  begin
    strChar := frmMain.GetChar(strWord,intPos);
    if (CompareStr(strChar,'0')<0) or (CompareStr(strChar,'9')>0) then
      if (strChar='.') and (intState=1) then
        intState := 2
      else
      begin
        funDFA := -1;
        exit;
      end
    else
    begin
      if intState=0 then intState := 1;
      if intState=2 then intState := 3;
    end;
  end;
end;

//判断单词是否结束,并给出结束单词的后一字符是空格、回车符、分隔符还是运算符
//参数:
//  strChar:要判断的字符
//返回值: Integer
//  未结束:0
//  空格: 32
//  回车: 13
//  分隔符: 3
//  运算符:2
function IsOver(strChar: String): Integer;
var
  intI: Integer;
begin
  IsOver := 0;
  if strChar=SPACE then //是空格?
  begin
    IsOver := 32;
    exit;
  end;
  if strChar=#13 then //是回车符?
  begin
    IsOver := 13;
    exit;
  end;
  for intI := Low(separator) to High(separator) do //是分隔符?
    if strChar=separator[intI] then
    begin
      IsOver := 3;
      exit;
    end;
  for intI := Low(operator) to High(operator) do  //是运算符?
    if strChar=operator[intI] then
    begin
      IsOver := 2;
      exit;
    end;
end;

//一个延时的过程,纯粹是演示用。
//MSecs单位为毫秒(千分之1秒)
procedure Delay(MSecs: Longint);
var
  FirstTickCount, Now: Longint;
begin
  FirstTickCount := GetTickCount();
  repeat
    Application.ProcessMessages;
    Now := GetTickCount();
  until (Now - FirstTickCount >= MSecs) or (Now < FirstTickCount);
end;

//从字符缓冲区strSource中读取指定位置的字符
//参数:
//  strSource: 字符缓冲区
//  intI: 当前读指针位置
//返回值: String
function TfrmMain.GetChar(strSource: String; intI: Integer): String;
begin
  GetChar := MidStr(strSource,intI,1);
end;

//在Panel容器panText的指定位置,显示指定颜色的文字
function TfrmMain.ShowLabel(intL,intT,intC: Integer; strCaption: String): Integer;
var
  labShow: TLabel;
begin
  labShow := TLabel.Create(nil);
  labShow.Top := intT;
  labShow.Left := intL;
  if intC=-1 then//为非法的单词(目前为非法的实数)时,加背景色
  begin
    labShow.Color := clRed;
    labShow.Font.Color := clBlack;
  end
  else
    labShow.Font.Color := AColor[intC];
  labShow.Font.Size := 11;
  labShow.Font.Style := [fsBold];
  labShow.Font.Name := 'Times New Roman';
  labShow.Caption := strCaption;
  labShow.Parent := panText;
  //显示横向出界时的处理
  if labShow.Left+labShow.Width>panText.Width then
  begin
    intX := 5;
    intY := intY + intH;
    labShow.Top := intY;
    labShow.Left := intX;
  end;
  ShowLabel := labShow.Width;
end;

//判断当前单词是否为:保留字,或实数,或不合法的实数
//参数:
//  strWord:要判断的单词
//  intExp: 是哪个实验
//返回值:Integer
//  保留字:1
//  实数: 4
//  不合法的实数:-1
//  其它情况视为标识符,返回0
function TfrmMain.WhatType(strWord: String;intExp: Integer): Integer;
var
  intI,intLen: Integer;
  strTmp: String;
begin
  WhatType := 0;
  for intI := Low(reservedwords) to High(reservedwords) do//是否是保留字
    if strWord=reservedwords[intI] then
    begin
      WhatType := 1;//是保留字,返回1
      exit;
    end;
  intLen := Length(strWord);    //是否是实数
  for intI := 1 to intLen do
  begin
    strTmp := GetChar(strWord,intI);
    if ((CompareStr(strTmp,'0')<0) or (CompareStr(strTmp,'9')>0)) and (strTmp<>'.') then
      if intI=1 then//第一个为非数字或"."
        break
      else
      begin//第一个字符为数字,后有数字及"."之外的字符
        if intExp=3 then
          WhatType := -1;//不正确的实数形式
        exit;
      end;
    if intI=intLen then
    begin
      if intExp=3 then
        WhatType := funDFA(strWord)//使用DFA来判断当前字是否为合法实数
      else
        WhatType := 4; //是实数,返回4
      break;
    end;
  end;
end;


//处理已经取出来的单词
//参数:
//  intType:结束单词的字符类型
//  intPos:读指针在整个字符串中的位置
//  intExp:用于哪个实验
//  strAWord:取出来的单词
//  strChar:结束单词的字符
//返回值:当前指针位置
function TfrmMain.WordProcess(intType,intPos,intExp: Integer;strAWord,strChar: String): Integer;
begin
  if intType=13 then//单词后的一个字符(strChar)是回车符
  begin
    ShowWord(strAWord,WhatType(Trim(strAWord),intExp),intExp);//将单词显示出来
    ShowWord(#13,0,intExp);
    intPos := intPos + 1; //回车后定会有换行符,所以去掉
  end;
  if intType=32 then//单词后的一个字符(strChar)是空格
    ShowWord(strAWord+strChar,WhatType(Trim(strAWord),intExp),intExp);//连同空格一起显示出来
  if intType=3 then//单词后的一个字符(strChar)是分隔符
  begin
    ShowWord(strAWord,WhatType(Trim(strAWord),intExp),intExp);//将单词显示出来
    showWord(strChar,intType,intExp);//显示分隔符
  end;
  if intType=2 then//单词后的一个字符(strChar)是运算符
  begin
    ShowWord(strAWord,WhatType(Trim(strAWord),intExp),intExp);//将单词显示出来
    strAWord := strChar;
    strChar := GetChar(strText,intPos+1);
    if IsOver(strChar)=2 then//运算符是否为两位。如:>=,<=
    begin

⌨️ 快捷键说明

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