开发者

How to parse S-expression in Erlang?

I am implementing client agent for Robocup Soccer simulator in Erlang. Simulator sends sensory information to client in form of S-expressions. Like this

(see 15 ((f c) 2 0 0 0) ((f r t) 64.1 -32) ((f r b) 64.1 32) ((f g r b) 55.1 7) 
 ((g r) 54.6 0) ((b) 2 0 -0 0) ((l r) 54.6 90)) 
(see 16 ((f r t) 72.2 -44) ((f r b) 54.1 20) ((f g r b) 52.5 -10) ((g r) 54.1 -17)
 ((l r) 51.4 -开发者_如何学编程89))

Simulator sends such type of sensor informatio in each cycle(100-200 msec). The main format of the information is:

(see Time ObjInfo ObjInfo . . . )

The ObjInfos are of the format below:

(ObjName Distance Direction [DistChange DirChange [BodyFac- ingDir HeadFacingDir]])

where the objects are like: (b) Ball, (g r) Right goal, (f ...) represents various flags.

What I want is to parse this information and store/update in some database(record) to use for analysis. The main difficulty I am facing is to Parse this information. Please suggest me some way of doing this? (does Erlang contain any library for such work)


Yecc and Leex are your friends: http://erlang.org/doc/apps/parsetools/index.html

Leex is a lexical analyzer generator for Erlang which will tokenize your data. Yecc is LALR-1 parser generator that can parse your tokens into meaningful structures.

There's a good blog post by Relops, Leex And Yecc, detailing some of the basics.


If you load LFE (Lisp Flavoured Erlang) it contains a lisp scanner and parser. The modules you need are lfe_scan, lfe_parse and lfe_io which wraps the other two. The scanner is written using leex (source is lfe_scan.xrl) while the parser is hand written as there are some features of how yecc works which didn't quite fit.


The correct approach would be to just write a small LISP reader.

The quick and (very) dirty way (for initial testing ONLY): Substitute whitespace with a comma, "(" with "{" and ")" with "}". Then you have an erlang literal.

Have a look at erl_scan and erl_parse.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜