Generated files from ANTLR4 for Verilog parsing
Project description
ANTLR4 Verilog Grammar Files
Generated files from ANTLR4 for Verilog parsing
Tutorial
Install this Python package
python3 -m pip install antlr4_verilog
Use your own listener to walk through the AST:
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.VerilogParserListener):
def exitModule_declaration(self, ctx):
self.identifier = ctx.module_identifier().getText()
lexer = VerilogLexer.VerilogLexer(InputStream(design))
stream = CommonTokenStream(lexer)
parser = VerilogParser.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
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.9-complete.jar -o extra/antlr-4.9-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.9-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/VerilogPreprocessorParser.g4 -o antlr4-verilog/verilog
- Call ANTLR for SystemVerilog grammar:
java -Xmx500M -cp "extra/antlr-4.9-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 -o antlr4-verilog/systemverilog
How to test the grammar
- Generate Java files:
java -Xmx500M -cp "extra/antlr-4.9-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/VerilogPreprocessorParser.g4 -o test/testrig/verilog
java -Xmx500M -cp "extra/antlr-4.9-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 -o test/testrig/systemverilog
- Compile these recently generated files:
javac -cp "extra/antlr-4.9-complete.jar:${CLASSPATH}" test/testrig/verilog/*.java
javac -cp "extra/antlr-4.9-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
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-2.0.0.tar.gz
(464.1 kB
view hashes)
Built Distribution
antlr4_verilog-2.0.0-py3-none-any.whl
(477.9 kB
view hashes)
Close
Hashes for antlr4_verilog-2.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1175f3c79985092854c5fb4dbffee36d6def6b7480943f841ebffcbc1f007b14 |
|
MD5 | 48703578a40e1eccac34d0909535dfde |
|
BLAKE2b-256 | 588cecca5985faccd598901e6d7360a0e69431ee06072a1f06aca2801f8f6cb8 |