Exemple #1
0
	def test_nonexistent_file(self):
		# Ensure no exceptions are thrown.
		grammar = Grammar("nonexistent.json")

		self.assertEqual(grammar.get_desc(), "")
Exemple #2
0
	def test_grammar_load(self):
		grammar = Grammar("grammars/grammar1.json")

		self.assertEqual(grammar.get_desc(), "{a^n # b^n | n > 0}")
Exemple #3
0
examples:
	./analyze.py grammars/grammar1.json "a#b"
	./analyze.py grammars/grammar1.json ""
"""

import sys
from grammar import Grammar
from grammaranalyzer import GrammarAnalyzer

args = sys.argv

# Helpful usage hints.
if len(args) != 3:
	print("usage:")
	print("\t./analyze.py <path to grammar json> <input string>\n")
	print("examples:")
	print("\t./analyze.py grammars/grammar1.json \"a#b\"")
	print("\t./analyze.py grammars/grammar1.json \"\"")
	sys.exit()

path_to_grammar = args[1]
input_string    = args[2]

grammar          = Grammar(path_to_grammar)
grammar_analyzer = GrammarAnalyzer(grammar)

if grammar_analyzer.test_string(input_string):
	print(grammar.get_desc() + ' CONTAINS "' + input_string + '"')
else:
	print(grammar.get_desc() + ' DOES NOT CONTAIN "' + input_string + '"')