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

📄 yacc.pas

📁 Yacc例子代码
💻 PAS
📖 第 1 页 / 共 4 页
字号:
{-----------------------------------------------------------------------------
 Unit Name: Not available
 Author:    hubdog(陈省)
 Email:     hubdog@263.net
 Purpose:
 History:
            2003-4-11 修改本单元,使其兼容Delphi 7的语法编译器
-----------------------------------------------------------------------------}



(* Yacc parser template (TP Yacc V3.0), V1.2 6-17-91 AG *)

(* global definitions: *)

(*

  TP Yacc - Yet Another Compiler Compiler for Turbo Pascal

  Copyright (C) 1990-92  Albert Graef <ag@muwiinfa.geschichte.uni-mainz.de>
  Copyright (C) 1996     Berend de Boer <berend@pobox.com>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


$Revision: 2 $
$Modtime: 96-08-01 11:24 $


Last changes:

  Version 3.0 as of April 91
  Version 3.0a as of May 92 (bug fixes in precedence and type information
    updates)

$History: YACC.PAS $
 * 
 * *****************  Version 2  *****************
 * User: Berend       Date: 96-10-10   Time: 21:16
 * Updated in $/Lex and Yacc/tply
 * Updated for protected mode, windows and Delphi 1.X and 2.X.



------------------------- Synopsis ------------------------

   Synopsis   yacc [options] yacc-file[.y] [output-file[.pas]]

   Options

   -v  "Verbose:" Yacc generates a readable description of the generated
       parser, written to yacc-file with new extension .lst.

   -d  "Debug:" Yacc generates parser with debugging output.

   Description

   This is a reimplementation of the popular UNIX compiler generator
   Yacc for MS-DOS and Turbo Pascal.

   Differences from UNIX Yacc:

   - Produces output code for Turbo Pascal, rather than for C.

   - Does not support %union definitions. Instead, a value type is declared
     by specifying the type identifier *itself* as the tag of a %token
     or %type definition. Yacc will automatically generate an appropriate
     yylval variable of a variant record type (YYSType) which is capable of
     holding values of any of the types used in %token and %type.

     Type checking is *very* strict. If you use type definitions, then
     any symbol referred to in an action *must* have a type introduced
     in a type definition. Either the symbol must have been assigned a
     type in the definitions section, or the $<type-identifier> notation
     must be used. The syntax of the %type definition has been changed
     slightly to allow definitions of the form
       %type <type-identifier>
     (omitting the nonterminals) which may be used to declare types which
     are not assigned to any grammar symbol, but are used with the
     $<...> construct.

   - The parse tables constructed by this Yacc version are slightly greater
     than those constructed by UNIX Yacc, since a reduce action will only be
     chosen as the default action if it is the *only* action in the state.
     In difference, UNIX Yacc chooses a reduce action as the default action
     whenever it is the only *reduce* action of the state (even if there are
     other shift actions).

     This solves a bug in UNIX Yacc that makes the generated parser start
     error recovery too late with certain types of error productions (see
     also Schreiner/Friedman, "Introduction to compiler construction with
     UNIX," 1985). Also, errors will be caught sooner in most cases where
     standard Yacc would carry out an additional (default) reduction before
     detecting the error.

------------------------- Synopsis ------------------------

*)

{$IFDEF MsDos}
{$M 16384,0,655360}
{$ENDIF}
{$IFDEF DPMI}
{$M 32768}
{$ENDIF}
{$IFDEF Windows}
{$M 32768,0}
{$ENDIF}

{$X+}
{$I-}
program Yacc;

uses
{$IFDEF Debug}
{$IFDEF DPMI}
  YaccChk,
{$ENDIF}
{$ENDIF}
{$IFDEF Windows}
{$IFNDEF Console}
  WinCrt,
{$ENDIF}
{$ENDIF}
  YaccLib, YaccBase, YaccMsgs, YaccSem, YaccTabl, YaccPars;

const ID = 257;
const C_ID = 258;
const LITERAL = 259;
const LITID = 260;
const NUMBER = 261;
const PTOKEN = 262;
const PLEFT = 263;
const PRIGHT = 264;
const PNONASSOC = 265;
const PTYPE = 266;
const PSTART = 267;
const PPREC = 268;
const PP = 269;
const LCURL = 270;
const RCURL = 271;
const ILLEGAL = 272;

var yylval : YYSType;

function yylex : Integer; forward;

function yyparse : Integer;

var yystate, yysp, yyn : Integer;
    yys : array [1..yymaxdepth] of Integer;
    yyv : array [1..yymaxdepth] of YYSType;
    yyval : YYSType;

procedure yyaction ( yyruleno : Integer );
  (* local definitions: *)
begin
  (* actions: *)
  case yyruleno of
   1 : begin
         yyval := yyv[yysp-0];
       end;
   2 : begin
         yyval := yyv[yysp-0];
       end;
   3 : begin
         yyval := yyv[yysp-0];
       end;
   4 : begin
         yyval := yyv[yysp-0];
       end;
   5 : begin
         yyval := yyv[yysp-0];
       end;
   6 : begin
         yyerrok; 
       end;
   7 : begin
         yyerrok; 
       end;
   8 : begin
         yyerrok; 
       end;
   9 : begin
         yyerrok; 
       end;
  10 : begin
         yyerrok; 
       end;
  11 : begin
         yyerrok; 
       end;
  12 : begin
         yyval := yyv[yysp-0];
       end;
  13 : begin
         yyerrok; 
       end;
  14 : begin
         yyval := yyv[yysp-0];
       end;
  15 : begin
         yyval := yyv[yysp-0];
       end;
  16 : begin
         error(rcurl_expected); 
       end;
  17 : begin
         yyval := yyv[yysp-0];
       end;
  18 : begin
         yyerrok; 
       end;
  19 : begin
         yyerrok; 
       end;
  20 : begin
         yyerrok; 
       end;
  21 : begin
         yyval := yyv[yysp-0];
       end;
  22 : begin
         yyval := yyv[yysp-0];
       end;
  23 : begin
         error(rbrace_expected); 
       end;
  24 : begin
         yyval := yyv[yysp-0];
       end;
  25 : begin
         yyval := yyv[yysp-0];
       end;
  26 : begin
         error(rangle_expected); 
       end;
  27 : begin
         yyval := yyv[yysp-0];
       end;
  28 : begin
         sort_types;
         definitions;
         next_section; 
       end;
  29 : begin
         next_section;
         generate_parser;
         next_section; 
       end;
  30 : begin
         yyval := yyv[yysp-5];
       end;
  31 : begin
       end;
  32 : begin
         copy_rest_of_file; 
       end;
  33 : begin
       end;
  34 : begin
         yyerrok; 
       end;
  35 : begin
         error(error_in_def); 
       end;
  36 : begin
         startnt := ntsym(yyv[yysp-0]); 
       end;
  37 : begin
         error(ident_expected); 
       end;
  38 : begin
         copy_code; 
       end;
  39 : begin
         yyval := yyv[yysp-2];
       end;
  40 : begin
         act_prec := 0; 
       end;
  41 : begin
         yyval := yyv[yysp-3];
       end;
  42 : begin
         act_prec := new_prec_level(left); 
       end;
  43 : begin
         yyval := yyv[yysp-3];
       end;
  44 : begin
         act_prec := new_prec_level(right); 
       end;
  45 : begin
         yyval := yyv[yysp-3];
       end;
  46 : begin
         act_prec := new_prec_level(nonassoc); 
       end;
  47 : begin
         yyval := yyv[yysp-3];
       end;
  48 : begin
         yyval := yyv[yysp-2];
       end;
  49 : begin
         yyval := yyv[yysp-1];
       end;
  50 : begin
         act_type := 0; 
       end;
  51 : begin
         act_type := yyv[yysp-1]; add_type(yyv[yysp-1]); 
       end;
  52 : begin
         yyval := yyv[yysp-0];
       end;
  53 : begin
         yyerrok; 
       end;
  54 : begin
         yyerrok; 
       end;
  55 : begin
         error(ident_expected); 
       end;
  56 : begin
         error(error_in_def); 
       end;
  57 : begin
         error(ident_expected); 
       end;
  58 : begin
         if act_type<>0 then
         sym_type^[yyv[yysp-0]] := act_type;
         if act_prec<>0 then
         sym_prec^[yyv[yysp-0]] := act_prec; 
       end;
  59 : begin
         litsym(yyv[yysp-0], 0);
         if act_type<>0 then
         sym_type^[litsym(yyv[yysp-0], 0)] := act_type;
         if act_prec<>0 then
         sym_prec^[litsym(yyv[yysp-0], 0)] := act_prec; 
       end;
  60 : begin
         litsym(yyv[yysp-0], 0);
         if act_type<>0 then
         sym_type^[litsym(yyv[yysp-0], 0)] := act_type;
         if act_prec<>0 then
         sym_prec^[litsym(yyv[yysp-0], 0)] := act_prec; 
       end;
  61 : begin
         litsym(yyv[yysp-1], 0);
         if act_type<>0 then
         sym_type^[litsym(yyv[yysp-1], yyv[yysp-0])] := act_type;
         if act_prec<>0 then
         sym_prec^[litsym(yyv[yysp-1], 0)]  := act_prec; 
       end;
  62 : begin
         litsym(yyv[yysp-1], 0);
         if act_type<>0 then
         sym_type^[litsym(yyv[yysp-1], yyv[yysp-0])] := act_type;
         if act_prec<>0 then
         sym_prec^[litsym(yyv[yysp-1], 0)]  := act_prec; 
       end;
  63 : begin
         yyval := yyv[yysp-0];
       end;
  64 : begin
         yyerrok; 
       end;
  65 : begin
         yyerrok; 
       end;
  66 : begin
         error(ident_expected); 
       end;
  67 : begin
         error(error_in_def); 
       end;
  68 : begin
         error(ident_expected); 
       end;
  69 : begin
         if act_type<>0 then
         sym_type^[ntsym(yyv[yysp-0])] := act_type; 
       end;
  70 : begin
         next_section; 
       end;
  71 : begin
         yyval := yyv[yysp-1];
       end;
  72 : begin
         copy_code; 
       end;
  73 : begin
         next_section; 
       end;
  74 : begin
         yyval := yyv[yysp-4];
       end;
  75 : begin
         yyerrok; 
       end;
  76 : begin
         error(error_in_rule); 
       end;
  77 : begin
         error(error_in_rule); 
       end;
  78 : begin
         start_rule(ntsym(yyv[yysp-0])); 
       end;
  79 : begin
         start_body; 
       end;
  80 : begin
         end_body; 
       end;
  81 : begin
         yyval := yyv[yysp-0];
       end;
  82 : begin
         start_body; 
       end;
  83 : begin
         end_body; 
       end;
  84 : begin
       end;
  85 : begin
         add_symbol(yyv[yysp-0]); yyerrok; 
       end;
  86 : begin
         add_symbol(sym(yyv[yysp-0])); yyerrok; 
       end;
  87 : begin
         add_symbol(sym(yyv[yysp-0])); yyerrok; 
       end;
  88 : begin
         add_action; yyerrok; 
       end;
  89 : begin
         error(error_in_rule); 
       end;
  90 : begin
         copy_action; 
       end;
  91 : begin
         yyval := yyv[yysp-2];
       end;
  92 : begin
         copy_single_action; 
       end;
  93 : begin
       end;
  94 : begin
         add_rule_prec(yyv[yysp-0]); 
       end;
  95 : begin
         yyval := yyv[yysp-3];
       end;
  96 : begin
         add_rule_prec(litsym(yyv[yysp-0], 0)); 
       end;
  97 : begin
         yyval := yyv[yysp-3];
       end;
  98 : begin
         add_rule_prec(litsym(yyv[yysp-0], 0)); 
       end;
  99 : begin
         yyval := yyv[yysp-3];
       end;
 100 : begin
         yyval := yyv[yysp-1];
       end;
 101 : begin
       end;
 102 : begin
         add_action; 
       end;
  end;
end(*yyaction*);

(* parse table: *)

type YYARec = record
                sym, act : Integer;
              end;
     YYRRec = record
                len, sym : Integer;
              end;

const

yynacts   = 251;
yyngotos  = 146;
yynstates = 128;
yynrules  = 102;

yya : array [1..yynacts] of YYARec = (
{ 0: }
{ 1: }
  ( sym: 256; act: 12 ),
  ( sym: 262; act: 13 ),
  ( sym: 263; act: 14 ),
  ( sym: 264; act: 15 ),
  ( sym: 265; act: 16 ),
  ( sym: 266; act: 17 ),
  ( sym: 267; act: 18 ),
  ( sym: 269; act: 19 ),
  ( sym: 270; act: 20 ),
{ 2: }
  ( sym: 0; act: 0 ),
{ 3: }
{ 4: }
{ 5: }
{ 6: }
  ( sym: 256; act: 24 ),
  ( sym: 257; act: 25 ),
{ 7: }
  ( sym: 60; act: 28 ),
  ( sym: 256; act: -50 ),
  ( sym: 257; act: -50 ),
  ( sym: 262; act: -50 ),
  ( sym: 263; act: -50 ),
  ( sym: 264; act: -50 ),
  ( sym: 265; act: -50 ),
  ( sym: 266; act: -50 ),
  ( sym: 267; act: -50 ),
  ( sym: 269; act: -50 ),
  ( sym: 270; act: -50 ),
{ 8: }
{ 9: }
{ 10: }
{ 11: }
{ 12: }
{ 13: }
{ 14: }
{ 15: }
{ 16: }
{ 17: }
{ 18: }
{ 19: }
{ 20: }
{ 21: }
  ( sym: 256; act: 34 ),
  ( sym: 271; act: 35 ),
{ 22: }
  ( sym: 256; act: 39 ),
  ( sym: 270; act: 20 ),
  ( sym: 258; act: -70 ),
{ 23: }
{ 24: }
{ 25: }
{ 26: }
  ( sym: 256; act: 43 ),
  ( sym: 257; act: 25 ),
  ( sym: 262; act: -49 ),
  ( sym: 263; act: -49 ),
  ( sym: 264; act: -49 ),
  ( sym: 265; act: -49 ),
  ( sym: 266; act: -49 ),
  ( sym: 267; act: -49 ),
  ( sym: 269; act: -49 ),
  ( sym: 270; act: -49 ),
{ 27: }
  ( sym: 257; act: 25 ),
{ 28: }
{ 29: }
  ( sym: 60; act: 28 ),
  ( sym: 256; act: -50 ),
  ( sym: 257; act: -50 ),
  ( sym: 259; act: -50 ),
  ( sym: 260; act: -50 ),
{ 30: }
  ( sym: 60; act: 28 ),
  ( sym: 256; act: -50 ),
  ( sym: 257; act: -50 ),
  ( sym: 259; act: -50 ),
  ( sym: 260; act: -50 ),
{ 31: }
  ( sym: 60; act: 28 ),
  ( sym: 256; act: -50 ),
  ( sym: 257; act: -50 ),
  ( sym: 259; act: -50 ),
  ( sym: 260; act: -50 ),
{ 32: }
  ( sym: 60; act: 28 ),
  ( sym: 256; act: -50 ),
  ( sym: 257; act: -50 ),
  ( sym: 259; act: -50 ),
  ( sym: 260; act: -50 ),
{ 33: }
{ 34: }
{ 35: }
{ 36: }
  ( sym: 258; act: 51 ),
{ 37: }
  ( sym: 124; act: 56 ),
  ( sym: 256; act: 57 ),
  ( sym: 258; act: 51 ),
  ( sym: 0; act: -29 ),
  ( sym: 269; act: -29 ),
{ 38: }
{ 39: }
{ 40: }
{ 41: }
  ( sym: 44; act: 61 ),
  ( sym: 256; act: 62 ),
  ( sym: 257; act: 25 ),
  ( sym: 262; act: -48 ),
  ( sym: 263; act: -48 ),
  ( sym: 264; act: -48 ),
  ( sym: 265; act: -48 ),
  ( sym: 266; act: -48 ),
  ( sym: 267; act: -48 ),
  ( sym: 269; act: -48 ),
  ( sym: 270; act: -48 ),
{ 42: }
{ 43: }

⌨️ 快捷键说明

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