本文整理汇总了Python中lexer.Lexer.data方法的典型用法代码示例。如果您正苦于以下问题:Python Lexer.data方法的具体用法?Python Lexer.data怎么用?Python Lexer.data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lexer.Lexer
的用法示例。
在下文中一共展示了Lexer.data方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Parser
# 需要导入模块: from lexer import Lexer [as 别名]
# 或者: from lexer.Lexer import data [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]]
#.........这里部分代码省略.........