예제 #1
0
 def process(self):
     LR1.get_allCharAndVT()
     LR1.get_first()
     LR1.items()
     LR1.get_table()
     print('get table done')
     self.output_table()
예제 #2
0
def funLR1():
    iMaxIterationNum = 60
    fBeta = 2.0
    fBetaMin = 1e-8
    fToleranceEpsilon = 0.0001
    listLRParameters = [
        iMaxIterationNum, fBeta, fBetaMin, fAlpha, fToleranceEpsilon
    ]
    listfUBEveIns = []
    listfLBEveIns = []
    f = open(insName, 'rb')
    textFile = open(
        'E:\\VSCodeSpace\\PythonWorkspace\\GAforFLP\\100-node_LR1(m=2).txt',
        'a')
    for i in range(iInsNum):
        ins = pickle.load(f)
        LagRela = LR1.LagrangianRelaxation(listLRParameters, ins)
        LagRela.funInitMultiplierLambda()
        upperBound, lowerBound = LagRela.funLR_main()
        listfUBEveIns.append(upperBound)
        listfLBEveIns.append(lowerBound)
    textFile.write("\nUpperbound for every instance:\n")
    textFile.write(str(listfUBEveIns))
    textFile.write("\n\nLowerbound for every instance:\n")
    textFile.write(str(listfLBEveIns))
예제 #3
0
def items(ruleSet, terminals, nonTerminals):
    """compute LALR1 items for ruleset"""
    symbols = nonTerminals + terminals
    #start with closure of [ [S' -> S, $] ]
    C = [LR1.closure([startItem], ruleSet, terminals)]
    added = 1
    while added:
        added = 0
        for I in C:
            for X in symbols:
                g = LR1.goto(I, X, ruleSet, terminals)

                if g and not g in C: #if no core already there:
                    added = 1
                    C.append(g)
                elif g and g in C: #if core is there:
                    target = C[C.index(g)]
                    if compareSet(target, g):
                        added = 1
                        updateLookaheads(target, g)
    return C
예제 #4
0
def items(ruleSet, terminals, nonTerminals):
    """compute LALR1 items for ruleset"""
    symbols = nonTerminals + terminals
    #start with closure of [ [S' -> S, $] ]
    C = [LR1.closure([startItem], ruleSet, terminals)]
    added = 1
    while added:
        added = 0
        for I in C:
            for X in symbols:
                g = LR1.goto(I, X, ruleSet, terminals)

                if g and not g in C:  #if no core already there:
                    added = 1
                    C.append(g)
                elif g and g in C:  #if core is there:
                    target = C[C.index(g)]
                    if compareSet(target, g):
                        added = 1
                        updateLookaheads(target, g)
    return C
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
            }
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
    }