eliben / pycparser

:snake: Complete C99 parser in pure Python
Other
3.26k stars 612 forks source link

How to tokenize C code using pycparser? #465

Closed xu-zhiwei closed 2 years ago

xu-zhiwei commented 2 years ago

For example, if I have a C code snippet:

int main() {
        int i = 0;
        return 0;
}

How to tokenize this code snippet and get the result like:

['int', 'main', '(', ')', 'int', 'i', '=', '0', ';', 'return', '0', ';', '}']

eliben commented 2 years ago

Use the CLexer class from https://github.com/eliben/pycparser/blob/master/pycparser/c_lexer.py#L15

You can use the tests as inspiration: https://github.com/eliben/pycparser/blob/master/tests/test_c_lexer.py