Open Code7R opened 1 year ago
This line:
lexer = CParser(input_stream)
should read:
lexer = CLexer(input_stream)
Why do people keep writing drivers with parser.buildParseTrees = True
when it's the default, even across all targets? https://github.com/antlr/antlr4/blob/8188dc5388dfe9246deb9b6ae507c3693fd55c3f/runtime/Python3/src/antlr4/Parser.py#L75
Yeah, okay, that happens if you copy-paste a bit in rush when adapting the example code.
Maybe you could add a simple type check there? Throwing a TypeError
exception when the wrong thing was passed over, after an isinstanceof-check or similar. (Maybe with something funny on top, a-la "418 I'm a teapot").
parser.buildParseTrees = True
might be not needed but that happens when you are struggling with a lack of information to understand.
You might consider an application generator for the grammar. The grammars-v4 repo uses the one I've been writing for several years now, trgen. It takes an Antlr grammar (the .g4's plus a desc.xml to say what targets are supported), and generates a complete command-line program for any target and OS supported. There are others around, but I don't think as good. To use it, install the Trash toolkit, clone the grammar-v4 repo, cd to c/, then type "trgen" and it will create a parser application for all targets.
Looks like there is some inconsistency in the Python backend - there is no nextToken method in the token source but yet the Parser seems to be generated like Java version?!
Target backend: python 3.11 (Debian Unstable), however I saw the same behavior on Windows too (also Python 3.11).
Used grammar: https://github.com/antlr/grammars-v4/blob/master/c/C.g4
Generated with: ~/.local/bin/antlr4 -Dlanguage=Python3 C.g4
Program code: that is basically by the book, see below. Any valid C header as parameter.
Exception stack: