def matchFactor(self, tokenName, tokenNameNeed=None): if (self.tokenList.token.tokenName) == tokenName: print( Style.green('Line({})-Pass '.format(self.tokenList.token.line)) + Style.reset('token {}'.format(self.tokenList.token))) self.tokenList.nextToken() else: if (tokenNameNeed): print( Style.red('Line({})-Pass Error '.format( self.tokenList.token.line)) + Style.reset('{} required.'.format(tokenNameNeed))) else: print( Style.red('Line({})-Pass Error '.format( self.tokenList.token.line)) + Style.reset('{} required.'.format(tokenName)))
def match(self, tokenAttributeValue): if (self.tokenList.token.attributeValue) == tokenAttributeValue: print( Style.green('Line({})-Match '.format( self.tokenList.token.line)) + 'token ({} {} {}) '.format( self.tokenList.token, Style.green('with'), tokenAttributeValue)) self.tokenList.nextToken() else: print( Style.red('Line({})-Syntax Error '.format( self.tokenList.token.line)) + Style.reset('{} required.'.format(tokenAttributeValue)))
def stmt(self): #grammer rule stmt --> eof if (self.tokenList.token.tokenName == 'eof'): return #grammer rule stmt --> if ( expr ) stmt if (self.tokenList.token.attributeValue == 'if'): self.match('if') self.match('leftparantheses') self.expr() self.match('rightparantheses') self.match('leftbracket') self.stmt() self.match('rightbracket') self.stmt() #grammer rule stmt --> while ( expr ) stmt elif (self.tokenList.token.attributeValue == 'while'): self.match('while') self.match('leftparantheses') self.expr() self.match('rightparantheses') self.stmt() #grammer rule stmt --> for ( optexpr; optexpr; optexpr ) stmt elif (self.tokenList.token.attributeValue == 'for'): self.match('for') self.match('leftparantheses') self.optexpr() self.match('semicolon') self.optexpr() self.match('semicolon') self.optexpr() self.match('rightparantheses') self.stmt() #grammer rule stmt --> { stmts } stmts elif (self.tokenList.token.attributeValue == 'leftbracket'): self.match('leftbracket') self.stmts() self.match('rightbracket') self.stmt() #grammer rule stmt --> # include < identifier .identifier? > stmt elif (self.tokenList.token.attributeValue == 'sharp'): self.match('sharp') self.match('include') self.match('LT') self.matchFactor('identifier') if (self.tokenList.token.tokenName == 'dot'): self.matchFactor('dot') self.matchFactor('identifier') self.match('GT') self.stmt() #grammer rule stmt --> do stmt while ( expr ); stmt elif (self.tokenList.token.attributeValue == 'do'): self.match('do') self.stmt() self.match('while') self.match('leftparantheses') self.expr() self.match('rightparantheses') self.match('semicolon') self.stmt() #grammer rule stmt --> type identifier ( params ) stmt #grammer rule stmt --> type identifier [ number ]; stmt #grammer rule stmt --> type identifier = expr; stmt elif (re.search('int|float|double', self.tokenList.token.attributeValue)): self.match(self.tokenList.token.attributeValue) self.matchFactor('identifier') if (self.tokenList.token.attributeValue == 'leftparantheses'): self.match('leftparantheses') self.params() self.match('rightparantheses') self.stmt() elif (self.tokenList.token.attributeValue == 'leftsqurebracket'): self.match('leftsqurebracket') self.matchFactor('number') self.match('rightsqurebracket') if (self.tokenList.token.attributeValue == 'EQ'): self.match('EQ') self.match('leftbracket') self.arrayNumber() self.match('rightbracket') self.match('semicolon') self.stmt() else: self.match('semicolon') self.stmt() elif (self.tokenList.token.attributeValue == 'EQ'): self.match('EQ') self.expr() self.match('semicolon') self.stmt() else: self.match('semicolon') self.stmt() #grammer rule stmt --> void identifier ( params ) stmt elif (self.tokenList.token.attributeValue == 'void'): self.match('void') self.matchFactor('identifier') self.match('leftparantheses') self.params() self.match('rightparantheses') self.stmt() #grammer rule stmt --> return expr ; stmt elif (self.tokenList.token.attributeValue == 'return'): self.match('return') self.expr() self.match('semicolon') self.stmt() #grammer rule stmt --> identifier = expr ; stmt elif (self.tokenList.token.tokenName == 'identifier'): self.matchFactor('identifier') self.match('EQ') self.expr() self.match('semicolon') self.stmt() #grammer rule stmt --> cout coutState stmt elif (self.tokenList.token.attributeValue == 'cout'): self.match('cout') self.coutState() self.stmt() #grammer rule stmt --> cin cinState stmt elif (self.tokenList.token.attributeValue == 'cin'): self.match('cin') self.cinState() self.stmt() else: print(Style.red('Syntax Error'))