Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def parse_c(codelet):
"""
Adds 'symbols' field to the codelet after parsing the c code.
:param codelet: The codelet object to parsed.
:type code: Codelet
.. todo::
Preprocess c code so that no ParseErrors are thrown.
"""
tree = c_parser.CParser().parse(codelet.code)
cutter = _TreeCutter()
cutter.visit(tree)
codelet.symbols = cutter.accum
def __init__(self, knowntypes=None, knowntypedefs=None):
if knowntypes is None:
knowntypes = {}
if knowntypedefs is None:
knowntypedefs = {}
self._types = dict(knowntypes)
self._typedefs = dict(knowntypedefs)
self.cpt = 0
self.loc_to_decl_info = {}
self.parser = c_parser.CParser()
self._cpt_decl = 0
self.ast_to_typeid_rules = {
c_ast.Struct: self.ast_to_typeid_struct,
c_ast.Union: self.ast_to_typeid_union,
c_ast.IdentifierType: self.ast_to_typeid_identifiertype,
c_ast.TypeDecl: self.ast_to_typeid_typedecl,
c_ast.Decl: self.ast_to_typeid_decl,
c_ast.Typename: self.ast_to_typeid_typename,
c_ast.FuncDecl: self.ast_to_typeid_funcdecl,
c_ast.Enum: self.ast_to_typeid_enum,
c_ast.PtrDecl: self.ast_to_typeid_ptrdecl,
c_ast.EllipsisParam: self.ast_to_typeid_ellipsisparam,
c_ast.ArrayDecl: self.ast_to_typeid_arraydecl,
}
def pycparser_getAstTree(statement):
text='int main(){\n'+statement+';\n}'
print text
# create a pycparser
parser = c_parser.CParser()
ast = parser.parse(text, filename='')
# generate the XML tree
ast.show()
codeAstXml = open('code_ast.xml','w')
ast.showXml(codeAstXml)
codeAstXml.close()
tree = ET.parse('code_ast.xml')
os.remove('code_ast.xml')
root = tree.getroot()
return root.find(".//FuncDef/Compound")
def explain_c_declaration(c_decl):
""" Parses the declaration in c_decl and returns a text
explanation as a string.
The last external node of the string is used, to allow
earlier typedefs for used types.
"""
parser = c_parser.CParser()
try:
node = parser.parse(c_decl, filename='')
except c_parser.ParseError:
e = sys.exc_info()[1]
return "Parse error:" + str(e)
if ( not isinstance(node, c_ast.FileAST) or
not isinstance(node.ext[-1], c_ast.Decl)):
return "Not a valid declaration"
return _explain_decl_node(node.ext[-1])
# Eli Bendersky [https://eli.thegreenplace.net/]
# License: BSD
#-----------------------------------------------------------------
# Generate c_ast.py
from _ast_gen import ASTCodeGenerator
ast_gen = ASTCodeGenerator('_c_ast.cfg')
ast_gen.generate(open('c_ast.py', 'w'))
import sys
sys.path[0:0] = ['.', '..']
from pycparser import c_parser
# Generates the tables
#
c_parser.CParser(
lex_optimize=True,
yacc_debug=False,
yacc_optimize=True)
# Load to compile into .pyc
#
import lextab
import yacctab
import c_ast
def pycparser_getAstTree(self,statement):
#text='int main(){\n'+statement+';\n}'
text=statement
#print text
# create a pycparser
parser = c_parser.CParser()
ast = parser.parse(text, filename='')
# generate the XML tree
if DEBUG:
ast.show()
codeAstXml = open('code_ast.xml','w')
ast.showXml(codeAstXml)
codeAstXml.close()
tree = ET.parse('code_ast.xml')
os.remove('code_ast.xml')
root = tree.getroot()
return root
#return root.find(".//FuncDef/Compound")
def parse_access(c_access):
"""Parse C access
@c_access: C access string
"""
main = '''
int main() {
%s;
}
''' % c_access
parser = c_parser.CParser()
node = parser.parse(main, filename='')
access = node.ext[-1].body.block_items[0]
return access
@staticmethod
def parse_header(header_data):
"""Return the AST corresponding to @header_data
@header_data: str of a C-like header file
"""
# We can't use add_c_decl, because we need the AST to get back
# function's arguments name
parser = pycparser.c_parser.CParser()
return c_to_ast(parser, header_data)
# Eli Bendersky [http://eli.thegreenplace.net]
# License: BSD
#-----------------------------------------------------------------
# Generate c_ast.py
from _ast_gen import ASTCodeGenerator
ast_gen = ASTCodeGenerator('_c_ast.cfg')
ast_gen.generate(open('c_ast.py', 'w'))
import sys
sys.path[0:0] = ['.', '..']
from pycparser import c_parser
# Generates the tables
#
c_parser.CParser(
lex_optimize=True,
yacc_debug=False,
yacc_optimize=True)
# Load to compile into .pyc
#
import lextab
import yacctab
import c_ast