Exemple #1
0
 def grammar(self):
     if not self.bnf:
         point = Literal( "." )
         fnumber = Combine( Word( nums ) + 
                            Optional( point + Optional( Word( nums ) ) ) 
                            )
         ident = Word(alphas.lower()+"_", alphanums+"_")
         plus  = Literal( "+" )
         minus = Literal( "-" )
         mult  = Literal( "*" )
         # passiverate = Word('infty') | Word('T')
         div   = Literal( "/" )
         lpar  = Literal( "(" ).suppress()
         rpar  = Literal( ")" ).suppress()
         addop  = plus | minus
         multop = mult | div
         assign = Literal('=')
         expop = Literal( "^" )
         expr = Forward()
         atom = Optional("-") + ( fnumber | ident + lpar + expr + rpar | ident).setParseAction(self._pushFirst ) | lpar + expr.suppress() + rpar 
         factor = Forward()
         factor << atom + ZeroOrMore( ( expop + factor )
                 .setParseAction(self._pushFirst) )
         term = factor + ZeroOrMore( ( multop + factor )
                 .setParseAction(self._pushFirst) )
         expr << term + ZeroOrMore( ( addop + term )
                 .setParseAction(self._pushFirst ) )
         bnf = (ident + assign).setParseAction(self._assignVar) + expr 
         self.bnf = bnf
     return self.bnf
Exemple #2
0
    def grammar(self):
        prefix_op = Literal('.')
        parallel = Literal('|')
        comm_op = Literal('||')
        choice_op = Literal('+')
        colon = Literal(':')
        loca_sym = Literal('@').suppress()
        lpar = Literal('(').suppress()
        rpar = Literal(')').suppress()
        pound = Literal('#').suppress()
        slash = Literal('/')
        # 8
        lbra = Literal('{').suppress()
        rbra = Literal('}').suppress()
        define = Literal('=')
        Ident = Word(alphas.upper(), alphanums + "_")
        ident = Word(alphas.lower(), alphanums + "_")
        pragma = Optional(pound + Literal('language') + colon + 'ras')
        # pseudo uri: /a/s/s/s/s/ or /a/ or /a/s/s/s
        puri = Combine(slash + ident + ZeroOrMore(slash + ident) + Optional(slash))
        loca = loca_sym + puri 

        comm = Forward()
        choice = Forward()
        prefix = Forward()

        process = ident | loca + lbra + choice + rbra | lpar + choice + rpar
        prefix << Group(process + ZeroOrMore(prefix_op + prefix) )
        choice << Group(prefix + ZeroOrMore(choice_op + prefix))
        comm << Group(choice + ZeroOrMore(comm_op + comm ))
        rmdef = Ident + define + loca+ lbra + comm + rbra
        ras = pragma + rmdef

        return ras
Exemple #3
0
def parse_weak_order(s: str) -> list:
    """
    Convert a string representing a weak order to a list of sets.

    :param s: a string.
    :return: a list of sets, where each set is an indifference class. The first set of the list contains the top
        (= most liked) candidates, while the last set of the list contains the bottom (= most disliked) candidates.

    >>> s = 'Alice ~ Bob ~ Catherine32 > me > you ~ us > them'
    >>> parse_weak_order(s) == [{'Alice', 'Bob', 'Catherine32'}, {'me'}, {'you', 'us'}, {'them'}]
    True
    """

    # Build the parser
    candidate = Word(alphas.upper() + alphas.lower() + nums + '_')
    equiv_class = Group(candidate +
                        ZeroOrMore(Word('~').suppress() + candidate))
    weak_preference = equiv_class + ZeroOrMore(
        Word('>').suppress() + equiv_class)
    empty_preference = ZeroOrMore(' ')

    # if s = 'Jean ~ Titi ~ tata32 > me > you ~ us > them', then
    # parsed = [['Jean', 'Titi', 'tata32'], ['me'], ['you', 'us'], ['them']]
    try:
        parsed = empty_preference.parseString(s, parseAll=True).asList()
    except ParseException:
        parsed = weak_preference.parseString(s, parseAll=True).asList()

    # Final conversion to format [{'Jean', 'tata32', 'Titi'}, {'me'}, {'us', 'you'}, {'them'}]
    return [NiceSet(s) for s in parsed]
Exemple #4
0
def gramma():
    ## Tokens
    point = Literal('.')
    prefix_op = Literal('.')
    choice_op = Literal('+')
    parallel = Literal("||") | Literal("<>")
    #ident = Word(alphas, alphanums+'_')
    ratename = Word(alphas.lower(), alphanums + "_")
    lpar = Literal('(').suppress()
    rpar = Literal(')').suppress()
    lsqpar = Literal('[').suppress()
    rsqpar = Literal(']').suppress()

    define = Literal('=')
    semicol = Literal(';').suppress()
    col = Literal(',').suppress()
    number = Word(nums)
    integer = number
    floatnumber = Combine(integer + Optional(point + Optional(number)))
    passiverate = Word('infty') | Word('T')
    internalrate = Word('tau')
    pound = Literal('#').suppress()
    percent = Literal('%').suppress()
    peparate = (ratename | floatnumber | internalrate
                | passiverate).setParseAction(_check_var)
    peparate_indef = floatnumber | internalrate | passiverate
    sync = Word('<').suppress() + ratename + ZeroOrMore(col + ratename) + Word(
        '>').suppress()
    coop_op = (parallel | sync).setParseAction(_create_sync_set)
    activity = (ratename + col + peparate).setParseAction(_create_activity)
    procdef = (Word(alphas.upper(), alphanums + "_") +
               Optional(lsqpar + peparate_indef +
                        rsqpar)).setParseAction(_create_procdef)
    ## RATES Definitions
    ratedef = (Optional(percent) + ratename + define +
               peparate_indef).setParseAction(assign_var) + semicol

    prefix = Forward()
    choice = Forward()
    coop = Forward()

    process = (activity
               | procdef
               | lpar + coop + rpar).setParseAction(_create_process)
    prefix << (process +
               ZeroOrMore(prefix_op + prefix)).setParseAction(_create_prefix)
    choice << (prefix +
               ZeroOrMore(choice_op + choice)).setParseAction(_create_choice)
    coop << (choice + ZeroOrMore(coop_op + coop)).setParseAction(_create_coop)
    rmdef = (Optional(pound) + procdef + define + coop +
             semicol).setParseAction(_create_definition)
    system_eq = Optional(pound) + coop
    pepa = ZeroOrMore(ratedef) + ZeroOrMore(rmdef) + system_eq.setParseAction(
        _create_system_equation)
    pepacomment = '//' + restOfLine
    pepa.ignore(pepacomment)
    return pepa
Exemple #5
0
def parser(filename=None):
    """
    Generate a callable parser for each file. The filename is necessary to get better Error messages.

    :param filename: Name of the file, on which the parser will be applied
    :type filename: str
    :return: A parser for the file on filename
    """
    line_comment = Group(Suppress("--") + restOfLine + SkipTo(LineEnd()))("SingleLineComment")
    block_comment = nestedExpr("{-", "-}")("MultiLineComment")
    comment = Group(line_comment | block_comment)
    module_name = Group(delimitedList(Word(alphas.upper(),alphas),"."))("Module Name")
    module_exports = Group(Keyword("..") | Group(delimitedList(Word(printables, excludeChars=',)'))))("Module Exports")
    module_declaration = Group(
        Suppress("module").setFailAction(e.ModuleDeclarationMissingException(filename)) +
        module_name +
        Suppress("exposing") +
        Suppress("(") +
        module_exports+Suppress(")"))("Module Declaration")

    LPAR, RPAR = map(Suppress, '()')
    integer = Word(nums)
    real = Optional(Word(nums)) + '.' + Word(nums)
    expr = Forward()
    operand = Optional("-") + (real | integer)
    factor = operand | Group(LPAR + expr + RPAR)
    term = factor + ZeroOrMore(oneOf('* /') + factor)
    expr <<= term + ZeroOrMore(oneOf('+ -') + term)

    function_name = Word(alphas.lower())
    function_parameter = Word(alphas.upper()+alphas.lower())
    function_parameters = ZeroOrMore(function_parameter)("Function Parameter")
    function_body = expr
    function_declaration = Group(function_name + function_parameters + Suppress("=") + function_body)\
        ("Function Declaration")

    return module_declaration + ZeroOrMore(function_declaration | comment)("Body")
Exemple #6
0
def gramma():
## Tokens
    point = Literal('.')
    prefix_op = Literal('.')
    choice_op = Literal('+')
    parallel = Literal("||") | Literal("<>")
#ident = Word(alphas, alphanums+'_')
    ratename = Word(alphas.lower(),alphanums+"_")
    lpar = Literal('(').suppress()
    rpar = Literal(')').suppress()
    lsqpar = Literal('[').suppress()
    rsqpar = Literal(']').suppress()

    define = Literal('=')
    semicol = Literal(';').suppress()
    col = Literal(',').suppress()
    number = Word(nums)
    integer = number
    floatnumber = Combine( integer + Optional( point + Optional(number)))
    passiverate = Word('infty') | Word('T')
    internalrate = Word('tau')
    pound = Literal('#').suppress()
    percent = Literal('%').suppress()
    peparate = (ratename | floatnumber | internalrate | passiverate).setParseAction(_check_var)
    peparate_indef = floatnumber | internalrate | passiverate
    sync = Word('<').suppress() + ratename + ZeroOrMore(col + ratename) + Word('>').suppress()
    coop_op = (parallel | sync).setParseAction(_create_sync_set)
    activity = (ratename + col + peparate).setParseAction(_create_activity)
    procdef = (Word(alphas.upper(), alphanums+"_") + Optional(lsqpar + peparate_indef + rsqpar)).setParseAction(_create_procdef)
## RATES Definitions
    ratedef = (Optional(percent)+ratename + define + peparate_indef).setParseAction(assign_var) + semicol

    prefix = Forward()
    choice = Forward()
    coop = Forward()

    process = ( activity
             | procdef
             | lpar + coop + rpar
            ).setParseAction(_create_process)
    prefix  << (process + ZeroOrMore(prefix_op + prefix)).setParseAction( _create_prefix)
    choice << (prefix + ZeroOrMore(choice_op + choice)).setParseAction(_create_choice)
    coop << (choice + ZeroOrMore(coop_op + coop)).setParseAction(_create_coop)
    rmdef = (Optional(pound) + procdef + define + coop + semicol).setParseAction(_create_definition)
    system_eq =  Optional(pound) + coop
    pepa = ZeroOrMore(ratedef)  + ZeroOrMore(rmdef) + system_eq.setParseAction(_create_system_equation)
    pepacomment = '//' + restOfLine
    pepa.ignore(pepacomment)
    return pepa
# http://www.antlr.org/grammar/ANTLR/ANTLRv3.g

# Tokens
EOL = Suppress(LineEnd()) # $
singleTextString = originalTextFor(ZeroOrMore(~EOL + (White(" \t") | Word(printables)))).leaveWhitespace()
XDIGIT = hexnums
INT = Word(nums)
ESC = Literal('\\') + (oneOf(list(r'nrtbf\">'+"'")) | ('u' + Word(hexnums, exact=4)) | Word(printables, exact=1))
LITERAL_CHAR = ESC | ~(Literal("'") | Literal('\\')) + Word(printables, exact=1)
CHAR_LITERAL = Suppress("'") + LITERAL_CHAR + Suppress("'")
STRING_LITERAL = Suppress("'") + Combine(OneOrMore(LITERAL_CHAR)) + Suppress("'") 
DOUBLE_QUOTE_STRING_LITERAL = '"' + ZeroOrMore(LITERAL_CHAR) + '"'
DOUBLE_ANGLE_STRING_LITERAL = '<<' + ZeroOrMore(Word(printables, exact=1)) + '>>'
TOKEN_REF = Word(alphas.upper(), alphanums+'_')
RULE_REF = Word(alphas.lower(), alphanums+'_')
ACTION_ESC = (Suppress("\\") + Suppress("'")) | Suppress('\\"') | Suppress('\\') + (~(Literal("'") | Literal('"')) + Word(printables, exact=1))
ACTION_CHAR_LITERAL = Suppress("'") + (ACTION_ESC | ~(Literal('\\') | Literal("'")) + Word(printables, exact=1)) + Suppress("'")
ACTION_STRING_LITERAL = Suppress('"') + ZeroOrMore(ACTION_ESC | ~(Literal('\\') | Literal('"')) + Word(printables, exact=1)) + Suppress('"') 
SRC = Suppress('src') + ACTION_STRING_LITERAL("file") + INT("line")
id = TOKEN_REF | RULE_REF
SL_COMMENT = Suppress('//') + Suppress('$ANTLR') + SRC | ZeroOrMore(~EOL + Word(printables)) + EOL
ML_COMMENT = cStyleComment
WS = OneOrMore(Suppress(' ') | Suppress('\t') | (Optional(Suppress('\r')) + Literal('\n')))
WS_LOOP = ZeroOrMore(SL_COMMENT | ML_COMMENT)
NESTED_ARG_ACTION = Forward()
NESTED_ARG_ACTION << Suppress('[') + ZeroOrMore(NESTED_ARG_ACTION | ACTION_STRING_LITERAL | ACTION_CHAR_LITERAL) + Suppress(']')
ARG_ACTION = NESTED_ARG_ACTION
NESTED_ACTION = Forward()
NESTED_ACTION << Suppress('{') + ZeroOrMore(NESTED_ACTION | SL_COMMENT | ML_COMMENT | ACTION_STRING_LITERAL | ACTION_CHAR_LITERAL) + Suppress('}')
ACTION = NESTED_ACTION + Optional('?')
from pyparsing import Word, Optional, OneOrMore, Group, ParseException, Regex
from pyparsing import alphas

atomicWeight = {
    "O": 15.9994,
    "H": 1.00794,
    "Na": 22.9897,
    "Cl": 35.4527,
    "C": 12.0107,
}

digits = "0123456789"

# Version 1
element = Word(alphas.upper(), alphas.lower(), max=2)
# for stricter matching, use this Regex instead
# element = Regex("A[cglmrstu]|B[aehikr]?|C[adeflmorsu]?|D[bsy]|"
#                 "E[rsu]|F[emr]?|G[ade]|H[efgos]?|I[nr]?|Kr?|L[airu]|"
#                 "M[dgnot]|N[abdeiop]?|Os?|P[abdmortu]?|R[abefghnu]|"
#                 "S[bcegimnr]?|T[abcehilm]|U(u[bhopqst])?|V|W|Xe|Yb?|Z[nr]")
elementRef = Group(element + Optional(Word(digits), default="1"))
formula = OneOrMore(elementRef)

fn = lambda elemList: sum(atomicWeight[elem] * int(qty)
                          for elem, qty in elemList)
formula.runTests(
    """\
    H2O
    C6H5OH
    NaCl
    ZeroOrMore(~EOL + (White(" \t") | Word(printables)))).leaveWhitespace()
XDIGIT = hexnums
INT = Word(nums)
ESC = Literal('\\') + (oneOf(list(r'nrtbf\">' + "'")) |
                       ('u' + Word(hexnums, exact=4))
                       | Word(printables, exact=1))
LITERAL_CHAR = ESC | ~(Literal("'") | Literal('\\')) + Word(printables,
                                                            exact=1)
CHAR_LITERAL = Suppress("'") + LITERAL_CHAR + Suppress("'")
STRING_LITERAL = Suppress("'") + Combine(
    OneOrMore(LITERAL_CHAR)) + Suppress("'")
DOUBLE_QUOTE_STRING_LITERAL = '"' + ZeroOrMore(LITERAL_CHAR) + '"'
DOUBLE_ANGLE_STRING_LITERAL = '<<' + ZeroOrMore(Word(printables,
                                                     exact=1)) + '>>'
TOKEN_REF = Word(alphas.upper(), alphanums + '_')
RULE_REF = Word(alphas.lower(), alphanums + '_')
ACTION_ESC = (Suppress("\\") + Suppress("'")) | Suppress('\\"') | Suppress(
    '\\') + (~(Literal("'") | Literal('"')) + Word(printables, exact=1))
ACTION_CHAR_LITERAL = Suppress("'") + (
    ACTION_ESC | ~(Literal('\\') | Literal("'")) +
    Word(printables, exact=1)) + Suppress("'")
ACTION_STRING_LITERAL = Suppress(
    '"') + ZeroOrMore(ACTION_ESC | ~(Literal('\\') | Literal('"')) +
                      Word(printables, exact=1)) + Suppress('"')
SRC = Suppress('src') + ACTION_STRING_LITERAL("file") + INT("line")
id = TOKEN_REF | RULE_REF
SL_COMMENT = Suppress('//') + Suppress('$ANTLR') + SRC | ZeroOrMore(
    ~EOL + Word(printables)) + EOL
ML_COMMENT = cStyleComment
WS = OneOrMore(
    Suppress(' ') | Suppress('\t')
Exemple #10
0
#! /usr/bin/env python3

from collections import namedtuple

from pyparsing import (
    alphanums, alphas, Combine, delimitedList, Forward, Group, infixNotation,
    Keyword, oneOf, opAssoc, Optional, Suppress, Word, ParserElement)
from pyparsing import pyparsing_common as ppc

ParserElement.enablePackrat()

LPAR, RPAR, LBRAK, RBRAK, COMMA = map(Suppress, "()[],")
kws = oneOf("and or not id true false forall exists")
BUILTIN = oneOf("abs max min not")
VARNAME = Word(alphas.lower(), alphanums + "_").ignore(kws).ignore(BUILTIN)
TYPENAME = Word(alphas.upper(), alphanums + "_").ignore(kws).ignore(BUILTIN)
EXTERN = Combine(Suppress("_") + VARNAME).setParseAction(lambda toks: toks[0])

# AST nodes for ATLAS properties
EnvNode = namedtuple("Env", ["var", "offset"])
OfNode = namedtuple("Of", ["var", "offset", "agent"])
BinOp = namedtuple("BinOp", ["e1", "op", "e2"])
BuiltIn = namedtuple("BuiltIn", ["fn", "args"])
Nary = namedtuple("Nary", ["fn", "args"])
Quant = namedtuple("Quant", ["quantifier", "typename", "varname", "inner"])
Prop = namedtuple("Prop", ["modality", "quant"])


def pprint(node):
    if isinstance(node, EnvNode):
        return f"{node.var}"
Exemple #11
0
    Word,
    ZeroOrMore,
)

region_identifier = Word(alphanums + '_')

region_start = (LineStart() + '@region $ ' +
                region_identifier.setResultsName('region_identifier') + '{' +
                LineEnd())
region_end = LineStart() + '}' + LineEnd()

region_param_valid = Word(alphanums + '_.')
region_param = (region_param_valid + ': ' + region_param_valid + ';' +
                LineEnd())

mod_identifier = Word(alphas.upper() + alphas.lower() + '_')
mod_param_name = Word(' ' + alphas.upper() + alphas.lower() + '_')

mods_start_line = '[' + LineEnd()

mod_param = mod_param_name + ':' + Word(nums) + ";"
mod_param_additional_line = Word(nums) + ':' + Word(nums) + ';'

mod_start = '@' + mod_identifier.setResultsName('mod_identifier') + '{'
mod_end = '}' + LineEnd()

mod = (mod_start + ZeroOrMore(mod_param).setResultsName('mod_params') +
       ZeroOrMore(mod_param_additional_line).setResultsName(
           'mod_params_additional') + mod_end)

mods_end_line = ']' + LineEnd()
Exemple #12
0
        print(pe)
    else:
        if fn != None:
            print(fn( bnf.parseString( strg ) ))
        else:
            print()

digits = "0123456789"

# Version 1
element = Regex("A[cglmrstu]|B[aehikr]?|C[adeflmorsu]?|D[bsy]|"
                "E[rsu]|F[emr]?|G[ade]|H[efgos]?|I[nr]?|Kr?|L[airu]|"
                "M[dgnot]|N[abdeiop]?|Os?|P[abdmortu]?|R[abefghnu]|"
                "S[bcegimnr]?|T[abcehilm]|U(u[bhopqst])?|V|W|Xe|Yb?|Z[nr]")

element = Word( alphas.upper(), alphas.lower(), max=2)
elementRef = Group( element + Optional( Word( digits ), default="1" ) )
formula = OneOrMore( elementRef )

fn = lambda elemList : sum( [ atomicWeight[elem]*int(qty) for elem,qty in elemList ] )
test( formula, "H2O", fn )
test( formula, "C6H5OH", fn )
test( formula, "NaCl", fn )
print()

# Version 2 - access parsed items by field name
elementRef = Group( element("symbol") + Optional( Word( digits ), default="1" )("qty") )
formula = OneOrMore( elementRef )

fn = lambda elemList : sum( [ atomicWeight[elem.symbol]*int(elem.qty) for elem in elemList ] )
test( formula, "H2O", fn )
Exemple #13
0
import numpy as np
import matplotlib.pyplot as plt
from pyparsing import Word, alphas, nums

source = open("simpsonsRule_acctest.out", "r")
line = Word(alphas.upper(),
            alphas.lower()) + ':' + Word('-' + nums + '.') + ','
line = line + Word(alphas) + ':' + Word(nums + '.') + ','
line = line + Word(alphas) + ':' + Word(nums) + ','
line = line + Word(alphas) + ':' + Word(nums + '.')

src = [line.strip() for line in source]
acc = list()
N = list()
for _ in src:
    N.append(line.parseString(_)[10])
for _ in src:
    acc.append(line.parseString(_)[2])

print(N)
print(acc)

x = np.array(N, dtype=float)
y = np.array(acc, dtype=float)

fig = plt.figure()
plt.plot(x, y, "o-", color='r')
plt.xlabel("N")
plt.ylabel("acc")
plt.title("f : acc(N)")
fig.savefig("simpsonsRule_acctest.png")