EBNF-Analysis

Ein Programm zur Syntax Analyse,ähnlich dem in "Niklais Wirth - Compilerbau,Seite 32ff.

First off all the programs EBNF for feed:
* Syntax =     {Production} ".".

  Production = Identifier "=" Expression ".".

  Expression = Term {"|" Term}.

  Term =       Factor {Factor}.

  Factor =     Identifier |
               Literal |
               "(" Expression ")" |
               "[" Expression "]" |
               "{" Expression "}" .

  Identifier=  "A" | "B" | "C" {Identifier}.

  Literal=     "'" ( "a" | "b" | "c" | "1" | "2" | "3") "'".
.
now her's the program:
{ ************************************************************

  Name        : EBNF

  Description : Syntax-Analysis for EBNF,equal to
                program 2,N.Wirth Compilerbau,page 32.

  Date        : 94.09.02

  Author      : H.Mucker

  ************************************************************}

PROGRAM Ebnf (input,output);

CONST MAXSTRLEN = 15;
      LOWER     = ['a'..'z'];
      UPPER     = ['A'..'Z'];
      CIPHER    = ['0'..'9'];


VAR   ch        : char;
      inFile    : text;
      symbol    : tString;


{ ************************************************************

              S T R I N G - P R O D E D U R E S

  ************************************************************}

PROCEDURE StrReadln (VAR ioString : tString);
VAR i  : integer;
    ch : char;
BEGIN
  i:= 1;
  REPEAT
    read(ch);
    ioString[i]:= ch;
    i:= i+1
  UNTIL EOLN(input) or (i > MAXSTRLEN);
  readln;
  IF i <= MAXSTRLEN THEN
    REPEAT
      ioString[i]:= chr(32);
      i:= i+1
    UNTIL i > MAXSTRLEN
END; { StrReadln }


PROCEDURE StrEmpty (var ioString : tString);

VAR i : integer;

BEGIN
  for i:= 1 to MAXSTRLEN do
    ioString[i]:= ' '
END; { StrEmpty }


PROCEDURE StrWrite (inString : tString);

VAR i,e : integer;

BEGIN
  e:= MAXSTRLEN;
  WHILE (inString[e] = chr(32)) and (e >= 1) DO
    e:= e-1;
  FOR i:= 1 TO e DO
    write(inString[i])
END; { StrWrite }


PROCEDURE StrWriteln (inString : tString);
BEGIN
  strWrite(inString);
  writeln
END; { StrWriteln }


FUNCTION StrLen (inString : tString) : integer;

VAR i : integer;

BEGIN
  i:= MAXSTRLEN;
  WHILE (inString[i] = chr(32)) and (i >= 1) DO
    i:= i-1;
  strLen:= i
END; { StrLen }


FUNCTION StrComp (inStringA,inStringB : tString) : boolean;
VAR i     : integer;
    equal : boolean;
BEGIN
  i:= 1;
  REPEAT
    equal:= (inStringA[i] = inStringB[i]);
    i:= i+1
  UNTIL (not equal) or (i > MAXSTRLEN);
  strComp:= equal
END; { StrComp }


{ ************************************************************

           A N A L I Z I N G  - P R O C E D U R E S

{ ************************************************************}

PROCEDURE Error;
BEGIN
  writeln('error');
  halt
END; { Error }


PROCEDURE GetSym;
BEGIN
  REPEAT
    read(inFile,ch);
    {
    write(ch);
    }
    {
    IF EOLN(inFile) THEN
      begin
        writeln;
      end
    }

  UNTIL (ch > ' ');
END; { GetSym }


PROCEDURE Production;

  PROCEDURE Identifier;
  VAR i : integer;
  BEGIN
    StrEmpty(symbol);
    i:= 1;
    WHILE ch in LOWER+UPPER DO
      BEGIN
        symbol[i]:= ch;
        i:= i+1;
        GetSym
      END;
    write('IDENTIFIER: '); StrWriteLn(symbol);
  END; { Identifier }

  PROCEDURE Literal;
  VAR i : integer;
  BEGIN
    StrEmpty(symbol);
    i:= 1;
    WHILE ch <> '"' DO
      BEGIN
        symbol[i]:= ch;
        i:= i+1;
        GetSym
      END;
    write('LITERAL:    '); StrWriteLn(symbol);
  END; { Literal }

  PROCEDURE Expression;

    PROCEDURE Term;

      PROCEDURE Factor;
      BEGIN { Factor }
        IF ch in UPPER+LOWER THEN { Non-Terminal Symbol }
          Identifier
        ELSE IF ch = '"' THEN { Terminal Symbol }
          BEGIN
            GetSym;
            Literal;
            IF ch = '"' THEN GetSym
            ELSE Error
          END
        ELSE IF ch = '(' THEN
          BEGIN
            GetSym;
            Expression;
            IF ch = ')' THEN GetSym
            ELSE Error
          END
        ELSE IF ch = '[' THEN
          BEGIN
            GetSym;
            Expression;
            IF ch = ']' THEN GetSym
            ELSE Error
          END
        ELSE IF ch = '{' THEN
          BEGIN
            GetSym;
            Expression;
            IF ch = '}' THEN GetSym
            ELSE Error
          END
        ELSE Error
      END; { Factor }

    BEGIN { Term }
      Factor;
      WHILE (ch in UPPER+LOWER) or
            (ch = '"') or (ch = '(') or
            (ch = '[') or (ch = '{')  DO
        Factor
    END; { Term }

  BEGIN { Expression }
    Term;
    WHILE ch = '|' DO
      BEGIN
        GetSym;
        Term
      END
  END; { Expression }

BEGIN { Production }
  Identifier;
  IF ch = '=' THEN GetSym
  ELSE error;
  Expression;
  IF ch = '.' THEN GetSym
  ELSE error
END; { Production }


{ ************************************************************

                       P R O G R A M

  ************************************************************}

BEGIN
  assign(inFile,'ebnf');
  reset(inFile);

  GetSym;

  WHILE ch <> '.' DO
    Production;

  writeln;


END.

Alle Touren

Schneebergwege

Raxsteige

Geführte Touren

Perl

Literatur

Musik