Generated files from ANTLR4 for Verilog parsing
Project description
ANTLR4-Verilog-Python
Generated files from ANTLR4 for Verilog parsing in Python
Tutorial
Install this Python package
python3 -m pip install antlr4_verilog
Use your own listener to walk through the AST:
from antlr4_verilog import InputStream, CommonTokenStream, ParseTreeWalker
from antlr4_verilog.verilog import VerilogLexer, VerilogParser, VerilogParserListener
design = '''
module ha(a, b, sum, c);
input a, b;
output sum, c;
assign sum = a ^ b;
assign c = a & b;
endmodule
'''
class ModuleIdentifierListener(VerilogParserListener):
def exitModule_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 for Verilog and SystemVerilog
You may find more examples in the test file
How to generate those files
System requirements (Ubuntu):
sudo apt-get install -y default-jre
sudo apt-get install -y default-jdk
sudo apt-get install -y curl
- Get ANTLR4:
curl https://www.antlr.org/download/antlr-4.10.1-complete.jar -o extra/antlr-4.10.1-complete.jar
- Get ANTLR grammars:
git clone https://github.com/antlr/grammars-v4.git extra/grammars-v4
- Call 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/verilog
- Call 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/systemverilog
How to test the grammar
- Generate 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/verilog
java -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/systemverilog
- Compile these recently generated files:
javac -cp "extra/antlr-4.10.1-complete.jar:${CLASSPATH}" test/testrig/verilog/*.java
javac -cp "extra/antlr-4.10.1-complete.jar:${CLASSPATH}" test/testrig/systemverilog/*.java
- Create JAR files:
jar cf test/testrig/verilog.jar -C test/testrig/verilog .
jar cf test/testrig/systemverilog.jar -C test/testrig/systemverilog .
- Finally, for
test.v
andtest.sv
files:
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 -tree
java -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 -tree
You can use
-gui
to test it interactively
Acknowledgement
We would like to appreciate the work from the ANTLR team and the Verilog/SystemVerilog grammar written by Mustafa Said Ağca.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
antlr4-verilog-4.0.0.tar.gz
(482.7 kB
view details)
Built Distribution
File details
Details for the file antlr4-verilog-4.0.0.tar.gz
.
File metadata
- Download URL: antlr4-verilog-4.0.0.tar.gz
- Upload date:
- Size: 482.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2cc71a0326620c437be335cbc0720c46131a510b236e20041df8e700c0b4f6b5 |
|
MD5 | 2cd0da6e3a4d0f6bc6a4b0ad47bacd3a |
|
BLAKE2b-256 | a097b516849a90c5f9a92d1c66b2ba383df9acfdfeca97e9ccc430a8cd80ba88 |
File details
Details for the file antlr4_verilog-4.0.0-py3-none-any.whl
.
File metadata
- Download URL: antlr4_verilog-4.0.0-py3-none-any.whl
- Upload date:
- Size: 495.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f44caf9c9603235592908bdabc40c1849d72ceb460263fe9cea5a3ec516a6fc |
|
MD5 | 5fac71afcae3bf3d4629978003418857 |
|
BLAKE2b-256 | 447acb9ff729e60b1b293002606e61f6faf0a606eb5895773c093f8ca2bdaf52 |