当前位置: 首页>>代码示例>>Python>>正文


Python Lexer.build方法代码示例

本文整理汇总了Python中lexer.Lexer.build方法的典型用法代码示例。如果您正苦于以下问题:Python Lexer.build方法的具体用法?Python Lexer.build怎么用?Python Lexer.build使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在lexer.Lexer的用法示例。


在下文中一共展示了Lexer.build方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: feed

# 需要导入模块: from lexer import Lexer [as 别名]
# 或者: from lexer.Lexer import build [as 别名]
	def feed(self, text):
		lexer = Lexer()
		lexer.build()
		
		# TODO is this a hack
		self.tokens = lexer.tokens
		
		parser = yacc.yacc(module=self)
		parser.parse(text,lexer=lexer.lexer)
		self.errors = lexer.errors
		
		# print self.classes		
开发者ID:jonhillmtl,项目名称:pyobjcidl,代码行数:14,代码来源:plyer.py

示例2: parse

# 需要导入模块: from lexer import Lexer [as 别名]
# 或者: from lexer.Lexer import build [as 别名]
def parse(source, debug=True):
    lexer = Lexer()
    lexer.build()
    yacc.yacc(debug=debug)
    return yacc.parse(source, lexer=lexer.lexer)
开发者ID:thramas,项目名称:PYL-Implementation,代码行数:7,代码来源:parser.py

示例3: Parser

# 需要导入模块: from lexer import Lexer [as 别名]
# 或者: from lexer.Lexer import build [as 别名]

#.........这里部分代码省略.........
        if isinstance(p[2], ast.Pointer):
            p[0] = p[2].expression
        else:
            p[0] = ast.Address(p[2], p.lineno(1))

    def p_unary_expr_val(self, p):
        '''unary-expr : TIMES unary-expr'''
        p[0] = ast.Pointer(p[2], p.lineno(1))

    def p_unary_expr_inc(self, p):
        '''unary-expr : identifier INC'''
        # p[0] = ast.Increment(p[1])
        p[0] = ast.BinaryOperators("ASSIGN", p[1], ast.BinaryOperators("PLUS", p[1], ast.Number(1), p.lineno(2)), p.lineno(2))

    def p_unary_expr_dec(self, p):
        '''unary-expr : identifier DEC'''
        # p[0] = ast.Decrement(p[1])
        p[0] = ast.BinaryOperators("ASSIGN", p[1], ast.BinaryOperators("MINUS", p[1], ast.Number(1), p.lineno(2)), p.lineno(2))


    # postfix expression
    def p_postfix_expr_primary(self, p):
        '''postfix-expr : primary-expr'''
        p[0] = p[1]

    def p_postfix_expr_array(self, p):
        '''postfix-expr : postfix-expr LBRACKET expression RBRACKET'''
        # p[0] = ast.ArrayExpression(p[1], p[3])
        p[0] = ast.Pointer(ast.BinaryOperators("PLUS", p[1], p[3], p.lineno(2)), p.lineno(2))

    def p_postfix_expr_nullarg(self, p):
        '''postfix-expr : identifier LPAREN RPAREN'''
        p[0] = ast.FunctionExpression(p[1], ast.ArgumentExpressionList(), p[1].lineno)

    def p_postfix_expr_arg(self, p):
        '''postfix-expr : identifier LPAREN argument-expression-list RPAREN'''
        p[0] = ast.FunctionExpression(p[1], p[3], p[1].lineno)


    # primary expression
    def p_primary_expr_id(self, p):
        '''primary-expr : identifier'''
        p[0] = p[1]

    def p_primary_expr_const(self, p):
        '''primary-expr : constant'''
        p[0] = p[1]

    def p_primary_expr_expr(self, p):
        '''primary-expr : LPAREN expression RPAREN'''
        p[0] = p[2]


    # argument expression
    def p_argument_expression_list_assign(self, p):
        '''argument-expression-list : assign-expr'''
        p[0] = ast.ArgumentExpressionList(p[1])

    def p_argument_expression_list_list(self, p):
        '''argument-expression-list : argument-expression-list COMMA assign-expr'''
        p[1].append(p[3])
        p[0] = p[1]


    # identifier
    def p_identifier(self, p):
        '''identifier : ID'''
        p[0] = ast.Identifier(p[1], p.lineno(1))


    # constant
    def p_constant(self, p):
        '''constant : NUMBER'''
        p[0] = ast.Number(p[1])


    # def p_empty(self, p):
    #     '''empty : '''
    #     p[0] = ast.NullNode()

    def p_error(self, p):
        if p:
            sys.exit("Syntax error at token", p.type)
            # Just discard the token and tell the parser it's okay.
            self.parser.errok()
        else:
            sys.exit("Syntax error at EOF")

    # 解析実行部
    def build(self, debug=False, **kwargs):
        # 字句解析
        self.lexer = Lexer()
        self.lexer.build(debug=debug)

        # 構文解析
        self.parser = yacc.yacc(module=self, debug=debug)

    def parse(self, data):
        result = self.parser.parse(data)
        return result
开发者ID:wdtch,项目名称:SmallC-compiler,代码行数:104,代码来源:parser.py

示例4: Parser

# 需要导入模块: from lexer import Lexer [as 别名]
# 或者: from lexer.Lexer import build [as 别名]
class Parser(object):
  """ 
  A parser object for the sire langauge.
  """
  def __init__(self, error, lex_optimise=True, lextab='lextab', 
      yacc_optimise=True, parsetab='parsetab', yacc_debug=False):
    """ 
    Create a new parser.
    """
    self.error = error

    # Instantiate and build the lexer
    self.lexer = Lexer(error_func=self.lex_error)
    self.lexer.build(
        optimize=lex_optimise, 
        lextab=lextab)
    self.tokens = self.lexer.tokens

    # Create and instantiate the parser
    self.parser = yacc.yacc(
        module=self, 
        debug=yacc_debug, 
        optimize=yacc_optimise)

  def parse(self, text, filename='', debug=0):
    """ 
    Parse a file and return the AST.
    """
    if filename:
      self.filename = os.path.basename(filename)
    else:
      self.filename = 'stdin'
    self.lexer.filename = self.filename
    self.lexer.reset()
    return self.parser.parse(text, lexer=self.lexer, debug=debug,
        tracking=True)

  def coord(self, p, index=1):
    """ 
    Return a coordinate for a production.
    """
    return Coord(file=self.filename, line=p.lineno(index), 
        column=self.lexer.findcol(self.lexer.data(), p.lexpos(index)))
  
  def tcoord(self, t):
    """ 
    Return a coordinate for a token.
    """
    return Coord(file=self.filename, line=t.lineno, 
        column=self.lexer.findcol(self.lexer.data(), t.lexpos))

  def lex_error(self, msg, line, col):
    self.error.report_error(msg, Coord(line, col))

  def parse_error(self, msg, coord=None, discard=True):
    self.error.report_error(msg, coord)

  # Define operator presidence
  precedence = (
    ('nonassoc', 'LT', 'GT', 'LE', 'GE', 'EQ', 'NE'), 
    ('left',   'LSHIFT', 'RSHIFT'),
    ('left',   'AND', 'OR', 'XOR'),
    ('left',   'PLUS', 'MINUS'),
    ('left',   'MULT', 'DIV', 'REM'),
    ('right',  'UMINUS', 'UNOT')
  )

  start = 'program'

  # Program declaration ======================================
  
  def p_program(self, p):
    'program : val_defs proc_defs'
    p[0] = ast.Program(p[1], p[2], self.coord(p, 1))

  # Program declaration error
  def p_program_error(self, p):
    'program : error'
    self.parse_error('Syntax error', p, 1)
    p[0] = None

  # Value definitions ========================================

  def p_val_defs_empty(self, p):
    'val_defs : empty'
    p[0] = []

  def p_val_defs(self, p):
    'val_defs : val_def_seq'
    p[0] = p[1]

  # Variable declaration sequence (return a single list)
  def p_val_def_seq(self, p):
    'val_def_seq : val_def SEQSEP'
    p[0] = [p[1]]

  # Variable declaration sequence (return a single list)
  def p_val_def_seq_(self, p):
    'val_def_seq : val_def SEQSEP val_def_seq'
    p[0] = [p[1]] + p[3] if p[3] else [p[1]]
#.........这里部分代码省略.........
开发者ID:xcore,项目名称:tool_sire,代码行数:103,代码来源:parser.py

示例5: Parser

# 需要导入模块: from lexer import Lexer [as 别名]
# 或者: from lexer.Lexer import build [as 别名]
class Parser(object):
    """FOPL formula parser"""

    def __init__(self):
        self.tokens = Lexer.tokens

    def p_start_1(self, p):
        'start : atom'
        p[0] = p[1]

    def p_start_2(self, p):
        'start : start BINARY_OP start'
        token = p[2]
        if token == ur'\u21D2': # Material Implication
            p[0] = Implication(p[1], p[3])
        elif token == ur'\u21D4': # Material Equivalence
            p[0] = Equivalence(p[1], p[3])
        elif token == ur'\u2227': # Conjuction
            p[0] = And(p[1], p[3])
        else: # token == ur'\u2228' Disjunction
            p[0] = Or(p[1], p[3])
        p[0].lhs.parent = p[0].rhs.parent = p[0]

    def p_start_3(self, p):
        'start : start BINARY_OP LB start RB'
        token = p[2]
        if token == ur'\u21D2': # Material Implication
            p[0] = Implication(p[1], p[4])
        elif token == ur'\u21D4': # Material Equivalence
            p[0] = Equivalence(p[1], p[4])
        elif token == ur'\u2227': # Conjuction
            p[0] = And(p[1], p[4])
        else: # token == ur'\u2228' Disjunction
            p[0] = Or(p[1], p[4])
        p[0].lhs.parent = p[0].rhs.parent = p[0]

    def p_start_4(self, p):
        'start : UNARY_OP start'
        p[0] = Not(p[2])
        p[0].lhs.parent = p[0]

    def p_start_5(self, p):
        'start : quantifier LB start RB'
        p[1].lhs = p[3]
        p[1].lhs.parent = p[1]
        p[0] = p[1]

    def p_term_1(self, p):
        'term : VAR_OR_FUNC LB term_list RB'
        p[0] = Func(p[1], p[3])
        p[0].lhs.parent = p[0].rhs.parent = p[0]

    def p_term_2(self, p):
        'term : VAR_OR_FUNC'
        p[0] = Term(p[1])

    def p_term_list_1(self, p):
        'term_list : term_list term'
        p[0] = p[1] + [p[2]]

    def p_term_list_2(self, p):
        'term_list : term'
        p[0] = [p[1]]

    def p_predicate(self, p):
        'predicate : PREDICATE LB term_list RB'
        p[0] = Predicate(p[1], p[3]) # Name and terms

    def p_atom_1(self, p):
        'atom : predicate'
        p[0] = p[1]

    # def p_atom_2(self, p):
    #     'atom : quantifier LB atom RB'
    #     p[1].lhs = p[3]
    #     p[0] = p[1]

    def p_quantifier_1(self, p):
        'quantifier : FOR_ALL var_list'
        p[0] = ForAll(p[2], None)

    def p_quantifier_2(self, p):
        'quantifier : EXISTS var_list'
        p[0] = Exists(p[2], None)

    def p_var_list_1(self, p):
        'var_list : var_list VAR_OR_FUNC'
        p[0] = p[1] + [p[2]]

    def p_var_list_2(self, p):
        'var_list : VAR_OR_FUNC'
        p[0] = [Term(p[1])]

    def build(self, **kwargs):
        self.lex = Lexer()
        self.lex.build()
        self.yacc = yacc.yacc(module=self, **kwargs)

    def parse(self, string):
        return self.yacc.parse(string, debug=0, lexer=self.lex)
开发者ID:ah450,项目名称:unification,代码行数:102,代码来源:parser.py

示例6: Parser

# 需要导入模块: from lexer import Lexer [as 别名]
# 或者: from lexer.Lexer import build [as 别名]
class Parser():
    def __init__(self):
        self.lexer = Lexer()
        self.lexer.build()
        
        self.tokens = self.lexer.tokens
        self.parser = ply.yacc.yacc(module=self, start='file')
        
    def parse(self, text):
        return self.parser.parse(text, lexer=self.lexer)

    precedence = (
        ('left', 'OR'),
        ('left', 'XOR'),
        ('left', 'AND'),
        ('left', 'RSHIFT', 'LSHIFT'),
        ('left', 'PLUS', 'MINUS'),
        ('left', 'TIMES', 'DIVIDE', 'MOD')
    )

    def p_file(self, p):
        """ file : namespace_decl_list
                 | """
        if len(p) == 2:
            p[0] = File(p[1])
        else:
            p[0] = File([])
    
    def p_namespace_decl_list(self, p):
        """ namespace_decl_list : namespace_decl
                                | namespace_decl namespace_decl_list """
        if len(p) == 2:
            p[0] = [p[1]]
        else:
            p[2].append(p[1])
            p[0] = p[2]

    def p_namespace_decl(self, p):
        """ namespace_decl : NAMESPACE scope_decl LBRACE toplevel_decl_list RBRACE """
        p[0] = Namespace(p[2], p[4])

    def p_toplevel_decl_list(self, p):
        """ toplevel_decl_list : toplevel_decl toplevel_decl_list
                               | empty """
        if len(p) == 3:
            p[2].append(p[1])
            p[0] = p[2]
        else:
            p[0] = []

    def p_toplevel_decl(self, p):
        """ toplevel_decl : namespace_decl
                          | type_decl """
        p[0] = p[1]

    def p_scope_decl(self, p):
        """ scope_decl : ID
                       | scope_decl PERIOD ID """
        if len(p) == 2:
            p[0] = [p[1]]
        else:
            p[1].append(p[3])
            p[0] = p[1]

    def p_type_decl(self, p):
        """ type_decl : struct_decl
                      | enum_decl
                      | flags_decl """
        p[0] = p[1]

    def p_declaration_list(self, p):
        """ declaration_list : declaration declaration_list
                             | empty """
        if len(p) == 3:
            p[2].append(p[1])
            p[0] = p[2]
        else:
            p[0] = []

    def p_declaration(self, p):
        """ declaration : type_decl 
                        | member_decl 
                        | const_decl """
        p[0] = p[1]

    def p_struct_decl(self, p):
        """ struct_decl : STRUCT ID LBRACE declaration_list RBRACE 
                        | STRUCT ID COLON ID LBRACE declaration_list RBRACE """
        if len(p) == 6:
            p[0] = Struct(p[2], None, p[4])
        else:
            p[0] = Struct(p[2], p[4], p[6])
    
    def p_enum_decl(self, p):
        """ enum_decl : ENUM ID LBRACE enum_list RBRACE """
        p[0] = Enum(p[2], p[4])
    
    def p_enum_list(self, p):
        """ enum_list : enumerator
                      | enum_list COMMA
#.........这里部分代码省略.........
开发者ID:devknoll,项目名称:ddl,代码行数:103,代码来源:parsing.py


注:本文中的lexer.Lexer.build方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。