import json import re import sys tokenpatterns = [ (r’#[^\n]‘, None), (r’\s+‘, None),…
- User
import json import re import sys
token_patterns = [ (r’#[^\n]‘, None), (r’\s+‘, None), (r’[‘, ‘LBRACKET’), (r’]‘, ‘RBRACKET’), (r’{‘, ‘LBRACE’), (r’}‘, ‘RBRACE’), (r’%{‘, ‘LMAPBRACE’), (r’=>‘, ‘ARROW’), (r’,‘, ‘COMMA’), (r’(true|false)‘, ‘BOOLEAN’), (r’(0|[1-9][0-9_])‘, ‘INTEGER’), (r’:[A-Za-z_]\w*‘, ‘ATOM’), (r’[A-Za-z_]\w*‘, ‘KEY’), ]
regex_parts = [f’(?P<{name}>{pattern})’ if name else f’(?:{pattern})’ for pattern, name in token_patterns] token_regex = ‘|’.join(regex_parts) token_re = re.compile(token_regex)
class TokenizerException(Exception): pass
class ParserException(Exception): pass
def tokenize(text): pos = 0 while pos < len(text): match = token_re.match(text, pos) if not match: raise TokenizerException(f’Illegal character {text[pos]!r} at index {pos}‘) pos = match.end() if match.lastgroup: yield match.lastgroup, match.group(match.lastgroup)
class Node: def init(self, kind, value): self.kind = kind self.value = value
def to_json(self): if self.kind == “map”: return {“%k”: self.kind, “%v”: {k.to_json()[“%v”]: v.to_json() for k, v in self.value}} elif self.kind == “list”: return {“%k”: self.kind, “%v”: [item.to_json() for item in self.value]} else: return {“%k”: self.kind, “%v”: self.value}
class Parser: def init(self, tokens): self.tokens = iter(tokens) self.current_token = None self.next_token()
def next_token(self): try: self.current_token = next(self.tokens) except StopIteration: self.current_token = None
def parse(self): result = self.parse_sentence() if self.current_token is not None: raise ParserException(‘Unexpected token at the end’) return result
def parse_sentence(self): nodes = [] while self.current_token is not None: node = self.parse_data_literal() nodes.append(node) if self.current_token and self.current_token[0] in {‘COMMA’, ‘COLON’, ‘ARROW’}: self.next_token() return nodes
def parse_data_literal(self): if self.current_token[0] == ‘LBRACKET’: return self.parse_list() elif self.current_token[0] == ‘LBRACE’: return self.parse_tuple() elif self.current_token[0] == ‘LMAPBRACE’: return self.parse_map() elif self.current_token[0] == ‘INTEGER’: value = int(self.current_token[1]) self.next_token() return Node(‘int’, value) elif self.current_token[0] == ‘ATOM’: value = self.current_token[1] self.next_token() return Node(‘atom’, value) elif self.current_token[0] == ‘BOOLEAN’: value = self.current_token[1] == ‘true’ self.next_token() return Node(‘bool’, value) elif self.current_token[0] == ‘KEY’: value = self.current_token[1] self.next_token() if self.current_token and self.current_token[0] == ‘COLON’: self.next_token() return Node(‘key’, value) return Node(‘string’, value) else: raise ParserException(f’Unexpected token {self.current_token[1]}’)
def parse_list(self): self.next_token() items = [] while self.current_token and self.current_token[0] != ‘RBRACKET’: item = self.parse_data_literal() items.append(item) if self.current_token and self.current_token[0] == ‘COMMA’: self.next_token() if not self.current_token or self.current_token[0] != ‘RBRACKET’: raise ParserException(‘List not properly terminated with ]’) self.next_token() return Node(‘list’, items)
def parse_tuple(self): items = [] self.next_token() while self.current_token is not None and self.current_token[0] != ‘RBRACE’: items.append(self.parse_data_literal()) if self.current_token and self.current_token[0] == ‘COMMA’: self.next_token() if self.current_token is None or self.current_token[0] != ‘RBRACE’: raise ParserException(‘Tuple not properly terminated with }’) self.next_token() return Node(‘tuple’, [item.to_json() for item in items])
def parse_map(self): self.next_token() map_items = {} while self.current_token and self.current_token[0] != ‘RBRACE’:
if self.current_token[0] not in {‘ATOM’, ‘KEY’}: raise ParserException(f’Expected map key, found {self.current_token}‘)
key = self.current_token[1] if self.current_token[0] == ‘ATOM’ else ‘:{}’.format(self.current_token[1]) self.next_token()
if self.current_token and self.current_token[0] not in {‘ARROW’, ‘COLON’}: raise ParserException(f’Expected “=>” or “:”, found {self.current_token}’) self.next_token()
value = self.parse_data_literal() map_items[key] = value if self.current_token and self.current_token[0] == ‘COMMA’: self.next_token() if not self.current_token or self.current_token[0] != ‘RBRACE’: raise ParserException(‘Map not properly terminated with }’) self.next_token() return Node(‘map’, map_items)
def main(): try: text = sys.stdin.read()
tokens = list(tokenize(text))
if not tokens: print(json.dumps([])) else: parser = Parser(tokens) result = parser.parse()
json_output = json.dumps([node.to_json() for node in result], separators=(‘,’, ‘:’)) print(json_output) except TokenizerException as e: sys.stderr.write(str(e) + ‘\n’) except ParserException as e: sys.stderr.write(str(e) + ‘\n’)
if name == ‘main’: main() The program above is elixir-data.py program in the prj1-sol directory. I will specifically need to submit a zip-archive which unpacks into a prj1-sol directory minimally containing a file elixir-data.ebnf plus two shell scripts make.sh and run.sh: Running make.sh from any directory should build any artifacts needed to run program within the prj1-sol directory. Running run.sh from any directory should read and parse a sentence of the above language from standard input and output on staingle line containing the JSON representation of the parse. let’s write the make.sh script, which in this case may be straightforward since the core of your program is a Python script (which does not require compilation). However, the make.sh script should ensure that the project is structured correctly. i want the make.sh file to be like #!/bin/sh
#sets dir to directory containing this script dir=
dirname $0#use $dir to access programs in this directory #so that this script can be run from any directory.
echo "no build done"
and the run.sh file to be like #!/bin/sh
#sets dir to directory containing this script scriptpath=
realpath $0dir=dirname $scriptpath#use dir/ as prefix to run any programs in this dir #so that this script can be run from any directory. python3 dir/arith.py I have written my program in vscode so write the programs and commands accordingly
- Assistant
Based on your requirements and the instructions provided, here are the
make.shandrun.shscripts for your project.First, let's create the
make.shscript. Since your project is a Python script and doesn't need compilation, themake.shscript will simply output a message and won't perform any actions. Create a file namedmake.shin theprj1-soldirectory with the following content:#!/bin/sh # Set dir to the directory containing this script dir=$(dirname "$0") # Use $dir to access programs in this directory # so that this script can be run from any directory. echo "No build necessary for Python scripts"Make sure to make
make.shexecutable by running:chmod +x prj1-sol/make.shNext, we'll create the
run.shscript. This script will run your Python program (elixir-data.py) and pass any input received through standard input. Create a file namedrun.shin theprj1-soldirectory with the following content:#!/bin/sh # Set scriptpath to the resolved path of this script scriptpath=$(realpath "$0") dir=$(dirname "$scriptpath") # Use $dir/ as a prefix to run any programs in this dir # so that this script can be run from any directory. python3 "$dir/elixir-data.py"Make
run.shexecutable as well:chmod +x prj1-sol/run.shNow with both scripts in your
prj1-soldirectory, you havemake.shwhich outputs a message indicating no build is necessary, andrun.shwhich will execute your Python script using the input from standard input.Just a little note, the script you provided appears to handle
python3 $dir/arith.pybut you mentioned your script is namedelixir-data.py, so ensure that you use the correct filename when callingpython3inrun.sh.Given that you wrote your program in VSCode, you should put these scripts in the root of
prj1-soldirectory, and call them from the command line as needed. Since they're written to handle running from any directory (thanks to detecting their own directory dynamically), they should work regardless of the current working directory from which you call them.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.