PL/0

PL/0(ピーエルゼロ)はプログラミング言語の名称。少なくとも2種類の言語が知られており、その1つは IBM の汎用プロ�…

PL/0(ピーエルゼロ)はプログラミング言語の名称。少なくとも2種類の言語が知られており、その1つは IBM の汎用プログラミング言語 PL/I のサブセットである。

本項で詳述するもう一方のPL/0は、教育目的でPascalを簡略化したバージョンのプログラミング言語である。主にコンパイラの設計開発の実例として使用される。1975年、ニクラウス・ヴィルトの著書 Algorithms + Data Structures = Programs で紹介されたのが最初である。言語の構成要素は非常に小さく、実数はサポートしておらず、算術演算子も必要最小限で、"if" と "while" 以外の制御構文を持たない。そのような様々な制限があるため、この言語で実用的プログラムを書くのは現実的ではないが、コンパイラ自体は非常に小さく単純に作成可能である。

文法

以下はEBNFで定義されたこの言語の構文規則である。

program = block "." .

block = [ "const" ident "=" number {"," ident "=" number} ";"]
        [ "var" ident {"," ident} ";"]
        { "procedure" ident ";" block ";" } statement .
statement = [ ident ":=" expression | "call" ident |
            "begin" statement {";" statement } "end" |
            "if" condition "then" statement |
            "while" condition "do" statement ].
condition = "odd" expression |
            expression ("="|"#"|"<"|"<="|">"|">=") expression .
expression = [ "+"|"-"] term { ("+"|"-") term}.
term = factor {("*"|"/") factor}.
factor = ident | number | "(" expression ")".

このような単純な文法に対して再帰下降パーサを書く方が学生にとっては易しい。従って、PL/0 コンパイラは世界中の教育機関で広く使われている。本来の仕様に機能が少ないため、学生は言語とコンパイラの拡張に時間をかけることになる。例えば、REPEAT .. UNTIL を導入したり、プロシージャへの引数渡しを実装したり、配列・文字列・浮動小数点数といったデータ型を追加したりといった拡張である。

コンパイラ構築

1976年12月、ヴィルトはコンパイラ構築に関する小さな本を出版したが、その中に PL/0 コンパイラの全ソースコードが含まれていた。前述の構文規則はそのヴィルトの本 Compilerbau の初版にあったものである[1]。同書の後の版ではヴィルトの研究の進展の影響も受けて、PL/0 の文法は変更されている。例えば、constprocedure といったキーワードは大文字に変更された。この変更で PL/0 は Modula-2 に見た目が似ることとなった。同時にヴィルトの友人アントニー・ホーアCommunicating Sequential Processesの中で感嘆符 ! と疑問符 ? を通信プリミティブの記法として採用した。ヴィルトはこれらを PL/0 に導入したが、その意味を書籍で解説していない。

使用例

以下の例は PL/0E という拡張された言語によるものである[2]

VAR x, squ;

PROCEDURE square;
BEGIN
   squ := x * x
END;

BEGIN
   x := 1;
   WHILE x <= 10 DO
   BEGIN
      CALL square;
      ! squ;
      x := x + 1;
   END
END.

このプログラムは 1 から 10 までの数の二乗を出力する。大学などで教える際には感嘆符の代わりに WriteLn プロシージャを使用することが多い。

以下の例はヴィルトの Compilerbau 第二版に掲載されたものである(1986年、ドイツ)[1]

CONST
  m =  7,
  n = 85;

VAR
  x, y, z, q, r;

PROCEDURE multiply;
VAR a, b;

BEGIN
  a := x;
  b := y;
  z := 0;
  WHILE b > 0 DO BEGIN
    IF ODD b THEN z := z + a;
    a := 2 * a;
    b := b / 2;
  END
END;

PROCEDURE divide;
VAR w;
BEGIN
  r := x;
  q := 0;
  w := y;
  WHILE w <= r DO w := 2 * w;
  WHILE w > y DO BEGIN
    q := 2 * q;
    w := w / 2;
    IF w <= r THEN BEGIN
      r := r - w;
      q := q + 1
    END
  END
END;

PROCEDURE gcd;
VAR f, g;
BEGIN
  f := x;
  g := y;
  WHILE f # g DO BEGIN
    IF f < g THEN g := g - f;
    IF g < f THEN f := f - g;
  END;
  z := f
END;

BEGIN
  x := m;
  y := n;
  CALL multiply;
  x := 25;
  y :=  3;
  CALL divide;
  x := 84;
  y := 36;
  CALL gcd;
END.

Oberon-0

ヴィルトはコンパイラ構築に関する本の第三版(最新版)で PL/0 から Oberon-0 に置き換えている。コンパイラ全体は同書の中で紹介されているが、PL/0 よりも Oberon-0 の方が言語としての機能は充実している。例えば、Oberon-0 には配列、レコード、型宣言、プロシージャ引数がある。ヴィルトの書籍の出版社(Addison-Wesley)は彼の書籍を絶版にすることを決定したが、ヴィルトは 2005年に第三版を完成させ、それをオンラインで公開した[3]

関連項目

脚注

  1. ^ a b Wirth, Niklaus (1986), Compilerbau, B.G. Teubner, Stuttgart ISBN 3-519-32338-9
  2. ^ PL/0E
  3. ^ The (2005) of Compiler Construction, Niklaus Wirth, 1996, ISBN 0-201-40353-6第三版の印刷された書籍は滅多にないが、オンラインで公開されている。

参考文献

外部リンク

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.