Beispiel #1
0
 def __init__(self, fileName):
     # filename refers to the toy language source code
     self.fileName = fileName
     # file to store the program internal form
     self.outputFileName = path.splitext(
         path.basename(fileName))[0] + "_pif.txt"
     # clear file if already exist
     open(self.outputFileName, 'w').close()
     # file to store the identifiers table
     self.outputIdentifiersTable = path.splitext(
         path.basename(fileName))[0] + "_id_table.txt"
     # file to store the constants table
     self.outputConstantsTable = path.splitext(
         path.basename(fileName))[0] + "_const_table.txt"
     # hashtable for all the program symbols (if, for, while, else, int, float, etc)
     self.symbolsTable = {}
     # hashtable for storing the identifiers, as a pair identifier -> integer id
     self.identifiersTable = {}
     # hashtable for storing the identifiers, as a pair constant -> integer id
     self.constantsTable = {}
     # load all the toy language symbols
     self.populateSymbolsTable()
     # Finite automatas
     with open('symbols.json') as f:
         self.symbols_fa = FiniteAutomata(json.load(f))
     with open('identifier.json') as f:
         self.identifers_fa = FiniteAutomata(json.load(f))
     with open('constant.json') as f:
         self.constants_fa = FiniteAutomata(json.load(f))
Beispiel #2
0
def main():
    if len(sys.argv) < 2:
        print("usage: {0} <file>".format(sys.argv[0]))
        return

    filename = sys.argv[1]
    with open(filename) as f:
        file_lines = list(f)
        states = file_lines[0].split()
        alphabet = list(file_lines[1].strip())
        initial_state = file_lines[2].strip()
        accepted_states = file_lines[3].split()
        file_lines = file_lines[4:]
        transitions = []
        for line in file_lines:
            transitions.append(line.strip().split())
    fa = FiniteAutomata(states, alphabet, initial_state, accepted_states)
    for transition in transitions:
        for char in transition[1]:
            fa.set_transition(transition[0], char, transition[2])

    do_option = {}
    do_option["1"] = show_states
    do_option["2"] = show_alphabet
    do_option["3"] = show_transitions
    do_option["4"] = show_initial_state
    do_option["5"] = show_accepted_states
    do_option["6"] = check_sequence
    do_option["7"] = determine_longest_prefix
    while True:
        print_menu()
        option = input("Type option: ").strip()
        if option is "0":
            break
        else:
            do_option[option](fa)
Beispiel #3
0
    def __init__(self, fileName):
        self.fileName = fileName
        self.outputFileName = path.splitext(fileName)[0] + "_pif.txt"
        #clear text from file if arleady exists
        open(self.outputFileName, 'w').close()
        self.outputIdentifiersTable = path.splitext(
            fileName)[0] + "_id_table.txt"
        self.outputConstantsTable = path.splitext(
            fileName)[0] + "_const_table.txt"
        #hastables for symbols, identifiers and constants
        self.symbolsTable = {}
        self.identifiersTable = {}
        self.constantsTable = {}

        self.populateSymbolsTable()
        # Finite automatas
        with open('symbols.json') as f:
            self.symbols_fa = FiniteAutomata(json.load(f))
        with open('identifier.json') as f:
            self.identifers_fa = FiniteAutomata(json.load(f))
        with open('constant.json') as f:
            self.constants_fa = FiniteAutomata(json.load(f))
Beispiel #4
0
    def __init__(self, fileName):
        self.fileName = fileName
        self.outputFileName = path.splitext(
            path.basename(fileName))[0] + "_pif.txt"
        open(self.outputFileName, 'w').close()
        self.outputIdentifiersTable = path.splitext(
            path.basename(fileName))[0] + "_id_table.txt"
        self.outputConstantsTable = path.splitext(
            path.basename(fileName))[0] + "_const_table.txt"
        self.symbolsTable = {}
        self.identifiersTable = {}
        self.constantsTable = {}
        self.populateSymbolsTable()

        with open('symbols.json') as f:
            self.symbols_fa = FiniteAutomata(json.load(f))

        with open('identifier.json') as f:
            self.identifers_fa = FiniteAutomata(json.load(f))

        with open('constant.json') as f:
            self.constants_fa = FiniteAutomata(json.load(f))
Beispiel #5
0
 def is_identifier(self, tok):
     # return re.match(r'^[a-zA-Z]([a-zA-Z]|[0-9]|_){,7}$', tok) is not None
     fa = FiniteAutomata.read_from_file("FAIDS.in")
     return fa.check_sequence(tok)
Beispiel #6
0
 def is_numerical_constant(self, tok):
     # return re.match(r'^(-?\d+\.\d+)$|^(-?\d+)$', tok) is not None
     fa = FiniteAutomata.read_from_file("FAINTS.in")
     return fa.check_sequence(tok)
Beispiel #7
0
class Scanner:
    def __init__(self, fileName):
        self.fileName = fileName
        self.outputFileName = path.splitext(fileName)[0] + "_pif.txt"
        #clear text from file if arleady exists
        open(self.outputFileName, 'w').close()
        self.outputIdentifiersTable = path.splitext(
            fileName)[0] + "_id_table.txt"
        self.outputConstantsTable = path.splitext(
            fileName)[0] + "_const_table.txt"
        #hastables for symbols, identifiers and constants
        self.symbolsTable = {}
        self.identifiersTable = {}
        self.constantsTable = {}

        self.populateSymbolsTable()
        # Finite automatas
        with open('symbols.json') as f:
            self.symbols_fa = FiniteAutomata(json.load(f))
        with open('identifier.json') as f:
            self.identifers_fa = FiniteAutomata(json.load(f))
        with open('constant.json') as f:
            self.constants_fa = FiniteAutomata(json.load(f))

    def populateSymbolsTable(self):
        try:
            f = open("symbols.dat")
            for line in f.readlines():
                (symbol, sid) = line.split()
                self.symbolsTable[symbol] = sid
        except IOError:
            print("> Error: Symbols file not found!")
            sys.exit()

    def randomNotIn(self, values):
        r = randint(1, 100000)
        while r in values:
            r = randint(1, 100000)
        return r

    def appendToOutput(self, buff):
        with open(self.outputFileName, "a") as f:
            f.write(buff)

    def writeTables(self):
        with open(self.outputIdentifiersTable, "w") as f:
            for (key, val) in self.identifiersTable.items():
                f.write("%s %s\n" % (key, val))
        with open(self.outputConstantsTable, "w") as f:
            for (key, val) in self.constantsTable.items():
                f.write("%s %s\n" % (key, val))

    def addSymbol(self, _symbol):
        if _symbol in self.symbolsTable:
            self.appendToOutput(str(self.symbolsTable[_symbol]) + " 0\n")
            return True
        else:
            return False

    def addIdentifier(self, _id):
        if _id not in self.identifiersTable:
            self.identifiersTable[_id] = self.randomNotIn(
                self.identifiersTable.values())
        self.appendToOutput(self.symbolsTable["identifier"] + " " +
                            str(self.identifiersTable[_id]) + "\n")
        return True

    def addConstant(self, _val):
        if _val not in self.constantsTable:
            self.constantsTable[_val] = self.randomNotIn(
                self.constantsTable.values())
        self.appendToOutput(self.symbolsTable["constant"] + " " +
                            str(self.constantsTable[_val]) + "\n")
        return True

    def tokenize(self):
        try:
            f = open(self.fileName, "r")
            for (i, line) in enumerate(f.readlines()):
                program = line
                while program != "":
                    program = program.strip()
                    # print(program)
                    _sym = self.symbols_fa.longest_accepted_prefix(program)
                    _id = self.identifers_fa.longest_accepted_prefix(program)
                    _const = self.constants_fa.longest_accepted_prefix(program)
                    # symbols have higher priority over identifiers
                    if _sym == _id:
                        _id = None
                    if _id is not None and _id != "":
                        self.addIdentifier(_id)
                        program = program[len(_id):]
                    elif _sym is not None and _sym != "":
                        self.addSymbol(_sym)
                        program = program[len(_sym):]
                    elif _const is not None and _const != "":
                        self.addConstant(_const)
                        program = program[len(_const):]
                    else:
                        print(
                            "> Error: Syntax error. Unexpected token at line %d:%s"
                            % (i + 1, program))
                        sys.exit(1)

            self.writeTables()
        except IOError:
            print("> Error: Source file not found!")
            sys.exit(1)
Beispiel #8
0
class Scanner:
    # Consturctor for the Scanner, takes as a parameter the fileName consisting of the
    # source code that we want our lexer to tokenize
    def __init__(self, fileName):
        # filename refers to the toy language source code
        self.fileName = fileName
        # file to store the program internal form
        self.outputFileName = path.splitext(
            path.basename(fileName))[0] + "_pif.txt"
        # clear file if already exist
        open(self.outputFileName, 'w').close()
        # file to store the identifiers table
        self.outputIdentifiersTable = path.splitext(
            path.basename(fileName))[0] + "_id_table.txt"
        # file to store the constants table
        self.outputConstantsTable = path.splitext(
            path.basename(fileName))[0] + "_const_table.txt"
        # hashtable for all the program symbols (if, for, while, else, int, float, etc)
        self.symbolsTable = {}
        # hashtable for storing the identifiers, as a pair identifier -> integer id
        self.identifiersTable = {}
        # hashtable for storing the identifiers, as a pair constant -> integer id
        self.constantsTable = {}
        # load all the toy language symbols
        self.populateSymbolsTable()
        # Finite automatas
        with open('symbols.json') as f:
            self.symbols_fa = FiniteAutomata(json.load(f))
        with open('identifier.json') as f:
            self.identifers_fa = FiniteAutomata(json.load(f))
        with open('constant.json') as f:
            self.constants_fa = FiniteAutomata(json.load(f))

    # method loads symbol table in memory from the disc
    def populateSymbolsTable(self):
        try:
            # open the file
            f = open("symbols.dat")
            # iterate through its lines
            for line in f.readlines():
                # get the symbol and the symbol id
                (symbol, sid) = line.split()
                # add to the symbols table
                self.symbolsTable[symbol] = sid
        except IOError:
            # In case there is no such file, fail fast!
            print("> Error: Symbols file not found!")
            sys.exit()

    # method returns a random integer that is not in the values array
    def randomNotIn(self, values):
        # returns a random vaue between 1 and 100000
        r = randint(1, 100000)
        # while that value already exists in the array values
        while r in values:
            # generate a new one
            r = randint(1, 100000)
        # return the number generated
        return r

    # method append buff to the file outputFileName
    def appendToOutput(self, buff):
        # open file
        with open(self.outputFileName, "a") as f:
            # write the string buff as a new line
            f.write(buff)

    # method write the identifier and constant tables
    def writeTables(self):
        # open file for identifiers table
        with open(self.outputIdentifiersTable, "w") as f:
            # iterate through the identifiers table
            for (key, val) in self.identifiersTable.iteritems():
                # write the pair on a new line
                f.write("%s %s\n" % (key, val))
        # open file for constant table
        with open(self.outputConstantsTable, "w") as f:
            # iterate through the constants table
            for (key, val) in self.constantsTable.iteritems():
                # write the pair on a new line
                f.write("%s %s\n" % (key, val))

    # method prints the symbol to the program internal form file output
    def addSymbol(self, _symbol):
        # if the symbol is in the symbol table
        if _symbol in self.symbolsTable:
            # print it
            self.appendToOutput(str(self.symbolsTable[_symbol]) + " 0\n")
            return True
        else:
            # return false because _symbol is not a valid symbol, and then throw an error
            return False

    # method prints identifier and it's id to the output file
    def addIdentifier(self, _id):
        # assign a new, unused integer id for the current identifier
        if _id not in self.identifiersTable:
            self.identifiersTable[_id] = self.randomNotIn(
                self.identifiersTable.values())
        # print to program internal form output file
        self.appendToOutput(self.symbolsTable["identifier"] + " " +
                            str(self.identifiersTable[_id]) + "\n")
        return True

    # method adds a constant to the table and prints it to the output file
    def addConstant(self, _val):
        # assign a new, unsued integer id for the current identifier
        if _val not in self.constantsTable:
            self.constantsTable[_val] = self.randomNotIn(
                self.constantsTable.values())
        # print to the program internl form output file
        self.appendToOutput(self.symbolsTable["constant"] + " " +
                            str(self.constantsTable[_val]) + "\n")
        return True

    # method tokenize the source file
    def tokenize(self):
        try:
            # read the file
            f = open(self.fileName, "r")
            # iterate on all the read lines
            for (i, line) in enumerate(f.readlines()):
                program = line
                while program != "":
                    program = program.strip()
                    # get the longest accepted prefix among all of the 3 finite automatas
                    _sym = self.symbols_fa.longest_accepted_prefix(program)
                    _id = self.identifers_fa.longest_accepted_prefix(program)
                    _const = self.constants_fa.longest_accepted_prefix(program)
                    # some of the symbols are also accepted identifiers (eg: while, int, etc)
                    # in case they are eqla, symbols have higher priority
                    if _sym == _id:
                        _id = None
                    if _id is not None and _id != "":
                        if len(_id) > 250:
                            print(
                                "> Error: Identifier has too many characters, line %d."
                                % (i + 1))
                            sys.exit(1)
                        # add the token to the interal hashmaps
                        self.addIdentifier(_id)
                        program = program[len(_id):]
                    elif _sym is not None and _sym != "":
                        self.addSymbol(_sym)
                        program = program[len(_sym):]
                    elif _const is not None and _const != "":
                        self.addConstant(_const)
                        program = program[len(_const):]
                    else:
                        print(
                            "> Error: Syntax error. Unexpected token at line %d:%s"
                            % (i + 1, program))
                        sys.exit(1)
            self.writeTables()
        except IOError:
            print("> Error: Source file not found!")
            sys.exit(1)
Beispiel #9
0
            input_file_content = input_file.read()
            input_file.close()
        except IOError as io_error:
            io_error.strerror = 'cannot open file for reading'
            print_stderr(sys.argv[0], io_error.strerror,
                    sep=': ', end=' ')
            io_error.filename = '\'{}\''.format(io_error.filename)
            print_stderr(io_error.filename)
            sys.exit(exit_codes.ERR_INPUT)
    else:
        input_file_content = sys.stdin.read()

    if 'case-insensitive' in options:
        input_file_content = input_file_content.lower()
    try:
        fa = FiniteAutomata(input_file_content)
        well_specified_fa = WellSpecifiedFA(fa)
    except FAException as exc:
        print_stderr(exc)
        sys.exit(exc.code)

    if 'output' in options:
        try:
            output_file = open(options['output'], 
                    encoding='utf-8', mode='w')
        except IOError as io_error:
            io_error.strerror = 'cannot open file for writing'
            print_stderr(sys.argv[0], io_error.strerror,
                    sep=': ', end=' ')
            io_error.filename = '\'{}\''.format(io_error.filename)
            print_stderr(io_error.filename)
Beispiel #10
0
from grammer import Grammar
from finite_automata import FiniteAutomata

# if __name__ == "__main__":
#     grammar = Grammar.fromFile('files/rg.txt')
#     print(grammar)
#
#     if grammar.isRegular():
#         finiteAutomata = FiniteAutomata.fromRegularGrammar(grammar)
#         print(finiteAutomata)

if __name__ == "__main__":
    finiteAutomata = FiniteAutomata.fromFile('files/fa.txt')
    print(finiteAutomata)

    grammar = Grammar.fromFiniteAutomata(finiteAutomata)
    print(grammar)

# if __name__ == "__main__":
#     grammar = Grammar.fromConsole()
#     print(grammar)

# if __name__ == "__main__":
#     fa = FiniteAutomata.fromConsole()
#     print(fa)
Beispiel #11
0
def execute():
    args = args_parse()

    print("Spuštění automatu")
    finite_automata = FiniteAutomata()
    finite_automata.start()