Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SystemVerilog Preprocessor directives cause ast to be empty #4295

Open
ccatrett opened this issue Oct 28, 2024 · 1 comment
Open

SystemVerilog Preprocessor directives cause ast to be empty #4295

ccatrett opened this issue Oct 28, 2024 · 1 comment

Comments

@ccatrett
Copy link

Minimal examples
Consider the following .svh files:

`ifndef ___SYS_DEFS_SVH__
`define ___SYS_DEFS_SVH__
`timescale 1ns/100ps
`define FALSE 1'h0
`define TRUE 1'h1
typedef logic [31:0] ADDR;
`endif
`define ___SYS_DEFS_SVH__
`timescale 1ns/100ps
`define FALSE 1'h0
`define TRUE 1'h1

`ifdef ___SYS_DEFS_SVH__
    typedef logic [31:0] ADDR;
`endif

Both files result in the following AST: (source_text <EOF>)

This should not be the case for either of these two files.

I believe from viewing the .g4 files, it seems like there is support for preprocessor definitions.

Here is the Python script I am running to generate this output:

import sys

from antlr4 import *
from SystemVerilogLexer import SystemVerilogLexer
from SystemVerilogParser import SystemVerilogParser

def main(input_file):
    input_stream = FileStream(input_file)
    lexer = SystemVerilogLexer(input_stream)
    token_stream = CommonTokenStream(lexer)
    parser = SystemVerilogParser(token_stream)

    tree = parser.source_text()

    print(tree.toStringTree(recog=parser))

if __name__ == '__main__':
    input_file = sys.argv[1]
    main(input_file)

I would appreciate any help to get these cases working.
Thank you.

@msagca
Copy link
Contributor

msagca commented Oct 28, 2024

Hi @ccatrett,
Tokens associated with the preprocessor directives are stored in a separate channel. If the input only consists of directives, the default channel will contain nothing (except the EOF token) and you will see an empty syntax tree. You need to specify the directives channel by its ID when creating a token stream and pass it to a SystemVerilogPreParser instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants