-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpt.py
More file actions
80 lines (69 loc) · 3.23 KB
/
Copy pathgpt.py
File metadata and controls
80 lines (69 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from openai import OpenAI
import re
import os
import glob
import sys
client = OpenAI()
def read_programs(seed_dir):
# Read all files from the directory and store their content with a newline at the end
programs = []
for file_path in sorted(glob.glob(os.path.join(seed_dir, "**"))):
if os.path.isfile(file_path): # Ensure it's a file
try:
with open(file_path, "r", encoding="utf-8") as file:
content = file.read().strip() # Remove extra spaces/newlines
programs.append(content + "\n") # Ensure a newline at the end
except UnicodeDecodeError:
print(f"⚠️ Skipping non-text file: {file_path}") # Handle binary files gracefully
return programs
def save_output_grammar(output_text, seed_name):
os.makedirs("results", exist_ok=True)
# Save to a text file
output_file = "results/gpt_grammar_"+seed_name+".txt"
#with open(output_file, "w", encoding="utf-8") as file:
# file.write(output_text)
# Extract only the "Production Rules" section
#match = re.search(r"### Production Rules\n\n```bnf\n(.*?)\n```", output_text, re.DOTALL)
match = re.search(r"<production-rules>(.*?)</production-rules>", output_text, re.DOTALL)
print(match)
if match:
print("HI")
production_rules = match.group(1).strip()
else:
production_rules = "Production rules not found."
with open(output_file, "w", encoding="utf-8") as file:
file.write(production_rules)
print(f"Production Rules saved to {output_file}")
def gpt_grammar_generation(seed_dir, seed_name):
programs = read_programs(seed_dir)
system_prompt = """You will derive a context-free grammar (CFG) in Backus-Naur Form (BNF) for the given example programs.
Ensure that:
1. The grammar **always** follows the same structure.
2. The **Production Rules** must be enclosed within `<production-rules>` and `</production-rules>` tags.
3. The alternative rules should be separated by a vertical bar `|` in the **same line**.
3. **Whitespaces** should be included as terminal tokens in the grammar rules.
4. Include all terminals within double quotes, don't use dots (...) to represent multiple terminals.
5. Do **not** add any extra explanations—only return the production rules inside the tags.
6. The start rule should be <stmt>.
7. You must not use any special characters for non-terminals (e.g. '-', '_' are not allowed). Only use letters and numbers.
"""
# Combine all program contents into one string
user_prompt = "Example programs:\n" + "".join(programs)
completion = client.chat.completions.create(
#model="gpt-4o",
model="o4-mini",
seed=101,
#temperature=0,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
]
)
#print(completion.choices[0].message.content)
output_text = completion.choices[0].message.content
print(output_text)
save_output_grammar(output_text, seed_name)
if __name__ == "__main__":
seed_dir = sys.argv[1] #"Seed_Programs/tinyc/tinyc-train-r1 tinyc-r1"
seed_name = sys.argv[2]
gpt_grammar_generation(seed_dir, seed_name)