当前位置: 首页>>代码示例>>Python>>正文


Python Scanner.next方法代码示例

本文整理汇总了Python中scanner.Scanner.next方法的典型用法代码示例。如果您正苦于以下问题:Python Scanner.next方法的具体用法?Python Scanner.next怎么用?Python Scanner.next使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在scanner.Scanner的用法示例。


在下文中一共展示了Scanner.next方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Parser

# 需要导入模块: from scanner import Scanner [as 别名]
# 或者: from scanner.Scanner import next [as 别名]
class Parser(object):

    def parse(self, sourceStr):
        self._completionMessage = "No errors"
        self._parseSuccessful = True
        self._scanner = Scanner(sourceStr)
        self._expression()
        self._accept(self._scanner.get(), Token.EOE,
                     "symbol after end of expression")
   
    def parseStatus(self):
        return self._completionMessage
    
    def _accept(self, token, expected, errorMessage):
        if token.getType() != expected:
            self._fatalError(token, errorMessage)

    def _fatalError(self, token, errorMessage):
        self._parseSuccessful = False
        self._completionMessage = "Parsing error -- " + \
                                  errorMessage + \
                                  "\nExpression so far = " + \
                                  self._scanner.stringUpToCurrentToken()
        raise Exception, self._completionMessage

    def _expression(self):
        """Syntax rule:
        expression = term { addingOperator term }  """           
        self._term()
        token = self._scanner.get()
        while token.getType() in (Token.PLUS, Token.MINUS):
            self._scanner.next()
            self._term()
            token = self._scanner.get()

    def _term(self):
        """Syntax rule:
        term = factor { multiplyingOperator factor }  """           
        self._factor()
        token = self._scanner.get()
        while token.getType() in (Token.MUL, Token.DIV):
            self._scanner.next()
            self._factor()
            token = self._scanner.get()

    def _factor(self):
        """Syntax rule:
        factor = number | "(" expression ")  """"          
        token = self._scanner.get()
        if token.getType() == Token.INT:
            self._scanner.next()
        elif token.getType() == Token.L_PAR:
            self._scanner.next()
            self._expression()
            self._accept(self._scanner.get(),
                         Token.R_PAR,
                         "')' expected")
            self._scanner.next()
        else:
            self._fatalError(token, "bad factor")
开发者ID:gregpuzzles1,项目名称:Sandbox,代码行数:62,代码来源:parsers.py

示例2: Parser

# 需要导入模块: from scanner import Scanner [as 别名]
# 或者: from scanner.Scanner import next [as 别名]
class Parser(object):

    def parse(self, sourceStr):
        self._completionMessage = "No errors"
        self._parseSuccessful = True
        self._scanner = Scanner(sourceStr)
        tree = self._expression()
        self._accept(self._scanner.get(), Token.EOE,
                     "symbol after end of expression")
        return tree
   
    def parseStatus(self):
        return self._completionMessage
    
    def _accept(self, token, expected, errorMessage):
        if token.getType() != expected:
            self._fatalError(token, errorMessage)

    def _fatalError(self, token, errorMessage):
        self._parseSuccessful = False
        self._completionMessage = "Parsing error -- " + \
                                  errorMessage + \
                                  "\nExpression so far = " + \
                                  self._scanner.stringUpToCurrentToken()
        raise Exception(self._completionMessage)

    def _expression(self):
        tree = self._term()
        token = self._scanner.get()
        while token.getType() in (Token.PLUS, Token.MINUS):
            op = str(token)
            self._scanner.next()
            tree = InteriorNode(op, tree, self._term())
            token = self._scanner.get()
        return tree

    def _term(self):
        tree = self._factor()
        token = self._scanner.get()
        while token.getType() in (Token.MUL, Token.DIV):
            op = str(token)
            self._scanner.next()
            tree = InteriorNode(op, tree, self._factor())
            token = self._scanner.get()
        return tree

    def _factor(self):
        token = self._scanner.get()
        if token.getType() == Token.INT:
            tree = LeafNode(token.getValue())
            self._scanner.next()
        elif token.getType() == Token.L_PAR:
            self._scanner.next()
            tree = self._expression()
            self._accept(self._scanner.get(),
                         Token.R_PAR,
                         "')' expected")
            self._scanner.next()
        else:
            tree = None
            self._fatalError(token, "bad factor")
        return tree
开发者ID:QaziPython,项目名称:CS-Books,代码行数:64,代码来源:parsers.py

示例3: format

# 需要导入模块: from scanner import Scanner [as 别名]
# 或者: from scanner.Scanner import next [as 别名]
 def format(self, sourceStr):
     normalizedStr = ""
     scanner = Scanner(sourceStr);
     while scanner.hasNext():
         normalizedStr += str(scanner.next()) + " "
     return normalizedStr;   
开发者ID:gregpuzzles1,项目名称:Sandbox,代码行数:8,代码来源:model.py


注:本文中的scanner.Scanner.next方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。