yyparse.cod 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. (* Yacc parser template (TP Yacc V3.0), V1.2 6-17-91 AG *)
  2. (* global definitions: *)
  3. %%
  4. var yylval : YYSType;
  5. function yylex : Integer; forward;
  6. function yyparse : Integer;
  7. var yystate, yysp, yyn : Integer;
  8. yys : array [1..yymaxdepth] of Integer;
  9. yyv : array [1..yymaxdepth] of YYSType;
  10. yyval : YYSType;
  11. procedure yyaction ( yyruleno : Integer );
  12. (* local definitions: *)
  13. %%
  14. begin
  15. (* actions: *)
  16. case yyruleno of
  17. %%
  18. end;
  19. end(*yyaction*);
  20. (* parse table: *)
  21. %%
  22. const _error = 256; (* error token *)
  23. function yyact(state, sym : Integer; var act : Integer) : Boolean;
  24. (* search action table *)
  25. var k : Integer;
  26. begin
  27. k := yyal[state];
  28. while (k<=yyah[state]) and (yya[k].sym<>sym) do inc(k);
  29. if k>yyah[state] then
  30. yyact := false
  31. else
  32. begin
  33. act := yya[k].act;
  34. yyact := true;
  35. end;
  36. end(*yyact*);
  37. function yygoto(state, sym : Integer; var nstate : Integer) : Boolean;
  38. (* search goto table *)
  39. var k : Integer;
  40. begin
  41. k := yygl[state];
  42. while (k<=yygh[state]) and (yyg[k].sym<>sym) do inc(k);
  43. if k>yygh[state] then
  44. yygoto := false
  45. else
  46. begin
  47. nstate := yyg[k].act;
  48. yygoto := true;
  49. end;
  50. end(*yygoto*);
  51. label parse, next, error, errlab, shift, reduce, accept, abort;
  52. begin(*yyparse*)
  53. (* initialize: *)
  54. yystate := 0; yychar := -1; yynerrs := 0; yyerrflag := 0; yysp := 0;
  55. {$ifdef yydebug}
  56. yydebug := true;
  57. {$else}
  58. yydebug := false;
  59. {$endif}
  60. parse:
  61. (* push state and value: *)
  62. inc(yysp);
  63. if yysp>yymaxdepth then
  64. begin
  65. yyerror('yyparse stack overflow');
  66. goto abort;
  67. end;
  68. yys[yysp] := yystate; yyv[yysp] := yyval;
  69. next:
  70. if (yyd[yystate]=0) and (yychar=-1) then
  71. (* get next symbol *)
  72. begin
  73. yychar := yylex; if yychar<0 then yychar := 0;
  74. end;
  75. if yydebug then writeln('state ', yystate, ', char ', yychar);
  76. (* determine parse action: *)
  77. yyn := yyd[yystate];
  78. if yyn<>0 then goto reduce; (* simple state *)
  79. (* no default action; search parse table *)
  80. if not yyact(yystate, yychar, yyn) then goto error
  81. else if yyn>0 then goto shift
  82. else if yyn<0 then goto reduce
  83. else goto accept;
  84. error:
  85. (* error; start error recovery: *)
  86. if yyerrflag=0 then yyerror('syntax error');
  87. errlab:
  88. if yyerrflag=0 then inc(yynerrs); (* new error *)
  89. if yyerrflag<=2 then (* incomplete recovery; try again *)
  90. begin
  91. yyerrflag := 3;
  92. (* uncover a state with shift action on error token *)
  93. while (yysp>0) and not ( yyact(yys[yysp], _error, yyn) and
  94. (yyn>0) ) do
  95. begin
  96. if yydebug then
  97. if yysp>1 then
  98. writeln('error recovery pops state ', yys[yysp], ', uncovers ',
  99. yys[yysp-1])
  100. else
  101. writeln('error recovery fails ... abort');
  102. dec(yysp);
  103. end;
  104. if yysp=0 then goto abort; (* parser has fallen from stack; abort *)
  105. yystate := yyn; (* simulate shift on error *)
  106. goto parse;
  107. end
  108. else (* no shift yet; discard symbol *)
  109. begin
  110. if yydebug then writeln('error recovery discards char ', yychar);
  111. if yychar=0 then goto abort; (* end of input; abort *)
  112. yychar := -1; goto next; (* clear lookahead char and try again *)
  113. end;
  114. shift:
  115. (* go to new state, clear lookahead character: *)
  116. yystate := yyn; yychar := -1; yyval := yylval;
  117. if yyerrflag>0 then dec(yyerrflag);
  118. goto parse;
  119. reduce:
  120. (* execute action, pop rule from stack, and go to next state: *)
  121. if yydebug then writeln('reduce ', -yyn);
  122. yyflag := yyfnone; yyaction(-yyn);
  123. dec(yysp, yyr[-yyn].len);
  124. if yygoto(yys[yysp], yyr[-yyn].sym, yyn) then yystate := yyn;
  125. (* handle action calls to yyaccept, yyabort and yyerror: *)
  126. case yyflag of
  127. yyfaccept : goto accept;
  128. yyfabort : goto abort;
  129. yyferror : goto errlab;
  130. end;
  131. goto parse;
  132. accept:
  133. yyparse := 0; exit;
  134. abort:
  135. yyparse := 1; exit;
  136. end(*yyparse*);