def __init__(self):
self._parser = yacc.yacc(debug=False)
python类yacc()的实例源码
def p_error(p):
msg = []
w = msg.append
w('%r\n' % p)
w('followed by:\n')
for i in range(5):
n = yacc.token()
if not n:
break
w(' %r\n' % n)
raise sherrors.ShellSyntaxError(''.join(msg))
# Build the parser
def parse_schema(data):
"""Parse schema string to schema dict.
"""
# Rebuild `lexer` and `parser` each time.
lexer = lex.lex()
parser = yacc.yacc(debug=False, write_tables=0)
lexer.lineno = 1
return parser.parse(data)
def __init__(self, filename):
self.filename = filename
self.tokens = [ r.upper() for r in self.reserved ] + [ a[2:] for a in dir(self) if a[:2] == 't_' and a[2:].isupper() ]
self.lexer = lex.lex(module=self, debug=False)
self.parser = yacc.yacc(
module=self,
debug=False,
write_tables=False,
picklefile=os.path.splitext(
sys.modules[self.__class__.__module__].__file__
)[0]+'.parsetab')
def __init__(self, verbose=False):
'''????
:param verbose: ???log??
:type verbose: boolean
'''
if verbose:
self._logger = None
else:
self._logger = yacc.PlyLogger(self._NullStream())
def __init__(self, debug=False, **kwargs):
self.default_lexer = DjangoQLLexer()
self.tokens = self.default_lexer.tokens
kwargs['debug'] = debug
self.yacc = yacc.yacc(module=self, **kwargs)
def parse(self, input=None, lexer=None, **kwargs):
lexer = lexer or self.default_lexer
return self.yacc.parse(input=input, lexer=lexer, **kwargs)
def __init__(self):
self.lexer = tokens.NMLLexer()
self.lexer.build()
self.tokens = self.lexer.tokens
self.parser = yacc.yacc(debug = False, module = self, write_tables = 0)
def __init__(self):
self.lexer = Lexer(error_callback=self._lexer_error)
self.tokens = self.lexer.tokens
self.parser = yacc.yacc(module=self)
def p_error(self, p):
next_t = yacc.token()
raise ParseError("invalid code before %s (at line %s)" % (repr(next_t.value), next_t.lineno))
def build(self, **kwargs):
"""Build the parser."""
self._yacc = yacc(**kwargs)
def compile_q(expr):
lexer = lex.lex()
parser = yacc.yacc()
return parser.parse(expr, lexer=lexer)
def p_error(p):
last_cr = p.lexer.lexdata.rfind('\n', 0, p.lexer.lexpos)
column = p.lexer.lexpos - last_cr - 1
if p:
print("Erro de sintaxe em {0} na linha {1} coluna {2}".format(p.value, p.lexer.lineno, column))
# yacc.yacc().errok()
else:
print("Erro de sintaxe EOF")
def main(argv):
try:
opts, args = getopt.getopt(argv, "hf:", ["file"])
except getopt.GetoptError:
print('program.py -f <file>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('program.py -f <file>')
sys.exit()
elif opt in ("-f", "--file"):
f = arg
input_ = ''
with open(f, 'r') as file:
file = open(f, 'r')
for line in file:
input_ += line
file.close()
# Build the parser
parser = yacc.yacc()
program = parser.parse(input_)
if program:
program.print_name()
program.check_node()
print('Variaveis declaradas', declared_variables)
def __init__(self):
self.t_add_rules()
self.lexer = get_script_lexer()
self.parser = yacc.yacc(module=self, errorlog=logging, start='term',
debug=False, optimize=True, picklefile=os.path.join(parser_folder, "script_parser.pickle"))
# rename the parsing method (can't name it directly parse with lru_cache due to ply checking)
self.parse = self.t_parse
def __init__(self, dictionary=None):
self._get_term = partial(term, dictionary=dictionary)
# Build the lexer and parser
self.lexer = get_lexer()
self.parser = yacc.yacc(module=self, errorlog=logging, start='proposition',
debug=False, optimize=True, picklefile=os.path.join(parser_folder, "ieml_parser.pickle"))
self._ieml = None
def __init__(self):
# Build the lexer and parser
self.lexer = get_lexer()
self.parser = yacc.yacc(module=self, errorlog=logging, start='usl',
debug=False, optimize=True, picklefile=os.path.join(parser_folder, "usl_parser.pickle"))
def __init__(self):
# Build the lexer and parser
self.lexer = get_lexer()
self.parser = yacc.yacc(module=self, errorlog=logging, start='path',
debug=False, optimize=True, picklefile="parser/path_parser.pickle")
# rename the parsing method (can't name it directly parse with lru_cache due to ply checking)
self.parse = self.t_parse
def p_init_declarator_list_1(self, p):
""" init_declarator_list : init_declarator
| init_declarator_list COMMA init_declarator
"""
p[0] = p[1] + [p[3]] if len(p) == 4 else [p[1]]
# If the code is declaring a variable that was declared a typedef in an
# outer scope, yacc will think the name is part of declaration_specifiers,
# not init_declarator, and will then get confused by EQUALS. Pass None
# up in place of declarator, and handle this at a higher level.
#
def p_direct_declarator_5(self, p):
""" direct_declarator : direct_declarator LPAREN parameter_type_list RPAREN
| direct_declarator LPAREN identifier_list_opt RPAREN
"""
func = c_ast.FuncDecl(
args=p[3],
type=None,
coord=p[1].coord)
# To see why _get_yacc_lookahead_token is needed, consider:
# typedef char TT;
# void foo(int TT) { TT = 10; }
# Outside the function, TT is a typedef, but inside (starting and
# ending with the braces) it's a parameter. The trouble begins with
# yacc's lookahead token. We don't know if we're declaring or
# defining a function until we see LBRACE, but if we wait for yacc to
# trigger a rule on that token, then TT will have already been read
# and incorrectly interpreted as TYPEID. We need to add the
# parameters to the scope the moment the lexer sees LBRACE.
#
if self._get_yacc_lookahead_token().type == "LBRACE":
if func.args is not None:
for param in func.args.params:
if isinstance(param, c_ast.EllipsisParam): break
self._add_identifier(param.name, param.coord)
p[0] = self._type_modify_decl(decl=p[1], modifier=func)