Giter Club home page Giter Club logo

parser-verilog's Introduction

Parser-Verilog

A Standalone Structural Verilog Parser

Get Started with Parser-Verilog

A Verilog is a programming language that is used to describe a digital circuit. Below is a circuit written in Verilog.

module simple (input1, input2, input3, out);

// primary inputs
input input1, input2, input3;

// primary output
output out; 

wire out;

// wires
wire w1;
wire w2;
wire w3;

// module instances
AND2_X1 g1 (.a(input1), .b(input2), .o(w1));
OR2_X1 g2  (.a(input3), .b(w1), .o(w2));
INV_X2 g3  (.a(w2), .o(w3));
NOR2_X1 g4 (.a(w1), .b(w3), .o(out));

endmodule

The following example demonstrates how to use Parser-Verilog to parse a Verilog file.

#include <iostream>

#include "verilog_driver.hpp"   // The only include you need

// Define your own parser by inheriting the ParserVerilogInterface
struct MyVerilogParser : public verilog::ParserVerilogInterface {

  virtual ~MyVerilogParser(){}

  // Function that will be called when encountering the top module name.
  void add_module(std::string&& name){
    std::cout << "Module: " << name << '\n';
  }

  // Function that will be called when encountering a port.
  void add_port(verilog::Port&& port) {
    std::cout << "Port: " << port << '\n';
  }  

  // Function that will be called when encountering a net.
  void add_net(verilog::Net&& net) {
    std::cout << "Net: " << net << '\n';
  }  

  // Function that will be called when encountering a assignment statement.
  void add_assignment(verilog::Assignment&& ast) {
    std::cout << "Assignment: " << ast << '\n';
  }  

  // Function that will be called when encountering a module instance.
  void add_instance(verilog::Instance&& inst) {
    std::cout << "Instance: " << inst << '\n';
  }
};

int main(){
  MyVerilogParser parser;
  parser.read("verilog_file.v");
  return EXIT_SUCCESS;
}

You need a C++ compiler with C++17 support, GNU Bison and Flex to compile Parser-Verilog.

~$ flex -o./verilog_lexer.yy.cc parser-verilog/verilog_lexer.l 
~$ bison -d -o verilog_parser.tab.cc parser-verilog/verilog_parser.yy
~$ g++ -std=c++17 -I parser-verilog/ verilog_parser.tab.cc verilog_lexer.yy.cc example/sample_parser.cpp -o sample_parser -lstdc++fs

Compile Tests

System Requirements

To use Parser-Verilog, you need following libraries:

Currently Parser-Verilog has been tested to run well on Linux distributions.

Build through CMake

We use CMake to manage the source and tests. We recommend using out-of-source build.

~$ git clone https://github.com/OpenTimer/Parser-Verilog.git
~$ cd Parser-Verilog
~$ mkdir build
~$ cd build
~$ cmake ../
~$ make 

Use Parser-Verilog

Parser-Verilog is extremely easy to use and understand. You create your own Verilog parser struct or class that inherits the VerilogParserInterface and define member functions to be invoked to process the components in a circuit.

Create your own Verilog parser

#include "verilog_driver.hpp"   // The only include you need

// Define your own parser by inheriting the ParserVerilogInterface
struct MyVerilogParser : public verilog::ParserVerilogInterface {

  virtual ~MyVerilogParser(){}

  // Implement below member functions to process different components

  void add_module(std::string&& name){
    // Process the module name
  }
  void add_port(verilog::Port&& port) {
    // Process a port
  }  
  void add_net(verilog::Net&& net) {
    // Process a net
  }
  void add_assignment(verilog::Assignment&& ast) {
    // Process an assignment
  }  
  void add_instance(verilog::Instance&& inst) {
    // Process a module instance
  }
};

Below are the required member functions in your custom Verilog parser

Name Argument Return Description
add_module std::string n/a invoked when encountering the name of top module
add_port Port n/a invoked when encountering a primary input/output of the module
add_net Net n/a invoked when encountering a net declaration
add_assignment Assignment n/a invoked when encountering an assignment statement
add_instance Instance n/a invoked when encountering a module instance

Data Structures

We define a set of structs storing the information of different components during parsing and we invoke your parser's member functions on those data structures.

Struct Port

The struct Port stores the information of a primary input/output of the module

Name Type Description
name std::vectorstd::string the names of ports
PortDirection enum class the direction of the port. The value could be either INPUT, OUTPUT or INOUT.
ConnectionType enum class the connection type of the port. The value could be either NONE, WIRE, REG.
beg, end int the bitwidth (range) of the port

Struct Net

The struct Net stores the information of a net declaration

Name Type Description
name std::vectorstd::string the names of nets
NetType enum class the type of a net. The value could be either NONE, REG, WIRE, WAND, WOR, TRI, TRIOR, TRIAND, SUPPLY0, SUPPLY1
beg, end int the bitwidth (range) of the net

Struct Assignment

The struct Assignment stores the information of an assignment statement

Name Type Description
lhs std::vector<std::variant<std::string, NetBit, NetRange>> the left hand side of the assignment statement
rhs std::vector<std::variant<std::string, NetBit, NetRange, Constant>> the right hand side of the assignment statement

Struct Instance

The struct Instance stores the information of a module instance

Name Type Description
module_name std::string the name of the module
inst_name std::string the name of the instance
pin_names std::vector<std::variant<std::string, NetBit, NetRange>> the input/output pins of the instance
net_names std::vector<std::vector<std::variant<std::string, NetBit, NetRange, Constant>>> the nets connecting to the pins

Struct Constant

The struct Constant records the possible number formats in Verilog

Name Type Description
value std::string the value of the number
ConstantType enum class the format of the number. The value could be NONE, INTEGER, BINARY, OCTAL, DECIMAL, HEX, REAL, EXP

Struct NetBit

The struct NetBit specifies a bit in a net

Name Type Description
name std::string the name of a net
bit int the index of the bit

Struct NetRange

The struct NetRange specifies a range in a net

Name Type Description
name std::string the name of a net
beg, end int the bit range of a net

Examples

The folder example contains several tutorial examples to demonstrate the usage of Parser-Verilog.

Example Description How to Run ?
sample_parser.cpp Read a Verilog and print the parsed data to screen ./sample_parser [file]
ot_parser.cpp A drop-in replacement Verilog parser for OpenTimer ./ot_parser [file]

License

Parser-Verilog is licensed under the MIT License:

Copyright © 2019 Chun-Xun Lin, Tsung-Wei Huang and Martin Wong

The University of Illinois at Urbana-Champaign, IL, USA

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


parser-verilog's People

Contributors

clin99 avatar gephaistos avatar tsung-wei-huang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

parser-verilog's Issues

Parse issues with delimiter.

I've found out that below wire definition with delimiter generated below problem.
Could you fix this issue?

For example, below two wire definition generated below logs.

wire \fifo_in/enq_i;
wire \fifo_in/tail_r;

These generated below error.

Failed to match : \
Parser error: syntax error, unexpected end of file, expecting ';' or ','
  begin at line 68 col 16
  end   at line 68 col 17
Aborted (core dumped)

OpenSTA's verilog parser could handle this, but ben-marshall's parser is not able to handle this...

Build error

Hi,
I am trying to build Parser-Verilog on my ubuntu system. I am getting the following error from make command.

/usr/bin/cmake -H/home/<...>/Parser-Verilog -B/home/<...>/Parser-Verilog/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/<...>/Parser-Verilog/build/CMakeFiles /home/<...>/Parser-Verilog/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/<...>/Parser-Verilog/build'
make -f CMakeFiles/regression.dir/build.make CMakeFiles/regression.dir/depend
make[2]: Entering directory '/home/<...>/Parser-Verilog/build'
[  5%] [FLEX][verilog_lexer] Building scanner with flex 2.6.4
cd /home/<...>/Parser-Verilog && /usr/bin/flex -o/home/<...>/Parser-Verilog/build/verilog_lexer.yy.cc /home/<...>/Parser-Verilog/parser-verilog/verilog_lexer.l
[ 11%] [BISON][verilog_parser] Building parser with bison 3.0.4
cd /home/<...>/Parser-Verilog && /usr/bin/bison -d -o /home/<...>/Parser-Verilog/build/verilog_parser.tab.cc /home/<...>/Parser-Verilog/parser-verilog/verilog_parser.yy
/home/<...>/Parser-Verilog/parser-verilog/verilog_parser.yy: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
/home/<...>/Parser-Verilog/parser-verilog/verilog_parser.yy:6.9-24: error: %define variable 'api.parser.class' is not used
 %define api.parser.class {VerilogParser}
         ^^^^^^^^^^^^^^^^
CMakeFiles/regression.dir/build.make:64: recipe for target 'verilog_parser.tab.cc' failed
make[2]: *** [verilog_parser.tab.cc] Error 1
make[2]: Leaving directory '/home/<...>/Parser-Verilog/build'
CMakeFiles/Makefile2:70: recipe for target 'CMakeFiles/regression.dir/all' failed
make[1]: *** [CMakeFiles/regression.dir/all] Error 2
make[1]: Leaving directory '/home/<...>/Parser-Verilog/build'
Makefile:97: recipe for target 'all' failed
make: *** [all] Error 2

Also attaching CMake log -

-- The CXX compiler identification is GNU 7.5.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found BISON: /usr/bin/bison (found version "3.0.4") 
-- Found FLEX: /usr/bin/flex (found version "2.6.4") 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/<...>/Parser-Verilog/build

Has anyone else encountered this error before ?

Out Of Tree Build Fails

Hi there,

I have cloned your repository, and found that the sequence of commands:

git clone https://github.com/OpenTimer/Parser-Verilog.git
cd Parser-Verilog
mkdir build
cd build
cmake ../
make

Fails to build.

It would appear that include path is not correct:

/home/mypath/Parser-Verilog/parser-verilog/verilog_scanner.hpp:8:10: fatal error: verilog_parser.tab.hh: No such file or directory
 #include "verilog_parser.tab.hh"

However, building in tree works fine.

My platform is Ubuntu 18.04.

Compile Issues

Thanks for really great work!
I've found two compile issues regarding with this Verilog parser.

  1. Flex 2.5.37 is compile-able, but flex 2.6.4 generated compile error.
    Below command,

     $ flex -o ./verilog_lexer.yy.cc parser-verilog/verilog_lexer.l 
    

    the verilog_lexer.yy.cc from Flex 2.5.37 is working for me, but the verilog_lexer.yy.cc from Flex 2.6.4 generated compile error.
    I've attached compiled verilog_lexer_yy.cc file just in case.
    ( Compile environment: Centos 7 / GCC 8.2.0 )
    verilog_lexer.zip

  2. The below command was working in my environments.

     $ g++ -std=c++17 -I./ -Iparser-verilog/ verilog_parser.tab.cc verilog_lexer.yy.cc example/sample_parser.cpp -o sample_parser -lstdc++fs
    

Publish to conan

Hi,

Would it be possible to publish the code to conan ?
I have tried modifying it to be publishable, but my conan-fu is miserable ...

escaped name that ends at end of line fails

If I have this structure, where the escaped name is the last item on the line.
wire \n_12 , \n_13
, \n_14 , \n_15 ;

I will get:
Failed to match :
Parser error: syntax error, unexpected end of file, expecting NAME or ESCAPED_NAME

The synthesis programs I use sometimes create the above structure.

If the line had the comma at the end, then it reads ok.
wire \n_12 , \n_13 ,
\n_14 , \n_15 ;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.