Combining similar alternative grammer definitions
16:29 06 Dec 2025

I am attempting to scan specific names from a CDL file (a variant of SPICE) which is an old line oriented syntax. Superficially, regex would work, but is in fact more complex given the full language definition.

The basic issue is that there are two types of line syntax which can yield the required name.

However the grammar of the first is embedded in the grammar of the second. This gives false results.

I am new to parsing and am curious about how to resolve this.

In the simplified example, I am attempting to parse mname1 and mname2.

it produces:

mname1
B2 # false detection
mname2

from pyparsing import *
import re
import argparse

cdl = """
A1 B1 C1 mname1 X=1 Y=2
A2 B2 X=2 SUB [mname2] C2
"""
# define grammar for the model names ------------------------------------------

ident = Word(alphas,alphanums)
intgr = Word(nums)
paramAssign = Combine(ident + Literal('=') + intgr)

modelExtract1 =             \
  ident('modelName') +      \
  OneOrMore(paramAssign )
  
modelExtract2 =             \
  Literal('SUB') +          \
  Literal('[') +            \
  ident('modelName')
  
modelExtract = modelExtract1 | modelExtract2

lines = cdl.splitlines()
      
for line in lines:
  for match, start, stop in modelExtract.scanString(line):
    print(match.modelName)
python pyparsing