示例#1
0
def main():
    print('Assignment #4-3, Trenton Green, [email protected]')
    count = 0
    data = []
    stack = sm.StackMachine()
    
    file = open(sys.argv[1], 'r')

    # data is a list of tokenized values for each line
    data = [tp.Tokenize(line) for line in file]
    
    # check for each token in data against the Parser
    for tok in data:
        tp.Parse(tok)

    count = len(data)

    # while the currentLine property of the stack is less than count
    # execute input from data list
    # if at any time there is any error it will print the error and its location
    # will print if program ran with no errors
    try:
        while(stack.currentLine < count):
            if(stack.currentLine < 0):
                print("Trying to execute invalid: " + str(stack.currentLine))
            else:
                stack.Executions(data[stack.currentLine])          
        print('Program terminated correctly')

    except IndexError:
        errorL = stack.currentLine
        errorT = data[stack.currentLine -1]
        print("Line " + str(errorL) + ": " + "'" + str(errorT) + "'" + " caused Invalid Memory Access")  
示例#2
0
def main():
    print("Assignment #4-3, Brandon Baniqued, [email protected]")
    with open(sys.argv[1], mode='r') as f:  # Open file designated in command line argument
        lines = [x.strip() for x in f.readlines()]  # Separate lines into list
        lines = [x for x in lines if len(x) > 0]  # Remove empty lines
        readTokens = []
        for x in lines:  # Tokenize each line
            readTokens.append(prog4_1.Tokenize(x))
        for x in readTokens:
            prog4_1.Parse(x)  # Parse each line

        StackM = prog4_2.StackMachine()
        lineNo = 0

        try:  # Attempt to execute operations
            while lineNo < len(readTokens):  # that are read from lines
                current = readTokens[lineNo]
                out = StackM.Execute(current)
                if out is not None:
                    print(out)
                lineNo = StackM.currentLine
                if (lineNo < 0):
                    print("Trying to execute invalid line: " + str(lineNo))
                    break
            print("Program terminated correctly")

        except IndexError as e:
            print("Line " + str(StackM.currentLine) + ": '" + current + "' caused " + e + ".")
示例#3
0
def main():
    print("Assignment #4-3, Brian Rafferty, [email protected]")
    executableContents = []
    stack = p2.StackMachine()

    file = open(sys.argv[1], 'r')
    fileContents = [x.strip() for x in file.readlines()]
    numLines = len(fileContents)

    for index in range(0, numLines):
        checkLine = fileContents[index]
        try:
            executableContents.append(p1.Tokenize(checkLine))
        except ValueError as error:
            print("Error on line " + str(index + 1) + ": " + str(error))
            return

    for index in range(0, numLines):
        checkLine = fileContents[index]
        if (not p1.Parse(executableContents[index])):
            print("Parse error on line " + str(index + 1) + ": " +
                  str(fileContents[index]))
            return

    inputLength = len(executableContents)
    while (stack.CurrentLine < inputLength):
        if (stack.CurrentLine < 0):
            print("Trying to execute invalid line: " + stack.CurrentLine)
            return
        try:
            callStack = stack.Execute(executableContents[stack.CurrentLine])
            if (callStack != None):
                print(callStack)
        except IndexError:
            print("Line " + str(stack.CurrentLine) + ": " +
                  fileContents[stack.CurrentLine - 1] +
                  " caused Invalid Memory Access")
            return

    print("Program terminated correctly")
示例#4
0
def main():

    _procLines = []

    s = StackMachine()

    with open(sys.argv[1], mode='r') as f:
        lines = [x.strip() for x in f.readlines()]

        for i in range(0, len(lines)):
            line = lines[i]
            try:
                _procLines.append(prog4_1.Tokenize(line))
            except ValueError as e:
                print("Error on line " + str(i + 1) + ": " + str(e))
                return

        for i in range(0, len(lines)):
            line = lines[i]
            if (not prog4_1.Parse(_procLines[i])):
                print("Parse error on line " + str(i + 1) + ": " +
                      str(lines[i]))
                return

    while (s.CurrentLine < len(_procLines)):
        if (s.CurrentLine < 0):
            print("Trying to execute invalid line: " + s.CurrentLine)
            return
        try:
            v = s.Execute(_procLines[s.CurrentLine])
            if (v != None):
                print(v)
        except IndexError:
            print("Line " + str(s.CurrentLine) + ": " +
                  lines[s.CurrentLine - 1] + "  caused Invalid Memory Access")
            return

    print("Program terminated correctly")
import prog4_1 
import prog4_2
import sys

print('Assignment #4-3, Anthony Reese, [email protected]')
fileName = sys.argv[1]
lines = []
tokenizedLines = []
parsedLines= []


lines = [line.rstrip('\n') for line in open(fileName)]

for x in lines :
    tokenizedLines.append(prog4_1.Tokenize(x))

for x in tokenizedLines:                
    parsedLines.append(prog4_1.Parse(x))

while prog4_2.currentLine <= len(tokenizedLines):
        prog4_2.StackMachine.Execute(tokenizedLines[prog4_2.currentLine-1])
        
print('Program terminated correctly')