I’m doing my own implementation of the Parser.expression() grammar from ex33 of LMPTHW.
I’m noticing that, unlike all the other grammars which only return Productions, the Parser.expression() grammar may return either a single Token (of NAME or INTEGER type) or an Expression Production. This appears to depend on whether or not the expression contains an operator (ie PLUS).
Understandably, the simplest expression is a single variable or number, but it might be confusing later when we have to analyze both Token and Production objects in the Parser’s output. Wouldn’t it be better to come up with an Expression Production class for consistency? I’ve never built an analyzer, so I don’t know what makes more sense. I’m just thinking ahead.
Here is the sample code from the book:
def expression(tokens):
"""expression = name / plus / integer"""
start = peek(tokens)
if start == 'NAME':
name = match(tokens, 'NAME')
if peek(tokens) == 'PLUS':
return plus(tokens, name)
else:
return name
elif start == 'INTEGER':
number = match(tokens, 'INTEGER')
if peek(tokens) == 'PLUS':
return plus(tokens, number)
else:
return number
else:
assert False, "Syntax error %r" % start