Go over grammars..

* Reduce duplication
* Remove unused grammar rules
* Add grammar checking when parsers run as standalone
This commit is contained in:
rocky
2016-06-17 22:59:17 -04:00
parent c4912be570
commit 724faf9a3a
5 changed files with 61 additions and 86 deletions

View File

@@ -307,7 +307,6 @@ class PythonParser(GenericASTBuilder):
expr ::= LOAD_DEREF
expr ::= load_attr
expr ::= binary_expr
expr ::= binary_expr_na
expr ::= build_list
expr ::= cmp
expr ::= mapexpr
@@ -441,12 +440,38 @@ class PythonParser(GenericASTBuilder):
# Positional arguments in make_function
pos_arg ::= expr
nullexprlist ::=
expr32 ::= expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr
expr1024 ::= expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32
'''
def p_designator(self, args):
'''
# Note. The below is right-recursive:
designList ::= designator designator
designList ::= designator DUP_TOP designList
## Can we replace with left-recursive, and redo with:
##
## designList ::= designLists designator designator
## designLists ::= designLists designator DUP_TOP
## designLists ::=
## Will need to redo semantic actiion
designator ::= STORE_FAST
designator ::= STORE_NAME
designator ::= STORE_GLOBAL
designator ::= STORE_DEREF
designator ::= expr STORE_ATTR
designator ::= expr STORE_SLICE+0
designator ::= expr expr STORE_SLICE+1
designator ::= expr expr STORE_SLICE+2
designator ::= expr expr expr STORE_SLICE+3
designator ::= store_subscr
store_subscr ::= expr expr STORE_SUBSCR
designator ::= unpack
designator ::= unpack_list
'''
def parse(p, tokens, customize):
p.add_custom_rules(tokens, customize)

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2015 Rocky Bernstein
# Copyright (c) 2015-2016 Rocky Bernstein
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
# Copyright (c) 1999 John Aycock
"""
@@ -64,8 +64,6 @@ class Python2Parser(PythonParser):
sstmt ::= ifelsestmtr
sstmt ::= return_stmt RETURN_LAST
stmts_opt ::= stmts
stmts_opt ::= passstmt
passstmt ::=
_stmts ::= _stmts stmt
@@ -113,23 +111,6 @@ class Python2Parser(PythonParser):
else_suitec ::= c_stmts
else_suitec ::= return_stmts
designList ::= designator designator
designList ::= designator DUP_TOP designList
designator ::= STORE_FAST
designator ::= STORE_NAME
designator ::= STORE_GLOBAL
designator ::= STORE_DEREF
designator ::= expr STORE_ATTR
designator ::= expr STORE_SLICE+0
designator ::= expr expr STORE_SLICE+1
designator ::= expr expr STORE_SLICE+2
designator ::= expr expr expr STORE_SLICE+3
designator ::= store_subscr
store_subscr ::= expr expr STORE_SUBSCR
designator ::= unpack
designator ::= unpack_list
stmt ::= classdef
stmt ::= call_stmt
@@ -172,7 +153,6 @@ class Python2Parser(PythonParser):
stmt ::= ifelsestmt
stmt ::= whilestmt
stmt ::= whilenotstmt
stmt ::= while1stmt
stmt ::= whileelsestmt
stmt ::= while1elsestmt
@@ -395,3 +375,8 @@ class Python2Parser(PythonParser):
class Python2ParserSingle(Python2Parser, PythonParserSingle):
pass
if __name__ == '__main__':
# Check grammar
p = Python2Parser()
p.checkGrammar()

View File

@@ -6,29 +6,16 @@
import string
from spark_parser import GenericASTBuilder, DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
from uncompyle6.parsers.astnode import AST
from uncompyle6.parser import PythonParserSingle, ParserError, nop_func
from uncompyle6.parser import PythonParser, PythonParserSingle, nop_func
class Python23Parser(PythonParser):
class Python23Parser(GenericASTBuilder):
def __init__(self, debug_parser=PARSER_DEFAULT_DEBUG):
GenericASTBuilder.__init__(self, AST, 'stmts', debug=debug_parser)
super(Python23Parser, self).__init__(AST, 'stmts', debug=debug_parser)
self.customized = {}
def cleanup(self):
"""
Remove recursive references to allow garbage
collector to collect this object.
"""
for dict in (self.rule2func, self.rules, self.rule2name, self.first):
for i in dict.keys():
dict[i] = None
for i in dir(self):
setattr(self, i, None)
def error(self, token):
raise ParserError(token, token.offset)
def typestring(self, token):
return token.type
# FIXME: A lot of the functions below overwrite what is in parse.py which
# have more rules. Probly that should be stripped down more instead.
def p_funcdef(self, args):
'''
@@ -171,23 +158,6 @@ class Python23Parser(GenericASTBuilder):
stmts_opt ::= passstmt
passstmt ::=
designList ::= designator designator
designList ::= designator DUP_TOP designList
designator ::= STORE_FAST
designator ::= STORE_NAME
designator ::= STORE_GLOBAL
designator ::= STORE_DEREF
designator ::= expr STORE_ATTR
designator ::= expr STORE_SLICE+0
designator ::= expr expr STORE_SLICE+1
designator ::= expr expr STORE_SLICE+2
designator ::= expr expr expr STORE_SLICE+3
designator ::= store_subscr
store_subscr ::= expr expr STORE_SUBSCR
designator ::= unpack
designator ::= unpack_list
stmt ::= classdef
stmt ::= call_stmt
call_stmt ::= expr POP_TOP
@@ -208,7 +178,7 @@ class Python23Parser(GenericASTBuilder):
stmt ::= raise_stmt
raise_stmt ::= exprlist RAISE_VARARGS
raise_stmt ::= nullexprlist RAISE_VARARGS
raise_stmt ::= RAISE_VARARGS
stmt ::= exec_stmt
exec_stmt ::= expr exprlist DUP_TOP EXEC_STMT
@@ -285,12 +255,12 @@ class Python23Parser(GenericASTBuilder):
try_end ::= except_else
except_else ::= END_FINALLY TRY_ELSE_START stmts TRY_ELSE_END
except_stmt ::= except_cond except_stmt
except_stmt ::= except_stmt except_cond
except_stmt ::= except_conds try_end
except_stmt ::= except try_end
except_stmt ::= try_end
except_conds ::= except_cond except_conds
except_conds ::= except_conds except_cond
except_conds ::=
except_cond ::= except_cond1
@@ -440,8 +410,6 @@ class Python23Parser(GenericASTBuilder):
exprlist ::= exprlist expr
exprlist ::= expr
nullexprlist ::=
'''
def nonterminal(self, nt, args):
@@ -532,5 +500,10 @@ class Python23Parser(GenericASTBuilder):
class Python23ParserSingle(Python23Parser, PythonParserSingle):
pass
if __name__ == '__main__':
# Check grammar
p = Python23Parser()
p.checkGrammar()
# local variables:
# tab-width: 4

View File

@@ -21,3 +21,8 @@ class Python26Parser(Python2Parser):
class Python26ParserSingle(Python2Parser, PythonParserSingle):
pass
if __name__ == '__main__':
# Check grammar
p = Python26Parser()
p.checkGrammar()

View File

@@ -1,7 +1,7 @@
# Copyright (c) 1999 John Aycock
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
# Copyright (c) 2005 by Dan Pascu <dan@windowmaker.org>
# Copyright (c) 2015, 2016 Rocky Bernstein
# Copyright (c) 2005 by Dan Pascu <dan@windowmaker.org>
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
# Copyright (c) 1999 John Aycock
#
# See LICENSE for license
"""
@@ -111,23 +111,6 @@ class Python3Parser(PythonParser):
else_suitec ::= c_stmts
else_suitec ::= return_stmts
designList ::= designator designator
designList ::= designator DUP_TOP designList
designator ::= STORE_FAST
designator ::= STORE_NAME
designator ::= STORE_GLOBAL
designator ::= STORE_DEREF
designator ::= expr STORE_ATTR
designator ::= expr STORE_SLICE+0
designator ::= expr expr STORE_SLICE+1
designator ::= expr expr STORE_SLICE+2
designator ::= expr expr expr STORE_SLICE+3
designator ::= store_subscr
store_subscr ::= expr expr STORE_SUBSCR
designator ::= unpack
designator ::= unpack_list
stmt ::= classdef
stmt ::= call_stmt
@@ -170,7 +153,6 @@ class Python3Parser(PythonParser):
stmt ::= ifelsestmt
stmt ::= whilestmt
stmt ::= whilenotstmt
stmt ::= while1stmt
stmt ::= whileelsestmt
stmt ::= while1elsestmt
@@ -646,3 +628,8 @@ class Python34ParserSingle(Python34Parser, PythonParserSingle):
class Python35onParserSingle(Python35onParser, PythonParserSingle):
pass
if __name__ == '__main__':
# Check grammar
p = Python3Parser()
p.checkGrammar()