Lesson 6 of 79 minBeginner

Parsing Reports

Strings, regexp, and reading files

A huge part of real EDA scripting is pulling numbers out of tool reports: how many violations, what is the worst slack, total power. This means string handling and file reading.

String basics

tcl
set line "WNS = -0.123 ns"
puts [string length $line]            ;# length
puts [string match "*WNS*" $line]     ;# 1 if it contains WNS
puts [string trim "  spm  "]          ;# -> spm (trims spaces)

Pulling out a number with regexp

Regular expressions extract exactly the piece you want. Here we grab the slack value from a report line.

tcl
set line "WNS = -0.123 ns"
if {[regexp {WNS = (\S+)} $line -> wns]} {
  puts "Worst negative slack is $wns"   ;# -0.123
}
Report scraping pipeline: read a line, match with regexp, capture the value (click to enlarge)

Reading a report file line by line

tcl
set viol 0
set fh [open "timing.rpt" r]
while {[gets $fh line] >= 0} {       ;# gets returns -1 at end
  if {[string match "*VIOLATED*" $line]} {
    incr viol
  }
}
close $fh
puts "Found $viol violated paths"

Writing a file

tcl
set out [open "summary.txt" w]
puts $out "Violations: $viol"
close $out
Pro tip

regexp with a capture group in parentheses, written to a variable after the ->, is the everyday tool for scraping numbers from reports. Combined with reading a file line by line, you can turn any tool report into a one-line summary or a pass/fail check.

Watch out

always close a file you open, especially in a loop, or you will run out of file handles on a long run. Open, use, close - every time.

Finished reading?

Mark it complete to track your progress through the path.

Get new lessons by email

New lessons, interview prep and real EDA tool workflows. No spam.

PrevNext