Skip to content

Instantly share code, notes, and snippets.

@zr-tex8r
Last active May 31, 2023 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zr-tex8r/05d15eef0c46746a826217bf070e8df5 to your computer and use it in GitHub Desktop.
Save zr-tex8r/05d15eef0c46746a826217bf070e8df5 to your computer and use it in GitHub Desktop.
\futurelet 芸人クイズ

問題

ファイルquiz.texのコードは
「引数に指定したトークン列に“ある変換”を適用した結果のトークン列を\Parsedに代入する」
という仕様のマクロ\Parseの実装である。

コードを読んで“ある変換”の内容を把握した上で、\Parse\futurelet使わずに実装せよ。

※LaTeXまたはplain TeXを前提とする。

補足

  • \Parseの入力のトークン列は以下に挙げる文字トークンのみを含むと仮定してよい。
    • カテゴリコード12のASCII数字(09
    • カテゴリコード1の{
    • カテゴリコード2の}
    • カテゴリコード10のASCII空白
\catcode`\@=11 %----------------------------------
\def\Parse#1{\let\Parsed\empty\my@parse@a#1\relax}
\def\my@parse@a{\futurelet\my@tok\my@parse@b}
\def\my@parse@b{\let\my@next\my@parse@c
\if\my@tok\relax \let\my@next\relax
\else\if\my@tok\space \let\my@next\my@parse@d
\else\if\my@tok\bgroup \let\my@next\my@parse@e
\fi\fi\fi \my@next}
\def\my@parse@c{\my@parse@f\S}
\expandafter\def\expandafter\my@parse@d\space{\my@parse@c{ }}
\def\my@parse@e{\my@parse@f\G}
\def\my@parse@f#1#2{%
\expandafter\def\expandafter\Parsed\expandafter{%
\Parsed#1{#2}}%
\my@parse@a}
\catcode`\@=12 %----------------------------------
% LaTeX上でquiz.texのコードを実行する
\input{quiz}
\Parse{ 12{34} 5 {{6}7} }
\show\Parsed %==>\S { }\S {1}\S {2}\G {34}\S { }\S {5}\S { }\G {{6}7}\S { }
\stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment