commit b0ffbe3428dc931fba229281a0b63dd4fd1f95fd Author: Super_JK Date: Sat Mar 5 21:56:48 2022 +0100 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/TP1.py b/TP1.py new file mode 100644 index 0000000..cf97031 --- /dev/null +++ b/TP1.py @@ -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)) diff --git a/lark/__pycache__/test.cpython-310.pyc b/lark/__pycache__/test.cpython-310.pyc new file mode 100644 index 0000000..b876c95 Binary files /dev/null and b/lark/__pycache__/test.cpython-310.pyc differ diff --git a/lark/test.py b/lark/test.py new file mode 100644 index 0000000..e475579 --- /dev/null +++ b/lark/test.py @@ -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) diff --git a/ply/example.py b/ply/example.py new file mode 100644 index 0000000..b5392ba --- /dev/null +++ b/ply/example.py @@ -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") diff --git a/ply/example2.py b/ply/example2.py new file mode 100644 index 0000000..51eaf21 --- /dev/null +++ b/ply/example2.py @@ -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) diff --git a/ply/test.txt b/ply/test.txt new file mode 100644 index 0000000..463d940 --- /dev/null +++ b/ply/test.txt @@ -0,0 +1,9 @@ +3+3 +00-0+12+++3 +3*4/5 +3.3 +4. 5+3 +% + +( 5. + 33) +(())()() \ No newline at end of file