[Haskell-cafe] How to deal with a translator???

Donald Bruce Stewart dons at cse.unsw.edu.au
Mon Feb 13 02:14:48 EST 2006

>    Hello all,
>    Now I am trying on a simple translator module. This module
>    needs to translate an input string( this string represent a
>    formula) as follows:
>    (x^2 - 5x + 4=0) /\ (x^3 - 5>0) -> x>3
>    And I want the output string represent a formula like:
>    (x^2 - 5x + 4=0) /\ (x^3 - 5>0) IMPLIES x>3, i.e, the
>    translator attempt to translate (->) into IMPLIES.
>    If you have ay suggesstions for that kind of problems,
>    please share with me. I appreciate for that. Thanks all for
>    your time.
>    Nguyen,

If that's all you want to do, then something like this:

    main = interact trans
            trans []           = []
            trans ('-':'>':xs) = "IMPLIES" ++ trans xs
            trans (x:xs)       = x : trans xs

Simple Char problems can usually be solved with pattern matching this way.

For more complex problems you'll probably want to write a lexer and parser for
your language, and then do transformations on the abstract syntax. Parsec and
Alex or Happy are good for this.


More information about the Haskell-Cafe mailing list