class WordGrammar(Grammar): r_phrase0 = Rule(TokVal('alpha'), TokVal('beta')) r_phrase1 = Rule(TokVal('gamma'), TokVal('zeta')) r_sof = Rule(Sof) r_eof = Rule(Eof) root = [r_phrase1, r_phrase0, r_sof, r_eof]
class ExprGrammar(Grammar): r_num = Rule(One, Except(Three), One) r_sof = Rule(Sof) r_eof = Rule(Eof) root = [r_num, r_sof, r_eof]
class ExprGrammar(Grammar): r_num0 = Rule(Four, Times(Only(Five, Two)), Four) r_num1 = Rule(Four, Times(Only(Five, Two)), One) r_sof = Rule(Sof) r_eof = Rule(Eof) root = [r_num0, r_num1, r_sof, r_eof]
class TupleGrammar(Grammar): # It means to accumulate as many Num tokens as possible. g_num = Times(Num, min=1, type=Num) # Then we trigge such a pattern in this rule. r_paren = Rule(LP, g_num, RP, type=Num) r_done = Rule(Sof, Num, Eof) root = [r_paren, r_done]
class ExprGrammar(Grammar): r_num0 = Rule(One, Two, Three, Four) r_num1 = Rule(One, Two, Three) r_num2 = Rule(One, Two) r_sof = Rule(Sof) r_eof = Rule(Eof) root = [r_num0, r_num1, r_num2, r_sof, r_eof]
class CalcGrammar(Grammar): r_paren = Rule(LP, Expr, RP, type=Expr) r_div = Rule(Expr, Div, Expr, type=Expr) r_mul = Rule(Expr, Mul, Expr, type=Expr) # The lookahead rules to fix ambiguity. o_div = Rule(Div) o_mul = Rule(Mul) r_plus = Rule(Expr, Plus, Expr, type=Expr, up=(o_mul, o_div)) r_minus = Rule(Expr, Minus, Expr, type=Expr, up=(o_mul, o_div)) r_num = Rule(Num, type=Expr) r_done = Rule(Sof, Expr, Eof) root = [r_paren, r_plus, r_minus, r_mul, r_div, r_num, r_done]
class CalcGrammar(Grammar): r_paren = Rule(LP, Num, RP, type=Num) r_div = Rule(Num, Div, Num, type=Num) r_mul = Rule(Num, Mul, Num, type=Num) o_div = Rule(Div) o_mul = Rule(Mul) r_plus = Rule(Num, Plus, Num, type=Num, up=(o_mul, o_div)) r_minus = Rule(Num, Minus, Num, type=Num, up=(o_mul, o_div)) r_done = Rule(Sof, Num, Eof) root = [r_paren, r_plus, r_minus, r_mul, r_div, r_done]
class CalcGrammar(Grammar): # The token patterns when matched them become # ParseTree objects which have a type. r_paren = Rule(LP, Num, RP, type=Num) r_div = Rule(Num, Div, Num, type=Num) r_mul = Rule(Num, Mul, Num, type=Num) o_div = Rule(Div) o_mul = Rule(Mul) o_plus = Rule(Plus) r_plus = Rule(Num, Plus, Num, type=Num, up=(o_mul, o_div)) r_minus = Rule(Num, Minus, Num, type=Num, up=(o_mul, o_div)) # The final structure that is consumed. Once it is # consumed then the process stops. r_done = Rule(Sof, Num, Eof) root = [r_paren, r_plus, r_minus, r_mul, r_div, r_done]
class ExprGrammar(Grammar): r_num = Rule(Two, DotTok(), Two) r_sof = Rule(Sof) r_eof = Rule(Eof) root = [r_num, r_sof, r_eof]
class ExprGrammar(Grammar): r_num = Rule(One, Times(Except(One)), One) r_sof = Rule(Sof) r_eof = Rule(Eof) root = [r_num, r_sof, r_eof]
class HClassGrammar(Grammar): r_seq = Rule(Char, Minus, Char, type=RegExpr) r_char = Rule(Char, type=RegExpr) r_done = Rule(Sof, T(RegExpr), Eof) root = [r_seq, r_char, r_done]
class RegexGrammar(Grammar): # r_escape = Rule(Escape, Char, type=Char) # No easy way out. o_pipe0 = Rule(LP) o_pipe1 = Rule(Mul) o_pipe2 = Rule(Question) o_pipe3 = Rule(LBR) o_pipe4 = Rule(LB) o_pipe5 = Rule(Char) o_pipe6 = Rule(Dot) o_pipe7 = Rule(Plus) o_pipe8 = Rule(Escape) o_pipe9 = Rule(Dollar) r_pipe = Rule(T(RegExpr), Pipe, T(RegExpr), type=RegExpr, up=(o_pipe0, o_pipe1, o_pipe2, o_pipe3, o_pipe4, o_pipe5, o_pipe6, o_pipe7, o_pipe8, o_pipe9)) r_group = Rule(LP, T(RegExpr, type=RegExpr), RP, type=RegExpr) r_ngroup = Rule(LP, Question, GroupSymbol, Lesser, GroupName, Greater, T(RegExpr), RP, type=RegExpr) r_word = Rule(Escape, WordSymbol, type=RegExpr) r_nword = Rule(Escape, NotWordSymbol, type=RegExpr) r_metab = Rule(Escape, MetabSymbol, type=RegExpr) r_metaB = Rule(Escape, MetaBSymbol, type=RegExpr) # Non capturing group. r_ncapture = Rule(LP, Question, Colon, T(RegExpr, type=RegExpr), RP, type=RegExpr) r_dot = Rule(Dot, type=RegExpr) r_caret = Rule(Caret, type=RegExpr) r_dollar = Rule(Dollar, type=RegExpr) r_times0 = Rule(RegExpr, LBR, T(Char, 1), Comma, T(Char, 1), RBR, type=RegExpr) r_times1 = Rule(RegExpr, LBR, T(Char, 1), RBR, type=RegExpr) r_times2 = Rule(RegExpr, LBR, T(Char, 1), Comma, RBR, type=RegExpr) r_times3 = Rule(RegExpr, LBR, Comma, T(Char, 1), RBR, type=RegExpr) r_times4 = Rule(RegExpr, Mul, type=RegExpr) r_times5 = Rule(RegExpr, Question, type=RegExpr) r_times6 = Rule(RegExpr, Plus, type=RegExpr) r_times7 = Rule(RegExpr, Plus, Question, type=RegExpr) r_times8 = Rule(RegExpr, Mul, Question, type=RegExpr) r_times9 = Rule(RegExpr, Question, Question, type=RegExpr) r_times10 = Rule(RegExpr, LBR, Char, Comma, Char, RBR, Question, type=RegExpr) r_times11 = Rule(RegExpr, LBR, Char, Comma, RBR, Question, type=RegExpr) r_times12 = Rule(RegExpr, LBR, Comma, Char, RBR, Question, type=RegExpr) r_times13 = Rule(RegExpr, LBR, Char, RBR, Question, type=RegExpr) r_include = Rule(LB, String, RB, type=RegExpr) r_exclude = Rule(LB, Caret, String, RB, type=RegExpr) r_gref = Rule(Escape, Num, type=RegExpr) r_comment = Rule(LP, Question, Hash, Comment, RP, type=RegExpr) r_ngref = Rule(LP, Question, GroupSymbol, Equal, GroupName, RP, type=RegExpr) r_cnext = Rule(LP, Question, Lesser, Equal, T(RegExpr), RP, T(RegExpr, min=0), type=RegExpr) r_ncnext = Rule(LP, Question, Lesser, Exclam, T(RegExpr), RP, T(RegExpr, 0), type=RegExpr) r_cback = Rule(T(RegExpr, 0), LP, Question, Equal, T(RegExpr), RP, type=RegExpr) r_ncback = Rule(T(RegExpr, 0), LP, Question, Exclam, T(RegExpr), RP, type=RegExpr) r_char = Rule(Char, type=RegExpr) r_done = Rule(Sof, T(RegExpr), Eof) root = [ r_gref, r_ngref, r_ncapture, r_comment, r_word, r_nword, r_metab, r_ngroup, r_group, r_dollar, r_caret, r_dot, r_cnext, r_ncnext, r_cback, r_metaB, r_ncback, r_times0, r_times1, r_times2, r_times3, r_times4, r_times5, r_times6, r_times7, r_times8, r_times9, r_times10, r_times11, r_times12, r_times13, r_pipe, r_exclude, r_include, r_char, r_done ]