6.9.6.2. Bootstrapping test

Start python section to interscript/parsers/lalr1.py[24 /24 ] Prev
   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: 
Start python section to interscript/tests/tgram.py[1 /1 ]
     1: #line 1205 "lalr1_parser.ipk"
     2: import interscript.parsers.lalr1
     3: interscript.parsers.lalr1._test1()
     4: interscript.parsers.lalr1._test2()
Start output section of "/usr/local/bin/python" interscript/tests/tgram.py
     1: Interscript version 1.0a8 build 1378
     2: Built by root on ruby at Sun Nov 29, 1998 at 08:48 PM (UTC)
     3: Generated by 1.0a8 buildno 1376 host ruby
     4: at Sun Nov 29, 1998 at 08:48 PM (UTC)
     5: actions
     6: 0 : {'id': ('s', 11), '(': ('s', 9)}
     7: 1 : {'+': ('s', 3), '<EOF>': ('a', None)}
     8: 2 : {'+': ('s', 3), ')': ('s', 10)}
     9: 3 : {'id': ('s', 11), '(': ('s', 9)}
    10: 4 : {'<EOF>': ('r', 1), '*': ('s', 6), '+': ('r', 1), ')': ('r', 1)}
    11: 5 : {'<EOF>': ('r', 2), '*': ('s', 6), '+': ('r', 2), ')': ('r', 2)}
    12: 6 : {'id': ('s', 11), '(': ('s', 9)}
    13: 7 : {'<EOF>': ('r', 3), '*': ('r', 3), '+': ('r', 3), ')': ('r', 3)}
    14: 8 : {'<EOF>': ('r', 4), '*': ('r', 4), '+': ('r', 4), ')': ('r', 4)}
    15: 9 : {'id': ('s', 11), '(': ('s', 9)}
    16: 10 : {'<EOF>': ('r', 5), '*': ('r', 5), '+': ('r', 5), ')': ('r', 5)}
    17: 11 : {'<EOF>': ('r', 6), '*': ('r', 6), '+': ('r', 6), ')': ('r', 6)}
    18: gotos
    19: 0 : {'F': 8, 'T': 5, 'E': 1}
    20: 1 : {}
    21: 2 : {}
    22: 3 : {'F': 8, 'T': 4}
    23: 4 : {}
    24: 5 : {}
    25: 6 : {'F': 7}
    26: 7 : {}
    27: 8 : {}
    28: 9 : {'F': 8, 'T': 5, 'E': 2}
    29: 10 : {}
    30: 11 : {}
    31: calling fdummy with args (('id', 3),)
    32: calling fdummy with args (('F', 3),)
    33: calling fdummy with args (('T', 3),)
    34: calling fdummy with args (('id', 4),)
    35: calling fdummy with args (('F', 4),)
    36: calling fdummy with args (('id', 2),)
    37: multiplying 4 with 2
    38: calling fdummy with args (('T', 8),)
    39: handling parens, returning whats in between
    40: calling fdummy with args (('F', 8),)
    41: adding 3 with 8
    42: handling parens, returning whats in between
    43: calling fdummy with args (('F', 11),)
    44: calling fdummy with args (('id', 2),)
    45: multiplying 11 with 2
    46: calling fdummy with args (('id', 5),)
    47: multiplying 22 with 5
    48: calling fdummy with args (('T', 110),)
    49: Accept
    50: RESULT= ('E', 110)
    51: bootgram= [('G', ['Plist'], <function just_copy at 8176c58>), ('Plist', ['Plist', 'P'], <function add_production_to_list at 8172860>), ('Plist', ['P'], <function new_production_list at 817ad10>), ('P', ['NAME', '=', 'RHS', '{', 'NAME', '}', 'NEWLINE'], <function build_production at 8186350>), ('RHS', ['RHS', 'SYM'], <function add_symbol_to_list at 8172500>), ('RHS', ['SYM'], <function new_symbol_list at 8172820>), ('SYM', ['NAME'], <function name_as_symbol at 80ba370>), ('SYM', ['STRING'], <function string_as_symbol at 80ba848>)]
    52: actions
    53: 0 : {'NAME': ('s', 5)}
    54: 1 : {'<EOF>': ('a', None)}
    55: 2 : {'<EOF>': ('r', 1), 'NAME': ('s', 5)}
    56: 3 : {'<EOF>': ('r', 2), 'NAME': ('r', 2)}
    57: 4 : {'<EOF>': ('r', 3), 'NAME': ('r', 3)}
    58: 5 : {'=': ('s', 6)}
    59: 6 : {'NAME': ('s', 14), 'STRING': ('s', 15)}
    60: 7 : {'{': ('s', 8), 'NAME': ('s', 14), 'STRING': ('s', 15)}
    61: 8 : {'NAME': ('s', 9)}
    62: 9 : {'}': ('s', 10)}
    63: 10 : {'NEWLINE': ('s', 11)}
    64: 11 : {'<EOF>': ('r', 4), 'NAME': ('r', 4)}
    65: 12 : {'{': ('r', 5), 'NAME': ('r', 5), 'STRING': ('r', 5)}
    66: 13 : {'{': ('r', 6), 'NAME': ('r', 6), 'STRING': ('r', 6)}
    67: 14 : {'{': ('r', 7), 'NAME': ('r', 7), 'STRING': ('r', 7)}
    68: 15 : {'{': ('r', 8), 'NAME': ('r', 8), 'STRING': ('r', 8)}
    69: gotos
    70: 0 : {'G': 1, 'Plist': 2, 'P': 4}
    71: 1 : {}
    72: 2 : {'P': 3}
    73: 3 : {}
    74: 4 : {}
    75: 5 : {}
    76: 6 : {'RHS': 7, 'SYM': 13}
    77: 7 : {'SYM': 12}
    78: 8 : {}
    79: 9 : {}
    80: 10 : {}
    81: 11 : {}
    82: 12 : {}
    83: 13 : {}
    84: 14 : {}
    85: 15 : {}
    86: 
    87: ---------------------------------------------------
    88:   TABLES FOR MANUALLY CONSTRUCTED GRAMMAR GENERATED
    89: ---------------------------------------------------
    90: 
    91: 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')]
    92: G = Plist { just_copy }
    93: Plist = Plist P { add_production_to_list }
    94: Plist = P { new_production_list }
    95: P = NAME = RHS { NAME } NEWLINE { build_production }
    96: RHS = RHS SYM { add_symbol_to_list }
    97: RHS = SYM { new_symbol_list }
    98: SYM = NAME { name_as_symbol }
    99: SYM = STRING { string_as_symbol }
   100: 
   101: Accept
   102: 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'}]
   103: BOUND= ['G' -> ['Plist'] {'func': <function just_copy at 8176c58>}, 'Plist' -> ['Plist', 'P'] {'func': <function add_production_to_list at 8172860>}, 'Plist' -> ['P'] {'func': <function new_production_list at 817ad10>}, 'P' -> ['NAME', '=', 'RHS', '{', 'NAME', '}', 'NEWLINE'] {'func': <function build_production at 8186350>}, 'RHS' -> ['RHS', 'SYM'] {'func': <function add_symbol_to_list at 8172500>}, 'RHS' -> ['SYM'] {'func': <function new_symbol_list at 8172820>}, 'SYM' -> ['NAME'] {'func': <function name_as_symbol at 80ba370>}, 'SYM' -> ['STRING'] {'func': <function string_as_symbol at 80ba848>}]
   104: 0 : 'G' -> ['Plist'] {'func': <function just_copy at 8176c58>}
   105: 1 : 'Plist' -> ['Plist', 'P'] {'func': <function add_production_to_list at 8172860>}
   106: 2 : 'Plist' -> ['P'] {'func': <function new_production_list at 817ad10>}
   107: 3 : 'P' -> ['NAME', '=', 'RHS', '{', 'NAME', '}', 'NEWLINE'] {'func': <function build_production at 8186350>}
   108: 4 : 'RHS' -> ['RHS', 'SYM'] {'func': <function add_symbol_to_list at 8172500>}
   109: 5 : 'RHS' -> ['SYM'] {'func': <function new_symbol_list at 8172820>}
   110: 6 : 'SYM' -> ['NAME'] {'func': <function name_as_symbol at 80ba370>}
   111: 7 : 'SYM' -> ['STRING'] {'func': <function string_as_symbol at 80ba848>}
   112: Symbols set('SYM', 'Plist', '=', 'NEWLINE', 'RHS', '}', 'STRING', 'NAME', 'P', '{')
   113: Terminals set('STRING', '}', 'NEWLINE', 'NAME', '=', '{')
   114: NonTerminals set('G', 'Plist', 'SYM', 'RHS', 'P')
   115: Directly Derive epsilon set()
   116: Derive epsilon set()
   117: First Sets:
   118: -1 -> set(-1)
   119: Plist -> set('NAME')
   120: } -> set('}')
   121: { -> set('{')
   122: P -> set('NAME')
   123: <EPS> -> set('<EPS>')
   124: NAME -> set('NAME')
   125: = -> set('=')
   126: SYM -> set('STRING', 'NAME')
   127: <EOF> -> set('<EOF>')
   128: NEWLINE -> set('NEWLINE')
   129: RHS -> set('STRING', 'NAME')
   130: G -> set('NAME')
   131: STRING -> set('STRING')
   132: Follow Sets:
   133: G -> set('<EOF>')
   134: Plist -> set('<EOF>', 'NAME')
   135: SYM -> set('{', 'STRING', 'NAME')
   136: RHS -> set('{', 'STRING', 'NAME')
   137: P -> set('<EOF>', 'NAME')
   138: found 4 more kernels
   139: found 2 more kernels
   140: found 4 more kernels
   141: found 2 more kernels
   142: found 1 more kernels
   143: found 1 more kernels
   144: found 1 more kernels
   145: initLALR1items, kernels done, calculating propagations and spontaneous lookaheads
   146: . . . . . . . . . . . . . . . . done init LALR1items
   147: done with lalr1items, reorganizing the data
   148: actions
   149: 0 : {'NAME': ('s', 5)}
   150: 1 : {'<EOF>': ('a', None)}
   151: 2 : {'<EOF>': ('r', 1), 'NAME': ('s', 5)}
   152: 3 : {'<EOF>': ('r', 2), 'NAME': ('r', 2)}
   153: 4 : {'<EOF>': ('r', 3), 'NAME': ('r', 3)}
   154: 5 : {'=': ('s', 6)}
   155: 6 : {'NAME': ('s', 14), 'STRING': ('s', 15)}
   156: 7 : {'{': ('s', 8), 'NAME': ('s', 14), 'STRING': ('s', 15)}
   157: 8 : {'NAME': ('s', 9)}
   158: 9 : {'}': ('s', 10)}
   159: 10 : {'NEWLINE': ('s', 11)}
   160: 11 : {'<EOF>': ('r', 4), 'NAME': ('r', 4)}
   161: 12 : {'{': ('r', 5), 'NAME': ('r', 5), 'STRING': ('r', 5)}
   162: 13 : {'{': ('r', 6), 'NAME': ('r', 6), 'STRING': ('r', 6)}
   163: 14 : {'{': ('r', 7), 'NAME': ('r', 7), 'STRING': ('r', 7)}
   164: 15 : {'{': ('r', 8), 'NAME': ('r', 8), 'STRING': ('r', 8)}
   165: gotos
   166: 0 : {'G': 1, 'Plist': 2, 'P': 4}
   167: 1 : {}
   168: 2 : {'P': 3}
   169: 3 : {}
   170: 4 : {}
   171: 5 : {}
   172: 6 : {'RHS': 7, 'SYM': 13}
   173: 7 : {'SYM': 12}
   174: 8 : {}
   175: 9 : {}
   176: 10 : {}
   177: 11 : {}
   178: 12 : {}
   179: 13 : {}
   180: 14 : {}
   181: 15 : {}
   182: 
   183: --------------------------------------------------------
   184:   TABLES FOR AUTOMATICALLY CONSTRUCTED GRAMMAR GENERATED
   185: --------------------------------------------------------
   186: 
   187: Accept
   188: 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'}]
   189: --------------------------------------------------------
   190:  IF THAT WORKED, WE HAVE A BOOTSTRAP
   191: --------------------------------------------------------
   192: Accept
   193: 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'}]
   194: BOUND= ['G' -> ['S'] {'func': <function dummy at 80f3d60>}, 'S' -> ['if', 'E', 'then', 'S', 'else', 'S'] {'func': <function dummy at 80f3d60>}, 'S' -> ['if', 'E', 'then', 'S'] {'func': <function dummy at 80f3d60>}, 'S' -> ['X'] {'func': <function dummy at 80f3d60>}, 'E' -> ['X'] {'func': <function dummy at 80f3d60>}]
   195: 0 : 'G' -> ['S'] {'func': <function dummy at 80f3d60>}
   196: 1 : 'S' -> ['if', 'E', 'then', 'S', 'else', 'S'] {'func': <function dummy at 80f3d60>}
   197: 2 : 'S' -> ['if', 'E', 'then', 'S'] {'func': <function dummy at 80f3d60>}
   198: 3 : 'S' -> ['X'] {'func': <function dummy at 80f3d60>}
   199: 4 : 'E' -> ['X'] {'func': <function dummy at 80f3d60>}
   200: Symbols set('if', 'X', 'E', 'else', 'S', 'then')
   201: Terminals set('if', 'then', 'X', 'else')
   202: NonTerminals set('S', 'G', 'E')
   203: Directly Derive epsilon set()
   204: Derive epsilon set()
   205: First Sets:
   206: if -> set('if')
   207: -1 -> set(-1)
   208: <EOF> -> set('<EOF>')
   209: X -> set('X')
   210: G -> set('if', 'X')
   211: <EPS> -> set('<EPS>')
   212: E -> set('X')
   213: else -> set('else')
   214: S -> set('if', 'X')
   215: then -> set('then')
   216: Follow Sets:
   217: G -> set('<EOF>')
   218: E -> set('then')
   219: S -> set('else', '<EOF>')
   220: found 4 more kernels
   221: found 2 more kernels
   222: found 1 more kernels
   223: found 1 more kernels
   224: found 1 more kernels
   225: found 1 more kernels
   226: initLALR1items, kernels done, calculating propagations and spontaneous lookaheads
   227: . . . . . . . . . . . done init LALR1items
   228: done with lalr1items, reorganizing the data
   229: Shift/Reduce Conflict, Use Shift [6,'else']: ('r', 3) -> ('s', 7)
   230: actions
   231: 0 : {'if': ('s', 3), 'X': ('s', 9)}
   232: 1 : {'<EOF>': ('a', None)}
   233: 2 : {'<EOF>': ('r', 1)}
   234: 3 : {'X': ('s', 10)}
   235: 4 : {'then': ('s', 5)}
   236: 5 : {'if': ('s', 3), 'X': ('s', 9)}
   237: 6 : {'else': ('s', 7), '<EOF>': ('r', 3)}
   238: 7 : {'if': ('s', 3), 'X': ('s', 9)}
   239: 8 : {'else': ('r', 2), '<EOF>': ('r', 2)}
   240: 9 : {'else': ('r', 4), '<EOF>': ('r', 4)}
   241: 10 : {'then': ('r', 5)}
   242: gotos
   243: 0 : {'S': 2, 'G': 1}
   244: 1 : {}
   245: 2 : {}
   246: 3 : {'E': 4}
   247: 4 : {}
   248: 5 : {'S': 6}
   249: 6 : {}
   250: 7 : {'S': 8}
   251: 8 : {}
   252: 9 : {}
   253: 10 : {}
   254: [('if', 'if'), ('X', 'X'), ('then', 'then'), ('X', 'X'), ('else', 'else'), ('X', 'X')]
   255: dummy:reduce 'E' -> ['X'] {'func': <function dummy at 80f3d60>} args= (('X', 'X'),)
   256: dummy:reduce 'S' -> ['X'] {'func': <function dummy at 80f3d60>} args= (('X', 'X'),)
   257: dummy:reduce 'S' -> ['X'] {'func': <function dummy at 80f3d60>} args= (('X', 'X'),)
   258: dummy:reduce 'S' -> ['if', 'E', 'then', 'S', 'else', 'S'] {'func': <function dummy at 80f3d60>} args= (('if', 'if'), ('E', None), ('then', 'then'), ('S', None), ('else', 'else'), ('S', None))
   259: dummy:reduce 'G' -> ['S'] {'func': <function dummy at 80f3d60>} args= (('S', None),)
   260: Accept
   261: RESULT= None
End output section to "/usr/local/bin/python" interscript/tests/tgram.py
     3: #line 17 "parsers.ipk"
     4: