# Macro for a code translator

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 :)

-
What I have seen is using etoolbox to replace certain tokens in a macro. –  canaaerus Jul 19 '12 at 16:17
This is probably doable with regular expressions. See tex.stackexchange.com/questions/4347/… 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 5:48
TikZ/PGF provides (and uses) a parser module... –  Paul Gaborit Jul 20 '12 at 5:56

(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:

-
Thanks, this is a really nice solution. This would be even better if we can produce the parser automatically from the code... –  Xoff Jul 20 '12 at 6:21
@Xoff: citation: "The syntax for the code definition is not important." ;-) –  Paul Gaborit Jul 20 '12 at 6:25
You're right, and it is enough for what I need. Thank you :) –  Xoff Jul 20 '12 at 6:28

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.

-
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