def test_simple(self): tokens = ( ('OPEN_PAREN', '('), ('INT', '3'), ('CLOSE_PAREN', ')'), ) start_state = 'A' transitions = { 'A': ( ('OPEN_PAREN', 'B'), ), 'B': ( ('INT', 'C'), ), 'C': ( ('CLOSE_PAREN', 'D'), ), 'D': (), } final_states = ('D') result = parse(tokens, start_state, transitions, final_states) self.assertEqual(result, ('(3)',))
def test_parser_empty(self): tokens = ( ('NOT_TOKEN', 'dsfsf'), ('TO', ' to '), ('AM_PM', ' PM'), ) result = () stmts = parse(tokens, pa1.parser_q0, pa1.parser_transitions, pa1.parser_F) self.assertEqual(stmts, result)
def test_parser_backtracking(self): tokens = ( ('24HOUR_TIME', '2322'), ('TO', ' to '), ('12HOUR_TIME', '11:00'), ('AT', ''), ('12HOUR_TIME', '11:00'), ) result = ( '11:00', '11:00', ) stmts = parse(tokens, pa1.parser_q0, pa1.parser_transitions, pa1.parser_F) self.assertEqual(stmts, result)
def test_parser_12_hour(self): tokens = ( ('12HOUR_TIME', '12:22'), ('TO', ' to '), ('24HOUR_TIME', '2300'), ('12HOUR_TIME', '12:00'), ('AM_PM', ' PM'), ('DASH', '-'), ('12HOUR_TIME', '1:12'), ('TO', ' to '), ) result = ( '12:22', '12:00 PM', '1:12', ) stmts = parse(tokens, pa1.parser_q0, pa1.parser_transitions, pa1.parser_F) self.assertEqual(stmts, result)
def test_parser_24_hour(self): tokens = ( ('24HOUR_TIME', '2322'), ('TO', ' to '), ('NOT_TOKEN', 'dsfsf'), ('24HOUR_TIME', '2300'), ('AT', ''), ('24HOUR_TIME', '1133'), ('24HOUR_TIME', '0028'), ('TO', ' to '), ('24HOUR_TIME', '1100'), ('AM_PM', ' PM'), ) result = ( '1133', '0028 to 1100', ) stmts = parse(tokens, pa1.parser_q0, pa1.parser_transitions, pa1.parser_F) self.assertEqual(stmts, result)
def test_parser_informal_time(self): tokens = ( ('INFORMAL_TIME', '1'), ('TO', ' to '), ('NOT_TOKEN', 'dsfsf'), ('INFORMAL_TIME', '12'), ('DASH', '-'), ('INFORMAL_TIME', '11'), ('INFORMAL_TIME', '8'), ('TO', ' to '), ('INFORMAL_TIME', '11'), ('AM_PM', ' PM'), ) result = ( '12-11', '8 to 11 PM', ) stmts = parse(tokens, pa1.parser_q0, pa1.parser_transitions, pa1.parser_F) self.assertEqual(stmts, result)
(r'TO|DASH', 'B'), ), 'E': (), 'F': ( (r'TO|DASH', 'G'), ), 'G': ( ('INFORMAL_TIME', 'H'), ), 'H': ( ('AM_PM', 'I'), ), 'I': (), 'J': ( ('AM_PM', 'I'), (r'TO|DASH', 'K'), ), 'K': ( ('12HOUR_TIME', 'L'), ), 'L': ( ('AM_PM', 'I'), ), } parser_F = ('C', 'H', 'I', 'J', 'L') if __name__ == '__main__': tokens = lex(string, lexer_q0, lexer_transitions, lexer_F, tokenize_events) print parse(tokens, parser_q0, parser_transitions, parser_F)
import os, sys import fsm_parser import fsm_builder if __name__ == "__main__": metadata = {} fsm_parser.parse(sys.argv[1], metadata) fsm_builder.build_world(metadata, sys.argv[2])