示例#1
0
def writeint():
    node = Node('<writeint>')
    children = []
    if next_token.getValue() == 'writeint':
        children.append(term('writeint'))
        children.append(expression())
    else:
        raise ParserError('writeint parsing failed')
    node.set_child_nodes(children)
    return node
示例#2
0
def assignment():
    assignment_node = Node('<assignment>')
    children = []
    if next_token.getType() == 'ident':
        children.append(identifier('ident'))
        children.append(term(':='))
        children.append(x())
    else:
        raise ParserError('assignment parsing failed')
    assignment_node.set_child_nodes(children)
    return assignment_node
示例#3
0
def type_dclr():
    typdcl = Node('<type>')
    children = []
    if next_token.getValue() == 'int':
        children.append(term('int'))
    elif next_token.getValue() == 'bool':
        children.append(term('bool'))
    else:
        raise ParserError('type_dclr parsing failed')
    typdcl.set_child_nodes(children)
    return typdcl
示例#4
0
def term_tl():
    node = Node('<term>')
    children = []
    if next_token.getType() == 'ident' or next_token.getType(
    ) == 'num' or next_token.getType() == 'boollit' or next_token.getValue(
    ) == '(':
        children.append(factor())
        children.append(v())
    else:
        raise ParserError('term_tl parsing failed')
    node.set_child_nodes(children)
    return node
示例#5
0
def simple_expression():
    node = Node('<simple_expression>')
    children = []
    if next_token.getType() == 'ident' or next_token.getType(
    ) == 'num' or next_token.getType() == 'boollit' or next_token.getValue(
    ) == '(':
        children.append(term_tl())
        children.append(z())
    else:
        raise ParserError('simple_expression parsing failed')
    node.set_child_nodes(children)
    return node
示例#6
0
def else_clause():
    node = Node('<else>')
    children = []
    if next_token.getValue() == 'end':
        children.append(epsilon())
    elif next_token.getValue() == 'else':
        children.append(term('else'))
        children.append(statement_sequence())
    else:
        raise ParserError('else_clause parsing failed')
    node.set_child_nodes(children)
    return node
示例#7
0
def while_statement():
    node = Node('<while statement>')
    children = []
    if next_token.getValue() == 'while':
        children.append(term('while'))
        children.append(expression())
        children.append(term('do'))
        children.append(statement_sequence())
        children.append(term('end'))
    else:
        raise ParserError('while_statement parsing failed')
    node.set_child_nodes(children)
    return node
示例#8
0
def x():
    x_node = Node('<x>')
    children = []
    if next_token.getType() == 'ident' or next_token.getType(
    ) == 'num' or next_token.getType() == 'boollit' or next_token.getValue(
    ) == '(':
        children.append(expression())
    elif next_token.getValue() == 'readint':
        children.append(term('readint'))
    else:
        raise ParserError('x parsing failed')
    x_node.set_child_nodes(children)
    return x_node
示例#9
0
def y():
    node = Node('<y>')
    children = []
    if next_token.getValue() == ';' or next_token.getValue(
    ) == 'then' or next_token.getValue() == 'do' or next_token.getValue(
    ) == ')':
        children.append(epsilon())
    elif next_token.getType() == 'COMPARE':
        children.append(term_type('COMPARE'))
        children.append(expression())
    else:
        raise ParserError('y parsing failed')
    node.set_child_nodes(children)
    return node
示例#10
0
def if_statement():
    if_node = Node('<if statement>')
    children = []
    if next_token.getValue() == 'if':
        children.append(term('if'))
        children.append(expression())
        children.append(term('then'))
        children.append(statement_sequence())
        children.append(else_clause())
        children.append(term('end'))
    else:
        raise ParserError('if_statement parsing failed')
    if_node.set_child_nodes(children)
    return if_node
示例#11
0
def statement_sequence():
    stsq = Node('<statement_sequence>')
    children = []
    if next_token.getValue() == 'else' or next_token.getValue() == 'end':
        children.append(epsilon())
    elif next_token.getType() == 'ident' or next_token.getValue(
    ) == 'if' or next_token.getValue() == 'while' or next_token.getValue(
    ) == 'writeint':
        children.append(statement())
        children.append(term(';'))
        children.append(statement_sequence())
    else:
        raise ParserError('statement_sequence parsing failed')
    stsq.set_child_nodes(children)
    return stsq
示例#12
0
def v():
    node = Node('<v>')
    children = []
    if next_token.getValue() == ';' or next_token.getValue(
    ) == 'then' or next_token.getValue() == 'do' or next_token.getValue(
    ) == ')' or next_token.getType() == 'ADDITIVE' or next_token.getType(
    ) == 'COMPARE':
        children.append(epsilon())
    elif next_token.getType() == 'MULTIPLICATIVE':
        children.append(term_type('MULTIPLICATIVE'))
        children.append(term_tl())
    else:
        raise ParserError('v parsing failed')
    node.set_child_nodes(children)
    return node
示例#13
0
def statement():
    statement_node = Node('<statement>')
    children = []
    if next_token.getType() == 'ident':
        children.append(assignment())
    elif next_token.getValue() == 'if':
        children.append(if_statement())
    elif next_token.getValue() == 'while':
        children.append(while_statement())
    elif next_token.getValue() == 'writeint':
        children.append(writeint())
    else:
        raise ParserError('statement parsing failed')
    statement_node.set_child_nodes(children)
    return statement_node
示例#14
0
def factor():
    node = Node('<factor>')
    children = []
    if next_token.getType() == 'num':
        children.append(term_type("num"))
    elif next_token.getType() == 'boollit':
        children.append(term_type('boollit'))
    elif next_token.getType() == 'ident':
        children.append(term_type('ident'))
    elif next_token.getValue() == '(':
        children.append(term('('))
        children.append(expression())
        children.append(term(')'))
    else:
        raise ParserError('factor parsing failed')
    node.set_child_nodes(children)
    return node
示例#15
0
def declaration():
    dclr = Node('<declaration>')
    children = []
    if next_token.getValue() == 'begin':
        global stop_declaration
        stop_declaration = True
        children.append(epsilon())
    elif next_token.getValue() == 'var':
        children.append(term('var'))
        children.append(identifier('ident'))
        children.append(term('as'))
        children.append(type_dclr())
        children.append(term(';'))
        children.append(declaration())
    else:
        raise ParserError('declaration parsing failed')
    dclr.set_child_nodes(children)
    return dclr
示例#16
0
def program():
    root = None
    children = []
    try:
        if next_token.getValue() == 'program':
            root = Node('<program>')
            children.append(term('program'))
            children.append(declaration())
            children.append(term('begin'))
            children.append(statement_sequence())
            children.append(term('end'))
            root.set_child_nodes(children)
        else:
            raise ParserError(
                "Parser error in program method. Expected 'program' but received {}"
                .format(next_token.getValue()))
    except ParserError as pe:
        print(pe.value)
    return root
示例#17
0
def term(token_param):
    global next_token
    if next_token.getValue() == token_param:
        node = Node(next_token.getValue(), True, next_token)
        try:
            next_token = tokens.pop(0)
        except IndexError:
            return node
        return node
    else:
        raise ParserError("Parsing Error: Expected {}, but received {}".format(
            token_param, next_token.getValue()))
示例#18
0
def identifier(token_param):
    global declared_variable_list
    global next_token
    if next_token.getType() == token_param:
        node = Node(next_token.getType(), True, next_token)
        if not stop_declaration:
            if next_token.getValue() in declared_variable_list:
                raise ParserError('trying to declare same variable again')
            declared_variable_list.append(next_token.getValue())
        else:
            if next_token.getValue() not in declared_variable_list:
                raise ParserError('Using undeclared variable.')
        try:
            next_token = tokens.pop(0)
        except IndexError:
            raise ParserError('Missing END keyword')
        return node
    else:
        raise ParserError('Parsing Error: Expected an identifier')
示例#19
0
def epsilon():
    return Node('E', True)