Welcome everyone, this is your host Nikhil Maan aka Sc0rpi0n101 and this week we’re talking about the C parser.
The Fortran Parser
The Fortran Parser is complete. The Pull Request has also been merged. The parser is merged in master and will be a part of the next SymPy release. You can check out the source code for the Parser at the Pull Request. You can use and test the parser according to your liking. If you encounter any problems or bugs while using the parser, you can report it at SymPy’s Issue Tracker.
The C Parser
Now that the Fortran Parser is complete, let’s discuss the C parser. The parser had to be modified to support SymPyExpression. So, the first task was to modify the parser and the top-level functions so that they could be used by SymPyExpression to manage the expressions. Two new functions were added to parse a C file or a string with C source code. The functions were also modified to distinguish between source string and filename and call the correct function.
def parse_c(source):
converter = CCodeConverter('output')
if os.path.exists(source):
src = converter.parse(source, flags = [])
else:
src = converter.parse_str(source, flags = [])
return src
sym_expr
was also modified to include the C parser along with the Fortran one. The option to parse C was added to the API.
Changes to API
The SymPyExpression now contains options to parse C code into SYmPy expressions. The C parser can be used by the initializer and convert_to_expr
by providing ‘f’ as an argument for mode.
While parsing C source code, the user can provide a string with the C source code, or the path to the file that contains C source code.
Documentation Changes
The documentation has been completed for the C parser. I have also updated the documentation for sym_expr
to include the c parser and how to use it including some examples. The docstrings for the c_parser
explain how the parser works and how each method is supposed to be used.
Travis Build
The Travis build was passing without installing clang and even after installing clang. But clang was available in conda-forge as clang
contained the llvm backend and clang tooling infrastructure. The python bindings for clang had to be installed explicitly using the PyPI package. The next step is to find a conda-forge package which supports the python bindings or create a new package like lfortran.