def element(): # Append function header to output list valid_types = ['STRING', 'LETTER', 'ICON', 'HCON', 'FCON', 'IDENTIFIER'] valid_values = ['MTRUE', 'MFALSE'] if scanner.lex[lex_en[ 'type']] in valid_types: #needs additional code for identifier if using arrays node = Node(scanner.lex[lex_en['type']], scanner.lex[lex_en['value']]) scanner.next() elif scanner.lex[lex_en['value']] in valid_values: node = Node('BOOL', scanner.lex[lex_en['value']]) scanner.next() # for arrays # if scanner.lex[lex_en['value']] == 'LB': # lex_list.append(popt_ref()) elif scanner.lex[lex_en['value']] == 'LP': scanner.next() node = expr() if scanner.lex[lex_en['value']] == 'RP': scanner.next() else: error('RP', 'element') else: # Append error message if case specific grammar not found error('IDENTIFIER or LP or TYPE or MTRUE or MFALSE', 'element') return node
def make_tree(self, s): stack = [] nodes = [] cur = None root = None parent = None flag = 0 for i, c in enumerate(s): if c == '(': flag = 0 cur = Node(i) #(的index是i if stack: stack[-1].children.append(cur) cur.parent = stack[-1] stack.append(cur) if self.root is None: self.root = cur elif c == ')' and stack: topnode = stack.pop() text = s[topnode.start + 1:i].split(" ")[1].split("(")[0] topnode.text = text elif flag == 0 and stack: if c == " ": flag = 1 continue cur = stack[-1] cur.type = cur.type + s[i] return self.root
def plist_const(): node = Node('plist_const') if scanner.lex[lex_en['value']] == 'LB': scanner.next() if (scanner.lex[lex_en['type']] == 'IDENTIFIER' or scanner.lex[lex_en['type']] == 'ICON'): node.children.append(scanner.lex[lex_en['value']]) scanner.next() else: error('IDENTIFIER or ICON', 'plist_const') if (scanner.lex[lex_en['value']] == 'RB'): scanner.next() else: error('RB', 'plist_const') while (scanner.lex[lex_en['value']] == 'LB'): scanner.next() if (scanner.lex[lex_en['type']] == 'IDENTIFIER' or scanner.lex[lex_en['type']] == 'ICON'): node.children.append(scanner.lex[lex_en['value']]) scanner.next() else: # Append error message if case specific grammar not found error('IDENTIFIER or ICON', 'plist_const') if (scanner.lex[lex_en['value']] == 'RB'): scanner.next() else: error('RB', 'plist_const') return node
def parray_dec(): node = Node('parray_dec') if (scanner.lex[lex_en['value']] == 'ARRAY'): scanner.next() node.children.append(plist_const()) node.children.append(popt_array_val()) return node
def func_main(): # Append function header to output list node = Node('func_main') if (scanner.lex[lex_en['value']] == 'MAIN'): scanner.next() return node elif (scanner.lex[lex_en['value']] == 'FUNCTION'): scanner.next() if (scanner.lex[lex_en['type']] == 'IDENTIFIER'): node.children.append(scanner.lex[lex_en['value']]) scanner.next() else: # Append error message if case specific grammar not found error('IDENTIFIER', 'func_main') if (scanner.lex[lex_en['value']] == 'RETURN'): scanner.next() else: # Append error message if case specific grammar not found error('RETURN', 'func_main') if (scanner.lex[lex_en['value']] == 'MVOID'): scanner.next() else: error('MVOID', 'func_main') else: # Append error message if case specific grammar not found error('MAIN or FUNCTION', 'func_main') return node
def pcond1(): if scanner.lex[lex_en['value']] == 'NOT': node = Node(scanner.lex[lex_en['value']]) scanner.next() node.children.append(pcond2()) else: node = pcond2() return node
def parse(): scanner.start() #node = pcondition() node = Node('Program') node.children.append(func_main()) node.children.append(f_globals()) node.children.append(implement()) return node
def parameters(): node = Node('parameters') if scanner.lex[lex_en['value']] == 'PARAMETERS': scanner.next() node.children.append(data_declaration()) while scanner.lex[lex_en['value']] == 'COMMA': scanner.next() node.children.append(data_declaration()) return node
def arg_list(): # Append function header to output list node = Node('arg_list') node.children.append(expr()) while (scanner.lex[lex_en['value']] == 'COMMA'): node.children.append(scanner.lex[lex_en['value']]) scanner.next() node.children.append(expr()) return node
def popt_array_val(): # Append function header to output list node = Node('popt_array_val') if scanner.lex[lex_en['value']] == 'VALUE' or scanner.lex[ lex_en['value']] == 'EQUOP': node.children.append(scanner.lex[lex_en['value']]) scanner.next() node.children.append(array_val()) return node
def var_dec(): node = Node('var_dec') if scanner.lex[lex_en['value']] == 'VARIABLES': scanner.next() else: error('VARIABLES', 'var_dec') node.children.append(data_declarations()) return node
def punary(): # Append function header to output list if scanner.lex[lex_en['value']] == 'NEGATE': node = Node(scanner.lex[lex_en['value']]) scanner.next() node.children.append(element()) else: node = element() return node
def data_declarations(): node = Node('data_declarations') if scanner.lex[lex_en[ 'value']] == 'DEFINE': #check validity before starting while loop while (scanner.lex[lex_en['value']] == 'DEFINE'): scanner.next() node.children.append(data_declaration()) else: error('DEFINE', 'data_declarations') return node
def funct_list(): node = Node('funct_list') if scanner.lex[lex_en['value']] == 'FUNCTION': #check validity before loop while scanner.lex[lex_en['value']] == 'FUNCTION': scanner.next() node.children.append(pother_oper_def()) else: error('FUNCTION', 'pother_oper_def') return node
def f_globals(): node = Node('f_globals') if scanner.lex[lex_en['value']] == 'GLOBAL': scanner.next() if (scanner.lex[lex_en['value']] == 'DECLARATIONS'): scanner.next() else: error('DECLARATIONS', 'f_globals') node.children.append(const_var_struct()) return node
def pcase_def(): node = Node('pcase_def') if scanner.lex[lex_en['value']] == 'DEFAULT': scanner.next() if scanner.lex[ lex_en['value']] == 'COLON': # Check if next lexeme is COLON scanner.next() node.children.append(pactions()) else: error('COLON', 'pcase_def') return node
def ptest_elsif(): node = Node('ptest_elsif') while scanner.lex[lex_en['value']] == 'ELSEIF': scanner.next() node.children.append(pcondition()) if scanner.lex[lex_en['value']] == 'THEN': scanner.next() node.children.append(pactions()) else: error('THEN', 'ptest_elsif') return node
def pcond2(): # Append function header to output list # For LP pcondition RP case if scanner.lex[lex_en['value']] == 'LP': scanner.next() print(scanner.lex[lex_en['value']]) node = pcondition() print(scanner.lex[lex_en['value']]) if scanner.lex[lex_en['value']] == 'RP': scanner.next() else: error('RP', 'pcond2') elif scanner.lex[lex_en['value']] == 'NOT': node = Node('NOT') scanner.next() if scanner.lex[lex_en['value']] == 'MTRUE' or scanner.lex[ lex_en['value']] == 'MFALSE': node.children.append( Node(scanner.lex[lex_en['type']], scanner.lex[lex_en['value']])) scanner.next() else: error('MTRUE or MFALSE', 'pcond2') elif scanner.lex[lex_en['value']] == 'MTRUE' or scanner.lex[ lex_en['value']] == 'MFALSE': node = Node(scanner.lex[lex_en['type']], scanner.lex[lex_en['value']]) scanner.next() else: first_expr = expr() word = scanner.lex[lex_en['value']] if (word == 'EQUALS' or word == 'GREATER' or word == 'LESS'): node = eq_v() node.children.append(first_expr) node.children.append(expr()) else: node = first_expr return node
def name_ref(): node = Node('name_ref') # Append function header to output list print(scanner.lex[lex_en['value']]) if scanner.lex[lex_en['type']] == 'IDENTIFIER': node.children.append(scanner.lex[lex_en['value']]) scanner.next() if (scanner.lex[lex_en['value']] == 'LB'): node.children.append(array_val) else: error('IDENTIFIER', 'name_ref') return node
def pcondition(): # Append function header to output list first_pcond1 = pcond1() word = scanner.lex[lex_en['value']] if word == 'OR' or word == 'AND': node = Node(scanner.lex[lex_en['value']]) node.children.append(first_pcond1) scanner.next() node.children.append(pcond1()) else: node = first_pcond1 return node
def data_type(): node = Node('data_type') valid_types = [ 'TUNSIGNED', 'CHAR', 'INTEGER', 'MVOID', 'DOUBLE', 'LONG', 'SHORT', 'FLOAT', 'REAL', 'TSTRING', 'TBOOL', 'TBYTE' ] if scanner.lex[lex_en['value']] in valid_types: node.children.append(scanner.lex[lex_en['value']]) scanner.next() else: error('valid type', 'data_type') return node
def pusing_ref(): node = Node('pusing_ref') if scanner.lex[lex_en['value']] == 'USING': scanner.next() node.children.append(arg_list()) elif scanner.lex[lex_en['value']] == 'LP': scanner.next() node.children.append(arg_list()) if scanner.lex[lex_en['value']] == 'RP': scanner.next() else: error('RP', 'pusing_ref') return node
def eq_v(): word = scanner.lex[lex_en['value']] if (word == 'EQUALS'): node = Node('EQUALS') scanner.next() elif (word == 'GREATER' or word == 'LESS'): scanner.next() if (scanner.lex[lex_en['value']] == 'THAN'): node = Node(word + ' ' + scanner.lex[lex_en['value']]) scanner.next() elif scanner.lex[lex_en['value']] == 'OR': scanner.next() if scanner.lex[lex_en['value']] == 'EQUAL': node = Node(word + ' OR ' + scanner.lex[lex_en['value']]) scanner.next() else: error('EQUAL', 'eq_v') else: # Append error message if case specific grammar not found error('THAN or OR', 'eq_v') else: error('EQUALS, GREATER, or LESS', 'eq_v') return node
def implement(): node = Node('implement') if scanner.lex[lex_en['value']] == 'IMPLEMENTATIONS': scanner.next() else: error('IMPLEMENTATIONS', 'implement') if scanner.lex[lex_en['value']] == 'MAIN': scanner.next() if scanner.lex[lex_en['value']] == 'DESCRIPTION': scanner.next() node.children.append(parameters()) else: error('DESCRIPTION', 'implement') node.children.append(funct_list()) return node
def pcase_val(): node = Node('pcase_val') if scanner.lex[lex_en['value']] == 'MWHEN': while scanner.lex[lex_en['value']] == 'MWHEN': scanner.next() node.children.append(expr()) if scanner.lex[lex_en['value']] == 'COLON': scanner.next() else: error('COLON', 'pcase_val') node.children.append(pactions()) else: error('MWHEN', 'pcase_val') return node
def data_declaration(): # Append function header to output list node = Node('data_declaration') if scanner.lex[lex_en['type']] == 'IDENTIFIER': node.children.append(scanner.lex[lex_en['value']]) scanner.next() else: error('IDENTIFIER', 'data_declaration') node.children.append(parray_dec()) if (scanner.lex[lex_en['value']] == 'OF'): scanner.next() else: error('OF', 'data_declaration') node.children.append(data_type()) return node
def array_val(): # Append function header to output list node = Node('array_val') if scanner.lex[lex_en['value']] == 'LB': node.children.append(scanner.lex[lex_en['value']]) scanner.next() else: error('LB', 'array_val') lex_list.append(arg_list()) if (scanner.lex[lex_en['value']] == 'RB'): node.children.append(scanner.lex[lex_en['value']]) scanner.next() else: error('RB', 'array_val') return node
def pactions(): # Append function header to output list print(scanner.lex[lex_en['value']]) valid_values = [ 'SET', 'READ', 'INPUT', 'DISPLAY', 'DISPLAYN', 'INCREMENT', 'DECREMENT', 'RETURN', 'CALL', 'IF', 'FOR', 'REPEAT', 'WHILE', 'CASE', 'MBREAK', 'MEXIT', 'POSTCONDITION', 'THEN', 'DO' ] if scanner.lex[lex_en['value']] not in valid_values: #print(scanner.lex[lex_en['value']]) error('action_def keyword', 'pactions') node = Node('pactions') while scanner.lex[lex_en['value']] in valid_values: node.children.append(action_def()) return node
def term(): # Append function header to output list first_punary = punary() this_lex = scanner.lex[lex_en['value']] if (this_lex == 'STAR' or this_lex == 'DIVOP' or this_lex == 'MOD' or this_lex == 'LSHIFT' or this_lex == 'RSHIFT'): node = Node(this_lex) node.children.append(first_punary) scanner.next() node.children.append(punary()) else: node = first_punary return node
def expr(): #process the first <term>, but don't add it to a node yet first_term = term() #check whether there are multiple terms this_lex = scanner.lex[lex_en['value']] if (this_lex == 'PLUS' or this_lex == 'MINUS' or this_lex == 'BAND' or this_lex == 'BOR' or this_lex == 'BXOR'): node = Node(this_lex) node.children.append(first_term) scanner.next() node.children.append(term()) else: node = first_term return node