summaryrefslogtreecommitdiff
path: root/examples/func_defs.py
diff options
context:
space:
mode:
authorEli Bendersky <eliben@gmail.com>2010-05-21 09:05:39 +0300
committerEli Bendersky <eliben@gmail.com>2010-05-21 09:05:39 +0300
commit3921e8e23ef7952e2e118e9d534cfc4a221e2450 (patch)
tree60efcf9f007a5657422a75ed0aad28cb97cb3fa2 /examples/func_defs.py
downloadpycparser-3921e8e23ef7952e2e118e9d534cfc4a221e2450.tar.gz
initial import from SVN
Diffstat (limited to 'examples/func_defs.py')
-rw-r--r--examples/func_defs.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/examples/func_defs.py b/examples/func_defs.py
new file mode 100644
index 0000000..fa15b38
--- /dev/null
+++ b/examples/func_defs.py
@@ -0,0 +1,51 @@
+#-----------------------------------------------------------------
+# pycparser: func_defs.py
+#
+# Using pycparser for printing out all the functions defined in a
+# C file.
+#
+# This is a simple example of traversing the AST generated by
+# pycparser.
+#
+# Copyright (C) 2008-2009, Eli Bendersky
+# License: LGPL
+#-----------------------------------------------------------------
+import sys
+
+# This is not required if you've installed pycparser into
+# your site-packages/ with setup.py
+#
+sys.path.insert(0, '..')
+
+from pycparser import c_parser, c_ast, parse_file
+from pycparser.portability import printme
+
+
+# A simple visitor for FuncDef nodes that prints the names and
+# locations of function definitions.
+#
+class FuncDefVisitor(c_ast.NodeVisitor):
+ def visit_FuncDef(self, node):
+ printme('%s at %s\n' % (node.decl.name, node.decl.coord))
+
+
+def show_func_defs(filename):
+ # Note that cpp is used. Provide a path to your own cpp or
+ # make sure one exists in PATH.
+ #
+ ast = parse_file(filename, use_cpp=True)
+
+ v = FuncDefVisitor()
+ v.visit(ast)
+
+
+if __name__ == "__main__":
+ if len(sys.argv) > 1:
+ filename = sys.argv[1]
+ else:
+ filename = 'c_files/memmgr.c'
+
+ show_func_defs(filename)
+
+
+