Created
August 5, 2018 14:16
-
-
Save nomaddo/6061a62d17ff07079b9758066ed867c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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