1

I am trying write a parser code for following production rule.

Statement: Variable+ | epsilon

My initial implementation:

'''
Statement: Variables |  epsilon
Variables: Variable Variables | Variable  (one or more variable) 
epsilon: null ('')
''' 
*** Variable is already implemented***
@_('Variables')
def Statement(self,p):
   return p.Variables

@_('Variable Variables', 'Variable')
def Variables(self,p):
   return p.Variable 

@_('')
def epsilon(self,p):
   pass

@_('epsilon')
def Statement(self,p):
    return p

I get following error:

WARNING: Symbol 'epsilon' is unreachable.

WARNING: Symbol 'Statement' is unreachable

Tom
  • 21
  • 5

1 Answers1

0

Without having your complete code on-hand, I suspect that what has happened is that you defined "Variable" earlier than "Statement". As per the documentation:

The very first grammar rule defines the top of the parse (the first rule listed in a BNF grammar).

Indeed, if I take the calc example shipped with the program, and add an additional rule before "statement", even one that is used by "statement", I get very similar errors to what you have listed, since Sly can't figure out how to get to "statement" from that first grammar rule.

Added Rules

@_('ASSIGN ASSIGN')
def variable(self, p):
    pass

@_('variable')
def statement(self, p):
    self.names[p.NAME] = p.variable

Error on running

WARNING: C:\dev\sly-master\example\calc\calc.py:62: Rule 'statement' defined, but not used
WARNING: There is 1 unused rule
WARNING: Symbol 'statement' is unreachable
WARNING: Symbol 'expr' is unreachable

Sean Duggan
  • 1,105
  • 2
  • 18
  • 48