Skip to content

Instantly share code, notes, and snippets.

@nomaddo
Created August 5, 2018 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nomaddo/6061a62d17ff07079b9758066ed867c2 to your computer and use it in GitHub Desktop.
Save nomaddo/6061a62d17ff07079b9758066ed867c2 to your computer and use it in GitHub Desktop.
grammar Debug;
prog returns [v]
: additive {$v = additive.v};
additive returns [v] :
l=additive s='+' r=primary {$v = $l.v + $r.v
print($v)}
| p=primary {$v = $p.v}
;
primary returns [v] :
n=number {
$v = int($n.text)
}
;
number returns [v] :
n=INT {$v = int($n)}
;
INT : '0' | [1-9] [0-9]* ;
WS : [ \t\r\n]+ -> skip;
def additive(self, _p:int=0):
_parentctx = self._ctx
_parentState = self.state
localctx = DebugParser.AdditiveContext(self, self._ctx, _parentState)
_prevctx = localctx
_startState = 2
self.enterRecursionRule(localctx, 2, self.RULE_additive, _p)
try:
self.enterOuterAlt(localctx, 1)
self.state = 12
localctx.p = self.primary()
localctx.v = localctx.p.v
self._ctx.stop = self._input.LT(-1)
self.state = 22
self._errHandler.sync(self)
_alt = self._interp.adaptivePredict(self._input,0,self._ctx)
while _alt!=2 and _alt!=ATN.INVALID_ALT_NUMBER:
if _alt==1:
if self._parseListeners is not None:
self.triggerExitRuleEvent()
_prevctx = localctx
localctx = DebugParser.AdditiveContext(self, _parentctx, _parentState)
localctx.l = _prevctx
self.pushNewRecursionContext(localctx, _startState, self.RULE_additive)
self.state = 15
if not self.precpred(self._ctx, 2):
from antlr4.error.Errors import FailedPredicateException
raise FailedPredicateException(self, "self.precpred(self._ctx, 2)")
self.state = 16
localctx.s = self.match(DebugParser.T__0)
self.state = 17
localctx.r = self.primary()
localctx.v = localctx.l.v + localctx.r.v
print(localctx.v)
self.state = 24
self._errHandler.sync(self)
_alt = self._interp.adaptivePredict(self._input,0,self._ctx)
except RecognitionException as re:
localctx.exception = re
self._errHandler.reportError(self, re)
self._errHandler.recover(self, re)
finally:
self.unrollRecursionContexts(_parentctx)
return localctx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment