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

📄 lexrules.pas

📁 Yacc例子代码
💻 PAS
📖 第 1 页 / 共 2 页
字号:
{
  Parser for Lex grammar rules.

  This module implements a parser for Lex grammar rules. It should
  probably be reimplemented using Lex and Yacc, but the irregular
  lexical structure of the Lex language makes that rather tedious,
  so I decided to use a conventional recursive-descent-parser
  instead.


  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 6:30 $

$History: LEXRULES.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.

}


unit LexRules;

interface

uses LexBase, LexTable;


procedure parse_rule ( rule_no : Integer );
  (* rule parser (rule_no=number of parsed rule) *)

(* Return values of rule parser: *)

var

expr, stmt : String;
  (* expression and statement part of rule *)
cf   : Boolean;
  (* caret flag *)
n_st : Integer;
  (* number of start states in prefix *)
st   : array [1..max_states] of Integer;
  (* start states *)
r    : RegExpr;
  (* parsed expression *)

implementation

uses LexMsgs;

(* Scanner routines:

   The following routines provide access to the source line and handle
   macro substitutions. To perform macro substitution, an input buffer
   is maintained which contains the rest of the line to be parsed, plus
   any pending macro substitutions. The input buffer is organized as
   a stack onto which null-terminated replacement strings are pushed
   as macro substitutions are processed (the terminating null-character
   is used as an endmarker for macros, in order to keep track of the
   number of pending macro substitutions); characters are popped from the
   stack via calls to the get_char routine.

   In order to perform macro substitution, the scanner also has to
   maintain some state information to be able to determine when it
   is scanning quoted characters, strings or character classes (s.t.
   no macro substitution is performed in such cases).

   The scanner also keeps track of the current source line position in
   variable act_pos; if there are any macro substitutions on the stack,
   act_pos will point to the position of the original macro call in the
   source line. This is needed to give proper error diagnostics. *)

const max_chars = 2048;

var

act_pos, bufptr : Integer;
  (* current position in source line and input stack pointer *)
buf : array [1..max_chars] of Char;
  (* input buffer *)
str_state, cclass_state, quote_state : Boolean;
  (* state information *)
n_macros : Integer;
  (* number of macros currently on stack *)

procedure mark_error ( msg : String; offset : Integer );
  (* mark error position (offset=offset of error position (to the left of
     act_pos) *)
  begin
    if n_macros=0 then
      error(msg, act_pos-offset)
    else
      error(msg+' in regular definition', act_pos)
  end(*mark_error*);

procedure put_str(str : String);
  (* push str onto input stack *)
  var i : Integer;
  begin
    inc(bufptr, length(str));
    if bufptr>max_chars then fatal(macro_stack_overflow);
    for i := 1 to length(str) do
      buf[bufptr-i+1] := str[i];
  end(*put_str*);

procedure init_scanner;
  (* initialize the scanner *)
  begin
    act_pos := 1; bufptr := 0;
    str_state := false; cclass_state := false; quote_state := false;
    n_macros := 0;
    put_str(line);
  end(*init_scanner*);

function act_char : Char;
  (* current character (#0 if none) *)
  function push_macro : Boolean;
    (* check for macro call at current position in input buffer *)
    function scan_macro ( var name : String ) : Boolean;
      var i : Integer;
      begin
        if (bufptr>1) and
           (buf[bufptr]='{') and (buf[bufptr-1] in letters) then
          begin
            name := '{'; i := bufptr-1;
            while (i>0) and (buf[i] in alphanums) do
              begin
                name := name+buf[i];
                dec(i);
              end;
            if (i>0) and (buf[i]='}') then
              begin
                scan_macro := true;
                name := name+'}';
                bufptr := i-1;
              end
            else
              begin
                scan_macro := false;
                mark_error(syntax_error, -length(name));
                bufptr := i;
              end
          end
        else
          scan_macro := false
      end(*scan_macro*);
    var name : String;
    begin
      if scan_macro(name) then
        begin
          push_macro := true;
{$ifdef fpc}
          with sym_table^[key(name, max_keys, @lookup, @entry)] do
{$else}
          with sym_table^[key(name, max_keys, lookup, entry)] do
{$endif}
            if sym_type=macro_sym then
              begin
                put_str(subst^+#0);
                inc(n_macros);
              end
            else
              mark_error(undefined_symbol, -1)
        end
      else
        push_macro := false
    end(*push_macro*);
  function pop_macro : Boolean;
    (* check for macro endmarker *)
    begin
      if (bufptr>0) and (buf[bufptr]=#0) then
        begin
          dec(bufptr);
          dec(n_macros);
          if n_macros=0 then act_pos := length(line)-bufptr+1;
          pop_macro := true;
        end
      else
        pop_macro := false
    end(*pop_macro*);
  begin
    if not (str_state or cclass_state or quote_state) then
      while push_macro do while pop_macro do ;
    if bufptr=0 then
      act_char := #0
    else
      begin
        while pop_macro do ;
        act_char := buf[bufptr];
      end
  end(*act_char*);

procedure get_char;
  (* get next character *)
  begin
    if bufptr>0 then
      begin
        case buf[bufptr] of
          '\' : quote_state := not quote_state;
          '"' : if quote_state then
                  quote_state := false
                else if not cclass_state then
                  str_state := not str_state;
          '[' : if quote_state then
                  quote_state := false
                else if not str_state then
                  cclass_state := true;
          ']' : if quote_state then
                  quote_state := false
                else if not str_state then
                  cclass_state := false;
          else  quote_state := false;
        end;
        dec(bufptr);
        if n_macros=0 then
          act_pos := length(line)-bufptr+1;
      end
  end(*get_char*);

(* Semantic routines: *)

procedure add_start_state ( symbol : String );
  (* add start state to st array *)
  begin
{$ifdef fpc}
    with sym_table^[key(symbol, max_keys, @lookup, @entry)] do
{$else}
    with sym_table^[key(symbol, max_keys, lookup, entry)] do
{$endif}
      if sym_type=start_state_sym then
        begin
          if n_st>=max_start_states then exit; { this shouldn't happen }
          inc(n_st);
          st[n_st] := start_state;
        end
      else
        mark_error(undefined_symbol, length(symbol))
  end(*add_start_state*);

(* Parser: *)

procedure parse_rule ( rule_no : Integer );

  procedure rule ( var done : Boolean );

    (* parse rule according to syntax:

       rule			: start_state_prefix caret
				  expr [ '$' | '/' expr ]
				;

       start_state_prefix	: /* empty */
				| '<' start_state_list '>'
				;

       start_state_list         : ident { ',' ident }
                                ;

       caret			: /* empty */
				| '^'
				;

       expr			: term { '|' term }
				;

       term			: factor { factor }
				;

       factor			: char
				| string
				| cclass
				| '.'
				| '(' expr ')'
				| factor '*'
				| factor '+'
				| factor '?'
				| factor '{' num [ ',' num ] '}'
				;
    *)

    procedure start_state_prefix ( var done : Boolean );
      procedure start_state_list ( var done : Boolean );
        procedure ident ( var done : Boolean );
          var idstr : String;
          begin(*ident*)
            done := act_char in letters;   if not done then exit;
            idstr := act_char;
            get_char;
            while act_char in alphanums do
              begin
                idstr := idstr+act_char;
                get_char;
              end;
            add_start_state(idstr);

⌨️ 快捷键说明

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