I am parsing a verilog file to extract the dependencies within it. I don’t need to parse most of the module contents, but am only interested in include statements and module instantiations. I first am attempting to only extract the include statements. This is my code so far:
include_pragma = Group(Keyword("`include") + quotedString + lineEnd.suppress())
module_definition = ZeroOrMore(~Keyword("endmodule") + MatchFirst([include_pragma, restOfLine])) + Keyword("endmodule")
My point is that I will either match an include pragma, or a line of anything until I reach “endmodule”. If I try this grammer on the following string, pyparsing gets into some kind of infinite loop.
`include "InternalInclude.v"
localparam COMMA_WIDTH = 10;
localparam UNKNOWN = 1'b0,
KNOWN = 1'b1;
reg [DATA_WIDTH-1:0] TtiuPosQ2;
reg TtiuDetQ2;
reg [ 7:0] TtiuDetQ2Save;
assign DebugQO = { DataQI, // 65:34
TxTrainingEnQI, // 33
TtiuDetQ2, // 32
TtiuPosQ2 // 31:0
};
always @(posedge Clk or posedge ResetI) begin
if (ResetI) begin
CommaPosQO <= {(DATA_WIDTH){1'b0}};
CommaDetQO <= 0;
CommaPosQ1 <= {(DATA_WIDTH){1'b0}};
CommaPosQ2 <= {(DATA_WIDTH){1'b0}};
CommaDetQ2 <= 0;
StateQ <= 0;
CommaPosSaveQ <= {(DATA_WIDTH){1'b0}};
TtiuPosQ1 <= 0;
TtiuPosQ2 <= 0;
TtiuDetQ2 <= 0;
TtiuDetQ2Save <= 8'h00;
end
else begin
CommaPosQO <= CommaPosC2;
CommaDetQO <= CommaDetC2;
CommaPosQ1 <= CommaPosC;
CommaPosQ2 <= CommaPosQ1;
CommaDetQ2 <= (| CommaPosQ1);
StateQ <= StateC;
CommaPosSaveQ <= CommaPosSaveC;
TtiuPosQ1 <= TtiuPosC1;
TtiuPosQ2 <= TtiuPosC2;
TtiuDetQ2 <= TtiuDetC2;
TtiuDetQ2Save <= TtiuDetQ2 ? 8'hFF : {1'b0, TtiuDetQ2Save[7:1]};
end
end
endmodule
I am probably misunderstanding the ~ operator. Any suggestions?
update
After using the setDebug() method suggested, I found that the infinite loops prints this:
Match {Group:({"`include" quotedString using single or double quotes Suppress:(lineEnd)}) | Re:('.*')} at loc 0(1,1)
Matched {Group:({"`include" quotedString using single or double quotes Suppress:(lineEnd)}) | Re:('.*')} -> [['`include'
, '"InternalInclude.v"']]
Match {Group:({"`include" quotedString using single or double quotes Suppress:(lineEnd)}) | Re:('.*')} at loc 31(4,1)
Matched {Group:({"`include" quotedString using single or double quotes Suppress:(lineEnd)}) | Re:('.*')} -> ['']
Match {Group:({"`include" quotedString using single or double quotes Suppress:(lineEnd)}) | Re:('.*')} at loc 31(4,1)
Matched {Group:({"`include" quotedString using single or double quotes Suppress:(lineEnd)}) | Re:('.*')} -> ['']
Match {Group:({"`include" quotedString using single or double quotes Suppress:(lineEnd)}) | Re:('.*')} at loc 31(4,1)
Matched {Group:({"`include" quotedString using single or double quotes Suppress:(lineEnd)}) | Re:('.*')} -> ['']
Is something that I do causing the parse position not to move forward?
The problem was with using the restOfLine expression. It can match anything, including ”. So, the parser would proceed as follows:
include "InternalInclude.v"To fix it, I changed
restOfLineto(restOfLine + lineEnd). This forced the parser to consume the \n after matching the line. The new parser reads: