Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
from parser import Parser
import ply.lex as lex
import ply.yacc as yacc
class Parser_Impl(Parser):
tokens = ('INT', 'BOOL', 'VAL', 'SYM', 'LParenthesis', 'RParenthesis') # Tokens can be assigned a list or a tuple
t_LParenthesis = r'('
t_RParenthesis = r')'
t_SYM = r'[*+-/]|and|or|neq|eq|if|not'
t_VAL = r'unknown|a|b'
# t_VAL = r'[a-z][a-zA-z0-9]*'
# t_BOOL = r'TRUE'
t_ignore = ' tn' # We ignore space, tabs, and newline
def t_INT(self, token):
r'-?[0-9]+'
token.value = int(token.value)
return token
def t_BOOL(self, token):
r'True|( )'
if token.value == r"()":
token.value = False
else:
token.value = True
return token
# def t_VAL(self, token):
# r'unknown|a|b'
# # r'[a-z][0-9a-zA-z]*'
# return token
def t_error(self, t):
print("Incorrect parsing!")
t.lexer.skip(1)
def p_calc(self, p):
'''
calc : func
'''
p[0] = self.execute(p[1])
def p_func(self, p):
'''
func : LParenthesis SYM atoms RParenthesis
'''
p[0] = [p[2], *p[3]]
def p_list(self, p):
'''
func : LParenthesis atoms RParenthesis
'''
p[0] = [*p[1]]
def p_empty(self, p):
'''
func : LParenthesis RParenthesis
'''
p[0] = None
def p_plural_calc(self, p):
'''
calc : calc func
'''
p[0] = self.execute(p[2])
def p_atoms_atom(self, p):
'''
atoms : atom
'''
p[0] = [p[1]]
def p_atom_atoms(self, p):
'''
atoms : atom atoms
'''
p[0] = [p[1], *p[2]]
def p_atom(self, p):
'''
atom : func
| INT
| BOOL
| VAL
'''
p[0] = p[1]
def execute(self, arr):
if not type(arr) == list:
return arr
if arr[0] == "-":
return arr[1] - sum([self.execute(element) for element in arr[2:]])
elif arr[0] == "+":
return sum([self.execute(element) for element in arr[1:]])
elif arr[0] == "*":
res = 1
for element in arr[1:]:
res *= self.execute(element)
return res
elif arr[0] == "/":
res = self.execute(arr[1])
for element in arr[2:]:
if self.execute(element) == 0:
raise ZeroDivisionError
res = res // self.execute(element)
return res
elif arr[0] == "and":
res = True
for element in arr[1:]:
res = res and self.execute(element)
if not res:
return None
return True if res else None
elif arr[0] == "or":
res = False
for element in arr[1:]:
res = res or self.execute(element)
if res:
return True
return self.execute(arr[1]) and self.execute(arr[2])
elif arr[0] == "eq":
res = self.execute(arr[1])
for element in arr[2:]:
res = self.execute(element) == res
if not res:
break
return True if res else None
elif arr[0] == "neq":
res = self.execute(arr[1])
for element in arr[2:]:
res = (self.execute(element) == res)
if not res:
break
return None if res else True
elif arr[0] == "not":
return None if self.execute(arr[1]) else True
elif arr[0] == "if":
return self.execute(arr[2]) if self.execute(arr[1]) else self.execute(arr[3])
def __init__(self):
lex.lex(module=self)
yacc.yacc(module=self)
def evaluate(self, input):
return yacc.parse(input)
def set_binding( self, binding ):
pass
parser = Parser_Impl()
# print(parser.evaluate( "( neq () True ) " ) )
print(parser.evaluate( "( not ( ) )" ))
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team