Finalisation du lexer

This commit is contained in:
Shadow 2020-05-28 10:07:08 +02:00
parent 47306b42c8
commit 001690c76a
1 changed files with 26 additions and 22 deletions

View File

@ -1,40 +1,44 @@
def lexer(prgm_src):
word = prgm_src.lower().split(" ")
l_token = list()
index, undef = 0, bool()
def lexer(prgm_src):
var_type = {"un entier naturel", "des entiers naturels", "un entier relatif", "des entiers relatifs", "une liste", "des listes", "un flottant", "des flottants", "une chaîne de caractères", "des chaînes de caractères"}
cmnd = {"si", "alors", "sinon", "tant que", "tantque", "pour"}
cmnd = {"afficher", "si", "alors", "sinon", "tant que", "tantque", "pour", "fin", "finsi", "fin si", "fintantque", "fin tantque", "fin tant que", "finpour", "fin pour"}
optr = {"+", "-", "/", "*", "^"}
sptr = {"et", ",", ";", "(", ")", "[", "]", "{", "}", "\"", "\n"}
sptr = {"et", "(", ")", "[", "]", "{", "}", "\"", "\n", "à", "entre", "de", ",", ";", "faire"}
comp = {"est supérieur à", "est supérieur ou égal à", "est inférieur à", "est inférieur ou égal à", "est différent de", "est égal à"}
user = {"saisir", "saisir la valeur de", "saisir les valeurs de", "demander la valeur de", "demander à l'utilisateur la valeur de"}
logi = {"et que", "ou que"}
assi = {"prend la valeur", "sont", "est"}
rang = {"allant", "variant"}
rang = {"allant de # à", "variant entre # et", "variant de # à"}
for i in {"+", "-", "/", "*", "^", "(", ")", "[", "]", "{", "}", "\"", "\n", ",", ";"}:
prgm_src = prgm_src.replace(i, " " + i + " ")
word = [i for i in prgm_src.lower().split(" ") if i != ""]
token = (var_type, cmnd, optr, comp, user, logi, assi, sptr)
name = ("TYPE", "CMND", "OPTR", "COMP", "USER", "LOGI", "ASSI", "SPTR")
l_token = list()
index, undef = 0, bool()
while index < len(word):
token = (var_type, cmnd, optr, comp, user, logi, assi, sptr, rang)
name = ("TYPE", "CMND", "OPTR", "COMP", "USER", "LOGI", "ASSI", "SPTR", "RANG")
while True:
undef = True
for j in range(len(token)):
for k in token[j]:
target = k.split(" ")
if index >= len(word): return l_token
if word[index] in target and detect(word, index, target):
l_token.append((name[j], k))
undef = False
index += len(target)-1
if undef:l_token.append(("UNDEF", word[index]))
index += 1
return l_token
l_token.append((name[j], k))
undef = False
index += len(target)
if undef:
l_token.append((("UNDEF", "NUM")[word[index].isdigit()], word[index]))
index += 1
def detect(mot, index, target):
try:
return not 0 in [target[i] == mot[i + index] for i in range(len(target))]