Esempio n. 1
0
 def Evaluate(self, table):
     #recupera no do ST, get symbol
     #evaluate de todos filhos
     #da o setter nos argumentos
     #evaluate no ultimo filho
     #retorna via o nome
     dec = table.get_value2(self.value)[0]  #no func ou sub
     tipo = table.get_value2(self.value)[1]  #tipo do no, func ou sub
     new_table = SymbolTable(table)
     init = 0
     if tipo == "FUNCAO":
         new_table.table[self.value] = [
             None, dec.children[0].Evaluate(table)
         ]
         init = 1
     j = 0
     for i in range(init, len(dec.children) - 1):
         dec.children[i].Evaluate(new_table)
         argument = self.children[j].Evaluate(table)
         argument_value = argument[0]
         argument_type = argument[1]
         parameter_type = dec.children[i].children[1].Evaluate(
             table)  #new_table.get_value(argument_type)
         if argument_type != parameter_type:
             raise Exception("tipos errados")
         j += 1
         new_table.set_value(dec.children[i].children[0].value,
                             argument_value)
     if (j) != len(self.children):
         raise Exception("quantidade de argumentos distintos")
     dec.children[-1].Evaluate(new_table)
     if tipo == "FUNCAO":
         return new_table.get_value(self.value)
def p_oscope(p):
	'''oscope : LEFTCURLYBRACKET'''
	main_table.inScope=main_table.prev_inScope+1
	main_table.prev_inScope+=1
	main_table.outScope+=1
	tab = SymbolTable(main_table.outScope)
	main_table.add_table(tab)
Esempio n. 3
0
 def __init__(self, input_stream, output_file):
     self.tokenizer = input_stream
     self.outfile = output_file
     self.class_name = None
     self.out_stream = []
     self.buffer = []
     self.if_count = 0
     self.while_count = 0
     self.generator = VMWriter(self.out_stream)
     self.symbol_table = SymbolTable()
     self.op_table = {
         '+': 'ADD',
         '-': 'SUB',
         '&': 'AND',
         '|': 'OR',
         '<': 'LT',
         '>': 'GT',
         '=': 'EQ'
     }
     self.convert_kind = {
         'ARG': 'ARG',
         'STATIC': 'STATIC',
         'VAR': 'LOCAL',
         'FIELD': 'THIS'
     }
Esempio n. 4
0
def assemble(fileName):
    import assemblerParser
    from assemblerParser import Parser
    import code
    import symbolTable
    from symbolTable import SymbolTable

    WORD_SIZE = 16  #words are 16 bits long
    currentDataAddr = 16
    file = open(fileName, 'r')
    parser = Parser(file)

    table = SymbolTable()
    #Symbol generating pass
    counter = 0
    while (parser.hasMoreCommands()):
        parser.advance()
        if (parser.commandType() == Parser.L_COMMAND):
            table.addEntry(parser.symbol(), counter)
        else:
            counter += 1

    newFileName = fileName[:fileName.find(".", 1)] + ".hack"
    outputFile = open(newFileName, 'w')
    #Code generating pass
    file = open(fileName, 'r')
    parser = Parser(file)
    while parser.hasMoreCommands():
        parser.advance()
        output = "BLANK"
        if (parser.commandType() == Parser.C_COMMAND):
            output = "111" + code.comp(parser.comp()) + code.dest(
                parser.dest()) + code.jump(parser.jump())
            outputFile.write(output + "\n")
        elif parser.commandType() == Parser.A_COMMAND:
            symbol = parser.symbol()
            try:
                symbol = int(symbol)
            except:
                pass

            if type(symbol) is int:
                binVal = bin(
                    int(symbol)
                )[2:]  #the slice is because the value will be in the form 0b# so we need to remove the 0b
            elif table.contains(symbol):
                binVal = bin(table.getAddress(symbol))[2:]
            else:
                table.addEntry(symbol, currentDataAddr)
                binVal = bin(currentDataAddr)[2:]
                currentDataAddr += 1
            output = "0" * (WORD_SIZE - len(binVal)) + binVal
            outputFile.write(output + "\n")
        elif parser.commandType() == Parser.L_COMMAND:
            pass
        else:
            print("Bad Munkey!")
        print("Original is " + parser.current() + " BIN: " + output +
              "COMP: " + parser.comp())
Esempio n. 5
0
 def __init__(self):
     self.tempCount = 0
     self.tempCountBool = 0
     self.codeGenerator = CodeGenerator()
     self.symbolTable = SymbolTable()
     self.array_length = 0
     self.var_stack = []
     self.latest_bool = None
Esempio n. 6
0
def main():
    test = read_file("input3.txt")
    try:
        parser = Parser(test)
        symbolTable = SymbolTable(None)
        result = parser.parseProgram()
        # percorrer_arvore(result)
        result.Evaluate(symbolTable)
        print(AssemblyCode.assembly_code)
        AssemblyCode.writeFile("teste.asm")
    except ValueError as err:
        print(err)
Esempio n. 7
0
def main():
        test = read_file("input3.txt")
        try:
            parser = Parser(test) 

            result = parser.parseProgram()

            symbolTable = SymbolTable(None)

            result.Evaluate(symbolTable)

        except ValueError as err:
            print(err)
Esempio n. 8
0
 def __init__(self, inp_file, out_file):
     self.tokenizer = Tokenizer(inp_file)
     self.sym_tbl = SymbolTable()
     self.vm_writer = VMWriter(out_file)
     self.out_file = open(out_file, "a")
     self.current_token = ""
     self.current_token_type = ""
     self.curr_token_ptr = -1
     self.label_counter = {
         "if": 0,
         "while": 0
     }
     self.advance()
     self.compileClass()
Esempio n. 9
0
def main(*args):
    scanner = Scanner()
    symbolTable = SymbolTable()

    while True:
        # Get the next token from scanner
        token = scanner.nextToken()

        # Pretty print the token
        token.__repr__()

        if token.TokenCode == 'tc_ID':
            # Check if token exists in SymbolTable
            entry = symbolTable.lookup(token.DataValue[0].lower())

            if entry == -1:  # -1 means not found in table
                # Entry does not exist -> add it!
                num = symbolTable.insert(token.DataValue[0].lower())

                # Associate the token with the entry
                token.setSymTabEntry(num)
            else:
                # Token exists:
                # Associate the token with the entry
                token.setSymTabEntry(entry)

        elif token.TokenCode == 'tc_NUMBER':
            # Same as for entry ..
            entry = symbolTable.lookup(token.DataValue[0].lower())
            if entry == -1:
                num = symbolTable.insert(token.DataValue[0].lower())
                token.setSymTabEntry(num)
            else:
                token.setSymTabEntry(entry)

        elif token.TokenCode == 'tc_EOF':
            # Reached end of input -> quit loop!
            break

    # Pretty print our table
    symbolTable.__repr__()
Esempio n. 10
0
        output = '\n'.join(compiler.output_line_list) + '\n'
        self.path_w = f'{self.p_file.parent}/{self.p_file.stem}.vm'
        with open(self.path_w, mode='w') as f:
            f.write(output)


if __name__ == '__main__':
    path: str = sys.argv[1]
    p_path: pathlib.Path = pathlib.Path(path)
    p_file_list: 'list[pathlib.Path]' = []
    if p_path.is_dir():
        p_file_list = list(p_path.glob('**/*.jack'))
    else:
        p_file_list = [p_path]

    symbol_table = SymbolTable()

    # for p_file in p_file_list:
    #     print(p_file)
    #     compiler = CompilationEngine(symbol_table, p_file)
    #     compiler.compile(is_first_run=True)

    # print(compiler.symbol_table.subroutine_dic)
    # print(compiler.symbol_table.class_scope)

    for p_file in p_file_list:
        print(p_file)
        compiler = CompilationEngine(symbol_table, p_file)
        compiler.compile()
        compiler.saveToFile()
Esempio n. 11
0
from PIF import ProgramInternalForm
from symbolTable import SymbolTable
from languageSpecs import *
import re

st = SymbolTable()
pif = ProgramInternalForm()
filename = 'input2'
pif_filename = 'PIF.out'
st_filename = 'ST.out'


def tokenize():
    result = []
    with open(filename) as file:
        re_separator = '('
        for separator in separators:
            re_separator += re.escape(separator) + '|'
        re_separator = re_separator[:-1] + ')'
        for line in file:
            line = line.strip()
            new_line = re.split(re_separator, line)
            result.append(new_line)
    return result


def algorithm():
    lines_array = tokenize()
    for line in lines_array:
        for token in line:
            if token != '':
Esempio n. 12
0
    fopen = open(filename, 'r')

    print("Code: ")
    for line in fopen:
        print(line)

    print("\nTOKENIZED: ")
    with open(filename, 'r') as fopen:
        for line in fopen:
            print([token for token in scanner.tokenize(line, separators)])

    print("\nCODES: ")
    for i in range(len(everything)):
        print(everything[i] + " " + str(codification[everything[i]]))

    symbolTable = SymbolTable()
    pif = ProgramInternalForm()

    with open(filename, 'r') as fopen:
        flag = 0
        count = 0
        for line in fopen:
            count += 1
            for token in scanner.tokenize(line[0:-1], separators):
                if token in everything:
                    if token != ' ':
                        pif.add(codification[token], -1)
                elif scanner.isIdentifier(token):
                    #print("ID: "+token)
                    symbolTable.add([token, codification['id']])
Esempio n. 13
0
from parser import Parser
from node import Node
from symbolTable import SymbolTable
import sys

if len(sys.argv) == 1:
    raise Exception("didn't pass the filename")
file = sys.argv[1]

f = open(file, "r")
code = f.read()
tree = Parser.run(code)
table = SymbolTable(None)
tree.Evaluate(table)
global ST
from symbolTable import SymbolTable
ST = SymbolTable()
Esempio n. 15
0
    def _pushdown(self):
        '''
        自顶向下预测分析程序语法,同时进行语义分析及中间代码生成
        '''
        tok = self.lexer.token()
        recentAttr = {}
        globalAttr = {'offset': 0, 'top': SymbolTable()}
        '''构建开始文法的结点并将其压入栈中'''
        self.root = MySyntaxer.syntaxNode(self.begin)
        curRecord = MySyntaxer.stackRecord('SYMBOL')
        curRecord.node = self.root
        endRecord = MySyntaxer.stackRecord('SYMBOL')
        endRecord.node = MySyntaxer.syntaxNode('EOF')
        symStack = [endRecord, MySyntaxer.stackRecord('SYN'), curRecord]
        curRecord = symStack[-1]
        syntaxError = False #若已发生语法错误,则停止语义分析

        while curRecord.type != 'SYMBOL' or curRecord.node.sym != 'EOF':
            if self.debug:
                MySyntaxer._debugPrint(symStack, curRecord)
            if curRecord.type == 'SYMBOL':
                '''若为文法结点'''
                X = curRecord.node
                if X.sym == tok.type:
                    '''匹配栈顶终结符号'''
                    recentAttr = {'value': tok.value, 'type': tok.type} #保存终结符号的综合属性
                    X.val = tok.value
                    tok = self.lexer.token()
                    symStack.pop()
                elif X.sym == 'BLANK':
                    '''匹配栈顶空串'''
                    symStack.pop()
                elif X.sym in self.tokens:
                    # 读入的token未能匹配当前栈顶的终结符:
                    # 弹出栈顶终结符,试图继续语法分析
                    self._error(tok, symStack)
                    print(f'pop {X.sym}, continue analyze.')
                    syntaxError = True
                    symStack.pop()
                else:
                    num = self.M[X.sym].get(tok.type)
                    if num is None:
                        # 找不到可行的产生式以展开当前栈顶的非终结符
                        # 采取错误恢复措施:
                        # 1) 若tok.type在X.sym的Follow集内,则弹出X.sym
                        # 2) 否则根据恐慌模式,忽略输入符号a
                        self._error(tok, symStack)
                        syntaxError = True
                        if tok.type in self.Follow[X.sym]:
                            print(f'pop {X.sym}, continue analyze.')
                            symStack.pop()
                        else:
                            print(f'ignore {tok.type}, continue analyze.')
                            tok = self.lexer.token()
                    else:
                        symStack.pop()
                        seq = self.APSet[X.sym][num]
                        _tmp = list(range(len(seq)))
                        actionRecord = None #用以寻找第一个动作片段
                        for i in range(len(seq)):
                            if not seq[i].startswith('act'):
                                _tmp[i] = MySyntaxer.syntaxNode(seq[i])
                        for i in range(len(seq)):
                            '''建立语法分析树'''
                            if not seq[i].startswith('act'):
                                X.children.append(_tmp[i])
                            '''将产生式倒序入栈'''    
                            _X = seq[len(seq)-i-1]
                            if _X.startswith('act'):
                                newRecord = MySyntaxer.stackRecord('ACTION')
                                newRecord.act = _X
                                actionRecord = newRecord
                                symStack.append(newRecord)
                            else:
                                if not _X in self.tokens:
                                    '''仅为非终结符号创建综合记录'''
                                    newRecord = MySyntaxer.stackRecord('SYN')
                                    symStack.append(newRecord)
                                newRecord = MySyntaxer.stackRecord('SYMBOL')
                                newRecord.node = _tmp[len(seq)-i-1]
                                symStack.append(newRecord)
                        if actionRecord:
                            actionRecord.inh = curRecord.inh.copy()
            elif curRecord.type == 'SYN':
                recentAttr = curRecord.syn.copy()
                symStack.pop()
            else:
                actID = int(curRecord.act[3:])
                top = len(symStack)-1
                if not syntaxError:
                    try:
                        SDTUtil.execAction(actID, recentAttr, globalAttr, symStack, top)
                    except Exception as e:
                        print(f"Error at Line {tok.lineno}:\n\t{e}")
                        raise e
                        break
                symStack.pop()

            curRecord = symStack[-1]
        print()
        print("-"*50)
        #print("symbol table:")
        print(globalAttr['top'])
Esempio n. 16
0
t_DEREF_ONE =r'\.'
t_DEREF_TWO =r'–>'
t_QUES_MARK = r'\?' 
t_COMMA = r','  
t_CHAR_CONST = r"\'.\'"
t_DOLAR='@'

def t_COMMENT(t):
    r"(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(//.*)"
    pass
    # No return value. Token discarded

# A regular expression rule with some action code

main_table = MainSymbolTable()
main_table.add_table(SymbolTable(main_table.outScope))

def t_FLOATNUM(t):
    r'[0-9]+\.[0-9]+'
    t.value = float(t.value)
    return t


def t_INTNUM(t):
    r'[0-9]+'
    t.value = int(t.value)
    return t


# Define a rule so we can track line numbers
def t_newline(t):
def main():
    # g = Grammar.readFromFile('grammar.txt')
    g = Grammar.readFromFile('my_mini_grammar.txt')
    parser = Parser(g)
    st = SymbolTable()
    pif = ProgramInternalForm()
    filename = 'input.txt'

    # def detectToken()
    # define token -> keyword, operator, separator (addToPif(code_of_token, -1)
    #             -> identifier (pos = pos(token, symTable), addToPif(token, st))
    #             -> constant (pos + addToPif(code_of_constant, pos))

    def isSeparator(char):
        return char in myLanguageSpecs.separators

    def isOperator(char):
        return char in myLanguageSpecs.operators

    def isReserved(word):
        return word in myLanguageSpecs.reservedWords

    def isIdentifier(word):
        return re.match(r'^[a-zA-Z]([a-zA-Z]|[0-9]){0,8}$', word) is not None

    def isConstant(token):
        return re.match(r'((\'[a-z]\'|\'[0-9]\')|(\+|-){0,1}[0-9]*\d$)', token) is not None

    def getCodeOfToken(token):
        try:
            return myLanguageSpecs.codification[token]
        except:
            raise Exception("The token is not in codification table")

    def tokenize():
        result = []
        with open(filename) as file:
            re_separator = r'('
            for separator in myLanguageSpecs.separators:
                re_separator += re.escape(separator) + '|'
            re_separator = re_separator[:-1] + ')'
            for line in file:
                line = line.strip()
                new_line = re.split(re.compile(re_separator), line)
                result.append(new_line)
        return result

    def algo():
        lines_array = tokenize()
        for line in lines_array:
            for token in line:
                if token is not '':
                    if token is ' ':
                        pass
                    elif isReserved(token) or isOperator(token) or isSeparator(token):
                        pif.add(getCodeOfToken(token), -1)
                    elif isIdentifier(token):
                        pos = st.addIdentifier(token)
                        pif.add(getCodeOfToken('identifier'), pos)
                    elif isConstant(token):
                        pos = st.addConstant(token)
                        pif.add(getCodeOfToken('constant'), pos)
                    else:
                        raise Exception("Lexical error at line", ''.join(line))

    algo()
    revereCodification = {}
    for key in myLanguageSpecs.codification:
        revereCodification[myLanguageSpecs.codification[key]] = key
    inputStack = []
    for (code, id) in pif.pif:
        inputStack += [str(code)]
    print("Productions: ", g.P)
    print(pif)

    # print(parser.parse(inputStack))
    # print(parser.parse('abbc'))
    # print(parser.derivationStrings('abbc'))
    print(parser.derivationStrings(inputStack))