Grammatical Terminal Symbols and Non-Terminals PYTHON?

0

I am doing a program for my automaton class. But now they have asked us for a somewhat complicated program and I would like them to help me with some detail (I do not want them to do my homework, but I am looking for ideas) The problem is, that in the following grammar:

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

I have terminal and non-terminal symbols. Until then everything clear. The activity proposes to carry out a program capable of reading from a txt the grammar shown and also separating the terminal symbols from the nonterminals. (Basically separate the left side of the right). All this I tried to do with lists, but it did not work for me. An important detail is that I HAVE NOT ALLOWED TO USE SPLIT (). Any ideas? Any contribution will be appreciated.

    
asked by Rainerio Amezquita Sanchez 05.11.2018 в 03:21
source

1 answer

1
"""
Puedes evitar el uso de split, usando algunas expresiones regulares.
Tambien puedes crear alguna variable que contenga tus "palabras reservadas", y luego evaluar cada posicion de RES (del ejemplo).
"""
import re
t = "statementtail > statement statementtail"
res = re.findall("(\w+|>)", t)
#res ['statementtail', '>', 'statement', 'statementtail']
    
answered by 05.11.2018 / 18:24
source