Exemplo n.º 1
0
def parseFile(fileName):
    with open(fileName, 'r') as source_file:
        line_number = 1
        for line in source_file:
            lexeme_number = 1
            if scanner.isComment(line):
                comments.append(line)
                continue
            words = parseWords(line)
            try:
                for word in words:
                    t = (scanner.getToken(word))
                    if t is not None:
                        symbol_table.append(t)
                        lexeme_number += 1
                    else:
                        not_tokens.append(word)
                line_number += 1
            except ValueError as e:
                print ("error in line: " + str(line_number))
                print ("line content: " + line)
                print e
Exemplo n.º 2
0
def tokenize(files, options):
    for fileName in files:
        with open(fileName, 'r') as source_file:
            line_number = 1
            for line in source_file:
                lexeme_number = 1
                if scanner.isComment(line):
                    comments.append(line)
                    continue
                words = parseWords(line)
                try:
                    for word in words:
                        t = (scanner.getToken(word))
                        if t is not None:
                            if t.Word not in symbol_table:
                                symbol_table[t.Word] = t
                            lexeme_number += 1
                        else:
                            not_tokens.append(word)
                            raise TokenizerException(word, line, line_number)
                    line_number += 1
                except ValueError as e:
                    print ("Tokenizer error in line: " + str(line_number))
                    print ("line content: " + line)
                    print e

                except TokenizerException as e:
                    print e
                    if optionTokenizeAll not in options:
                        raise e

    if(optionPrintTokens in options):
        printTokens()
        printNotTokens()


    return symbol_table
Exemplo n.º 3
0
        emp.append(father)
    return empty


def parser():
    global tokens, curToken, curpos, syntaxTree, syntaxTreeNode,nodenum
    init()
    curpos = -1
    nodenum = 0
    getNextToken()
    syntaxTreeNode[nodenum] = 'Program'
    doParser(0)

def printTree(nodeid,deep):
    if nodeid in emp: return
    print '  '*deep,nodeid,syntaxTreeNode[nodeid]
    if nodeid not in syntaxTree: return
    for child in syntaxTree[nodeid]:
        printTree(child,deep+1)


tokens = scanner.getToken(r'test/t2.txt')
print 'Parser start!'
parser()

if haveError == False:
    print 'Print syntax tree!'
    printTree(0,0)
else:
    for err in errorList: print err
Exemplo n.º 4
0

def parser():
    global tokens, curToken, curpos, syntaxTree, syntaxTreeNode, nodenum
    init()
    curpos = -1
    nodenum = 0
    getNextToken()
    syntaxTreeNode[nodenum] = 'Program'
    doParser(0)


def printTree(nodeid, deep):
    if nodeid in emp: return
    print('  ' * deep, nodeid, syntaxTreeNode[nodeid])
    if nodeid not in syntaxTree: return
    for child in syntaxTree[nodeid]:
        printTree(child, deep + 1)


tokens = scanner.getToken(r'test/t3.txt')
print('Parser start!')
parser()

if haveError == False:
    print('Print syntax tree!')
    printTree(0, 0)
else:
    for err in errorList:
        print(err)
Exemplo n.º 5
0
def printTreeTraversalHelper(currentNode):
    if currentNode is None:
        return None

    leftType = printTreeTraversalHelper(currentNode.leftChild)
    rightType = printTreeTraversalHelper(currentNode.rightChild)

    if scanner.isConstant(currentNode.data):
        if scanner.isReal(currentNode.data):
            print currentNode.data + "e "
        elif scanner.isString(currentNode.data):
            print "s\" " + currentNode.data[1:]
        else:
            print currentNode.data
        return (scanner.getToken(currentNode.data)).Type

    elif scanner.isOperator(currentNode.data):
        numberOperands = scanner.getArgCount(currentNode.data)
        if numberOperands == 1 and rightType is None:
            # if operand is Real add a f in front of the operator
            if leftType == "Real" and rightType is None:
                print "f" + currentNode.data

            #if there is an integer using a real operation then convert the integer to real
            if leftType == "Integer" and scanner.isRealOperator(currentNode.data):
                print currentNode.data
                print "s>f"
            #is boolean type child and operator is boolean
            if leftType == "Boolean" and scanner.isBooleanOperator(currentNode.data):
                print itblToGforth(currentNode.data)
            #is integer type child and is integer operator
            if leftType == "Integer" and not scanner.isBooleanOperator(currentNode.data):
                print currentNode.data
            else:
                raise ParseTreeException(currentNode)
            return leftType

        elif numberOperands == 2:
            #children are same value no conversion needed
            if leftType == rightType:
                # children are real and real operator
                if leftType == "Real" and scanner.isRealOperator(currentNode.data):
                    print "f" + itblToGforth(currentNode.data) + " "
                # children are string and a +
                elif leftType == "String":
                    if currentNode.data == "+":
                        print "s" + itblToGforth(currentNode.data) + " "
                    else:
                        raise ParseTreeException(currentNode)
                # children are boolean and boolean operator
                elif leftType == "Boolean" and scanner.isBooleanOperator(currentNode.data):
                    print itblToGforth(currentNode.data)
                # children are integers and integer operator
                elif leftType == "Integer" and not scanner.isBooleanOperator(currentNode.data):
                    if currentNode.data == "^":
                        print "s>f"
                        print "s>f"
                        print "f" + itblToGforth(currentNode.data)
                        leftType = "Real"
                    else:
                        print itblToGforth(currentNode.data)
                # bad parse
                else:
                    raise ParseTreeException(currentNode)
                return leftType
            # type conversion needed to float
            elif (((leftType == "Integer" and rightType == "Real") or (leftType == "Real" and rightType == "Integer")) and not scanner.isBooleanOperator(currentNode.data)):
                print "s>f"
                if leftType == "Integer":
                    print "fswap"
                print "f" + itblToGforth(currentNode.data)
                return "Real"
            else:
                raise ParseTreeException(currentNode)
    else:
        return None