📄 expressionl3.pas
字号:
(* lexical analyzer template (TP Lex V3.0), V1.0 3-2-91 AG *)
(* global definitions: *)
{*
下面的例子改编自lex和yacc第二版
识别简单算术表达式的lex
用法:Lex Expression ExpressionL.dpr
*}
unit ExpressionL3;
interface
uses
SysUtils, LexLib, Yacclib;
const
NUMBER=258;
NAME=257;//注意这里的token必须是hard-coding的常数,yacc生成的和lex生成的是不匹配的
function yylex: Integer;
implementation
uses ExpressionY3;
function yylex : Integer;
procedure yyaction ( yyruleno : Integer );
(* local definitions: *)
begin
(* actions: *)
case yyruleno of
1:
begin
yylval.yyreal:=StrToFloat(yytext);
return(NUMBER);
end;
2:
; {* 忽略空格 *}
3:
begin
yylval.yyinteger := ord(yytext[1]) - ord('a');
return(NAME);
end;
4:
return(0); {* 输入结束 *}
5,
6:
returnc(yytext[1]);
end;
end(*yyaction*);
(* DFA table: *)
type YYTRec = record
cc : set of Char;
s : Integer;
end;
const
yynmarks = 14;
yynmatches = 14;
yyntrans = 26;
yynstates = 15;
yyk : array [1..yynmarks] of Integer = (
{ 0: }
{ 1: }
{ 2: }
1,
6,
{ 3: }
6,
{ 4: }
2,
6,
{ 5: }
3,
6,
{ 6: }
4,
6,
{ 7: }
5,
{ 8: }
6,
{ 9: }
1,
{ 10: }
{ 11: }
1,
{ 12: }
{ 13: }
{ 14: }
1
);
yym : array [1..yynmatches] of Integer = (
{ 0: }
{ 1: }
{ 2: }
1,
6,
{ 3: }
6,
{ 4: }
2,
6,
{ 5: }
3,
6,
{ 6: }
4,
6,
{ 7: }
5,
{ 8: }
6,
{ 9: }
1,
{ 10: }
{ 11: }
1,
{ 12: }
{ 13: }
{ 14: }
1
);
yyt : array [1..yyntrans] of YYTrec = (
{ 0: }
( cc: [ #1..#8,#11..#31,'!'..'#','%'..'-','/',':'..'`',
'{'..#255 ]; s: 8),
( cc: [ #9,' ' ]; s: 4),
( cc: [ #10 ]; s: 7),
( cc: [ '$' ]; s: 6),
( cc: [ '.' ]; s: 3),
( cc: [ '0'..'9' ]; s: 2),
( cc: [ 'a'..'z' ]; s: 5),
{ 1: }
( cc: [ #1..#8,#11..#31,'!'..'#','%'..'-','/',':'..'`',
'{'..#255 ]; s: 8),
( cc: [ #9,' ' ]; s: 4),
( cc: [ #10 ]; s: 7),
( cc: [ '$' ]; s: 6),
( cc: [ '.' ]; s: 3),
( cc: [ '0'..'9' ]; s: 2),
( cc: [ 'a'..'z' ]; s: 5),
{ 2: }
( cc: [ '.' ]; s: 10),
( cc: [ '0'..'9' ]; s: 9),
{ 3: }
( cc: [ '0'..'9' ]; s: 11),
{ 4: }
{ 5: }
{ 6: }
{ 7: }
{ 8: }
{ 9: }
( cc: [ '.' ]; s: 10),
( cc: [ '0'..'9' ]; s: 9),
{ 10: }
( cc: [ '0'..'9' ]; s: 11),
{ 11: }
( cc: [ '0'..'9' ]; s: 11),
( cc: [ 'E','e' ]; s: 12),
{ 12: }
( cc: [ '+','-' ]; s: 13),
( cc: [ '0'..'9' ]; s: 14),
{ 13: }
( cc: [ '0'..'9' ]; s: 14),
{ 14: }
( cc: [ '0'..'9' ]; s: 14)
);
yykl : array [0..yynstates-1] of Integer = (
{ 0: } 1,
{ 1: } 1,
{ 2: } 1,
{ 3: } 3,
{ 4: } 4,
{ 5: } 6,
{ 6: } 8,
{ 7: } 10,
{ 8: } 11,
{ 9: } 12,
{ 10: } 13,
{ 11: } 13,
{ 12: } 14,
{ 13: } 14,
{ 14: } 14
);
yykh : array [0..yynstates-1] of Integer = (
{ 0: } 0,
{ 1: } 0,
{ 2: } 2,
{ 3: } 3,
{ 4: } 5,
{ 5: } 7,
{ 6: } 9,
{ 7: } 10,
{ 8: } 11,
{ 9: } 12,
{ 10: } 12,
{ 11: } 13,
{ 12: } 13,
{ 13: } 13,
{ 14: } 14
);
yyml : array [0..yynstates-1] of Integer = (
{ 0: } 1,
{ 1: } 1,
{ 2: } 1,
{ 3: } 3,
{ 4: } 4,
{ 5: } 6,
{ 6: } 8,
{ 7: } 10,
{ 8: } 11,
{ 9: } 12,
{ 10: } 13,
{ 11: } 13,
{ 12: } 14,
{ 13: } 14,
{ 14: } 14
);
yymh : array [0..yynstates-1] of Integer = (
{ 0: } 0,
{ 1: } 0,
{ 2: } 2,
{ 3: } 3,
{ 4: } 5,
{ 5: } 7,
{ 6: } 9,
{ 7: } 10,
{ 8: } 11,
{ 9: } 12,
{ 10: } 12,
{ 11: } 13,
{ 12: } 13,
{ 13: } 13,
{ 14: } 14
);
yytl : array [0..yynstates-1] of Integer = (
{ 0: } 1,
{ 1: } 8,
{ 2: } 15,
{ 3: } 17,
{ 4: } 18,
{ 5: } 18,
{ 6: } 18,
{ 7: } 18,
{ 8: } 18,
{ 9: } 18,
{ 10: } 20,
{ 11: } 21,
{ 12: } 23,
{ 13: } 25,
{ 14: } 26
);
yyth : array [0..yynstates-1] of Integer = (
{ 0: } 7,
{ 1: } 14,
{ 2: } 16,
{ 3: } 17,
{ 4: } 17,
{ 5: } 17,
{ 6: } 17,
{ 7: } 17,
{ 8: } 17,
{ 9: } 19,
{ 10: } 20,
{ 11: } 22,
{ 12: } 24,
{ 13: } 25,
{ 14: } 26
);
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 + -