aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorManuel Jacob <me@manueljacob.de>2017-01-15 18:54:16 +0100
committerEli Bendersky <eliben@users.noreply.github.com>2017-01-15 09:54:16 -0800
commit44137334bac69df72c6378fa84931006179d8bdf (patch)
tree1e41b2b0c5b38a44b4933b40a46b24780f4f26a9
parent7c4d5c4a400365f9e61a9aae14af6e128ab1e4dc (diff)
downloadpycparser-44137334bac69df72c6378fa84931006179d8bdf.tar.gz
Add argument to CParser.__init__ for overriding the yacc start symbol. (#159)
* Add argument to CParser.__init__ for overriding the yacc start symbol. * Add a test for the new 'start' argument of CParser.__init__. * Add documentation for the new 'start' argument of CParser.__init__.
-rw-r--r--pycparser/c_parser.py9
-rwxr-xr-xtests/test_c_parser.py9
2 files changed, 16 insertions, 2 deletions
diff --git a/pycparser/c_parser.py b/pycparser/c_parser.py
index 0856dc2..a6aaa4b 100644
--- a/pycparser/c_parser.py
+++ b/pycparser/c_parser.py
@@ -25,7 +25,8 @@ class CParser(PLYParser):
yacc_optimize=True,
yacctab='pycparser.yacctab',
yacc_debug=False,
- taboutputdir=''):
+ taboutputdir='',
+ start='translation_unit_or_empty'):
""" Create a new CParser.
Some arguments for controlling the debug/optimization
@@ -74,6 +75,10 @@ class CParser(PLYParser):
taboutputdir:
Set this parameter to control the location of generated
lextab and yacctab files.
+
+ start:
+ Sets the parser start symbol. By default, it is
+ 'translation_unit_or_empty'.
"""
self.clex = lexer(
error_func=self._lex_error_func,
@@ -109,7 +114,7 @@ class CParser(PLYParser):
self.cparser = yacc.yacc(
module=self,
- start='translation_unit_or_empty',
+ start=start,
debug=yacc_debug,
optimize=yacc_optimize,
tabmodule=yacctab,
diff --git a/tests/test_c_parser.py b/tests/test_c_parser.py
index 2bd43d1..826f875 100755
--- a/tests/test_c_parser.py
+++ b/tests/test_c_parser.py
@@ -1898,6 +1898,15 @@ class TestCParser_typenames(TestCParser_base):
self.assertRaises(ParseError, self.parse, s2)
+class TestCParser_extra(unittest.TestCase):
+ def test_start_symbol(self):
+ parser = c_parser.CParser(lex_optimize=False, yacc_debug=True,
+ yacc_optimize=False, yacctab='yacctab',
+ start='expression')
+ ast = parser.parse("1 + 2")
+ assert isinstance(ast, BinaryOp)
+
+
if __name__ == '__main__':
#~ suite = unittest.TestLoader().loadTestsFromNames(
#~ ['test_c_parser.TestCParser_fundamentals.test_typedef'])