Parsing pseudo code/language in Python
So I need to write a parser (or simulator) which would take an input file with simple code written in my own pseudo code like language, for instance:
a = 5
b = 5 * a
[FOR 10]
b = b * 5
[ENDFOR]
[IF b>30]
a = a + 3
[ENDIF]
So the pseudo language only supports integer variables, basic operations with them (+,-,/,*), a basic for loop and a basic if statement. I need to build parser that will in the end deliver the final values of a and b (or any other variables used in the code).
I was thinking of trying to do this in XML so I simulate the loop and the if with tags, but I am not really sure if this is the right (or most efficient) approach. Any suggestions?
Quick edit ^^: It's not about my own programming language...it's part of a bigger project...I need a simple way of eval开发者_运维知识库uating small snippets of code written like the example and get the states of the variables used after simulating it...thats why I wanted to use XML...this is not intended to be a programming lanuage of any sort...
Much of this may already be implemented using the examples from the pyparsing wiki, such as this one, or this one, which uses the more current operatorPrecedence
helper method.
EDIT Links to PyParsing Wikispace are dead but you can found an another wiki on the github repository from here : https://github.com/pyparsing/pyparsing/wiki
Take a look at PLY. Python's implementation of glorious LEX/YACC. You can definitely write a compiler or interpreter for your language with this tool.
精彩评论