0

I need some help; I'm trying to program a sort of command prompt with python

I need to split a text file into lines then split them into strings

example :

splitting

command1 var1 var2;
command2 (blah, bleh);
command3 blah (b bleh);
command4 var1(blah b(bleh * var2));

into :

line1=['command1','var1','var2']

line2=['command2']
line2_sub1=['blah','bleh']

line3=['blah']
line3_sub1=['b','bleh']

line4=['command4']
line4_sub1=['blah','b']
line4_sub2=['bleh','var2']
line4_sub2_operand=['*']

Would that be possible at all? If so could some one explain how or give me a piece of code that would do it?

Thanks a lot,

  • 1
    What have you coded so far? – David Harris Nov 24 '12 at 05:25
  • 2
    There's no rhyme or reason here as to why one thing should be one way and another thing should be another way. e.g. why is `command3` completely absent? Why is it `command1` in the first line and `command 2` in the second line? – mgilson Nov 24 '12 at 05:26
  • 3
    Using variables with names like `line1`, `line2`, etc. is not the right way to go about storing data processed from a file, unless you know in advance exactly how many lines you will have (and often even then). You need a data structure to store the results, perhaps a list of lists or a list of dictionaries. Splitting and text file into lines and then splitting lines into words is very easy; however, it looks like you have some fairly complex and possibly poorly-defined rules for how to handle input beyond just splitting things and you'll need to think about those in more detail. – Andrew Gorcester Nov 24 '12 at 05:29
  • Also the representation would usually be a tree, so that instead of `line4_sub1` you should have `line4_sub1_sub1` (to use your own notation). Operations like multiply are usually converted to an RPN-like representation like [multiply [operand1 operand2]]. – tripleee Nov 24 '12 at 08:44

4 Answers4

1

It's been pointed out, that there appears to be no reasoning to your language. All I can do is point you to pyparsing, which is what I would use if I were solving a problem similar to this, here is a pyparsing example for the python language.

John
  • 13,197
  • 7
  • 51
  • 101
0

Like everyone else is saying, your language is confusingly designed and you probably need to simplify it. But I'm going to give you what you're looking for and let you figure that out the hard way.

The standard python file object (returned by open()) is an iterator of lines, and the split() method of the python string class splits a string into a list of substrings. So you'll probably want to start with something like:

for line in command_file
    words = line.split(' ')

http://docs.python.org/3/library/string.html

krasnerocalypse
  • 966
  • 6
  • 6
0

you could use this code to read the file line by line and split it by spaces between words.

a= True
f = open(filename)
while a:
    nextline=f.readline()
    wordlist= nextline.split("")
    print(wordlist)
    if nextline=="\n":
        a= False
scottydelta
  • 1,786
  • 4
  • 19
  • 39
0

What you're talking about is writing a simple programming language. It's not extraordinarily difficult if you know what you're doing, but it is the sort of thing most people take a full semester class to learn. The fact that you've got multiple different types of lexical units with what looks to be a non-trivial, recursive syntax means that you'll need a scanner and a parser. If you really want to teach yourself to do this, this might not be a bad place to start.

If you simplify your grammar such that each command only has a fixed number of arguments, you can probably get away with using regular expressions to represent the syntax of your individual commands.

Give it a shot. Just don't expect it to all work itself out overnight.

acjay
  • 34,571
  • 6
  • 57
  • 100