ときどきの雑記帖 RE* (新南口)
人間なんて
しずえさんと違って 60秒チャレンジ!ルイージを救え!!|任天堂 は三択なので 今一つ面白みが…
one true awk
one true awk のリポジトリで白熱したやり取り(違)が。
syntax error in regular expression $^ · Issue #145 · onetrueawk/awk
要はgawkやmawkなどはそうではないのに、one true awk で
$^
という正規表現を使うと「syntax error」になるのはおかしい
というのがissueを起こした人の主張のようなんだけど。
awk/b.c at 275a80ff33ef2782f27c4fd720b65645c89e2bbb · onetrueawk/awk
Node *reparse(const char *p) /* parses regular expression pointed to by p */
{ /* uses relex() to scan regular expression */
Node *np;
DPRINTF("reparse <%s>\n", p);
lastre = prestr = (const uschar *) p; /* prestr points to string to be parsed */
rtok = relex();
/* GNU compatibility: an empty regexp matches anything */
if (rtok == '\0') {
/* FATAL("empty regular expression"); previous */
return(op2(EMPTYRE, NIL, NIL));
}
np = regexp();
if (rtok != '\0')
FATAL("syntax error in regular expression %s at %s", lastre, prestr);
return(np);
}
エラーメッセージからするとここか。 まあ「syntax error」ではなくても他のawkでも 無効な正規表現にはなっていると思うんだがなあ。
ドキュメントを読もう(1)
Qiitaからネタ拾い。
末尾に空白フィールドができるような場合に、splitの結果から その空白フィールドが消えて困ったから、代わりのメソッドを自作しました。 ということなんだけど、引数二つのString.split使えばいい話のような。
public String[] split(String regex, int limit)
この文字列を、指定された正規表現に一致する位置で分割します。
(略)
limitパラメータは、このパターンの適用回数を制御するため、結果となる配列の長さに影響を及ぼします。制限nがゼロより大きい場合、 このパターンは最大でn - 1回まで適用され、配列の長さはn以下となります。配列の最後のエントリには、 最後に一致した区切り文字以降の入力シーケンスがすべて含まれます。nが負の値の場合、このパターンの適用回数と配列の長さは制限されません。 nがゼロの場合、このパターンの適用回数と配列の長さは制限されませんが、後続の空の文字列は破棄されます。
public String[] split(String regex)
この文字列を、指定された正規表現に一致する位置で分割します。
このメソッドの動作は、2つの引数を取るsplitメソッドを、指定された式および制限引数ゼロを指定して呼び出した場合と同じになります。 つまり、結果として得られる配列には後続の空の文字列は含まれません。
まあこういう振る舞いの変更を符号付整数で指定するのが良いのかどうかはまた別の問題 (なんかそう遠くない過去にPythonだかRubyで同じような話を書いたような記憶が?)。
ドキュメントを読もう(2)
C言語のstatic がメモリを保つことの検証実験 - Qiita
あるひとつの処理系(実装)での結果をもって「〇〇では▲▲である」 みたいなことを言うのは止めよう。 とか、コンパイラーの出力を見るにしてもアセンブリ言語レベルで調べてほしいとか。
このコメント は華麗にスルーされているっぽいな😓
GNU Compiler Collection
前回 GCCからJavaが削除されていた という話を書いたけど、他にもGCCを使って実装したプログラミング言語あったよな ということでざっと調べた。
Some of these front ends are very much works in progress; others are very mature.
とはあるけど Pascalとか止まってない?
- GNU Pascal Compiler (GPC).
- Mercury, a declarative logic/functional language. The University of Melbourne Mercury compiler is written in Mercury; originally it compiled via C but now it also has a back end that generates assembler directly, using the GCC back end.
- Cobol For GCC (at an early stage of development).
- GNU Modula-2 implements the PIM2, PIM3, PIM4 and ISO dialects of the language. The compiler is fully operational with GCC 10 and GCC 11 (on GNU/Linux x86 systems). Work is in progress to move the front end to the GCC trunk. The front end is mostly written in Modula-2 and it includes a bootstrap tool which translates Modula-2 into C/C++.
- Modula-3 (for links see www.modula3.org); SRC M3 is based on an old version of GCC and PM3 and CAM3 derive from SRC M3. This compiler is written in Modula-3; for copyright and licensing reasons neither the small amount of C code that links to GCC and provides the interface to the back end, nor the front end proper, is likely to be integrated in GCC, nor is the front-end likely to change to a more normal interface of linking directly to the back end.
- GHDL is a GCC front end for the VHDL (IEEE 1076) hardware design language. GHDL and its runtime library are written in Ada95 using GNAT and are distributed under the GPL. Currently they only support GNU/Linux x86 systems.
- PL/1 for GCC is a GCC front end for the PL/I language.
- GCC Unified Parallel C (GCC UPC) is a compilation and execution environment for Unified Parallel C.
Algol-68も見た覚えがあるんだけどここにはないな。
Pascal
GNU Pascalのページを見ると
Download - The GNU Pascal Manual
GNU Pascal uses the compiler back-end from the GNU Compiler Collection, GNU CC or GCC. If you want to compile GPC, you will need the source of GCC as well as the source of GPC itself. From the same place as GPC, please download GCC 2.8.1, 2.95.x, 3.2.x, 3.3.x or 3.4.x. (It is also available from any GNU mirror; see http://www.gnu.org/software/gcc/.)
これだものねえ…
情報処理技術者試験
IPA 独立行政法人 情報処理推進機構:情報処理技術者試験における出題範囲・シラバス等の変更内容の公表について(基本情報技術者試験、情報セキュリティマネジメント試験の通年試験化)
個別プログラム言語(C、Java、Python、アセンブラ言語、表計算ソフト)による出題は、 普遍的・本質的なプログラミング的思考力を問う擬似言語による出題に統一します。
ということらしいので
https://www.jitec.ipa.go.jp/1_13download/fe_kamoku_b_sample.pdf
にあるサンプルを見てみると
○整数型: fee(整数型: age)
整数型: ret
if (age が 3 以下)
ret ← 100
elseif ( )
ret ← 300
else
ret ← 500
endif
return ret
整数型の配列: array ← {1, 2, 3, 4, 5}
整数型: right, left
整数型: tmp
for (left を 1 から (arrayの要素数 ÷ 2 の商) まで 1 ずつ増やす)
right ← □
tmp ← array[right]
array[right] ← array[left]
■ ← tmp
endfor
大域: ListElement: listHead ← 未定義の値
○append(文字型: qVal)
ListElement: prev, curr
curr ← ListElement(qVal)
if (listHead が □)
listHead ← curr
else
prev ← listHead
while (prev.next が 未定義でない)
prev ← prev.next
endwhile
prev.next ← ■
endif
○整数型配列の配列: transformSparseMatrix(整数型の二次元配列: matrix)
整数型: i, j
整数型配列の配列: sparseMatrix
sparseMatrix ← {{}, {}, {}} /* 要素数0の配列を三つ要素にもつ配列 */
for (i を 1 から matrixの行数 まで 1 ずつ増やす)
for (j を 1 から matrixの列数 まで 1 ずつ増やす)
if (matrix[i, j] が 0 でない)
sparseMatrix[1]の末尾 に iの値 を追加する
sparseMatrix[2]の末尾 に jの値 を追加する
sparseMatrix[3]の末尾 に matrix[i, j]の値 を追加する
endif
endfor
endfor
return sparseMatrix
大域: Words: words /* 英単語群が格納されている */
/* c1の次にc2が出現する割合を返す */
○実数型: prob(文字型: c1, 文字型: c2)
文字列型: s1 ← c1の1文字だけから成る文字列
文字列型: s2 ← c2の1文字だけから成る文字列
if (words.freq(s1 + s2) が 0 より大きい)
return □
else
return 0
endif
ふむ。 ifやfor、whileのあとの式にカッコが必須(らしい)のはCっぽい一方で それらの本体を括るのはブレースでもbegin/endでもなく、 特別なキーワード(endwhileなど)で 文の並びを閉じると。
行頭の〇が関数定義の印?
Why I like shell scripts
— Marek Gibney (@marekgibney) April 24, 2022
Every machine has bash installed
Other languages change faster
Maintenance cost of bash scripts is lower
Figured out something in the terminal?
Paste it into a bash script verbatim
Perfect glue language
To glue together commands, nothing beats bash
Every machine has bash installed
にちょっと引っかかるものがあるけど、
世間一般(ってなんですか)的にはそういうものなのかもしれない。