antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
BSD 3-Clause "New" or "Revised" License
16.98k stars 3.26k forks source link

antlr-4.12.0 cpp to parse verilog,runtime lib error. #4247

Open jhhe66 opened 1 year ago

jhhe66 commented 1 year ago

step: 1、java -jar .\antlr-4.12.0-complete.jar -Dlanguage=Cpp -visitor .\SystemVerilogLexer.g4 .\SystemVerilogParser.g4 .\SystemVerilogPreParser.g4 .\VerilogLexer.g4 .\VerilogParser.g4 .\VerilogPreParser.g4 -o ..\generated\ 2、add runtime lib to my project. 3、code:

include

include "VerilogLexer.h"

include "ANTLRFileStream.h"

include "VerilogParser.h"

include "tree/ParseTree.h"

include "VerilogPreParser.h"

int main() { std::string file = "D:\Works\MyVerilog\MyAntlr4\verilogFile\axiluart.v"; antlr4::ANTLRFileStream fileStream; fileStream.loadFromFile(file);

VerilogLexer verilogLexer(&fileStream);
antlr4::CommonTokenStream tokens(&verilogLexer);
VerilogPreParser parser(&tokens);
antlr4::tree::ParseTree *tree = parser.source_text();

auto s = tree->toStringTree(&parser);
std::cout << "Parse Tree: " << s << std::endl;

return 0;

}

image

kaby76 commented 1 year ago

There is not enough information to determine why your code is not working. I suspect you have compiled the runtime and your driver program with inconsistent compiler flags. The fact that you manually execute java -jar .\antlr-4.12.0-complete.jar -Dlanguage=Cpp -visitor .\SystemVerilogLexer.g4 .\SystemVerilogParser.g4 .\SystemVerilogPreParser.g4 .\VerilogLexer.g4 .\VerilogParser.g4 .\VerilogPreParser.g4 -o ..\generated to generate the parser and lexer means you are not using cmake. Yet the "Debug Assert Failed" pop-up box shows the executable in a directory with "cmake" in its name. It's unclear what is your build script, and with that, any way to find out why it's failing.

Note, before you use any grammar in grammars-v4, you should check the <targets> line in the desc.xml file to know whether the grammar has been ported to the target you want. Cpp isn't listed for verilog/, and the VerilogPreParser grammar has never been tested ever!

After fiddling around with the desc.xml and using the latest driver generator, I generated a driver for the VerilogLexer/VerilogPreParser grammar pair and it seems to build and parse okay, although the parse trees are empty for all input files! The VerilogLexer/VerilogParser grammar pair works fine, parsing all input files. I suspect that the VerilogPreParser grammar never worked!

See attached zip files for the drivers I generated for VerilogLexer/VerilogPreParser and VerilogLexer/VerilogParser grammar pairs. The VerilogLexer/VerilogParser pair is in Release mode, and the VerilogLexer/VerilogPreParser pair is in Debug mode.

Generated-Cpp.zip Generated-Cpp-Pre.zip

msagca commented 1 year ago

Hi @jhhe66

VerilogLexer separates preprocessor tokens from the others by sending them on to the DIRECTIVES channel. You have to feed the correct token stream to VerilogPreParser. This can be achieved by providing the channel ID to the CommonTokenStream constructor.