Skip to main content

A simple BLIF parser

Project description

BLIFPARSER

This is a simple Python library that parses BLIF (Berkeley Logic Interchange Format) files (used by SIS, Sequential Interactive Synthesis).

And also evalutes if some keyword's parameters have syntactically correct values.

Only the basic keywords are parsed (.model, .inputs, .outputs, .names, all the FSM keywords, .latch, .exdc, .end).

More complex BLIF files with .clock, .gate, .mlatch, .clock_event and delay constraits are only partially parsed.

This is because the workflow I am supporting does not use these keywords

Currently only one .model keyword per file is supported.

You can see this as a "feature" because it forces the use of a good practise: use many files, one per each component.

You can also use this library as a basic BLIF validator.

Complex checks such as cross file definition checks and input-output names check are NOT implemented because the primary intent of this library is to parse BLIF files.

A more full/complex validator is in the works (and it will use this parser).



WARNING

This parser DOES NOT use grammar files NOR PEG for parsing blif files:

this means that the parsing could be "not perfect".

If someone wants to contribute and change this feel free to make pull requests!

If this library inspires you to write a better parser from scratch, please let me know by making a GH issue

This means that the library works because:

  • the BLIF (format) is simple: most of the time parameters are on the same line of the keywords
  • (some) unit and end to end tests were written


Index

Requirements

  • python 3

Installation

First, install the library using PIP:

pip install blifparser

Then:

  • if you want to use this library as a validation tool, check the "As a validator tool" section

  • if you want to use this library inside your software, check the "As a library" section

    If this is the only way you want to use this software, you can also install it using the installer on the Github Release page

Usage

As a validator tool

Execute the script using this command:

blifparser <input_path>

You can also execute it this way:

python -m blifparser <input_path>

Replace <input_path> with the path to the BLIF file to validate

When you have fixed the errors, execute the script until you have fixed all the errors.

Not all errors appear after the first script execution.

As a library

Basic/common usage:

# import the os library: useful to get the absolute path to the input file
import os
# import this library
import blifparser.blifparser as blifparser

# get the file path and pass it to the parser
filepath = os.path.abspath("example.blif")
parser = blifparser.BlifParser(filepath)

# get the object that contains the parsed data
# from the parser
blif = parser.blif

Now you can:

# get the name of the model
print(blif.model.name)

# get the list of the inputs
print(blif.inputs.inputs)

# get the list of the outputs
print(blif.outputs.outputs)

# get the list of .search keyword
print(blif.imports)

# get the imported file name/path of the first .search keyword
first_import = blif.imports[0]
print(first_import.filepath)

# get the list of subcircuits (.subckt)
print(blif.subcircuits)

# get data from the first subcircuit definition
first_subcircuit = blif.subcircuits[0]
print(first_subcircuit.modelname)  # name of the model
print(first_subcircuit.params)     # subcircuit's parameters

# get the list of boolean functions (.names)
print(blif.booleanfunctions)

# get data from the first boolean function definition
first_boolfunc = blif.booleanfunctions[0]
print(first_boolfunc.inputs)      # list with the names of the inputs
print(first_boolfunc.output)      # string with the name of the output
print(first_boolfunc.truthtable)  # list of lists (each row is a truth table row)

# get the dictionary with the number of occurrencies of each keyword
print(blif.nkeywords)

# get the list of problems/issues
print(blif.problems)

# get the list of the latches
print(blif.latches)

# get the data of the first latch
first_latch = blif.latches[0]
print(first_latch.input)    # name of the input
print(first_latch.output)   # name of the output
print(first_latch.type)     # type of latch (like "re", ...)
print(first_latch.control)  # clock name
print(first_latch.initval)  # initial value

# get the data of the FSM (Finite State Machine)
print(blif.fsm.i.num)       # number of inputs
print(blif.fsm.o.num)       # number of outputs
print(blif.fsm.s.num)       # number of states
print(blif.fsm.p.num)       # number of state transitions
print(blif.fsm.r.name)      # name of the reset state
print(blif.fsm.transtable)  # list of lists (contains the transition table)

Description

These are the first steps to use this library:

# import the os library: useful to get the absolute path to the input file
import os
# import this library
import blifparser.blifparser as blifparser

# get the file path and pass it to the parser
filepath = os.path.abspath("example.blif")
parser = blifparser.BlifParser(filepath)

# get the object that contains the parsed data
# from the parser
blif = parser.blif

The blifparser.BlifParser() object is the parser: it prepares the file for parsing using the prepare_file() method and then creates a keywords.generic.Blif() object that will contain all the information parsed from the file.

The prepare_file() method copies the file and then removes (on the copy):

  • the newlines made with the backslash "\"
  • the comments made with "#".

Then each line is read and parsed:

  • if the line contains a keyword, a "keyword" object is created and then "linked" to the keywords.generic.Blif() object (its parameters get parsed with the keyword)

    For example: if a .model keyword is found, a keywords.generic.Model() object gets created and set in the keywords.generic.Blif() object. (keywords.generic.Blif().model)

  • if the line contains text and the line comes after the .names keyword it is interpreted as the truth table of the latest boolean function (defined by .names)

    This behavior stops when the next keyword is found

  • if the line contains text and the line comes after the .start_kiss keyword it is interpreted as the transition table of the Finite State Machine.

    This behavior stops when the next keyword is found

  • If an unexpected text/keyword is found a "problem" or issue is collected inside the keywords.generic.Blif().problems list.

At the end of the parsing:

  • if an FSM was found, a validation step checks if it is syntactically correct
  • if some boolean functions were found, a validation step checks if they are syntactically correct

The other validation steps are executed during object definition

Now you can use the blif object to get the parsed data

Check the "Usage > As a library" section for more details

Changelog

2021-04-23 1.0.0:

First commit

Author

Zenaro Stefano (mario33881)

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

blifparser-1.0.0.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

blifparser-1.0.0-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file blifparser-1.0.0.tar.gz.

File metadata

  • Download URL: blifparser-1.0.0.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.1.post20201107 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5

File hashes

Hashes for blifparser-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5d81c8cd62678fc289b39b5e45fb578215335e66ffb41c9cf2340ed96471b414
MD5 297bf2161546a83569cda016d795afef
BLAKE2b-256 7807df155c36b5cfcdf21d499919d996a9df5693fb2b4364e352cc65ead58d3f

See more details on using hashes here.

File details

Details for the file blifparser-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: blifparser-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.1.post20201107 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5

File hashes

Hashes for blifparser-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dca41d9453196e1e3cc992c68cc4fc842bd4dd51c031e66ad6ba868d796a643f
MD5 ec8eedec554d7c771f2210f3447744b3
BLAKE2b-256 c74c7be42e918b2993b24cbbb43eea1fb14ce8a0ec89cbf3db83ad880822438c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page