dimanche 19 avril 2015

How to split code string properly

I am trying to write a basic lexical analyzer for Java in Python. The problem I am facing right now is splitting a line of string into words/tokens.


Example:



if (x < 3)
{
x = 3;
}
else
{
x = 0;
}


I want this to return a list like this:



["if", "(", "x", "<", "3", ")", ...


But my code is returning



["if", "(x", "<", "3)"]


My Code:



for line in code.readlines():
for word in line.split():
print word


I searched for a solution but only found solutions using regular expressions, is there a way to do this without regular expressions? Because I have no idea how to use them and I do not have enough time right now to learn it...


Any help will be appreciated...


Aucun commentaire:

Enregistrer un commentaire