In [1]:
# coding: utf-8
In [2]:
from Parser import Parser
from Node import Node
In [3]:
Il faut voir dans le terme 'token' la notion abstraite 'lexeme', c'est à dire le représentant de plusieurs formes.
ainsi 'U' représentera 'deux', 'trois', 'cinq' ou 'six' pour le parseur.
In [7]:
class GrammarNumbers(Parser):
"""
Classe à découper en au moins deux classes, le lexique, et la syntaxe
afin de rendre ces classes modulables.
"""
tokens = (
"ET",
"PLURIEL",
"U1",
"U",
"U4",
"U7",
"D1",
"UV1",
"UV",
"D2",
"D",
"D6",
"C",
"M",
"MM",
"MMM"
)
# Les variables et fonctions prefixées par 't_' signifient qu'elles sont lexicales au sens d'un parseur
# Vous pouvez tester cette fonction en instanciant GrammaireNumbers et en lançant la fonction
# lexicalise(data) héritée de Parser.
# Elle restreint les éléments que nous pouvons utilisées
# Une règle lexicale peut se présenter sont la forme d'une variable ou d'une fonction
t_U = r"deux|trois|cinq|six"
t_U1 = r"une?"
t_U4 = r"quatre"
t_U7 = r"sept|huit|neuf"
t_UV = r"douze|treize|quatorze|quinze|seize"
t_UV1 = r"onze"
t_D = r"trente|quarante|cinquante"
t_D1 = r"dix"
t_D2 = r"vingt"
t_D6 = r"soixante"
t_C = r"cent"
t_M = r"mille"
t_MM = r"million"
t_MMM = r"milliard"
@staticmethod
def t_ET(t):
r"""et"""
return t
@staticmethod
def t_PLURIEL(t):
r"""s"""
return t
t_ignore = ' \t\n'
@staticmethod
def t_error(t):
print('Je ne peux pas reconnaitre {0}'.format(t.value[0]))
t.lexer.skip(1)
@staticmethod
def p_num(p):
"""
Num : MMMNum
| MMNum
| MNum
| CNum
"""
p[0] = Node(
pere="Num",
children=p[1:]
)
@staticmethod
def p_mmmnum(p):
"""
MMMNum : Milliards MMNum
| Milliards
"""
p[0] = Node(
pere="MMMNum",
children=p[1:]
)
@staticmethod
def p_mmnum(p):
"""
MMNum : Millions MNum
| Millions
"""
p[0] = Node(
pere="MMNum",
children=p[1:]
)
@staticmethod
def p_mnum(p):
"""
MNum : Milliers CNum
| Milliers
"""
p[0] = Node(
pere="MNum",
children=p[1:]
)
@staticmethod
def p_cnum(p):
"""
CNum : CNum1
| U1
"""
p[0] = Node(
pere="CNum",
children=p[1:]
)
@staticmethod
def p_cnum1_1(p):
"""
CNum1 : Centaine DNum
"""
p[0] = Node(
pere="CNum1",
children=p[1:]
)
@staticmethod
def p_cnum1_2(p):
"""
CNum1 : Unite Centaines
"""
p[0] = Node(
pere="CNum1",
children=p[1:]
)
@staticmethod
def p_cnum1_3(p):
"""
CNum1 : DNum1
"""
p[0] = Node(
pere="CNum1",
children=p[1:]
)
@staticmethod
def p_dnum1(p):
"""
DNum1 : Dizaine Un1Unite
"""
p[0] = Node(
pere="DNum1",
children=p[1:]
)
@staticmethod
def p_un1unite(p):
"""
Un1Unite : Un1
| Unite
"""
p[0] = Node(
pere="Un1Unite",
children=p[1:]
)
@staticmethod
def p_dnum1_1(p):
"""
DNum1 : Dizaine8 UniVingt
"""
p[0] = Node(
pere="DNum1",
children=p[1:]
)
@staticmethod
def p_dnum1_2(p):
"""
DNum1 : Dizaine8 PLURIEL
"""
p[0] = Node(
pere="DNum1",
children=p[1:]
)
@staticmethod
def p_dnum1_3(p):
"""
DNum1 : D6 Un11UniVingt11
"""
p[0] = Node(
pere="DNum1",
children=p[1:]
)
@staticmethod
def p_un11univingt11(p):
"""
Un11UniVingt11 : Un11
| UniVingt11
"""
p[0] = Node(
pere="Un11UniVingt11",
children=p[1:]
)
@staticmethod
def p_dnum1_4(p):
"""
DNum1 : UniVingt1
"""
p[0] = Node(
pere="DNum1",
children=p[1:]
)
@staticmethod
def p_dnum(p):
"""
DNum : DNum1
| U1
"""
p[0] = Node(
pere="DNum",
children=p[1:]
)
@staticmethod
def p_un11(p):
"""
Un11 : ET U1
| UV1
"""
p[0] = Node(
pere="Un11",
children=p[1:]
)
@staticmethod
def p_un1(p):
"""
Un1 : ET U1
"""
p[0] = Node(
pere="Un1",
children=p[1:]
)
@staticmethod
def p_unite(p):
"""
Unite : U
| U4
| U7
"""
p[0] = Node(
pere="Unite",
children=p[1:]
)
@staticmethod
def p_univingt11_1(p):
"""
UniVingt11 : Unite
| D1
| UV
"""
p[0] = Node(
pere="UniVingt11",
children=p[1:]
)
@staticmethod
def p_univingt11_2(p):
"""
UniVingt11 : D1 U7
"""
p[0] = Node(
pere="UniVingt11",
children=p[1:]
)
@staticmethod
def p_univingt1(p):
"""
UniVingt1 : UniVingt11
| UV1
"""
p[0] = Node(
pere="UniVingt1",
children=p[1:]
)
@staticmethod
def p_univingt(p):
"""
UniVingt : UniVingt1
| U1
"""
p[0] = Node(
pere="UniVingt",
children=p[1:]
)
@staticmethod
def p_dizaine(p):
"""
Dizaine : D
| D2
"""
p[0] = Node(
pere="Dizaine",
children=p[1:]
)
@staticmethod
def p_dizaine8(p):
"""
Dizaine8 : U4 D2
"""
p[0] = Node(
pere="Dizaine8",
children=p[1:]
)
@staticmethod
def p_centaine(p):
"""
Centaine : Unite C
| C
"""
p[0] = Node(
pere="Centaine",
children=p[1:]
)
@staticmethod
def p_centaines(p):
"""
Centaines : C PLURIEL
"""
p[0] = Node(
pere="Centaines",
children=p[1:]
)
@staticmethod
def p_milliers(p):
"""
Milliers : CNum1 M
| M
"""
p[0] = Node(
pere="Milliers",
children=p[1:]
)
@staticmethod
def p_millions(p):
"""
Millions : CNum MM
| MM
"""
p[0] = Node(
pere="Millions",
children=p[1:]
)
@staticmethod
def p_milliards(p):
"""
Milliards : CNum MMM
| MMM
"""
p[0] = Node(
pere="Milliards",
children=p[1:]
)
@staticmethod
def p_error(p):
print("Syntax error at '%s'" % p.value)
In [11]:
def main():
d = GrammarNumbers()
print(d.parse("un million deux cent onze mille trois cent quatre vingts"))
main()
In [9]:
if __name__ == '__main__':
main()
In [ ]: