Question
This is a code in Python, when I run nothing pops up can someone please fix that and send me solution for that. thanks and
This is a code in Python, when I run nothing pops up can someone please fix that and send me solution for that.
thanks and appreciated!
import unittest from timeout_decorator import timeout
##from s_expression_parser import lex
class LexerTest(unittest.TestCase): @timeout(1) def test_basic_lex(self): self.assertEqual(lex(\"hi\", {}), [\"hi\"]) self.assertEqual(lex(\"123\", {}), [\"123\"]) self.assertEqual(lex('\"\"', {}), ['\"\"']) self.assertEqual( lex(r'\"hi hi hi hi hi hi\"', {}), [r'\"hi hi hi hi hi hi\"'] )
@timeout(1) def test_lex_with_comments(self): self.assertEqual( lex(\"hi; tahosetnuhasnetuhsnatehu thasntuht this doesn't matter\", {}), [\"hi\"], ) self.assertEqual(lex(\"hi;;;;;;; comment can have multiple things\", {}), [\"hi\"]) self.assertEqual( lex( \"hi;;;;;;; comment can have multiple things but this is not a comment ; and this is\", {}, ), [\"hi\", \"but\", \"this\", \"is\", \"not\", \"a\", \"comment\"], )
@timeout(1) def test_parens_lex(self): self.assertEqual(lex(\"()]\", {}), [\"(\", \")\", \"]\"]) self.assertEqual(lex(\"(1 2 3)\", {}), [\"(\", \"1\", \"2\", \"3\", \")\"]) self.assertEqual(lex(\"(1 223 3213)\", {}), [\"(\", \"1\", \"223\", \"3213\", \")\"])
@timeout(1) def test_with_symbols(self): self.assertEqual(lex(\"abc\", {}), [\"abc\"]) self.assertEqual(lex(\"abc\", {\"a\"}), [\"a\", \"bc\"])
@timeout(1) def test_multiple_symbols(self): self.assertEqual(lex(\"abc\", {\"a\", \"ab\"}), [\"ab\", \"c\"]) self.assertEqual(lex(\"aaaaa\", {\"a\", \"aa\"}), [\"aa\", \"aa\", \"a\"]) self.assertEqual(lex(\"abc\", {\"b\"}), [\"a\", \"b\", \"c\"])
@timeout(1) def test_with_spaces_and_symbols(self): self.assertEqual(lex(\"a . b\", {\".\"}), [\"a\", \".\", \"b\"]) self.assertEqual(lex('a . \"a . b\"', {\".\"}), [\"a\", \".\", '\"a . b\"'])
PARENS = {\"(\": \")\", \"[\": \"]\"}
def lex(data, special_symbols): \"\"\" Lexes the given string, as 61a-scheme compatible scheme code. This handles numbers, parentheses, and the given special symbols.
Arguments data: the data to parse special_symbols: the set of symbols to handle \"\"\" special_symbols = set(special_symbols) | set(PARENS.keys()) | set(PARENS.values()) | {\";\"}
special_symbols = sorted(special_symbols, key=lambda x: (-len(x), x))
symbol_stream = list(data)[::-1] # reverse to pop from the front tokens = []
def lex_single_token(): current = symbol_stream[-1] if current.isspace(): symbol_stream.pop() return if current == \";\": while symbol_stream and symbol_stream[-1] != \" \": symbol_stream.pop() return for symbol in special_symbols: last_idx = -len(symbol) if symbol_stream[last_idx:] == list(symbol)[::-1]: symbol_stream[last_idx:] = [] tokens.append(symbol) return if current == '\"': token_items = [symbol_stream.pop()] while symbol_stream: current = symbol_stream[-1] token_items.append(symbol_stream.pop()) if current == '\"': break if current == \"\\\\\": if not symbol_stream: raise SyntaxError(\"unexpected EOF when parsing string\") token_items.append(symbol_stream.pop()) tokens.append(\"\".join(token_items)) return token_items = [symbol_stream.pop()] while symbol_stream: current = symbol_stream[-1] if current.isspace() or current in special_symbols: break token_items.append(symbol_stream.pop()) tokens.append(\"\".join(token_items)) return
while symbol_stream: lex_single_token() return tokens
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started