Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
instead of rules in file, we have rules as folders and files (outer f…
Browse files Browse the repository at this point in the history
…older translates to inner folder/file)
  • Loading branch information
jasminapegan authored and aJuvan committed Apr 14, 2022
1 parent 481c650 commit 108a79a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
4 changes: 2 additions & 2 deletions challs/context_free_grammar/challenge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ author: "SnackOverflow"
category: "Misc"

description: |
The flag is in format dctf{7_4_2_3_7_4}.
The flag is in format dctf{7_4_2_3_7_4} where numbers represent number of characters between underscores.
value: 100
type: "standard"

files:
- "chall/cfg.txt"
- "chall/cfg.zip"
flags:
- "dctf{c0nt3xt_fR33_15_n07_m34n1ng_fr33}"
tags:
Expand Down
36 changes: 33 additions & 3 deletions challs/context_free_grammar/solve.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import itertools
import os
import string
from random import shuffle, randint, sample, choice

Expand Down Expand Up @@ -318,11 +319,40 @@ def get_production(string_production, nonterminal=None, mappings=[]):
else:
return nonterminal, mappings

def generate_folders(file, folder):
grammar = Grammar(file)
for s, ms in grammar.productions.items():
s_path = os.path.join(folder, s)
os.mkdir(s_path)

grammar = Grammar("chall/cfg.txt")
for m in ms:
d_path = os.path.join(s_path, m)

if m in alphabet:
open(d_path + ".txt", "w").close()
else:
os.mkdir(d_path)

def get_rules(path, outf):
with open(outf, "w") as f:
for dir in os.listdir(path):
line = dir + " -> " + "|".join([x.replace(".txt", "") for x in os.listdir(os.path.join(path, dir))]) + "\n"
f.write(line)

file = "chall/cfg.txt"
grammar = Grammar(file)
#grammar.reduce_mappings()
#grammar.add_useless_productions()
#grammar.scramble_nonterminals()
grammar.simplify()
#grammar.simplify()
#grammar.print_productions()
grammar.get_flag()
#grammar.get_flag()
#generate_folders(file, "test")
get_rules("cfg", "test.txt")
grammar = Grammar("test.txt")
grammar.reduce_mappings()
grammar.add_useless_productions()
grammar.scramble_nonterminals()
grammar.simplify()
grammar.print_productions()
grammar.get_flag()

0 comments on commit 108a79a

Please sign in to comment.