6

Is there an existing macro that "translate" tokens using some code ? For example :

\def\code{{{a,b},{b,c},{c,a}}}
\translate[\code]{caac} % => would typeset "abba"

or even better something like that :

\def\code{{{ab,ba},{ac,ca},{b,c}{c,b}}}
\translate[\code]{abbcac} % => would typeset "bacbca"

The syntax for the code definition is not important.

If there is no such macro, any proposition is welcomed to obtain an efficient solution to this problem.

Thank you :)

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
Xoff
  • 1,691
  • What I have seen is using etoolbox to replace certain tokens in a macro. – bodo Jul 19 '12 at 16:17
  • This is probably doable with regular expressions. See http://tex.stackexchange.com/questions/4347/regular-expressions-with-pdftex for a starting point. There are also regex questions and answers on stackoverflow. Regular expressions have a steep learning curve leading to a big payoff. – Ethan Bolker Jul 19 '12 at 16:45
  • In fact, we need some kind of parsing strategy. Regexp can be used for such a thing but are not useful for doing the parse itself... – Xoff Jul 20 '12 at 05:48
  • TikZ/PGF provides (and uses) a parser module... – Paul Gaborit Jul 20 '12 at 05:56

2 Answers2

6

A reasonably efficient implementation of the first one is

\def\translate[#1]#2{{%
\expandafter\xlc#1\relax
\lowercase{#2}}}

\def\xlc#1{%
 \ifx\relax#1\else
 \xxlc#1%
 \expandafter
 \xlc
 \fi}

\def\xxlc#1,#2{\lccode`#1=`#2 }

\def\code{{a,b}{b,c}{c,a}}

\translate[\code]{caac}


\bye

which typesets abba if run through plain TeX.

David Carlisle
  • 757,742
  • This is not as general as I hoped, but it's very nice to use the lowercase macro to achieve the goal, I like it :) – Xoff Jul 19 '12 at 16:45
6

(edit: first parser of my initial answer was not using the right rules.)

Here's a TikZ/PGF solution:

\documentclass{standalone}
\usepackage{pgf}
\usepgfmodule{parser}

\pgfparserdef{myparser}{initial}{the letter a}{b}
\pgfparserdef{myparser}{initial}{the letter b}{c}
\pgfparserdef{myparser}{initial}{the letter c}{a}
\pgfparserdef{myparser}{initial}{the character ;}{\pgfparserswitch{final}}


\pgfparserdef{myparser2}{initial}{the letter a}{\pgfparserswitch{s2}}
\pgfparserdef{myparser2}{s2}{the letter b}{ba\pgfparserswitch{initial}}
\pgfparserdef{myparser2}{s2}{the letter c}{ca\pgfparserswitch{initial}}
\pgfparserdef{myparser2}{initial}{the letter b}{c}
\pgfparserdef{myparser2}{initial}{the letter c}{b}
\pgfparserdef{myparser2}{initial}{the character ;}{\pgfparserswitch{final}}

\newcommand\myparserone[1]{%
  \expandafter\pgfparserparse\expandafter{\expandafter myparser\expandafter}#1;%
}

\newcommand\myparsertwo[1]{%
  \expandafter\pgfparserparse\expandafter{%
    \expandafter myparser2\expandafter}#1;%
}

\begin{document}
\textbf{\pgfparserparse{myparser}caac; \pgfparserparse{myparser2}abbcac;}
\emph{\myparserone{caac} \myparsertwo{abbcac}}
\end{document}

The result:

parsers results

Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283