From 243329893ac595ce3e4ba65c7d8cfb8228b4eeb8 Mon Sep 17 00:00:00 2001 From: s464968 Date: Tue, 4 Jul 2023 20:29:27 +0200 Subject: [PATCH] archive projects --- P2/__pycache__/app_opener.cpython-39.pyc | Bin 0 -> 840 bytes P2/__pycache__/browser_opener.cpython-39.pyc | Bin 0 -> 542 bytes P2/__pycache__/my_parser.cpython-39.pyc | Bin 0 -> 1919 bytes P2/app_opener.py | 11 ++ P2/browser_opener.py | 5 + P2/main.py | 15 ++ P2/my_parser.py | 53 ++++++ wyklady/3.py | 171 +++++++++++++++++++ wyklady/parser.out | 136 +++++++++++++++ wyklady/parsetab.py | 36 ++++ 10 files changed, 427 insertions(+) create mode 100644 P2/__pycache__/app_opener.cpython-39.pyc create mode 100644 P2/__pycache__/browser_opener.cpython-39.pyc create mode 100644 P2/__pycache__/my_parser.cpython-39.pyc create mode 100644 P2/app_opener.py create mode 100644 P2/browser_opener.py create mode 100644 P2/main.py create mode 100644 P2/my_parser.py create mode 100644 wyklady/3.py create mode 100644 wyklady/parser.out create mode 100644 wyklady/parsetab.py diff --git a/P2/__pycache__/app_opener.cpython-39.pyc b/P2/__pycache__/app_opener.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..790055008771e9dde951092e49a3d7229e3fbf7c GIT binary patch literal 840 zcmbVL!EV$r5VhlM>Mf0siUbm;)r!j=Xe7=E74^VvLF#QUmbGJGsS^h~QI&`bqJM#q z;MgzWAAIG+FZ9GrR_QK?Q!UN&jP03uGn42%KV-OmexAWGXY3al`%9p4iV_|p00VB> zioIfxfVg5Hz9cIS{G2V)-+0fKYr_4NpmB;4o*)>8uh}XA4#^c;2@sItp27q&+$V5= zkz|~{tn1gcsf=Hf`?yG-pq!zEhX@V(j>$jquSn;c_`x|6!~kO1PaV3?p6c2BhR?5) zn79zL(1pg7Xr;l}w^8c4R+TQzHS^Sin`QCdm8NK1)m@t4Y*(B)FvWS(!bTUh?@e>5 zeXpyHQ6Eg-U1)S~TCZ(o-nrtkg zb~g7kB~NMm0Rfe>g?p&Q>DszrhzZ?}Odjl*+}_`nN;dR+kH;eMl2Yg3)@^C3X6W?4 z#PXp9;}c>%l6$lx_t%@{B8yTD!IX-JN|g>;YfwK@>SK$ez=0~|k1&?c3GNf@8Zv0q l6;A`9zJ&}yaw()3&X78NWrO}d$0(m~lfJjMgD9BfBXA{Infz5pS?$d~XB zS(*3+CeCg_;iS9V+4tVNi)FvxC1@WH`|1h5pT1~YgvA)$4lpR9Xi5@tN)%JknyBcJ zB~;NF8Fs$#o=g`$wvAyiMz?zyfM7RCn4*fUNfN2ZBb)w-(WM1*mO}<+;UT}~^A7q1 zJy1CEih}p_nY^$!N^vBlVADFjb}qMvoDW&kEnz<2P$oT%q#}b-I623=j)nBH>0yUm zIuMy$!8T(w{27WC9w#j3DI#!oOS4ifO2a%=n{_xBC6!#)F#pXd`n>|B&nlRri6Z literal 0 HcmV?d00001 diff --git a/P2/__pycache__/my_parser.cpython-39.pyc b/P2/__pycache__/my_parser.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..64ec9692eed9dd634db90aa7aeb30391dc55ffd5 GIT binary patch literal 1919 zcmbVNOOM+&5GE0;%PO1+MOB_q?lI)tT`NvB4YN z7i{ER!aHolZSPT*JzXn zs)Dtvy2^uZNA*-6zFl=q4dCl3AJh+R=Xqp6)0D$gZNoY0cmh4&gP~v>R=MkFqRG3A}M|WB9Vh-)w?KGS`d4cg%Poe*gRURT}GYktXYv&eO$u{3KO+ z{GwQ@c{I*Usf$%)$|#xZ@U<@2M^TtW#nMEJM93YI?M?7{cLT$Fq|jzA`CG& zxrBjnkC)eL%kH<*DPnCK9!02R;@>b)yyAZ{^GXT&{!p=VR`CmdDk}DxpNmRdK+KNM zltc2TAcq;q9g@q+Y2=dRXCQY;EWvr7T=&tn3d|qhlP8VseM?LF@GhKuji6n}(K?4I1uj8J*Mbh><9SMAr zwtMy_9ckXJAL-yD13K312R+h(aQU#ztZc3#Bz6A4TY@46I4 zoaT37z_@w5&xaq4xK|FEc+!=fU}7LK>=aDoE9SyXVCc}f;8Fp6btdo0l!L~brB9}` z%Xi!k$phfkn4pgwGSJ+B7lR(!w@Qu5Yxrac0yjPk`_MlyZ?9$Ec0|4iaY->cXgyjE zYsl@|d)Gpo^xGY@ zcec^CcL^+Vpl!(gD?VV2RG6mX7xu?@r;wh&2}nlRag;c6j8{1YujGdR#7>=K5BLH# z^fnaqw_+dboSYaeh?oCDyTsW@Sa~>4lyw$5X|jMwV!b21CQGafa(!a?(Z_5-dVUW* zAHcx4B&K`7`10#*+pb7Bfz*Zqhc ') + except EOFError: + break + if not text: + continue + parser.parse(text.lower()) + +if __name__ == '__main__': + main() diff --git a/P2/my_parser.py b/P2/my_parser.py new file mode 100644 index 0000000..47a9bfc --- /dev/null +++ b/P2/my_parser.py @@ -0,0 +1,53 @@ +from app_opener import AppOpener +from browser_opener import BrowserOpener + +class Parser: + def __init__(self): + self.app_opener = AppOpener() + self.browser_opener = BrowserOpener() + + def parse(self, text): + if 'open' in text: + if 'program' in text: + app_name = self._get_app_name(text) + self.app_opener.open_app(app_name) + elif 'document' in text: + document_name = self._get_document_name(text) + self.app_opener.open_document(document_name) + elif 'website' in text: + website_name = self._get_website_name(text) + self.browser_opener.open_website(website_name) + elif 'close' in text: + if 'program' in text: + app_name = self._get_app_name(text) + self.app_opener.close_app(app_name) + + def _get_app_name(self, text): + app_words = ['open program','close program'] + return self._get_item_name(text, app_words) + + def _get_document_name(self, text): + document_words = ['open document', 'close document'] + return self._get_item_name(text, document_words) + + def _get_website_name(self, text): + website_words = ['open website','close website'] + return self._get_item_name(text, website_words) + + def _get_item_name(self, text, keywords): + index = 0 + for k in keywords: + index = text.find(k) + if index != -1: + index += len(k) + return text[index+1:] + # words = text.split() + # for i, word in enumerate(words): + # if word in keywords: + # return ' '.join(words[i + 1:]) + # return '' + + +p = Parser() + +print(p._get_item_name('open program firefox',['open program'])) diff --git a/wyklady/3.py b/wyklady/3.py new file mode 100644 index 0000000..95270d4 --- /dev/null +++ b/wyklady/3.py @@ -0,0 +1,171 @@ +# Import lexer and parser from ply module +import ply.lex as lex +import ply.yacc as yacc + +# List of token types. +tokens = ( + 'NUMBER', + 'OPERATE', + 'SIZE', + 'KIND', + 'COLOR', + 'MATERIAL' +) + +# Token types may be defined as regular expressions, e.g. r'Buy | Sell' + + +def t_OPERATE(t): + r'Buy | Sell' + return t + + +def t_MATERIAL(t): + r'metal | plastic' + if t.value == 'metal': + t.value = 1 + elif t.value == 'plastic': + t.value = 2 + return t + + +def t_COLOR(t): + r'(black | white | red | green | blue)' + if t.value == 'black': + t.value = 1 + elif t.value == 'white': + t.value = 2 + elif t.value == 'red': + t.value = 3 + elif t.value == 'green': + t.value = 4 + elif t.value == 'blue': + t.value = 5 + return t + + +def t_SIZE(t): + r'tiny | small | big | large' + if t.value == 'tiny': + t.value = 1 + elif t.value == 'small': + t.value = 2 + elif t.value == 'big': + t.value = 3 + elif t.value == 'large': + t.value = 4 + return t + + +def t_KIND(t): + r'box(es)? | ring(s)?' + if t.value[0] == 'b': + t.value = 1 + else: + t.value = 2 + return t + + +def t_NUMBER(t): + r'\d+' + t.value = int(t.value) + return t + +# Lexer error handling rule (Handle words out of vocabulary) + + +def t_error(t): + print("Illegal character '%s'" % t.value[0]) + t.lexer.skip(1) + +# Ignore white spaces +t_ignore = ' \t' + +# Main parser rule (command) + + +def p_command(p): + 'command : OPERATE NUMBER article' + index = p[3] + # Buy article + if p[1] == 'Buy': + tab[index] += p[2] + print('OK. I am buying ' + str(p[2]) + + ' new articles indexed as ' + str(index) + '.') + print('No of articles in shop: ' + str(tab[index])) + # Sell article + elif p[1] == 'Sell': + if p[2] > tab[index]: + print('I do not have as many articles as you want.') + else: + tab[index] -= p[2] + print('OK. I am selling ' + str(p[2]) + + ' articles indexed as ' + str(index) + '.') + print('No of articles in shop: ' + str(tab[index])) + +# Parser rule (attribute) + + +def p_attribute_color(p): + 'attribute : COLOR' + p[0] = p[1] + +# Parser rule (attribute) + + +def p_attribute_material(p): + 'attribute : MATERIAL' + p[0] = 10 * p[1] + + +# Parser rule (attribute) + + +def p_attribute_size(p): + 'attribute : SIZE' + p[0] = 100 * p[1] + +# Parser rule (article - stop) + + +def p_article_kind(p): + 'article : KIND' + p[0] = 1000 * p[1] + +# Parser rule (article - recursion) + + +def p_article_attribute(p): + 'article : attribute article' + p[0] = p[1] + p[2] + +# Syntax error handling rule + + +def p_error(p): + print("Syntax error in input!") + +####################################### +# Main program + +# Initialize table of articles (zero articles at the beginning) +tab = [] +for index in range(3000): + tab.append(0) + +# Build the lexer +lexer = lex.lex() + +# Tokenize (short version) +# for tok in lexer: +# print(tok) + +# Build the parser +parser = yacc.yacc() + +# Main loop +while True: + s = input('What can I do for you? \n') + if s == 'Bye': + break + parser.parse(s) diff --git a/wyklady/parser.out b/wyklady/parser.out new file mode 100644 index 0000000..e871fba --- /dev/null +++ b/wyklady/parser.out @@ -0,0 +1,136 @@ +Created by PLY version 3.11 (http://www.dabeaz.com/ply) + +Grammar + +Rule 0 S' -> command +Rule 1 command -> OPERATE NUMBER article +Rule 2 attribute -> COLOR +Rule 3 attribute -> MATERIAL +Rule 4 attribute -> SIZE +Rule 5 article -> KIND +Rule 6 article -> attribute article + +Terminals, with rules where they appear + +COLOR : 2 +KIND : 5 +MATERIAL : 3 +NUMBER : 1 +OPERATE : 1 +SIZE : 4 +error : + +Nonterminals, with rules where they appear + +article : 1 6 +attribute : 6 +command : 0 + +Parsing method: LALR + +state 0 + + (0) S' -> . command + (1) command -> . OPERATE NUMBER article + + OPERATE shift and go to state 2 + + command shift and go to state 1 + +state 1 + + (0) S' -> command . + + + +state 2 + + (1) command -> OPERATE . NUMBER article + + NUMBER shift and go to state 3 + + +state 3 + + (1) command -> OPERATE NUMBER . article + (5) article -> . KIND + (6) article -> . attribute article + (2) attribute -> . COLOR + (3) attribute -> . MATERIAL + (4) attribute -> . SIZE + + KIND shift and go to state 5 + COLOR shift and go to state 7 + MATERIAL shift and go to state 8 + SIZE shift and go to state 9 + + article shift and go to state 4 + attribute shift and go to state 6 + +state 4 + + (1) command -> OPERATE NUMBER article . + + $end reduce using rule 1 (command -> OPERATE NUMBER article .) + + +state 5 + + (5) article -> KIND . + + $end reduce using rule 5 (article -> KIND .) + + +state 6 + + (6) article -> attribute . article + (5) article -> . KIND + (6) article -> . attribute article + (2) attribute -> . COLOR + (3) attribute -> . MATERIAL + (4) attribute -> . SIZE + + KIND shift and go to state 5 + COLOR shift and go to state 7 + MATERIAL shift and go to state 8 + SIZE shift and go to state 9 + + attribute shift and go to state 6 + article shift and go to state 10 + +state 7 + + (2) attribute -> COLOR . + + KIND reduce using rule 2 (attribute -> COLOR .) + COLOR reduce using rule 2 (attribute -> COLOR .) + MATERIAL reduce using rule 2 (attribute -> COLOR .) + SIZE reduce using rule 2 (attribute -> COLOR .) + + +state 8 + + (3) attribute -> MATERIAL . + + KIND reduce using rule 3 (attribute -> MATERIAL .) + COLOR reduce using rule 3 (attribute -> MATERIAL .) + MATERIAL reduce using rule 3 (attribute -> MATERIAL .) + SIZE reduce using rule 3 (attribute -> MATERIAL .) + + +state 9 + + (4) attribute -> SIZE . + + KIND reduce using rule 4 (attribute -> SIZE .) + COLOR reduce using rule 4 (attribute -> SIZE .) + MATERIAL reduce using rule 4 (attribute -> SIZE .) + SIZE reduce using rule 4 (attribute -> SIZE .) + + +state 10 + + (6) article -> attribute article . + + $end reduce using rule 6 (article -> attribute article .) + diff --git a/wyklady/parsetab.py b/wyklady/parsetab.py new file mode 100644 index 0000000..1df067c --- /dev/null +++ b/wyklady/parsetab.py @@ -0,0 +1,36 @@ + +# parsetab.py +# This file is automatically generated. Do not edit. +# pylint: disable=W,C,R +_tabversion = '3.10' + +_lr_method = 'LALR' + +_lr_signature = 'COLOR KIND MATERIAL NUMBER OPERATE SIZEcommand : OPERATE NUMBER articleattribute : COLORattribute : MATERIALattribute : SIZEarticle : KINDarticle : attribute article' + +_lr_action_items = {'OPERATE':([0,],[2,]),'$end':([1,4,5,10,],[0,-1,-5,-6,]),'NUMBER':([2,],[3,]),'KIND':([3,6,7,8,9,],[5,5,-2,-3,-4,]),'COLOR':([3,6,7,8,9,],[7,7,-2,-3,-4,]),'MATERIAL':([3,6,7,8,9,],[8,8,-2,-3,-4,]),'SIZE':([3,6,7,8,9,],[9,9,-2,-3,-4,]),} + +_lr_action = {} +for _k, _v in _lr_action_items.items(): + for _x,_y in zip(_v[0],_v[1]): + if not _x in _lr_action: _lr_action[_x] = {} + _lr_action[_x][_k] = _y +del _lr_action_items + +_lr_goto_items = {'command':([0,],[1,]),'article':([3,6,],[4,10,]),'attribute':([3,6,],[6,6,]),} + +_lr_goto = {} +for _k, _v in _lr_goto_items.items(): + for _x, _y in zip(_v[0], _v[1]): + if not _x in _lr_goto: _lr_goto[_x] = {} + _lr_goto[_x][_k] = _y +del _lr_goto_items +_lr_productions = [ + ("S' -> command","S'",1,None,None,None), + ('command -> OPERATE NUMBER article','command',3,'p_command','3.py',88), + ('attribute -> COLOR','attribute',1,'p_attribute_color','3.py',110), + ('attribute -> MATERIAL','attribute',1,'p_attribute_material','3.py',117), + ('attribute -> SIZE','attribute',1,'p_attribute_size','3.py',124), + ('article -> KIND','article',1,'p_article_kind','3.py',131), + ('article -> attribute article','article',2,'p_article_attribute','3.py',138), +]