Python Program to separate terminal and non-terminal symbols?

0

A few days ago I left another question about it, back then I did not have any code, but now I have something done and I need help. The idea is the following: I have this in a txt:

program > begin statementlist end
statementlist > statement statementtail
statementtail > statement statementtail
statementtail > 
statement > ip dosp igual expresion pcoma
statement > read pari exprlist pard pcoma
statement > write pari exprlist pard pcoma
idlist > id idtail
idtail > coma id idtail
idtail > 
exprlist > expresion exprtail
exprtail > coma expresion exprtail
exprtail > 
expresion > primary primarytail
primarytail > addop primary primarytail
primarytail > 
primary > pari expresion pard
primary > id
primary > intliteral
addop > mas
addop > menos
systemgoal > program

The activity is "simple". I have to make a program that separates the terminal symbols from the nonterminals, basically separating the left side of the right one. BUT NOT USING SPLIT (). My idea was to take all the txt as a string, give it a list () and have all the letters in a list, then go through the list with a variable var taking letter by letter and add them to a list (Let's call it lnoterm), find the symbol '>' stop adding to lnoterm and start adding to another list called lterm, and still, add values to that list until you find a '\ n', and keep adding to the lnoterm list. I intended to do that in the following code, but it does not work for me. Any observation is welcome, the truth is that I am resuming the programming and it has been difficult for me to do the exercise. I feel that I have very simple logical errors and it frustrates me that I do not get out. Help! D:

with open("D:/Tecnologico/Materias Actuales/Lenguajes y Automatas/Gramatica/gramatica.txt",'r') as myfile:
    data=myfile.read()
print(list(data))
separate=list(data)
lnoterm=[]
lterm=[]
x=0
y=0
var=0


def noterm(var):
    global x    
    car=separate[var]
    if car!='>':
        lnoterm.insert(x,separate[var])
        x=x+1
        var=var+1        
        return noterm(var)
    else:
        term(var)

def term(var):
    global y
    car=separate[var]
    if car!='\n':
        lterm.insert(y,separate[var])
        y=y+1
        var=var+1
        return term(var)
    else:
        noterm(var)

def main():    
    car=separate[var]    
    if var!=len(separate):
        noterm(var)
    else:        
        print('No terminales: ',' ',join(lnoterm))

        print('Terminales: ',' ',join(lterm))


if __name__=='__main__':
    main()
    
asked by Rainerio Amezquita Sanchez 06.11.2018 в 23:54
source

0 answers