简体   繁体   English

如何将数学表达式拆分为标记? (字符串-> [String])

[英]How to split a mathematical expression into tokens? (String -> [String])

I know my question sounds generic and I didn't really know how to google for such a thing, so I'll provide examples of what I mean: 我知道我的问题听起来很普通,我真的不知道如何用Google搜索这种事情,因此,我将提供一些示例说明我的意思:

ghci> splitTok "12 + 564 * (5 - 38) / 223"
["12", "+", "564", "*", "(", "5", "-", "38", ")", "/", "223"]
ghci> splitTok " 5+2-42  *    (46/5 )"
["5", "+", "2", "-", "42", "*", "(", "46", "/", "5", ")"]

How could something like that be implemented? 这样的事情怎么实现? Or maybe there is a function in Prelude that does this type of thing? 或者在Prelude中有一个函数可以执行此类操作?

It seems just to group the digits together and removes the space. 似乎只是将数字分组在一起并删除空格。 Try 尝试

import Data.Char (isSpace, isDigit)
import Data.List (groupBy)

splitTok = groupBy (\x y->isDigit x && isDigit y) . filter (not . isSpace)

Okay, I came up with an ugly hack that does the job. 好的,我想到了一个可以完成此工作的丑陋的骇客。 (Unfortunately introduces a bug) (不幸地引入了一个错误)

import Data.Char (isSpace, isDigit)

strip :: String -> String
strip = filter (not . isSpace)

addSpacing :: String -> String
addSpacing [a] = [a]
addSpacing (x:y:cs)
    | isDigit x && isDigit y = x : addSpacing rest
    | otherwise = x : ' ' : addSpacing rest
  where rest = y : cs

splitTok :: String -> [String]
splitTok = words . addSpacing . strip

It fails in producing a correct string of tokens in this example: 在此示例中,它无法生成正确的令牌字符串:

ghci> splitTok "125 +   12 62 - 12  *( 51/  3)        "
["125","+","1262","-","12","*","(","51","/","3",")"]

For most expressions it works fine though: 对于大多数表达式,它可以正常工作:

ghci> splitTok "4123-36522+12"
["4123","-","36522","+","12"]
ghci> splitTok "124 *(12 -(4+5*(331/7)))"
["124","*","(","12","-","(","4","+","5","*","(","331","/","7",")",")",")"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM