6.9.6.2. Bootstrapping test

6.9.6.2. Bootstrapping test

Start python section to interscript/parsers/lalr1.py[24]
   829: #line 1086 "lalr1_parser.ipk"
   830: def _test2():
   831:     def dummy(prod,*args):
   832:       print 'dummy:reduce',prod,'args=',args
   833:       return None
   834: 
   835:     def new_production_list(prod, *args):
   836:       return [args[0][1]]
   837: 
   838:     def add_production_to_list(prod, *args):
   839:       return args[0][1]+[args[1][1]]
   840: 
   841:     def build_production(prod, *args):
   842:       return Production(args[0][1],args[2][1],func=args[4][1])
   843: 
   844:     def new_symbol_list(prod, *args):
   845:       return [args[0][1]]
   846: 
   847:     def add_symbol_to_list(prod, *args):
   848:       return args[0][1]+[args[1][1]]
   849: 
   850:     def name_as_symbol(prod, *args):
   851:       return args[0][1]
   852: 
   853:     def string_as_symbol(prod, *args):
   854:       return args[0][1]
   855: 
   856:     def just_copy(prod, *args):
   857:       return args[0][1]
   858: 
   859:     bootgram = [
   860:       ('G',    ['Plist'], just_copy),
   861:       ('Plist', ['Plist','P'], add_production_to_list),
   862:       ('Plist', ['P'], new_production_list),
   863:       ('P', ['NAME','=','RHS','{','NAME','}','NEWLINE'],build_production),
   864:       ('RHS',['RHS','SYM'],add_symbol_to_list),
   865:       ('RHS',['SYM'],new_symbol_list),
   866:       ('SYM',['NAME'],name_as_symbol),
   867:       ('SYM',['STRING'],string_as_symbol)
   868:     ]
   869:     print 'bootgram=',bootgram
   870:     prods = map(lambda x: Production(x[0], x[1], func=x[2]), bootgram)
   871: 
   872:     g = LALRGrammar(prods, "G", verbosity=1)
   873:     pr_tab(g)
   874: 
   875:     print
   876:     print '---------------------------------------------------'
   877:     print '  TABLES FOR MANUALLY CONSTRUCTED GRAMMAR GENERATED'
   878:     print '---------------------------------------------------'
   879:     print
   880: 
   881:     input = """G       = Plist { just_copy }
   882: Plist   = Plist P { add_production_to_list }
   883: Plist   = P { new_production_list }
   884: P       = NAME \= RHS \{ NAME \} NEWLINE { build_production }
   885: RHS     = RHS SYM { add_symbol_to_list }
   886: RHS     = SYM { new_symbol_list }
   887: SYM     = NAME { name_as_symbol }
   888: SYM     = STRING { string_as_symbol }
   889: """
   890:     tokens = tokenise(input)
   891:     print 'tokens=',tokens
   892:     for t in tokens: print t[1],
   893:     print
   894: 
   895:     res = g.parse(tokens)
   896:     prods = res[1]
   897:     print 'RESULT=',prods
   898: 
   899:     for p in prods: p.func = eval(p.func)
   900:     print 'BOUND=',prods
   901: 
   902:     g = LALRGrammar(prods, "G", verbosity=2)
   903:     pr_tab(g)
   904: 
   905:     print
   906:     print '--------------------------------------------------------'
   907:     print '  TABLES FOR AUTOMATICALLY CONSTRUCTED GRAMMAR GENERATED'
   908:     print '--------------------------------------------------------'
   909:     print
   910: 
   911:     res = g.parse(tokens)
   912:     prods = res[1]
   913:     print 'RESULT=',prods
   914: 
   915:     print '--------------------------------------------------------'
   916:     print ' IF THAT WORKED, WE HAVE A BOOTSTRAP                    '
   917:     print '--------------------------------------------------------'
   918: 
   919:     input = """G = S {dummy}
   920: S = if E then S else S {dummy}
   921: S = if E then S {dummy}
   922: S = X {dummy}
   923: E = X {dummy}
   924: """
   925:     tokens = tokenise(input)
   926:     res = g.parse(tokens)
   927:     prods = res[1]
   928:     print 'RESULT=',prods
   929: 
   930:     for p in prods: p.func = eval(p.func)
   931:     print 'BOUND=',prods
   932: 
   933:     ifthenelse = LALRGrammar(prods, "G", verbosity=2)
   934:     pr_tab(ifthenelse)
   935:     input = """if X then X else X"""
   936:     tokens = tokenise(input)
   937:     toks = []
   938:     for tok in tokens[:]:
   939:       toks.append((tok[1],tok[1]))
   940:     print toks
   941:     res = ifthenelse.parse(toks)
   942:     prods = res[1]
   943:     print 'RESULT=',prods
   944: 
   945: 
End python section to interscript/parsers/lalr1.py[24]
Start python section to interscript/tests/tgram.py[1]
     1: #line 1205 "lalr1_parser.ipk"
     2: import interscript.parsers.lalr1
     3: interscript.parsers.lalr1._test1()
     4: interscript.parsers.lalr1._test2()
End python section to interscript/tests/tgram.py[1]
Start output section of /usr/local/bin/python interscript/tests/tgram.py
     1: Interscript Package: version 1.0a7 build 1253
     2: Using iscrcopt
     3: thread available
     4: Interscript version 1.0a7 build 1253
     5: Built by root on ruby at Mon Nov 09, 1998 at 07:08 PM (UTC)
     6: Generated by 1.0a7 buildno 1252 host ruby
     7: at Mon Nov 09, 1998 at 07:08 PM (UTC)
     8: actions
     9: 0 : {'id': ('s', 11), '(': ('s', 9)}
    10: 1 : {'+': ('s', 3), '<EOF>': ('a', None)}
    11: 2 : {'+': ('s', 3), ')': ('s', 10)}
    12: 3 : {'id': ('s', 11), '(': ('s', 9)}
    13: 4 : {'<EOF>': ('r', 1), '*': ('s', 6), '+': ('r', 1), ')': ('r', 1)}
    14: 5 : {'<EOF>': ('r', 2), '*': ('s', 6), '+': ('r', 2), ')': ('r', 2)}
    15: 6 : {'id': ('s', 11), '(': ('s', 9)}
    16: 7 : {'<EOF>': ('r', 3), '*': ('r', 3), '+': ('r', 3), ')': ('r', 3)}
    17: 8 : {'<EOF>': ('r', 4), '*': ('r', 4), '+': ('r', 4), ')': ('r', 4)}
    18: 9 : {'id': ('s', 11), '(': ('s', 9)}
    19: 10 : {'<EOF>': ('r', 5), '*': ('r', 5), '+': ('r', 5), ')': ('r', 5)}
    20: 11 : {'<EOF>': ('r', 6), '*': ('r', 6), '+': ('r', 6), ')': ('r', 6)}
    21: gotos
    22: 0 : {'F': 8, 'T': 5, 'E': 1}
    23: 1 : {}
    24: 2 : {}
    25: 3 : {'F': 8, 'T': 4}
    26: 4 : {}
    27: 5 : {}
    28: 6 : {'F': 7}
    29: 7 : {}
    30: 8 : {}
    31: 9 : {'F': 8, 'T': 5, 'E': 2}
    32: 10 : {}
    33: 11 : {}
    34: calling fdummy with args (('id', 3),)
    35: calling fdummy with args (('F', 3),)
    36: calling fdummy with args (('T', 3),)
    37: calling fdummy with args (('id', 4),)
    38: calling fdummy with args (('F', 4),)
    39: calling fdummy with args (('id', 2),)
    40: multiplying 4 with 2
    41: calling fdummy with args (('T', 8),)
    42: handling parens, returning whats in between
    43: calling fdummy with args (('F', 8),)
    44: adding 3 with 8
    45: handling parens, returning whats in between
    46: calling fdummy with args (('F', 11),)
    47: calling fdummy with args (('id', 2),)
    48: multiplying 11 with 2
    49: calling fdummy with args (('id', 5),)
    50: multiplying 22 with 5
    51: calling fdummy with args (('T', 110),)
    52: Accept
    53: RESULT= ('E', 110)
    54: bootgram= [('G', ['Plist'], <function just_copy at 80aef08>), ('Plist', ['Plist', 'P'], <function add_production_to_list at 8156368>), ('Plist', ['P'], <function new_production_list at 8163be8>), ('P', ['NAME', '=', 'RHS', '{', 'NAME', '}', 'NEWLINE'], <function build_production at 815a2a8>), ('RHS', ['RHS', 'SYM'], <function add_symbol_to_list at 80ba860>), ('RHS', ['SYM'], <function new_symbol_list at 8156128>), ('SYM', ['NAME'], <function name_as_symbol at 8155e60>), ('SYM', ['STRING'], <function string_as_symbol at 80aeee8>)]
    55: actions
    56: 0 : {'NAME': ('s', 5)}
    57: 1 : {'<EOF>': ('a', None)}
    58: 2 : {'<EOF>': ('r', 1), 'NAME': ('s', 5)}
    59: 3 : {'<EOF>': ('r', 2), 'NAME': ('r', 2)}
    60: 4 : {'<EOF>': ('r', 3), 'NAME': ('r', 3)}
    61: 5 : {'=': ('s', 6)}
    62: 6 : {'NAME': ('s', 14), 'STRING': ('s', 15)}
    63: 7 : {'{': ('s', 8), 'NAME': ('s', 14), 'STRING': ('s', 15)}
    64: 8 : {'NAME': ('s', 9)}
    65: 9 : {'}': ('s', 10)}
    66: 10 : {'NEWLINE': ('s', 11)}
    67: 11 : {'<EOF>': ('r', 4), 'NAME': ('r', 4)}
    68: 12 : {'{': ('r', 5), 'NAME': ('r', 5), 'STRING': ('r', 5)}
    69: 13 : {'{': ('r', 6), 'NAME': ('r', 6), 'STRING': ('r', 6)}
    70: 14 : {'{': ('r', 7), 'NAME': ('r', 7), 'STRING': ('r', 7)}
    71: 15 : {'{': ('r', 8), 'NAME': ('r', 8), 'STRING': ('r', 8)}
    72: gotos
    73: 0 : {'G': 1, 'Plist': 2, 'P': 4}
    74: 1 : {}
    75: 2 : {'P': 3}
    76: 3 : {}
    77: 4 : {}
    78: 5 : {}
    79: 6 : {'RHS': 7, 'SYM': 13}
    80: 7 : {'SYM': 12}
    81: 8 : {}
    82: 9 : {}
    83: 10 : {}
    84: 11 : {}
    85: 12 : {}
    86: 13 : {}
    87: 14 : {}
    88: 15 : {}
    89: 
    90: ---------------------------------------------------
    91:   TABLES FOR MANUALLY CONSTRUCTED GRAMMAR GENERATED
    92: ---------------------------------------------------
    93: 
    94: tokens= [('NAME', 'G'), ('=', '='), ('NAME', 'Plist'), ('{', '{'), ('NAME', 'just_copy'), ('}', '}'), ('NEWLINE', '\012'), ('NAME', 'Plist'), ('=', '='), ('NAME', 'Plist'), ('NAME', 'P'), ('{', '{'), ('NAME', 'add_production_to_list'), ('}', '}'), ('NEWLINE', '\012'), ('NAME', 'Plist'), ('=', '='), ('NAME', 'P'), ('{', '{'), ('NAME', 'new_production_list'), ('}', '}'), ('NEWLINE', '\012'), ('NAME', 'P'), ('=', '='), ('NAME', 'NAME'), ('STRING', '='), ('NAME', 'RHS'), ('STRING', '{'), ('NAME', 'NAME'), ('STRING', '}'), ('NAME', 'NEWLINE'), ('{', '{'), ('NAME', 'build_production'), ('}', '}'), ('NEWLINE', '\012'), ('NAME', 'RHS'), ('=', '='), ('NAME', 'RHS'), ('NAME', 'SYM'), ('{', '{'), ('NAME', 'add_symbol_to_list'), ('}', '}'), ('NEWLINE', '\012'), ('NAME', 'RHS'), ('=', '='), ('NAME', 'SYM'), ('{', '{'), ('NAME', 'new_symbol_list'), ('}', '}'), ('NEWLINE', '\012'), ('NAME', 'SYM'), ('=', '='), ('NAME', 'NAME'), ('{', '{'), ('NAME', 'name_as_symbol'), ('}', '}'), ('NEWLINE', '\012'), ('NAME', 'SYM'), ('=', '='), ('NAME', 'STRING'), ('{', '{'), ('NAME', 'string_as_symbol'), ('}', '}'), ('NEWLINE', '\012')]
    95: G = Plist { just_copy }
    96: Plist = Plist P { add_production_to_list }
    97: Plist = P { new_production_list }
    98: P = NAME = RHS { NAME } NEWLINE { build_production }
    99: RHS = RHS SYM { add_symbol_to_list }
   100: RHS = SYM { new_symbol_list }
   101: SYM = NAME { name_as_symbol }
   102: SYM = STRING { string_as_symbol }
   103: 
   104: Accept
   105: RESULT= ['G' -> ['Plist'] {'func': 'just_copy'}, 'Plist' -> ['Plist', 'P'] {'func': 'add_production_to_list'}, 'Plist' -> ['P'] {'func': 'new_production_list'}, 'P' -> ['NAME', '=', 'RHS', '{', 'NAME', '}', 'NEWLINE'] {'func': 'build_production'}, 'RHS' -> ['RHS', 'SYM'] {'func': 'add_symbol_to_list'}, 'RHS' -> ['SYM'] {'func': 'new_symbol_list'}, 'SYM' -> ['NAME'] {'func': 'name_as_symbol'}, 'SYM' -> ['STRING'] {'func': 'string_as_symbol'}]
   106: BOUND= ['G' -> ['Plist'] {'func': <function just_copy at 80aef08>}, 'Plist' -> ['Plist', 'P'] {'func': <function add_production_to_list at 8156368>}, 'Plist' -> ['P'] {'func': <function new_production_list at 8163be8>}, 'P' -> ['NAME', '=', 'RHS', '{', 'NAME', '}', 'NEWLINE'] {'func': <function build_production at 815a2a8>}, 'RHS' -> ['RHS', 'SYM'] {'func': <function add_symbol_to_list at 80ba860>}, 'RHS' -> ['SYM'] {'func': <function new_symbol_list at 8156128>}, 'SYM' -> ['NAME'] {'func': <function name_as_symbol at 8155e60>}, 'SYM' -> ['STRING'] {'func': <function string_as_symbol at 80aeee8>}]
   107: 0 : 'G' -> ['Plist'] {'func': <function just_copy at 80aef08>}
   108: 1 : 'Plist' -> ['Plist', 'P'] {'func': <function add_production_to_list at 8156368>}
   109: 2 : 'Plist' -> ['P'] {'func': <function new_production_list at 8163be8>}
   110: 3 : 'P' -> ['NAME', '=', 'RHS', '{', 'NAME', '}', 'NEWLINE'] {'func': <function build_production at 815a2a8>}
   111: 4 : 'RHS' -> ['RHS', 'SYM'] {'func': <function add_symbol_to_list at 80ba860>}
   112: 5 : 'RHS' -> ['SYM'] {'func': <function new_symbol_list at 8156128>}
   113: 6 : 'SYM' -> ['NAME'] {'func': <function name_as_symbol at 8155e60>}
   114: 7 : 'SYM' -> ['STRING'] {'func': <function string_as_symbol at 80aeee8>}
   115: Symbols set('SYM', 'Plist', '=', 'NEWLINE', 'RHS', '}', 'STRING', 'NAME', 'P', '{')
   116: Terminals set('STRING', '}', 'NEWLINE', 'NAME', '=', '{')
   117: NonTerminals set('G', 'Plist', 'SYM', 'RHS', 'P')
   118: Directly Derive epsilon set()
   119: Derive epsilon set()
   120: First Sets:
   121: -1 -> set(-1)
   122: Plist -> set('NAME')
   123: } -> set('}')
   124: { -> set('{')
   125: P -> set('NAME')
   126: <EPS> -> set('<EPS>')
   127: NAME -> set('NAME')
   128: = -> set('=')
   129: SYM -> set('STRING', 'NAME')
   130: <EOF> -> set('<EOF>')
   131: NEWLINE -> set('NEWLINE')
   132: RHS -> set('STRING', 'NAME')
   133: G -> set('NAME')
   134: STRING -> set('STRING')
   135: Follow Sets:
   136: G -> set('<EOF>')
   137: Plist -> set('<EOF>', 'NAME')
   138: SYM -> set('{', 'STRING', 'NAME')
   139: RHS -> set('{', 'STRING', 'NAME')
   140: P -> set('<EOF>', 'NAME')
   141: found 4 more kernels
   142: found 2 more kernels
   143: found 4 more kernels
   144: found 2 more kernels
   145: found 1 more kernels
   146: found 1 more kernels
   147: found 1 more kernels
   148: initLALR1items, kernels done, calculating propagations and spontaneous lookaheads
   149: . . . . . . . . . . . . . . . . done init LALR1items
   150: done with lalr1items, reorganizing the data
   151: actions
   152: 0 : {'NAME': ('s', 5)}
   153: 1 : {'<EOF>': ('a', None)}
   154: 2 : {'<EOF>': ('r', 1), 'NAME': ('s', 5)}
   155: 3 : {'<EOF>': ('r', 2), 'NAME': ('r', 2)}
   156: 4 : {'<EOF>': ('r', 3), 'NAME': ('r', 3)}
   157: 5 : {'=': ('s', 6)}
   158: 6 : {'NAME': ('s', 14), 'STRING': ('s', 15)}
   159: 7 : {'{': ('s', 8), 'NAME': ('s', 14), 'STRING': ('s', 15)}
   160: 8 : {'NAME': ('s', 9)}
   161: 9 : {'}': ('s', 10)}
   162: 10 : {'NEWLINE': ('s', 11)}
   163: 11 : {'<EOF>': ('r', 4), 'NAME': ('r', 4)}
   164: 12 : {'{': ('r', 5), 'NAME': ('r', 5), 'STRING': ('r', 5)}
   165: 13 : {'{': ('r', 6), 'NAME': ('r', 6), 'STRING': ('r', 6)}
   166: 14 : {'{': ('r', 7), 'NAME': ('r', 7), 'STRING': ('r', 7)}
   167: 15 : {'{': ('r', 8), 'NAME': ('r', 8), 'STRING': ('r', 8)}
   168: gotos
   169: 0 : {'G': 1, 'Plist': 2, 'P': 4}
   170: 1 : {}
   171: 2 : {'P': 3}
   172: 3 : {}
   173: 4 : {}
   174: 5 : {}
   175: 6 : {'RHS': 7, 'SYM': 13}
   176: 7 : {'SYM': 12}
   177: 8 : {}
   178: 9 : {}
   179: 10 : {}
   180: 11 : {}
   181: 12 : {}
   182: 13 : {}
   183: 14 : {}
   184: 15 : {}
   185: 
   186: --------------------------------------------------------
   187:   TABLES FOR AUTOMATICALLY CONSTRUCTED GRAMMAR GENERATED
   188: --------------------------------------------------------
   189: 
   190: Accept
   191: RESULT= ['G' -> ['Plist'] {'func': 'just_copy'}, 'Plist' -> ['Plist', 'P'] {'func': 'add_production_to_list'}, 'Plist' -> ['P'] {'func': 'new_production_list'}, 'P' -> ['NAME', '=', 'RHS', '{', 'NAME', '}', 'NEWLINE'] {'func': 'build_production'}, 'RHS' -> ['RHS', 'SYM'] {'func': 'add_symbol_to_list'}, 'RHS' -> ['SYM'] {'func': 'new_symbol_list'}, 'SYM' -> ['NAME'] {'func': 'name_as_symbol'}, 'SYM' -> ['STRING'] {'func': 'string_as_symbol'}]
   192: --------------------------------------------------------
   193:  IF THAT WORKED, WE HAVE A BOOTSTRAP
   194: --------------------------------------------------------
   195: Accept
   196: RESULT= ['G' -> ['S'] {'func': 'dummy'}, 'S' -> ['if', 'E', 'then', 'S', 'else', 'S'] {'func': 'dummy'}, 'S' -> ['if', 'E', 'then', 'S'] {'func': 'dummy'}, 'S' -> ['X'] {'func': 'dummy'}, 'E' -> ['X'] {'func': 'dummy'}]
   197: BOUND= ['G' -> ['S'] {'func': <function dummy at 81563a8>}, 'S' -> ['if', 'E', 'then', 'S', 'else', 'S'] {'func': <function dummy at 81563a8>}, 'S' -> ['if', 'E', 'then', 'S'] {'func': <function dummy at 81563a8>}, 'S' -> ['X'] {'func': <function dummy at 81563a8>}, 'E' -> ['X'] {'func': <function dummy at 81563a8>}]
   198: 0 : 'G' -> ['S'] {'func': <function dummy at 81563a8>}
   199: 1 : 'S' -> ['if', 'E', 'then', 'S', 'else', 'S'] {'func': <function dummy at 81563a8>}
   200: 2 : 'S' -> ['if', 'E', 'then', 'S'] {'func': <function dummy at 81563a8>}
   201: 3 : 'S' -> ['X'] {'func': <function dummy at 81563a8>}
   202: 4 : 'E' -> ['X'] {'func': <function dummy at 81563a8>}
   203: Symbols set('if', 'X', 'E', 'else', 'S', 'then')
   204: Terminals set('if', 'then', 'X', 'else')
   205: NonTerminals set('S', 'G', 'E')
   206: Directly Derive epsilon set()
   207: Derive epsilon set()
   208: First Sets:
   209: if -> set('if')
   210: -1 -> set(-1)
   211: <EOF> -> set('<EOF>')
   212: X -> set('X')
   213: G -> set('if', 'X')
   214: <EPS> -> set('<EPS>')
   215: E -> set('X')
   216: else -> set('else')
   217: S -> set('if', 'X')
   218: then -> set('then')
   219: Follow Sets:
   220: G -> set('<EOF>')
   221: E -> set('then')
   222: S -> set('else', '<EOF>')
   223: found 4 more kernels
   224: found 2 more kernels
   225: found 1 more kernels
   226: found 1 more kernels
   227: found 1 more kernels
   228: found 1 more kernels
   229: initLALR1items, kernels done, calculating propagations and spontaneous lookaheads
   230: . . . . . . . . . . . done init LALR1items
   231: done with lalr1items, reorganizing the data
   232: Shift/Reduce Conflict, Use Shift [6,'else']: ('r', 3) -> ('s', 7)
   233: actions
   234: 0 : {'if': ('s', 3), 'X': ('s', 9)}
   235: 1 : {'<EOF>': ('a', None)}
   236: 2 : {'<EOF>': ('r', 1)}
   237: 3 : {'X': ('s', 10)}
   238: 4 : {'then': ('s', 5)}
   239: 5 : {'if': ('s', 3), 'X': ('s', 9)}
   240: 6 : {'else': ('s', 7), '<EOF>': ('r', 3)}
   241: 7 : {'if': ('s', 3), 'X': ('s', 9)}
   242: 8 : {'else': ('r', 2), '<EOF>': ('r', 2)}
   243: 9 : {'else': ('r', 4), '<EOF>': ('r', 4)}
   244: 10 : {'then': ('r', 5)}
   245: gotos
   246: 0 : {'S': 2, 'G': 1}
   247: 1 : {}
   248: 2 : {}
   249: 3 : {'E': 4}
   250: 4 : {}
   251: 5 : {'S': 6}
   252: 6 : {}
   253: 7 : {'S': 8}
   254: 8 : {}
   255: 9 : {}
   256: 10 : {}
   257: [('if', 'if'), ('X', 'X'), ('then', 'then'), ('X', 'X'), ('else', 'else'), ('X', 'X')]
   258: dummy:reduce 'E' -> ['X'] {'func': <function dummy at 81563a8>} args= (('X', 'X'),)
   259: dummy:reduce 'S' -> ['X'] {'func': <function dummy at 81563a8>} args= (('X', 'X'),)
   260: dummy:reduce 'S' -> ['X'] {'func': <function dummy at 81563a8>} args= (('X', 'X'),)
   261: dummy:reduce 'S' -> ['if', 'E', 'then', 'S', 'else', 'S'] {'func': <function dummy at 81563a8>} args= (('if', 'if'), ('E', None), ('then', 'then'), ('S', None), ('else', 'else'), ('S', None))
   262: dummy:reduce 'G' -> ['S'] {'func': <function dummy at 81563a8>} args= (('S', None),)
   263: Accept
   264: RESULT= None
End output section to /usr/local/bin/python interscript/tests/tgram.py