Package: antlr4-verilog
ANTLR4-Verilog-PythonGenerated files from ANTLR4 for Verilog parsing in PythonTutorialInstall this Python packagepython3 -m pip install antlr4_verilogUse your own listener to walk through the AST:fromantlr4_verilogimportInputStream,CommonTokenStream,ParseTreeWalkerfromantlr4_verilog.verilogimportVerilogLexer,VerilogParser,VerilogParserListenerdesign='''module ha(a, b, sum, c);input a, b;output sum, c;assign sum = a ^ b;assign c = a & b;endmodule'''classModuleIdentifierListener(VerilogParserListener):defexitModule_declaration(self,ctx):self.identifier=ctx.module_identifier().getText()lexer=VerilogLexer(InputStream(design))stream=CommonTokenStream(lexer)parser=VerilogParser(stream)tree=parser.source_text()listener=ModuleIdentifierListener()walker=ParseTreeWalker()walker.walk(listener,tree)print(listener.identifier)# 'ha'Take a look at other listener methods forVerilogandSystemVerilogYou may find more examples in thetest fileHow to generate those filesSystem requirements (Ubuntu):sudo apt-get install -y default-jre
sudo apt-get install -y default-jdk
sudo apt-get install -y curlGet ANTLR4:curlhttps://www.antlr.org/download/antlr-4.10.1-complete.jar-oextra/antlr-4.10.1-complete.jarGet ANTLR grammars:git clone https://github.com/antlr/grammars-v4.git extra/grammars-v4Call ANTLR for Verilog grammar:java -Xmx500M -cp "extra/antlr-4.10.1-complete.jar:${CLASSPATH}" org.antlr.v4.Tool -Dlanguage=Python3 -visitor `pwd`/extra/grammars-v4/verilog/verilog/VerilogLexer.g4 `pwd`/extra/grammars-v4/verilog/verilog/VerilogParser.g4 `pwd`/extra/grammars-v4/verilog/verilog/VerilogPreParser.g4 -o src/antlr4_verilog/verilogCall ANTLR for SystemVerilog grammar:java -Xmx500M -cp "extra/antlr-4.10.1-complete.jar:${CLASSPATH}" org.antlr.v4.Tool -Dlanguage=Python3 -visitor `pwd`/extra/grammars-v4/verilog/systemverilog/SystemVerilogLexer.g4 `pwd`/extra/grammars-v4/verilog/systemverilog/SystemVerilogParser.g4 `pwd`/extra/grammars-v4/verilog/systemverilog/SystemVerilogPreParser.g4 -o src/antlr4_verilog/systemverilogHow to test the grammarGenerate Java files:java -Xmx500M -cp "extra/antlr-4.10.1-complete.jar:${CLASSPATH}" org.antlr.v4.Tool -Dlanguage=Java -visitor `pwd`/extra/grammars-v4/verilog/verilog/VerilogLexer.g4 `pwd`/extra/grammars-v4/verilog/verilog/VerilogParser.g4 `pwd`/extra/grammars-v4/verilog/verilog/VerilogPreParser.g4 -o test/testrig/verilogjava -Xmx500M -cp "extra/antlr-4.10.1-complete.jar:${CLASSPATH}" org.antlr.v4.Tool -Dlanguage=Java -visitor `pwd`/extra/grammars-v4/verilog/systemverilog/SystemVerilogLexer.g4 `pwd`/extra/grammars-v4/verilog/systemverilog/SystemVerilogParser.g4 `pwd`/extra/grammars-v4/verilog/systemverilog/SystemVerilogPreParser.g4 -o test/testrig/systemverilogCompile these recently generated files:javac -cp "extra/antlr-4.10.1-complete.jar:${CLASSPATH}" test/testrig/verilog/*.javajavac -cp "extra/antlr-4.10.1-complete.jar:${CLASSPATH}" test/testrig/systemverilog/*.javaCreate JAR files:jar cf test/testrig/verilog.jar -C test/testrig/verilog .jar cf test/testrig/systemverilog.jar -C test/testrig/systemverilog .Finally, fortest.vandtest.svfiles:java -Xmx500M -cp "extra/antlr-4.9-complete.jar:test/testrig/verilog.jar:${CLASSPATH}" org.antlr.v4.gui.TestRig Verilog source_text test/testrig/test.v -treejava -Xmx500M -cp "extra/antlr-4.9-complete.jar:test/testrig/systemverilog.jar:${CLASSPATH}" org.antlr.v4.gui.TestRig SystemVerilog source_text test/testrig/test.sv -treeYou can use-guito test it interactivelyAcknowledgementWe would like to appreciate the work from the ANTLR team and the Verilog/SystemVerilog grammar written byMustafa Said Ağca.