コード例 #1
0
ファイル: parser.py プロジェクト: Vells/Clite-Interpreter
    def program(self):
        """
        Program -> int  main '(' ')' '{' Declarations Statements '}'
        :return: An ast.Program object
        :raise CliteSyntaxError if an unexpected token is seen
        """
        # Indicates the level of indentation
        level = 1

        self.match_main()
        self.decls = self.declarations()
        # Create a program object with declarations and a level
        program = ast.Program(self.decls, level)
        # Process statements and add them to the program object
        self.stmts = self.statements(level)
        program.add_statements(self.stmts)

        # Match final closing brace
        if self.curr_tok[self.CODE] != tokens.SINGLE_TOKENS[tokens.RBRACE][self.CODE]:
            raise errors.CliteSyntaxError("Missing final closing brace '}'!",
                                          self.curr_tok[self.LINE])
        # Consume closing brace
        self.curr_tok = self.lex.__next__()

        return program
コード例 #2
0
ファイル: parser.py プロジェクト: tolziplohu/rhea
 def parse(self, source):
     self.source = source
     tree = ast.Program([])
     while True:
         tree.append(self.parse_expr())
         self.skip_indent()
     return tree
コード例 #3
0
ファイル: e_parser.py プロジェクト: greeny277/cb1
def p_program(p):
    '''program : program vardecl ';'
               | program funcdecl
               | vardecl ';'
               | funcdecl '''
    if len(p) == 2:
        p[0] = ast.Program().addloc(p.lineno(1))
        p[0].addfunc(p[1])
    elif len(p) == 3 and not isinstance(p[1], ast.Program):
        p[0] = ast.Program().addloc(p.lineno(1))
        p[0].addvar(p[1])
    elif len(p) == 3 and isinstance(p[1], ast.Program):
        p[0] = p[1]
        p[0].addfunc(p[2])
    elif len(p) == 4:
        p[0] = p[1]
        p[0].addvar(p[2])
コード例 #4
0
    def program(self):
        """
         Program   ⇒  int  main ( ) { Declarations Statements }
        :return:
        """

        # Check if the current token is a keyword Int
        if self.currtok[0] == lexer.Lexer.KWDINT:
            self.currtok = next(self.tokens)

            # Check if the current token is "main"
            if self.currtok[0] == lexer.Lexer.MAIN:
                self.currtok = next(self.tokens)

                # Check if the current token is an open parenthesis
                if self.currtok[0] == lexer.Lexer.OPENP:
                    self.currtok = next(self.tokens)

                    # Check if the current token is a closed parenthesis
                    if self.currtok[0] == lexer.Lexer.CLOSEDP:
                        self.currtok = next(self.tokens)

                        # Check if the current token is an open bracket
                        if self.currtok[0] == lexer.Lexer.OPENB:
                            self.currtok = next(self.tokens)

                            # Run the declation and statements methods on
                            # the entire program
                            decls = self.declarations()
                            stmts = self.statements()

                            # Check if the current token is a closed bracket
                            if self.currtok[0] == lexer.Lexer.CLOSEDB:
                                return ast.Program(decls, stmts)

                            # Closed bracket not found, throw syntax error
                            else:
                                raise CliteSyntaxError("Expected curly bracket on line " + self.currtok[2])

                        # Open bracket not found, throw syntax error
                        else:
                            raise CliteSyntaxError("Expected curly bracket on line " + self.currtok[2])

                    # Closed parenthesis not found, throw syntax error
                    else:
                        raise CliteSyntaxError("Expected Parenthesis on line " + self.currtok[2])

                # Open parenthesis not found, throw syntax error
                else:
                    raise CliteSyntaxError("Expected Parenthesis on line " + self.currtok[2])

            # "main" not found, throw syntax error
            else:
                raise CliteSyntaxError("Expected main on line " + self.currtok[2])

        # keyword int not found, throw syntax error
        else:
            raise CliteSyntaxError("Expected int on line " + self.currtok[2])
コード例 #5
0
 def ParseProgram(self):
     program = ast.Program(None)
     program.Statements = list()
     while self.curToken.Type != tokens.EOF:
         stmt = self.parseStatement()
         if stmt:
             program.Statements.append(stmt)
         self.nextToken()
     
     return program
コード例 #6
0
    def parse_program(self):
        prog = ast.Program([])

        while not self.cur_is(token.EOF):
            stmt = self.parse_stmt()

            if stmt != None:
                prog.statements.append(stmt)

            self.next()

        return prog
コード例 #7
0
ファイル: astTests.py プロジェクト: aguilarearle/monkey
    def TestString(self):
        program = ast.Program()
        program.Statements = [
            ast.LetStatement(
                tkn.Token(tkn.LET, "let"),
                ast.Identifier(tkn.Token(tkn.IDENT, "myVar"), "myVar"),
                ast.Identifier(tkn.Token(tkn.IDENT, "anotherVar"),
                               "anotherVar"))
        ]

        if program.String() != "let myVar = anotherVar":
            raise Exception(f'program.String() wrong got={program.String()}')
コード例 #8
0
    def ParseProgram(self):
        program = ast.Program()
        program.Statements = []

        while self.currToken.TokenType != tkn.EOF:
            statement = self.ParseStatement()
            if statement is not None:
                program.Statements.append(statement)

            self.nextToken()

        return program
コード例 #9
0
    def __init__(self, filename):
        # Create a lexer object.
        lex = lexer.Lexer()

        # Create a token generator from the Lexer object.
        self.tokens = lex.token_generator(filename)

        # Initialize the current token.
        # This is the first token of the program.
        self.currToken = next(self.tokens)

        # Create a Program object into which we add declarations and statements.
        self.parseTree = ast.Program()
コード例 #10
0
ファイル: test_ast.py プロジェクト: omar-3/Monkey-Compiler
def TestString():
    program = ast.Program(Statements=[
        ast.LetStatement(
            Token=_token.Token(Type=_token.tokens.LET, Literal="let"),
            Name=ast.Identifier(Token=_token.Token(Type=_token.tokens.IDENT,
                                                   Literal="myVar"),
                                Value="myVar"),
            Value=ast.Identifier(Token=_token.Token(Type=_token.tokens.IDENT,
                                                    Literal="anotherVar"),
                                 Value="anotherVar"))
    ])

    if program.String() == "let myVar = anotherVar;":
        print("TestString test is success")
コード例 #11
0
ファイル: parser.py プロジェクト: ischaojie/nan
 def program(self):
     """program : PROGRAM variable SEMI block DOT
     """
     self.eat(TokenType.PROGRAM)
     # get the program name
     var_node = self.variable()
     program_name = var_node.value
     # ;
     self.eat(TokenType.SEMI)
     # block
     block_node = self.block()
     program_node = ast.Program(program_name, block_node)
     # .
     self.eat(TokenType.DOT)
     return program_node
コード例 #12
0
 def checkprogram_uncaught(self,
                           main_unit: ast.TranslationUnit) -> ast.Program:
     # DEBUG
     #print("Global symbols:", main_unit.symbols_global)
     #print(main_unit.symbol_tree)
     #print(main_unit)
     # Theoretically, everything is accessible via this chain:
     # self.program.main_unit.symbols_global
     # Anyways, for convenience, we make everything accessible here.
     self.main_unit = main_unit
     self.symbols_global = main_unit.symbols_global
     program = ast.Program(self.modules, main_unit)
     # Resolve module imports first
     self.parse_imports(main_unit)
     # Then resolve types
     self.resolve_types(main_unit)
     for unit in self.modules.values():
         self.symbols_global = unit.symbols_global
         self.resolve_types(unit)
     # Resolve function interfaces
     self.symbols_global = main_unit.symbols_global
     self.resolve_function_interfaces(main_unit)
     for unit in self.modules.values():
         self.symbols_global = unit.symbols_global
         self.resolve_function_interfaces(unit)
     # Typecheck the rest (also assigning variable types)
     # Functions in modules
     for unit in self.modules.values():
         self.symbols_global = unit.symbols_global
         for func in unit.function_definitions.values():
             res, turn = self.check(func)
     # Functions in main_module / main_unit
     self.symbols_global = main_unit.symbols_global
     for func in main_unit.function_definitions.values():
         res, turn = self.check(func)
     # Statements in main_module / main_unit
     for stmt in main_unit.statements:
         res, turn = self.check(stmt)
         # If there is a possible return value,
         if turn != Return.NO:
             # ensure it is an int
             expect = bongtypes.TypeList([bongtypes.Integer()])
             if not res.sametype(expect):
                 raise TypecheckException(
                     "Return type of program does not evaluate to int.",
                     stmt)
     return program
コード例 #13
0
    def parse_program(self):
        self.indentator.indent('Parsing Program')
        declarations = self.parse_declarations()

        self.expect('MAIN')
        stats = self.parse_statements()
        self.indentator.dedent()

        AST_Root = ast.Program(declarations, stats)

        if (self.errors == 1):
            print('WARNING: 1 error found!')
        elif (self.errors > 1):
            print('WARNING: ' + str(self.errors) + ' errors found!')
        else:
            print('parser: syntax analysis successful!')

        return AST_Root
コード例 #14
0
ファイル: parse.py プロジェクト: gtr909/pymonkey
	def ParseProgram(self):
		program = ast.Program()
		program.Statements = []
		
		#self.curToken.printout()
		
		print("ParseProgram()1: ", self.curToken.Type)
		
		while self.curToken.Type != token.EOF:
			stmt = self.parseStatement()
			print("ParseProgram()2: ", stmt)
			if stmt != None:
				program.Statements.append(stmt)
			self.nextToken()
		
		print("ParseProgram()3: ", program)
		
		return program
コード例 #15
0
ファイル: parser.py プロジェクト: tjpell/C-Lite
    def program(self):

        # match int main ( ) {
        if self.curr_tok[0] != lexer.Lexer.INT:
            print("'int' expected: found", self.curr_tok[1], "on line",
                  self.curr_tok[2])
            sys.exit(1)
        self.curr_tok = next(self.lex)

        if self.curr_tok[0] != lexer.Lexer.MAIN:
            print("'main' expected: found", self.curr_tok[1], "on line",
                  self.curr_tok[2])
            sys.exit(1)
        self.curr_tok = next(self.lex)

        if self.curr_tok[0] != lexer.Lexer.LPAREN:
            print("'(' expected: found", self.curr_tok[1], "on line",
                  self.curr_tok[2])
            sys.exit(1)
        self.curr_tok = next(self.lex)

        if self.curr_tok[0] != lexer.Lexer.RPAREN:
            print("')' expected: found", self.curr_tok[1], "on line",
                  self.curr_tok[2])
            sys.exit(1)
        self.curr_tok = next(self.lex)

        if self.curr_tok[0] != lexer.Lexer.LCBRACK:
            print("'{' expected: found", self.curr_tok[1], "on line",
                  self.curr_tok[2])
            sys.exit(1)

        self.curr_tok = next(self.lex)

        self.decls = self.declarations()
        self.stmts = self.statements()

        if self.curr_tok[0] != lexer.Lexer.RCBRACK:
            print("'}' expected: found", self.curr_tok[1], "on line",
                  self.curr_tok[2])
            sys.exit(1)
        self.curr_tok = next(self.lex)

        return ast.Program(self.decls, self.stmts)
コード例 #16
0
    def parse_program(self):
        statements = []
        while not isinstance(self.current, tokens.EOF):
            if isinstance(self.current, tokens.SEMICOLON):
                self.step()
                continue
            if isinstance(self.current, tokens.LET):
                stmt = self.parse_let()
            elif isinstance(self.current, tokens.FUNC):
                stmt = self.parse_function_literal()
            elif isinstance(self.current, tokens.RETURN):
                stmt = self.parse_return()
            elif isinstance(self.current, tokens.IF):
                stmt = self.parse_conditional()
            elif isinstance(self.current, tokens.LBRACE):
                stmt = self.parse_block()
            else:
                stmt = self.parse_expr()

            statements.append(stmt)

        return ast.Program(statements)
コード例 #17
0
ファイル: parser.py プロジェクト: ronnieholm/Monkey-Python
    def parse_program(self) -> ast.Program:
        stmts = []
        while not self._current_token_is(TokenType.EOF):
            stmt = self._parse_statement()

            # Many _parse_x() return null which bubbles up the call stack to
            # here where null enables continued parsing on error. The
            # alternative would be for _parse_x() to immediately stop parsing
            # and signal a parser error. But that wouldn't allow collecting
            # multiple errors to improve the user experience. Instead parsing
            # would halt on the first error. As an example, _expect_peek()
            # returns null if the expected token isn't found _and_ adds an error
            # message to the _errors list. That null bubbles up to
            # _parse_statement() which skips adding the statement to the AST and
            # moves on the next statement. Evaluation of the AST shouldn't be
            # attempted in case of parser errors as the faulting parts of the
            # program is missing from the AST, likely resulting in evaluation
            # errors.
            if stmt is not None:
                stmts.append(stmt)
            self._next_token()
        return ast.Program(stmts)
コード例 #18
0
    def program(self):
        '''
        Program -> int  main ( ) { Declarations Statements }

        :return: Program object
        '''

        # match int main ( ) open brace
        if self.curr_tok[0] != values.INT:
            raise CliteSyntaxError("Keyword 'int' expected")
        self.curr_tok = self.lex_gen.__next__()

        if self.curr_tok[0] != values.MAIN:
            raise CliteSyntaxError("Keyword 'main' expected")
        self.curr_tok = self.lex_gen.__next__()

        if self.curr_tok[0] != values.PARENL:
            raise CliteSyntaxError(" ( expected")
        self.curr_tok = self.lex_gen.__next__()

        if self.curr_tok[0] != values.PARENR:
            raise CliteSyntaxError(" ) expected")
        self.curr_tok = self.lex_gen.__next__()

        if self.curr_tok[0] != values.LBRACE:
            raise CliteSyntaxError(" { expected")
        self.curr_tok = self.lex_gen.__next__()

        self.decls = self.declarations()
        self.stmts = self.statements()

        prog = ast.Program(self.decls, self.stmts, self.declList)

        if self.curr_tok[0] != values.RBRACE:
            raise CliteSyntaxError(" } expected")
        self.curr_tok = self.lex_gen.__next__()

        return prog
コード例 #19
0
ファイル: parser.py プロジェクト: StefKal/CS364CLite
    def program(self):
        if self.currtok[0] == "KWDINT":
            self.currtok = next(self.tokens)
            if self.currtok[0] == "KWDMAIN":
                self.currtok = next(self.tokens)
                if self.currtok[0] == "PCTLPAR":
                    self.currtok = next(self.tokens)
                    if self.currtok[0] == "PCTRPAR":
                        self.currtok = next(self.tokens)
                        if self.currtok[0] == "PCTLBRA":

                            self.currtok = next(self.tokens)

                            # follow grammatically correct syntax (declarations and statements)
                            d = self.decls()
                            s = self.statements()

                            # run the declarations and statements in the abstract syntax tree for program
                            if self.currtok[0] == "PCTRBRA":
                                return ast.Program(d, s)
                            else:
                                raise CLiteSyntaxError("Right brace expected",
                                                       self.currtok[1])
                        else:
                            raise CLiteSyntaxError("Left brace expected",
                                                   self.currtok[1])
                    else:
                        raise CLiteSyntaxError("Right parenthesis expected",
                                               self.currtok[1])
                else:
                    raise CLiteSyntaxError("Left parenthesis expected",
                                           self.currtok[1])
            else:
                raise CLiteSyntaxError("keyword main expected",
                                       self.currtok[1])
        else:
            raise CLiteSyntaxError("keyword int expected", self.currtok[1])
コード例 #20
0
ファイル: parser.py プロジェクト: libra-eric/mini_triangle
    def parse_program(self):
        """ Program ::=  Command EOT """

        command = self.parse_command()
        self.token_accept(scanner.TK_EOT)
        return ast.Program(command)
コード例 #21
0
indent_level = 0
is_undent = False
rets = {
    'System print': 'void',
    'Float -': 'Float',
    'Float *': 'Float',
    'Float +': 'Float',
    'Float /': 'Float',
    'Int -': 'Int',
    'Int *': 'Int',
    'Int +': 'Int',
    'Int /': 'Int',
    '$z 0': 'void'
}
index = {'$z': ast.Call('System', 'print', [])}
tree = ast.Program(
    [ast.Definition('$main', ast.Function(ast.Args([]), [], ret='int'), True)])
anon_actor_num = 0
buff = ''  # For names and values


def parse(source):
    while True:
        try:
            q = parse_top_level(source)
            #raise_error(source)
            tree.append(q)
        except ValueError:
            break
    for i in tree:
        if isinstance(i, ast.Actor):
            tree[-1].value.append(ast.Call('$z', str(anon_actor_num), []))
コード例 #22
0
ファイル: parser.py プロジェクト: javidhsueh/Flask-GAE
 def p_program(self, p):
     """program : source_elements"""
     p[0] = ast.Program(p[1])
コード例 #23
0
 def p_program(self, p):
     """
     program : cells
     """
     p[0] = ast.Program(p[1])
コード例 #24
0
def p_program(p):
    '''program : declarations'''
    p[0] = ast.Program(p[1]).at(loc(p))
コード例 #25
0
def program(p):
    head, decpart, bodypart, tail = p
    assert head.getstr() == tail.getstr()
    return ast.Program(head.getstr(), decpart, bodypart)
コード例 #26
0
 def p_program(self, p):
     """ program  : global_declaration_list
     """
     p[0] = ast.Program(p[1])
コード例 #27
0
ファイル: parser.py プロジェクト: jutge-org/cpp2many
def p_program_01(t):
    '''program : program_part'''
    t[0] = ast.Program(t[1])
コード例 #28
0
 def parse_program(self):
     """ Program :== Command """
     e1 = self.parse_command()
     return ast.Program(e1)
コード例 #29
0
ファイル: parser.py プロジェクト: afsan7/coolpy
 def p_program(self, parse):
     '''
     program : class_list
     '''
     parse[0] = AST.Program(classes=parse[1])
コード例 #30
0
ファイル: ucparser.py プロジェクト: AgustinaDiamint/mc921
 def p_program(self, p):
     """ program  : global_declaration_list
     """
     p[0] = ast.Program(p[1], coord=self._token_coord(p, 1))