Question

I have some code being generated by a netlogo simulation which I need to read-in and parse with with Python. The code needs to then be run in a very specific simulation, which will have functions written to match the netlogo code.

For example :

netlogoCode = "ifelse ahead-clear [ move ][ turn-left move ]"

def ahead-cear(self, )
  ### returns true if the environment ahead of the agent is clear

def turn-left(self, )
  ### turns the agent 90 degrees to the left

What I'm struggling to get my head around is how to actually process the program... my main worry is that the ifelse statement could have another ifelse within it and Im not sure how to translate that...

could anyone point me in the right direction?

Was it helpful?

Solution

NetLogo in its full generality is a pretty difficult language to parse. (Here's our real parser: https://github.com/NetLogo/NetLogo/blob/5.0.x/src/main/org/nlogo/compiler/ExpressionParser.scala; and the Repast people also wrote one, apparently using ANTLR, when they built ReLogo's NetLogo model converter, here.)

How large or small is the subset of NetLogo that you need to support?

If the subset you're targeting is small enough, then this becomes mainly a question about how to do parsing in Python. I'm not a Pythonista so I'm not the best person to answer that.

If the subset you're targeting is large enough, you'd be better off writing Python code that fires up a JVM and invokes the NetLogo parser directly, so that you don't have to write your own parser, but can just write Python code that processes already-parsed syntax trees. See my post at https://groups.google.com/d/msg/netlogo-devel/mDBskQMboYs/MycJtLh32S0J for some information on this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top