refactored code into BNFStack, added more test cases