📄 expressionl.pas
字号:
(* lexical analyzer template (TP Lex V3.0), V1.0 3-2-91 AG *)
(* global definitions: *)
{*
下面的例子改编自lex和yacc第二版
识别简单算术表达式的lex
用法:Lex Expression ExpressionL.dpr
*}
unit ExpressionL;
interface
uses
SysUtils, LexLib, Yacclib;
const
NUMBER=258;
function yylex: Integer;
implementation
uses ExpressionY;
function yylex : Integer;
procedure yyaction ( yyruleno : Integer );
(* local definitions: *)
begin
(* actions: *)
case yyruleno of
1:
begin
yylval := StrToInt(yytext);
return(NUMBER);
end;
2:
; {/* 忽略空格 *}
3:
return(0); {* 当回车时,重置状态 *}
4:
returnc(yytext[1]); {returnc用来返回字符串,另外要返回的第一个字符是yytext[1]而不是yytext[0]}
end;
end(*yyaction*);
(* DFA table: *)
type YYTRec = record
cc : set of Char;
s : Integer;
end;
const
yynmarks = 7;
yynmatches = 7;
yyntrans = 10;
yynstates = 7;
yyk : array [1..yynmarks] of Integer = (
{ 0: }
{ 1: }
{ 2: }
1,
4,
{ 3: }
2,
4,
{ 4: }
3,
{ 5: }
4,
{ 6: }
1
);
yym : array [1..yynmatches] of Integer = (
{ 0: }
{ 1: }
{ 2: }
1,
4,
{ 3: }
2,
4,
{ 4: }
3,
{ 5: }
4,
{ 6: }
1
);
yyt : array [1..yyntrans] of YYTrec = (
{ 0: }
( cc: [ #1..#8,#11..#31,'!'..'/',':'..#255 ]; s: 5),
( cc: [ #9,' ' ]; s: 3),
( cc: [ #10 ]; s: 4),
( cc: [ '0'..'9' ]; s: 2),
{ 1: }
( cc: [ #1..#8,#11..#31,'!'..'/',':'..#255 ]; s: 5),
( cc: [ #9,' ' ]; s: 3),
( cc: [ #10 ]; s: 4),
( cc: [ '0'..'9' ]; s: 2),
{ 2: }
( cc: [ '0'..'9' ]; s: 6),
{ 3: }
{ 4: }
{ 5: }
{ 6: }
( cc: [ '0'..'9' ]; s: 6)
);
yykl : array [0..yynstates-1] of Integer = (
{ 0: } 1,
{ 1: } 1,
{ 2: } 1,
{ 3: } 3,
{ 4: } 5,
{ 5: } 6,
{ 6: } 7
);
yykh : array [0..yynstates-1] of Integer = (
{ 0: } 0,
{ 1: } 0,
{ 2: } 2,
{ 3: } 4,
{ 4: } 5,
{ 5: } 6,
{ 6: } 7
);
yyml : array [0..yynstates-1] of Integer = (
{ 0: } 1,
{ 1: } 1,
{ 2: } 1,
{ 3: } 3,
{ 4: } 5,
{ 5: } 6,
{ 6: } 7
);
yymh : array [0..yynstates-1] of Integer = (
{ 0: } 0,
{ 1: } 0,
{ 2: } 2,
{ 3: } 4,
{ 4: } 5,
{ 5: } 6,
{ 6: } 7
);
yytl : array [0..yynstates-1] of Integer = (
{ 0: } 1,
{ 1: } 5,
{ 2: } 9,
{ 3: } 10,
{ 4: } 10,
{ 5: } 10,
{ 6: } 10
);
yyth : array [0..yynstates-1] of Integer = (
{ 0: } 4,
{ 1: } 8,
{ 2: } 9,
{ 3: } 9,
{ 4: } 9,
{ 5: } 9,
{ 6: } 10
);
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*);
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -