initial commit

This commit is contained in:
Super_JK
2022-03-05 21:56:48 +01:00
commit b0ffbe3428
7 changed files with 231 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
.idea

52
TP1.py Normal file
View File

@@ -0,0 +1,52 @@
import re
# ex 9
mots = ['a', 'aab', 'aaab', 'aabaab', 'baabaab', 'abaabaaabaaaab']
regExps = [r"aa*b", r'a(a|b)*', r'(aa*b)*', r'(a|aab)*', r'a*|aab*']
for r in regExps:
print("Match for regex '" + r + "' are :")
for mot in mots:
m = re.fullmatch('(' + r + ')', mot)
if m:
print(" " + mot)
# ex 11
def say_hello(text):
regex = r'([a-zA-Z]+)\.[a-zA-Z]+@umons\.ac\.be'
m = re.search(regex, text)
if m:
print("Hello " + m.group(1))
say_hello("jean.bob@umons.ac.be")
say_hello("Patrick.BOUl@umons.ac.be")
say_hello("s.ac.be")
# ex 12
def newlines(text,nbr):
regex = r'((\w+ ){'+str(nbr-1)+r'}\w+)|(\w+)'
m = re.findall(regex, text)
res = ""
if m:
for group in m:
mots = group[0].split(r" ")
if len(mots) == nbr:
for i in range(nbr):
res += str(i+1) + ". " + mots[i] + "\t"
res += "\n"
else:
res += " "+group[2]
return res
def newlines_with_varying_size(text, n=5):
pattern = r'\s+'.join(r'(\w+)' for _ in range(n))
group = [rf'{i}. \{i}' for i in range(1, n + 1)]
repl = '\t'.join(group) + '\n'
return re.sub(pattern, repl, text)
print(newlines('cet exercice travaille avec une suite de mots', 4))

Binary file not shown.

50
lark/test.py Normal file
View File

@@ -0,0 +1,50 @@
import sys
from lark import Lark, Transformer
json_grammar = r"""
?value: dict
| list
| string
| SIGNED_NUMBER -> number
| "true" -> true
| "false" -> false
| "null" -> null
list : "[" [value ("," value)*] "]"
dict : "{" [pair ("," pair)*] "}"
pair : string ":" value
string : ESCAPED_STRING
%import common.ESCAPED_STRING
%import common.SIGNED_NUMBER
%import common.WS
%ignore WS
"""
class TreeToJson(Transformer):
def string(self, s):
(s,) = s
return s[1:-1]
def number(self, n):
(n,) = n
return float(n)
list = list
pair = tuple
dict = dict
null = lambda self, _: None
true = lambda self, _: True
false = lambda self, _: False
json_parser = Lark(json_grammar, start='value', parser='lalr', transformer=TreeToJson())
if __name__ == '__main__':
text = '{"key": ["item0", "item1", 3.14, true]}'
tree = json_parser.parse(text)
print(tree)

69
ply/example.py Normal file
View File

@@ -0,0 +1,69 @@
import ply.lex as lex
tokens = (
'INTEGER',
'FLOAT',
'ADD_OP',
'MUL_OP',
'BRACKET',
)
def t_BRACKET(t):
r'\(|\)'
if t.value == '(':
t.value = "L"
else:
t.value = "R"
return t
def t_MUL_OP(t):
r'\*|/'
return t
def t_ADD_OP(t):
r'\+|-'
return t
def t_FLOAT(t):
r'\d+\.\d*'
t.value = float(t.value)
return t
def t_INTEGER(t):
r'\d+'
t.value = int(t.value)
return t
def t_newline(t):
r'\n+'
t.lexer.lineno += len(t.value)
t_ignore = ' |\t'
def t_error(t):
print("Illegalcharacter '%s'" %t.value[0])
t.lexer.skip(1)
if __name__ == "__main__":
import sys
lexer = lex.lex()
lexer.input(sys.stdin.read())
lb = 0
for token in lexer:
if token.type == "BRACKET":
if token.value == "L":
lb += 1
else:
lb -= 1
if lb < 0:
print("BRACKET missing")
print("line %d : %s (%s) " % (token.lineno, token.type, token.value))
if lb != 0:
print("BRACKET missing")

50
ply/example2.py Normal file
View File

@@ -0,0 +1,50 @@
import ply.lex as lex
tokens = (
'COMMENTBEGIN',
'COMMENTEND',
'CODE',
)
states = (
('inComment', 'inclusive'),
)
def t_inComment_COMMENTEND(t):
r'\*/'
t.lexer.begin('INITIAL')
return t
def t_inComment_CODE(t):
r'.'
t.value = ''
return t
def t_COMMENTBEGIN(t):
r'/\*'
t.lexer.begin('inComment')
return t
t_CODE = r'.'
def t_inComment_newline(t):
r'\n+'
def t_newline(t) :
r'\n+'
t.lexer.lineno += len(t.value)
if __name__ == "__main__" :
import sys
lexer = lex.lex()
lexer.input(sys.stdin.read())
ret = ""
lineno = 1
for token in lexer:
if token.lineno > lineno :
lineno += 1
ret += '\n'
if token.type == 'CODE' :
ret += token.value
print(ret)

9
ply/test.txt Normal file
View File

@@ -0,0 +1,9 @@
3+3
00-0+12+++3
3*4/5
3.3
4. 5+3
%
( 5. + 33)
(())()()