This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
Project Description
Purple has two main parts:
  • parser
  • tree maker

Parser

Purple parser is dynamic as he can parse any non-left-recursive grammar. Grammar is part defined by the user and part which is necessary to create ParseText obj. Grammar is represented in form of a dict, eg:

grammar =  {"baseexpr" : [["mathop"]],
            "mathop": [["number","operator","mathop"],["number"]],
            "operator":[["plus"],["minus"]]}

2nd argument which is required to create ParseText obj is grammar’s start symbol. In this case “baseexpr” would be the one.

After creating ParseText obj

parser = ParserText(grammar, "baseexpr")

to check if some list of tokens (list of token type to be more precise) can be generated from specified grammar just call parse method (which returns True or False) with token list as its argument:

parser.parse(token_list)

During parsing process, parser is making trace which indicates how to build something that very much resembles to AST.

AST

Every symbol in a grammar should be represented with a class of its own. Depending if symbol is a “leaf” or a “node” (it’s a leaf if it doesn’t have production rule otherwise it’s a node) corresponding class should inherit from LeafNode or Node class.

Some “leaf” symbols obviously don’t have any semantic meaning like and so for them there is no need to be represented with a class.

Semantic meaning of each symbol is defined by overriding Node’s dooperation() method. For ‘leaf’ symbols if not overriden dooperation will return value of token associated with that particular symbol. So in our example for symbols mathop, operator and plus we would have:

import operator

class MathOpNode(Node):
        def dooperation(self):
        if len(self.childrens) == 3:
            op_func = self.childrens[1].dooperation()
            arg_1 = self.childrens[0].dooperation()
            arg_2 = self.childrens[2].dooperation()
            return op_func(arg_1,arg_2)
        else:
            return self.childrens[0].dooperation()

class OperatorNode(Node):
        def dooperation(self):
                return self.childrens[0].dooperation()

class PlusNode(LeafNode):
        dooperation():
                return operator.add

Final stage is to create dict with symbols as keys and theirs corresponding classes as values.:

nodes={
        "mathopnode" : MathOpNode,
        "operator" : OperatorNode,
        "plus" : PlusNode,
        .
        .
        .
}

To build AST(SDT) first create AST obj with token list, start node (object corresponding to start symbol), grammar and nodes as arguments ast = AST(token_list, start_node, grammar, nodes) and then call create_tree method with start symbol and trace (from parser) as arguments ast.create_tree(start_symbol, trace). After that tree is created and its root is tree_nodes’s first element.

Assuming you have defined semantics (with overriding dooperation()) for every symbol, to execute your source code you can just call ast.execute() , which calls dooperation() on the root of the ast.

Release History

Release History

0.1

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS HPE HPE Development Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting