📄 commandline.dpr
字号:
(* lexical analyzer template (TP Lex V3.0), V1.0 3-2-91 AG *)
(* global definitions: *)
{参考lex和yacc第二版实现的对于命令行参数的分析}
//重新定义get_char和unget_char来实现对命令行参数的分析
//注意这里没有实现yylineno和yycolno因为没有意义
program CommandLine.dpr;
{$APPTYPE CONSOLE} //注意别忘了指定控制台模式
uses
//CommandLex是改写后的LexLib,重定义了get_char, unget_char和put_char三个函数,以便
//使用命令行参数作为解析对象。
SysUtils, Classes, CommandLex, Windows;
function yylex: Integer;
procedure yyaction(yyruleno: Integer);
(* local definitions: *)
begin
(* actions: *)
case yyruleno of
1,
2,
3:
begin
writeln(format('用法是: %s [-help | -h | -? ] [-verbose | -v] [(-file| -f) filename]', [GetCommandLine]));
return(0);
end;
4,
5:
writeln('打开Verbose模式');
end;
end (*yyaction*);
(* DFA table: *)
type
YYTRec = record
cc: set of Char;
s: Integer;
end;
const
yynmarks = 5;
yynmatches = 5;
yyntrans = 14;
yynstates = 15;
yyk: array[1..yynmarks] of Integer = (
{ 0: }
{ 1: }
{ 2: }
{ 3: }
1,
{ 4: }
2,
{ 5: }
4,
{ 6: }
{ 7: }
{ 8: }
{ 9: }
{ 10: }
3,
{ 11: }
{ 12: }
{ 13: }
{ 14: }
5
);
yym: array[1..yynmatches] of Integer = (
{ 0: }
{ 1: }
{ 2: }
{ 3: }
1,
{ 4: }
2,
{ 5: }
4,
{ 6: }
{ 7: }
{ 8: }
{ 9: }
{ 10: }
3,
{ 11: }
{ 12: }
{ 13: }
{ 14: }
5
);
yyt: array[1..yyntrans] of YYTrec = (
{ 0: }
(cc: ['-']; s: 2),
{ 1: }
(cc: ['-']; s: 2),
{ 2: }
(cc: ['?']; s: 4),
(cc: ['h']; s: 3),
(cc: ['v']; s: 5),
{ 3: }
(cc: ['e']; s: 6),
{ 4: }
{ 5: }
(cc: ['e']; s: 7),
{ 6: }
(cc: ['l']; s: 8),
{ 7: }
(cc: ['r']; s: 9),
{ 8: }
(cc: ['p']; s: 10),
{ 9: }
(cc: ['b']; s: 11),
{ 10: }
{ 11: }
(cc: ['o']; s: 12),
{ 12: }
(cc: ['s']; s: 13),
{ 13: }
(cc: ['e']; s: 14)
{ 14: }
);
yykl: array[0..yynstates - 1] of Integer = (
{ 0: } 1,
{ 1: } 1,
{ 2: } 1,
{ 3: } 1,
{ 4: } 2,
{ 5: } 3,
{ 6: } 4,
{ 7: } 4,
{ 8: } 4,
{ 9: } 4,
{ 10: } 4,
{ 11: } 5,
{ 12: } 5,
{ 13: } 5,
{ 14: } 5
);
yykh: array[0..yynstates - 1] of Integer = (
{ 0: } 0,
{ 1: } 0,
{ 2: } 0,
{ 3: } 1,
{ 4: } 2,
{ 5: } 3,
{ 6: } 3,
{ 7: } 3,
{ 8: } 3,
{ 9: } 3,
{ 10: } 4,
{ 11: } 4,
{ 12: } 4,
{ 13: } 4,
{ 14: } 5
);
yyml: array[0..yynstates - 1] of Integer = (
{ 0: } 1,
{ 1: } 1,
{ 2: } 1,
{ 3: } 1,
{ 4: } 2,
{ 5: } 3,
{ 6: } 4,
{ 7: } 4,
{ 8: } 4,
{ 9: } 4,
{ 10: } 4,
{ 11: } 5,
{ 12: } 5,
{ 13: } 5,
{ 14: } 5
);
yymh: array[0..yynstates - 1] of Integer = (
{ 0: } 0,
{ 1: } 0,
{ 2: } 0,
{ 3: } 1,
{ 4: } 2,
{ 5: } 3,
{ 6: } 3,
{ 7: } 3,
{ 8: } 3,
{ 9: } 3,
{ 10: } 4,
{ 11: } 4,
{ 12: } 4,
{ 13: } 4,
{ 14: } 5
);
yytl: array[0..yynstates - 1] of Integer = (
{ 0: } 1,
{ 1: } 2,
{ 2: } 3,
{ 3: } 6,
{ 4: } 7,
{ 5: } 7,
{ 6: } 8,
{ 7: } 9,
{ 8: } 10,
{ 9: } 11,
{ 10: } 12,
{ 11: } 12,
{ 12: } 13,
{ 13: } 14,
{ 14: } 15
);
yyth: array[0..yynstates - 1] of Integer = (
{ 0: } 1,
{ 1: } 2,
{ 2: } 5,
{ 3: } 6,
{ 4: } 6,
{ 5: } 7,
{ 6: } 8,
{ 7: } 9,
{ 8: } 10,
{ 9: } 11,
{ 10: } 11,
{ 11: } 12,
{ 12: } 13,
{ 13: } 14,
{ 14: } 14
);
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
yylex;
readln;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -