Esempio n. 1
0
 def callback(res):
     lexical = Lexical()
     lexical.get_dfa('./help/dfa.json')  # 读取DFA转换表
     lexical_res = lexical.lexical_run(str(res).replace(
         '\r\n', '\n'))  # 得到词法分析的token序列
     tokens, nums_attr = [], []
     if not lexical_res[0] and not lexical_res[1]:
         QMessageBox.warning(self, '输入无效', '请输入有效程序文本')
         return
     for idx in range(len(
             lexical_res[0])):  # item[1]为种别码,item[3]为行号,item[2]为属性值
         item = lexical_res[0][idx]
         if 'comment' not in item[1]:
             tokens.append(item[1])
             nums_attr.append((item[3], item[2]))
     if not self.syntax:
         self.syntax = Syntax()
         self.syntax.syntax_init('./help/semantic.json')
     semantic = Semantic()
     res_lst, res_err = semantic.semantic_run(tokens, nums_attr,
                                              self.syntax)
     self.semantic_win = QDialog()
     ui = semantic_res.Ui_Dialog()
     init_win(self.semantic_win, ui)
     set_semantic_win(ui, semantic)
Esempio n. 2
0
    def Run(self, event=0):

        self.objMain = Main()

        self.consoleT.delete(1.0, END)

        #open file and write all the IDE code text area content
        self.codeFile = open(Main._codeFile, 'w')
        self.codeFile.write(self.codeT.get('1.0', END))
        self.codeFile.close()

        #open file for reading
        self.codeFile = open(Main._codeFile, 'rb+')
        self.objMain.mainMethod(self.codeFile)
        self.codeFile.close()

        #write tokens to console area
        self.consoleT.insert(1.0, Main._fileData)

        #write tokens to test file
        self.outputFile = open(Main._outputFile, 'w')
        self.outputFile.write(Main._fileData)
        self.outputFile.close()

        #call syntax
        # self.objMain.PROG()

        self.objSyntax = Syntax(Main._tokens)
        self.objSemantic = Semantic(Main._tokens)

        if self.objSyntax.PROG():
            print("Code is parsed")
        else:
            print("there is error in code")
Esempio n. 3
0
 def semantic_info(self):  # 语义相关信息
     if not self.syntax:
         self.syntax = Syntax()
         self.syntax.syntax_init('./help/semantic.json')
     self.semantic_info_win = QDialog()
     ui = semantic_info.Ui_Dialog()
     init_win(self.semantic_info_win, ui)
     _set_grammar_tbl(ui.grammar_tbl, self.syntax)
Esempio n. 4
0
 def __init__(self, input_phrase: str):
     self.input_phrase = input_phrase
     self.root = Node()
     self.load_config()
     self.syntax = Syntax(self.config)
     self.parser = Parser(input_phrase, self.config)
     self.evaluator = Evaluator(self.eval_config, self.config)
     self.output = ""
Esempio n. 5
0
    def grammar(self):  # 展示语法分析中可以计算的集合和表
        syntax = Syntax()
        syntax.syntax_init('./help/syntax.json')
        syntax.get_follow()

        self.grammar_window = QDialog()
        ui = syntax_grammar.Ui_dialog()
        init_win(self.grammar_window, ui)
        set_grammar_tbl(ui, syntax)
Esempio n. 6
0
    def parse(self, expression):

        self.expression = expression
        syntactic = Syntax(self.expression)

        # try:
        self.ast = syntactic.parse()

        # except ValueError:
        # print("erro!")

        # try:
        return self.evaluate(self.ast)
Esempio n. 7
0
def multipleTestCases():
    print("=" * 6, "multipleTestCases", "=" * 6)

    s = Syntax("testCases/target_test1/lex/out.lex", "none")
    s.go()
    result = evaluator(s.root.getChild(0).getChild(1))
    assertThat((5 - 3) * (8 / 2), result)

    s = Syntax("testCases/target_test2/lex/out.lex", "none")
    s.go()
    result = evaluator(s.root.getChild(0).getChild(1))
    assertThat(5 + 1 * 3 - 4 / 5 * 6 - 1 * 2 - 3 * 4, result)

    s = Syntax("testCases/target_test4/lex/out.lex", "none")
    s.go()
    result = evaluator(s.root.getChild(0).getChild(1))
    assertThat(24 + 4 - 1 * 3 / 2 + 34 - 1, result)

    s = Syntax("testCases/target_test3/lex/out.lex", "none")
    s.go()
    result = evaluator(s.root.getChild(0).getChild(1))
    assertThat(19 - 8 - 7 - 6 - 5 - 4 - 3 - 2 - 1 * (2 + 3 + 4 + 3 + 2),
               result)
Esempio n. 8
0
def syntax_matching(message):
    pattern = "^(?P<span>次の?|毎(週|月)|来週の?|来月の?|来年の?)?(?P<days>([0-9]{4}年[0-9]{1,2}月[0-9]{1,2}日|[0-9]{1,2}月[0-9]{1,2}日|[0-9]{1,2}日|末|(第[1-5])?[日月火水木金土]曜日?)+)の?(?P<time>(午前|午後)?[0-9]{1,2}時([0-9]{1,2}分)?|(AM|PM|am|pm)?[0-9]{1,2}:[0-9]{1,2})に?(?P<lab>研究室にいる|研究室の)?(?P<member>B4|M1|M2|awareness|AWARENESS|novel\s?interface|NOVEL\s?INTERFACE|cmc|CMC|全員|人)に(?P<item>\S+)をリマインド$"
    #pattern = "^(?P<span>次の?|毎(週|月)|来週の?|来月の?|来年の?)?(?P<days>([0-9]{4}年[0-9]{1,2}月[0-9]{1,2}日|[0-9]{1,2}月[0-9]{1,2}日|[0-9]{1,2}日|末|(第[1-5])?[日月火水木金土]曜日?)+)の?(?P<time>(午前|午後)?[0-9]{1,2}時([0-9]{1,2}分)?|(AM|PM|am|pm)?[0-9]{1,2}:[0-9]{1,2})に?(?P<lab>研究室にいる|研究室の)?(?P<member>B4|M1|M2|awareness|AWARENESS|novel\s?interface|NOVEL\s?INTERFACE|cmc|CMC|全員|人)に?(?P<item>\S+)をリマインド$"
    match = re.search(pattern, message)

    if match:
        print("match")
        print(match.group("span"))
        s = Syntax(match.group("span"), match.group("days"),
                   match.group("time"), match.group("lab"),
                   match.group("member"), match.group("item"))
        s.show_all()
        return s
    else:
        print("unmatch")
        return False
Esempio n. 9
0
def main():
    print('Welcome to IPOL interpreter!')

    # returns lines of string containing the cleaned code
    file_reader = FileReader()
    # tabs removed, double spaces removed
    lines = file_reader.read_file()

    tokenizer = Tokenizer()
    # returns a 2d list containing the tokens per line of code
    tokens_list = tokenizer.tokenize(lines)
    tokens_list_copy = tokens_list.copy()

    # create instance of the parser with the syntax declared in Syntax class
    parser = Parser(syntax=Syntax().get_syntax())

    # iterate each line of the list containing the tokens
    for line in tokens_list:
        recursive_parse(parser, line, callback)


    # create a new instance of the parser now with the syntax for recuding operations to expressions
    parser = Parser(syntax=Syntax().get_final_syntax())

    # Parse to an expression to see if it is valid
    for line in parsed_list:
        recursive_parse(parser, line, callback1)  
        


    exception_checker = ExceptionCheker()

    for i in range(len(final_parsed_list)):
        # there must be a syntax error because it cannot be converted to a single statement
        # check which kind of exception it is
        if len(final_parsed_list[i]) > 1:
            exception = exception_checker.check_exception(
                final_parsed_list[i], i)

            if isinstance(exception, IpolException):
                exceptions.append(exception)

    # now check if the overall structure of the code is valid
    # check if there are unused values
    # for index, token in enumerate(reduce(final_parsed_list)):
    #     if token.type == Type.NUMBER or token.type == Type.STR:
    #         exceptions.append(IpolException(
    #             ExceptionType.UNUSED_VALUE_ERROR, None, index))

    # print exceptions if there are any and halt the build process
    if len(exceptions) > 0:
        for exception in exceptions:
            exception.print()
        return
    else:
        # create a new instance of the parser now with the syntax of the overall ipol code
        parser = Parser(syntax=Syntax().get_ipol_syntax())

        # finally, verify that the full code is valid
        reduced_final_parsed_list = reduce(final_parsed_list)

        # recursive_parse(parser, reduced_final_parsed_list, callback2)
        reduced_final_parsed_list[:] = (token for token in reduced_final_parsed_list \
        if token.type != Type.EMPTY_LINE)

        recursive_parse(parser, reduced_final_parsed_list, callback2)

        for line in ipol_code_verified:
            for token in line:
                print(token.type)

        # check syntax in class Syntax
        # Type.E means accepted
        build_failed_message = 'Build Failed.'
        try:
            if ipol_code_verified[0][0].type == Type.E:
                print('Build Successful\n')
            else:
                print(build_failed_message)
                return
        except:
            print(build_failed_message)
            return

        # there are no exceptions
        # continue with code generation
        tokens_list_copy.pop(0)
        tokens_list_copy.pop(len(tokens_list_copy) - 1)

        generated_code = CodeGenerator().generate(tokens_list_copy)

        # this may return a bool data type
        if isinstance(generated_code, list):
            runnable_code = '\n'.join(generated_code)
            runnable_code = runnable_code.replace('&n0', '')
            # run the generated python code
            with open('ic.py', '+w') as ic:
                ic.write(runnable_code)

            print('\nBuild Complete.\nView logs on ipol_logs.txt\nView generated code on ic.py\n')
            exec(runnable_code, globals())

            with open('ipol_logs.txt', '+w') as logs:
                text_to_write = 'PARSING LOGS\n\nGENERATED TOKENS\n'
                for line in tokens_list:
                    for token in line:
                        text_to_write = text_to_write + '{} -> {}'.format(token.type, token.val) + ", "
                    text_to_write = text_to_write + '\n'

                text_to_write = text_to_write + '\PARSED AS...\n'
                for line in parsed_list:
                    for token in line:
                        text_to_write = text_to_write + str(token.type) + ', '
                    text_to_write = text_to_write + '\n'

                text_to_write = text_to_write + '\nGENERATED INTERMEDIATE CODE\n' + runnable_code
                logs.write(text_to_write)
        # if bool is returned, that means there was something wrong with the ipol code
        else:
            print('Build failed')
Esempio n. 10
0
 def __init__(self, code):
     self.tabelaSimbolos = {}
     self.escopo = "global"
     self.tree = Syntax(code).ast
     self.programa(self.tree)
     self.verificacoes(self.tabelaSimbolos)
Esempio n. 11
0
 def __init__(self, code=""):
     self.syntax = Syntax()
     self.code = re.sub('\s+', '', code)
     self.position = 0
Esempio n. 12
0
    args = parser.parse_args()

    if args.source:
        abs_file_path = os.path.join(script_dir, args.source)

        with open(abs_file_path) as src:
            source_code = src.read()

    elif args.cmd:
        source_code = args.cmd

    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')

        lexer = Lexer(source_code)
        syntax = Syntax(lexer)

        parse_tree = syntax.parse()

        print(1, parse_tree.declarator)
        print(2, parse_tree.declarator.identifier)
        print(3, parse_tree.next)
        print(parse_tree.next.next)
        print(parse_tree.next.next.next)
        # for node in parse_tree:
        #     print(node)

        Node.proccess_traversal_semantics(parse_tree)
        Node.symtable.show()

        for error in w:
Esempio n. 13
0
args = parser.parse_args()

file_path = TEST_PATH if args.test else args.f

if not os.path.isfile(file_path):
    raise Exception('File does not exists')


def get_source_text():
    with open(file_path, 'r') as source_file:
        return source_file.read()


program = get_source_text()
print(
    f'source program:\n--------------------\n{program}\n--------------------\n'
)

print('Starting Lexical Analysis')
lexer = Lexer()
token_list = lexer.tokenize(program)
print('Token list:')
pprint(token_list)

print('Starting Syntax Analyzer')
syntax = Syntax(token_list)
result = syntax.run()

if result:
    print('Syntax analyzer run successfully')
Esempio n. 14
0
def sintactic(file, outputType):
    syntax = Syntax(file, outputType)
    global syntaxTree
    syntaxTree = syntax.go(file)
Esempio n. 15
0
from utils import load, gui_l
from lexer import Lexer
from sys import argv
from syntax import Syntax
import os
import webbrowser

Syntax(Lexer(load('code.for')).get_tokens(''),
       debug="all").export_to_file('syntax.dot')

os.system("dot syntax.dot -Tpng -o syntax.png")

# Gambiarra Fedorenta.
#os.system('%SystemRoot%\\System32\\rundll32.exe "%ProgramFiles%\\Windows Photo Viewer\\PhotoViewer.dll", ImageView_Fullscreen D:\\Google Drive\\Computação\\Atual\\_Working\\Compiler\\syntax.png')
webbrowser.get(
    "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s"
).open_new(
    "file:///D:/Google%20Drive/Computa%C3%A7%C3%A3o/Atual/_Working/Compiler/syntax.png"
)
'''
if __name__ == '__main__':

  if len(argv) > 1:
    if '-gui' in argv:
      argv.remove('-gui')
      gui_l(Lexer(load(argv[1])).get_tokens())
    else:
      Lexer(load(argv[1]),verbose=True).get_tokens()
  else:
    Lexer(load('code.for'),verbose=True).get_tokens()
'''
Esempio n. 16
0
from lexer import Lexer
from syntax import Syntax
import os, webbrowser, time, sys

now = time.time()

#Syntax(Lexer('code.for').run()).export_to_file('syntax.dot')
if 'debug' in sys.argv: Syntax(Lexer('code.for').run(), debug=True).start()
else: Syntax(Lexer('code.for').run(), debug=False).start()

done = time.time()

#print(done - now) # imrpime tempo levado para análise

# Graphviz - Converter o arquivo .dot gerado em uma imagem PNG
#os.system("dot syntax.dot -Tpng -o syntax.png")

# Abrir o arquivo no Chrome
#webbrowser.get("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s").open_new("file:///D:/Google%20Drive/Computa%C3%A7%C3%A3o/Atual/_Working/Compiler/syntax.png")