提升:37/37基准程序全量解析+O(N)路径枚举+运行时gcov验证

## 核心变更

### 1. 新PROCEDURE DIVISION解析器(procedure_parser.py)
- 行级状态机替换旧的BrParser regex解析器
- 覆盖:IF/ELSE/END-IF(嵌套)、EVALUATE/WHEN/ALSO、
  PERFORM UNTIL/VARYING、READ/AT END/NOT AT END、
  SORT/MERGE、GO TO DEPENDING ON
- 之前:3/37程序有分支检测  →  现在:37/37全部有分支
- 速度:~20ms/程序,纯规则引擎

### 2. 桥接层(pipeline_bridge.py)
- 新解析器为主,旧解析器3秒超时兜底
- 自动选取分支数更多的结果

### 3. 线性路径枚举(design_mcdc.py)
- 替换旧的Cartesian积路径枚举(O(2^N))为每决策点独立枚举(O(N))
- 28-sysin: 162分支仅163条路径(之前需截断到60DP)
- 消除了500路径硬上限和60DP截断

### 4. 条件解析修复(cond.py)
- NOT运算符规范化:X NOT = 5 → X <> 5
- 88-level反向:NOT WS-EOF-Y → parent <> value
- 裸字段引用:NOT WS-EOF → WS-EOF <> 'Y'
- 验证:1182个IF条件中0个NOT污染

### 5. 约束字段过滤(__init__.py)
- OF限定词剥离:STD-KEY OF MASTER-REC → STD-KEY
- 下标字段解析:WS-ITEM(SUB) → WS-ITEM
- 跳过不在fields_dict中的字段(group item/伪影)

### 6. 预处理器增强(read.py)
- VALUE ALL剥离(VALUE ALL '*' → VALUE '*')
- &续行合并(COBOL多行字符串拼接)
- PIC小数点点→V转换(Z(9)9.99. → Z(9)9V99.)
- 缺少点号补全

### 7. Grammar修复(grammar.lark)
- OCCURS 1 TIME支持(原只认TIMES)
- USAGE IS COMP支持(可选IS)
- $符号在PICTURE_STRING中
- 无NAME条款支持(clause+)

### 8. Flatfile写入(flatfile.py)
- 多记录FD支持(选字段最多的记录)
- Path类型强制转换
- 回退零值记录

### 9. Bug修复
- trace_to_root空列表保护(core.py)

### 10. 测试套件(S16-S21)
- S16: 全量基准程序端到端
- S17: gcov运行时对比
- S18/S19: 桥接器验证
- S20: DISPLAY插桩运行时验证+gcov分支覆盖率
- S21: 条件解析修复验证
- 全部17/17回归测试通过

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
NB-076
2026-06-22 23:41:22 +08:00
parent 097f5449da
commit e5ab3baa46
18 changed files with 2313 additions and 38 deletions
+56 -7
View File
@@ -43,6 +43,31 @@ def preprocess(source: str) -> str:
return re.sub(r'\s*,\s*', ' ', m.group(0))
source = re.sub(r'VALUE\s+[^.\n]+', _strip_value_commas, source, flags=re.IGNORECASE)
# Strip ALL from VALUE ALL (VALUE ALL '*.' → VALUE '*.')
source = re.sub(r'\bVALUE\s+ALL\b', 'VALUE', source, flags=re.IGNORECASE)
# Collapse &-concatenated VALUE continuation lines
# COBOL uses & to split long literals across lines:
# "............................" &
# "............................"
# Match: (quote/X'...') + " &" + newline + (quote/X'...')
source = re.sub(
r'([Xx]?["\'])\s*&\s*\n\s*([Xx]?["\'])',
lambda m: m.group(1) + m.group(2),
source
)
# Remove trailing & at end of lines (standalone continuation markers)
source = re.sub(r'&(?=[^"\']*$)', '', source, flags=re.MULTILINE)
# Convert PIC decimal dots to V (implied decimal) for Lark compatibility
# PIC Z(9)9.99. → PIC Z(9)9V99. (only within PIC clause before DOT)
source = re.sub(
r'(PIC\s+)([A-Z0-9(),\-*/V\$]+)\.(\d+)',
r'\1\2V\3',
source, flags=re.IGNORECASE
)
fixed = _is_fixed_format(source)
lines = []
for raw_line in source.splitlines():
@@ -67,9 +92,25 @@ def preprocess(source: str) -> str:
line = line.strip()
if not line:
continue
# Strip bare * comment lines in free format (after *> removal)
if line.startswith('*') and not line.startswith('*>'):
continue
content = line
lines.append(re.sub(r'\s+FALSE\s+[^\s.]+', '', content.upper()))
return '\n'.join(lines)
# Ensure DATA DIVISION lines with PIC/VALUE but no trailing DOT get one
# (handles COBOL programs where the period on a PIC clause is optional/omitted)
fixed_lines = []
for i, line in enumerate(lines):
stripped = line.strip()
if stripped and not stripped.endswith('.'):
# Lines inside DATA DIVISION that have PIC or VALUE but no DOT
if re.search(r'\b(PIC|VALUE|REDEFINES|OCCURS|USAGE)\b', stripped, re.IGNORECASE):
# Only fix if the NEXT line also looks like a data_item (level_num)
if i + 1 < len(lines) and re.match(r'^\s*(0[1-9]|[0-4][0-9]|49|66|77|88)\s', lines[i + 1]):
line = line.rstrip() + ' .'
fixed_lines.append(line)
return '\n'.join(fixed_lines)
def extract_data_division(source: str) -> str:
@@ -97,13 +138,18 @@ def extract_procedure_division(source: str) -> str:
_COPYBOOK_EXTENSIONS = ['.cpy', '.cbl', '.cpb', '']
def resolve_copybooks(source: str, source_dir: str, _recursion_depth: int = 0) -> str:
"""Find COPY statements and replace with copybook content."""
def resolve_copybooks(source: str, source_dir: str, _recursion_depth: int = 0,
extra_search_paths: list[str] = None) -> str:
"""Find COPY statements and replace with copybook content.
Searches from source_dir first, then extra_search_paths.
"""
_RE_COPY = re.compile(
r"^\s*COPY\s+(\w[\w-]*|\"[^\"]*\"|\'[^\']*\')(?:\s+REPLACING\s+(.+?))?\s*\.?\s*$",
re.IGNORECASE
)
_RE_PAIR = re.compile(r"==(.+?)==\s+BY\s+==(.+?)==", re.IGNORECASE)
search_dirs = [source_dir] + (extra_search_paths or [])
lines = source.split('\n')
result = []
@@ -113,10 +159,13 @@ def resolve_copybooks(source: str, source_dir: str, _recursion_depth: int = 0) -
raw_name = m.group(1)
name = raw_name.strip('"').strip("'").upper()
found = None
for ext in _COPYBOOK_EXTENSIONS:
p = Path(source_dir, name + ext)
if p.exists():
found = p
for sd in search_dirs:
for ext in _COPYBOOK_EXTENSIONS:
p = Path(sd, name + ext)
if p.exists():
found = p
break
if found:
break
if found:
if _recursion_depth > 10: