def compileGrammar(method, ruleSet, showStates = None, showItems = None,
                   statusMsg = 1):
    """given the method and the ruleset, generate a parsing table.
    the other options are debug shit
    """
    if statusMsg:
        print 'scanning grammar for terminals and nonterminals'
    terminals, nonTerminals = siftTokens(ruleSet)
    digestedRules = digestRules(ruleSet)

    if statusMsg:
        print 'producing items for grammar'
        
    if method == 'LALR1': #LALR via canonical LR
        C = LR1.items(ruleSet, terminals, nonTerminals)
        C = LALR.canonicalLRToLALR(C)

    elif method == 'LALR0': #LALR via LR1 -roundaboutly
        C = LALR.items(ruleSet, terminals, nonTerminals)
        #C = LALR.computeLALRKernels(C, ruleSet, terminals, nonTerminals)

    elif method == 'LR1': #canonical LR
        C = LR1.items(ruleSet, terminals, nonTerminals)

    elif method == 'LR0': #SLR (LR0)
        C = LR0.items(ruleSet, terminals, nonTerminals)

    if showItems != None:
        printItems('%s Items' % method, C)
        if showItems == 2:
            return {}

    if statusMsg:
        print 'generating state and goto tables'
        
    if method == 'LALR1': #LALR via canonical LR
        stateTable, gotoTable = LR1.generateStateTable(C, ruleSet, terminals,
                                                       C.index)

    elif method == 'LALR0': #LALR via CLR - roundaboutly
        stateTable, gotoTable = LR1.generateStateTable(C, ruleSet, terminals,
                                                        C.index)

    elif method == 'LR1': #canonical LR
        stateTable, gotoTable = LR1.generateStateTable(
            C, ruleSet, terminals, lambda x, y=C: LR1.fullIndex(y, x))

    elif method == 'LR0': #SLR (LR0)
        stateTable, gotoTable = LR0.generateStateTable(C, ruleSet, terminals,
                                                       nonTerminals)
        
        
    if showStates != None:
        printStateTable(C, terminals, nonTerminals, stateTable, gotoTable)
        
    return {'states': stateTable,
            'gotos': gotoTable,
            'terminals': terminals,
            'rules': digestedRules
            }
Example #2
0
def sel():
   global count
   count+=1
   selection = "You selected the option " + str(var.get())
   f=open("grammar.txt","w")
   if(count==1):
      s=T.get("1.0", END)
      f.write(s)
      print("Done")
      f.close()
      LALR.main()
   if(var.get()==3):
        table1.foo()
   elif(var.get()==1):
        #lines =T.get("1.0", END).splitlines()
        table.func()
   label.config(text = selection)
def compileGrammar(method,
                   ruleSet,
                   showStates=None,
                   showItems=None,
                   statusMsg=1):
    """given the method and the ruleset, generate a parsing table.
    the other options are debug shit
    """
    if statusMsg:
        print 'scanning grammar for terminals and nonterminals'
    terminals, nonTerminals = siftTokens(ruleSet)
    digestedRules = digestRules(ruleSet)

    if statusMsg:
        print 'producing items for grammar'

    if method == 'LALR1':  #LALR via canonical LR
        C = LR1.items(ruleSet, terminals, nonTerminals)
        C = LALR.canonicalLRToLALR(C)

    elif method == 'LALR0':  #LALR via LR1 -roundaboutly
        C = LALR.items(ruleSet, terminals, nonTerminals)
        #C = LALR.computeLALRKernels(C, ruleSet, terminals, nonTerminals)

    elif method == 'LR1':  #canonical LR
        C = LR1.items(ruleSet, terminals, nonTerminals)

    elif method == 'LR0':  #SLR (LR0)
        C = LR0.items(ruleSet, terminals, nonTerminals)

    if showItems != None:
        printItems('%s Items' % method, C)
        if showItems == 2:
            return {}

    if statusMsg:
        print 'generating state and goto tables'

    if method == 'LALR1':  #LALR via canonical LR
        stateTable, gotoTable = LR1.generateStateTable(C, ruleSet, terminals,
                                                       C.index)

    elif method == 'LALR0':  #LALR via CLR - roundaboutly
        stateTable, gotoTable = LR1.generateStateTable(C, ruleSet, terminals,
                                                       C.index)

    elif method == 'LR1':  #canonical LR
        stateTable, gotoTable = LR1.generateStateTable(
            C, ruleSet, terminals, lambda x, y=C: LR1.fullIndex(y, x))

    elif method == 'LR0':  #SLR (LR0)
        stateTable, gotoTable = LR0.generateStateTable(C, ruleSet, terminals,
                                                       nonTerminals)

    if showStates != None:
        printStateTable(C, terminals, nonTerminals, stateTable, gotoTable)

    return {
        'states': stateTable,
        'gotos': gotoTable,
        'terminals': terminals,
        'rules': digestedRules
    }