1

So I know this may be a stupid question and is most likely impossible but is there a way in pyparsing to create keywords (such as print for python) I am trying to create a interpreter for a different language in python so that you can write in this language on android (as python files can be run on python but the other language can't). For instance in this language there is a PUT statement that prints out is there a way in pyparsing to "define" this put statement so that when I import this interpreter I can write PUT "Hello, World!" instead of (a = 'PUT "Hello, World"', Result = p.parseString(a), print result[1])

brendan
  • 11
  • 3
  • 1
    I'm confused, are you asking how to run your custom language within a Python interpreter? – jonrsharpe Aug 05 '15 at 15:47
  • Yeah that is the basic idea. – brendan Aug 05 '15 at 17:12
  • You can [edit Python's syntax](http://stackoverflow.com/q/214881/3001761), but that's a long way from trivial. Note, however, that implementing an interpreter using Python is not the same thing as modifying Python's interpreter to read your language. – jonrsharpe Aug 05 '15 at 17:15

1 Answers1

0

Rather than write a whole new language, you can add keywords to Python like is done in this example (http://pyparsing.wikispaces.com/file/view/stateMachine2.py/110934709/stateMachine2.py) on the pyparsing wiki. The parser picks out your custom keywords, and replaces them with the expanded Python code that implements that command, then compiles this code as a regular Python module.

If you want to write a pure DSL, look at the adventure example on that same wiki examples page, in which I implemented a simple DSL for running an adventure game using simple commands.

PaulMcG
  • 62,419
  • 16
  • 94
  • 130