我如何检查给定的部分或不完整的 python 程序在语法上是否正确
How can I check given partial or incomplete python program is syntacticly correct or not
我如何编写一个程序来解析不完整的 return 如果到目前为止的标记字符串可能生成句法正确的 python 程序则为真 python 如果句法不正确则为假 python程序可以永远生成。
这是一个例子。
programs = ["def foo():", "def ():", "def foo(): return ", "def foo(x):\n else"]
def check(program):
'''
returns True if program can be possibly completed as a valid python
program else False if there doesn't exist any string which can
be appended after the 'program' to make it valid python program.
'''
pass
print([check(program) for program in programs])
>> [True, False, True, False]
我知道我可以使用 antlr-python 运行time 来检查 运行 解析器以适当的顺序检查语法扩展规则,但我需要纯 python 解决方案。我可以仅使用 python ast 模块来执行此操作吗?如果不能,我如何使用 python-antlr-运行time.
来执行此操作?
我被管理到这个antlr,这是我的解决方案
from antlr4 import *
from antlr4.error import ErrorListener
from Python3Lexer import Python3Lexer
from Python3Parser import Python3Parser
class MyErrorListener(ErrorListener.ErrorListener):
def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
if offendingSymbol.type == -1:
pass
else:
raise Exception("syntaxError")
def reportAmbiguity(self, recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs):
raise Exception("reportAmbiguity")
def reportAttemptingFullContext(self, recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs):
raise Exception("reportAttemptingFullContext")
def reportContextSensitivity(self, recognizer, dfa, startIndex, stopIndex, prediction, configs):
raise Exception("reportContextSensitivity")
def check(program):
try:
input_stream = InputStream(program)
lexer = Python3Lexer(input_stream)
stream = CommonTokenStream(lexer)
parser = Python3Parser(stream)
parser.addErrorListener(MyErrorListener())
parser.file_input()
return True
except Exception:
return False
if __name__ == '__main__':
programs = ["def foo():", "def ():", "def foo(): return ", "def foo(x):\n else"]
print([check(program) for program in programs])
我如何编写一个程序来解析不完整的 return 如果到目前为止的标记字符串可能生成句法正确的 python 程序则为真 python 如果句法不正确则为假 python程序可以永远生成。
这是一个例子。
programs = ["def foo():", "def ():", "def foo(): return ", "def foo(x):\n else"]
def check(program):
'''
returns True if program can be possibly completed as a valid python
program else False if there doesn't exist any string which can
be appended after the 'program' to make it valid python program.
'''
pass
print([check(program) for program in programs])
>> [True, False, True, False]
我知道我可以使用 antlr-python 运行time 来检查 运行 解析器以适当的顺序检查语法扩展规则,但我需要纯 python 解决方案。我可以仅使用 python ast 模块来执行此操作吗?如果不能,我如何使用 python-antlr-运行time.
来执行此操作?我被管理到这个antlr,这是我的解决方案
from antlr4 import *
from antlr4.error import ErrorListener
from Python3Lexer import Python3Lexer
from Python3Parser import Python3Parser
class MyErrorListener(ErrorListener.ErrorListener):
def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
if offendingSymbol.type == -1:
pass
else:
raise Exception("syntaxError")
def reportAmbiguity(self, recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs):
raise Exception("reportAmbiguity")
def reportAttemptingFullContext(self, recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs):
raise Exception("reportAttemptingFullContext")
def reportContextSensitivity(self, recognizer, dfa, startIndex, stopIndex, prediction, configs):
raise Exception("reportContextSensitivity")
def check(program):
try:
input_stream = InputStream(program)
lexer = Python3Lexer(input_stream)
stream = CommonTokenStream(lexer)
parser = Python3Parser(stream)
parser.addErrorListener(MyErrorListener())
parser.file_input()
return True
except Exception:
return False
if __name__ == '__main__':
programs = ["def foo():", "def ():", "def foo(): return ", "def foo(x):\n else"]
print([check(program) for program in programs])