def __init__(self): self.grammar = parglare.Grammar.from_file( os.path.join(os.path.dirname(os.path.realpath(__file__)), "busstops_grammar.pg")) self.parser = parglare.Parser(self.grammar, actions=self.actions, ws="\r")
def initialize(): parser_dir = os.path.dirname(os.path.abspath(__file__)) file_name = '%s/mklogparser.pg' % (parser_dir) grammar = parglare.Grammar.from_file(file_name) actions = { "CmdList": [ lambda _, nodes: BuildCommandGroup(None, None, '.', [nodes[0]] if nodes[0] is not None else []), lambda _, nodes: nodes[0].append(nodes[1]) if nodes[1] is not None else nodes[0], lambda _, nodes: BuildCommandGroup(None, None, '.', []), ], "TitledCmdList": [ lambda _, nodes: nodes[1].set_target("Library", nodes[0]), lambda _, nodes: nodes[1].set_target("Program", nodes[0]), ], "Command": [ lambda _, nodes: nodes[1].set_run_dir(nodes[0]), lambda _, nodes: (nodes[2].set_run_dir(nodes[0]).prepend_list(nodes[1]) if nodes[2] is not None else nodes[1]), lambda _, nodes: SimpleBuildCommand(nodes[0][0], nodes[0][1], nodes[1]), lambda _, nodes: SimpleBuildCommand(None, None, nodes[0]), lambda _, nodes: None, lambda _, nodes: None, lambda _, nodes: None, ], "EnteringDir": [lambda _, nodes: nodes[1]], "LeavingDir": [lambda _, nodes: nodes[1]], "LibraryTitle": [lambda _, nodes: nodes[1]], "ProgramTitle": [lambda _, nodes: nodes[1]], "BuildCommandTitle": [lambda _, nodes: [nodes[0].strip(), nodes[2]]], "SimpleCommandList": [ lambda _, nodes: [nodes[0]], lambda _, nodes: nodes[0] + [nodes[1]], ], "SimpleCommand": [lambda _, nodes: nodes[0]], "SimpleCommandText": [ lambda _, nodes: nodes[0], lambda _, nodes: nodes[0] + nodes[1], ], "SimpleCommandPart": [ lambda _, nodes: nodes[0], lambda _, nodes: nodes[0], lambda _, nodes: ' ', ], } parser = parglare.Parser(grammar, ws='\r', actions=actions) return parser
def getParser(debugParser): """Parse the Parglare Souffle grammar and get the parser""" global parglareParser, verbose if parglareParser is None: if verbose: print "Creating parser" fileName = "souffle-grammar.pg" directory = os.path.dirname(os.path.abspath(__file__)) g = parglare.Grammar.from_file(directory + "/" + fileName) parglareParser = parglare.Parser(g, build_tree=True, debug=debugParser) if verbose: print "Parser constructed" return parglareParser
def __init__(self): """Inicijalizuj parglare parser na osnovu gramatike kompleksnih upita.""" # TODO moze li se ovo naterati da prepoznaje & i | kao reci?? grammar_string = r""" expression: oroperand | expression oroperand | expression '||' oroperand ; oroperand: andoperand | oroperand '&&' andoperand ; andoperand: notoperand | '!' notoperand ; notoperand: word | '(' expression ')' ; terminals word: /[^&|!() \t\n]+/ ; """ actions = { "expression": [ lambda _, nodes: nodes[0], lambda _, nodes: OrNode(nodes[0], nodes[1]), lambda _, nodes: OrNode(nodes[0], nodes[2]) ], "oroperand": [ lambda _, nodes: nodes[0], lambda _, nodes: AndNode(nodes[0], nodes[2]) ], "andoperand": [lambda _, nodes: nodes[0], lambda _, nodes: NotNode(nodes[1])], "notoperand": [lambda _, nodes: nodes[0], lambda _, nodes: nodes[1]], "word": lambda _, value: WordNode(value), } grammar = parglare.Grammar.from_string(grammar_string) self.parser = parglare.Parser(grammar, actions=actions)
def __init__(self): grammar = r""" E: E "||" T | T ; T: T "&&" F | F ; F: "!" X | X ; X: reci | "(" E ")" ; terminals reci: /[a-zA-Z_][_a-zA-Z0-9]*/; """ actions = { "E": [ lambda _, nodes: Operacije(nodes[0], nodes[2], 1), lambda _, nodes: nodes[0] ], "T": [ lambda _, nodes: Operacije(nodes[0], nodes[2], 2), lambda _, nodes: nodes[0] ], "F": [ lambda _, nodes: Operacije(nodes[1], nodes[1], 3), lambda _, nodes: nodes[0] ], "X": [lambda _, nodes: nodes[0], lambda _, nodes: nodes[1]], "reci": lambda _, value: Rec(value), } g = parglare.Grammar.from_string(grammar) self.parser = parglare.Parser(g, actions=actions)
def initialize(): parser_dir = os.path.dirname(os.path.abspath(__file__)) file_name = '%s/sclogparser.pg' % (parser_dir) grammar = parglare.Grammar.from_file(file_name) actions = { "FullLog": [lambda _, nodes: nodes[5] ], "BuildCommandsList": [ lambda _, nodes: nodes[2] ], "CmdList": [lambda _, nodes: BuildCommandGroup(None, None, '.', [ nodes[0] ] if nodes[0] is not None else []), lambda _, nodes: nodes[0].append(nodes[1]) if nodes[1] is not None else nodes[0], ], "Command": [lambda _, nodes: SimpleBuildCommand(nodes[0][0], nodes[0][1], nodes[1]), lambda _, nodes: SimpleBuildCommand(None, None, nodes[0]), lambda _, nodes: None, lambda _, nodes: None, ], "BuildCommandTitle": [lambda _, nodes: [ nodes[0].strip(), nodes[2] ] ], "SimpleCommandList": [lambda _, nodes: [ nodes[0] ], lambda _, nodes: nodes[0] + [ nodes[1] ], ], "SimpleCommand": [lambda _, nodes: nodes[0] ], "SimpleCommandText": [lambda _, nodes: nodes[0], lambda _, nodes: nodes[0] + nodes[1], lambda _, nodes: '', ], "SimpleCommandPart": [lambda _, nodes: nodes[0], lambda _, nodes: nodes[0], lambda _, nodes: ' ', ], } parser = parglare.Parser(grammar, ws='\r', actions=actions) return parser
def getParser(): fileName = "convert.pg" g = parglare.Grammar.from_file(fileName) return parglare.Parser(g, build_tree=True)
word: /\w+/; number: /\d+/; dot: /[.]/; """ # Simplify the syntax tree: actions = { 'LINE': lambda _, nodes: (nodes[0], nodes[2]), 'COLOR': lambda _, nodes: "{} {}".format(nodes[0], nodes[1]), 'CONTENTS': [lambda _, nodes: [], lambda _, nodes: nodes[0]], 'BAG_AMOUNT_LIST': lambda _, nodes: [(int(nodes[0]), nodes[1])] + nodes[2], 'COMMA_AMOUNT_COLOR': lambda _, nodes: (int(nodes[1]), nodes[2]) } g = parglare.Grammar.from_string(grammar) parser = parglare.Parser(g, actions=actions) graph = {} graph_rev = collections.defaultdict(set) for line in desc: color, contents = parser.parse(line) assert color not in graph graph[color] = contents for _, contents_col in contents: graph_rev[contents_col].add(color) # A contains_shiny_gold = set() def dfs(n):
def initialize(): parser_dir = os.path.dirname(os.path.abspath(__file__)) file_name = '%s/mkparser.pg' % (parser_dir) grammar = parglare.Grammar.from_file(file_name) actions = { "CmdList": [ lambda _, nodes: mkevaluator.MkScript(nodes[0]), lambda _, nodes: nodes[0].append_command(nodes[2]), ], "Command": [ lambda _, nodes: (mkevaluator.MkCmdAppend(nodes[0][0], nodes[1][3], export=False) if nodes[1][1] == '+=' else mkevaluator. MkCmdRecursiveExpandAssign(nodes[0][0], nodes[1][3], export=False) if nodes[1][1] == '=' else mkevaluator.MkCmdSimpleExpandAssign( nodes[0][0], nodes[1][3], export=False) if nodes[1][1] == ':=' else mkevaluator.MkCmdOptAssign( nodes[0][0], nodes[1][3], export=False) if nodes[1][1] == '?=' else "Invalid command oper %s" % (nodes[1][1])), lambda _, nodes: (mkevaluator.MkCmdExport(nodes[1][0]) if nodes[2] is None else mkevaluator.MkCmdAppend(nodes[1][0], nodes[2][3], export=True) if nodes[2][1] == '+=' else mkevaluator. MkCmdRecursiveExpandAssign(nodes[1][0], nodes[2][3], export=True) if nodes[2][1] == '=' else mkevaluator.MkCmdSimpleExpandAssign( nodes[1][0], nodes[2][3], export=True) if nodes[2][1] == ':=' else mkevaluator.MkCmdOptAssign( nodes[1][0], nodes[2][3], export=True) if nodes[2][1] == '?=' else "Invalid command oper %s" % (nodes[2][1])), lambda _, nodes: (mkevaluator.MkCmdAppend(nodes[0] + nodes[1] + nodes[2] + nodes[3], nodes[5][3], export=False) if nodes[5][1] == '+=' else mkevaluator.MkCmdRecursiveExpandAssign(nodes[0] + nodes[1] + nodes[2] + nodes[3], nodes[5][3], export=False) if nodes[5][1] == '=' else mkevaluator.MkCmdSimpleExpandAssign( nodes[0] + nodes[1] + nodes[2] + nodes[3], nodes[5][3], export=False) if nodes[5][1] == ':=' else mkevaluator.MkCmdOptAssign( nodes[0] + nodes[1] + nodes[2] + nodes[3], nodes[5][3], export=False) if nodes[5][1] == '?=' else "Invalid command oper %s" % (nodes[5][1])), lambda _, nodes: (mkevaluator.MkCmdExport(nodes[1] + nodes[2] + nodes[3] + nodes[4]) if nodes[6] is None else mkevaluator.MkCmdAppend( nodes[1] + nodes[2] + nodes[3] + nodes[4], nodes[6][3], export=True) if nodes[6][ 1] == '+=' else mkevaluator.MkCmdRecursiveExpandAssign( nodes[1] + nodes[2] + nodes[3] + nodes[4], nodes[6][3], export=True) if nodes[6][1] == '=' else mkevaluator. MkCmdSimpleExpandAssign(nodes[1] + nodes[2] + nodes[3] + nodes[4], nodes[6][3], export=True) if nodes[6][1] == ':=' else mkevaluator.MkCmdOptAssign( nodes[1] + nodes[2] + nodes[3] + nodes[4], nodes[6][3], export=True) if nodes[6][1] == '?=' else "Invalid command oper %s" % (nodes[6][1])), lambda _, nodes: nodes[0], lambda _, nodes: nodes[0], lambda _, nodes: nodes[0], lambda _, nodes: (mkevaluator.MkCmdExpr(nodes[0]) if nodes[1] is None else mkevaluator. MkCmdRuleHeader(nodes[0], nodes[1][1])), lambda _, nodes: mkevaluator.MkCmdRuleCommand(nodes[0]), lambda _, nodes: None, lambda _, nodes: mkevaluator.MkCmdComment(nodes[0]), ], "CmdAssign": [ lambda _, nodes: [None, nodes[0], nodes[1], nodes[2]], ], "RuleList": [ lambda _, nodes: [nodes[0]], lambda _, nodes: nodes[0] + [nodes[1]], ], "RuleCommand": [ lambda _, nodes: nodes[1], ], "CondCmd": [ lambda _, nodes: mkevaluator.MkCmdCond(nodes[0], nodes[2], None), lambda _, nodes: mkevaluator.MkCmdCond(nodes[0], nodes[2], nodes[6] ), lambda _, nodes: mkevaluator.MkCmdCond(nodes[0], nodes[2], [nodes[6]]), ], "IncludeCmd": [ lambda _, nodes: mkevaluator.MkCmdInclude(nodes[2]), lambda _, nodes: mkevaluator.MkCmdInclude(nodes[2], optional=True), ], "VPathCmd": [ lambda _, nodes: mkevaluator.MkCmdVpath(nodes[2], nodes[4]), ], "WSOPT": [ lambda _, nodes: nodes[0], lambda _, nodes: "", ], "Condition": [ lambda _, nodes: (mkevaluator.MkCondIfdef(nodes[2]) if nodes[0] == 'ifdef' else mkevaluator.MkCondIfndef(nodes[2]) if nodes[0] == 'ifndef' else "Invalid conditional oper %s" % (nodes[0])), lambda _, nodes: (mkevaluator.MkCondIfeq(nodes[3], nodes[5]) if nodes[0] == 'ifeq' else mkevaluator.MkCondIfneq( nodes[3], nodes[5]) if nodes[0] == 'ifneq' else "Invalid conditional oper %s" % (nodes[0])), ], "OptRValueExpr": [ lambda _, nodes: nodes[0], lambda _, nodes: mkevaluator.MkRValueExpr([]), ], "RValueExpr": [ lambda _, nodes: mkevaluator.MkRValueExpr([nodes[0]]), lambda _, nodes: nodes[0].append_part(nodes[1]), lambda _, nodes: nodes[0].append_part(mkevaluator.MkRValueSpace()), ], "RValuePart": [ lambda _, nodes: nodes[0], lambda _, nodes: nodes[0], ], "RValuePartExpr": [ lambda _, nodes: nodes[1], lambda _, nodes: mkevaluator.MkRValueFun1(nodes[2], nodes[4]), lambda _, nodes: mkevaluator.MkRValueFun2(nodes[2], nodes[4], nodes[6]), lambda _, nodes: mkevaluator.MkRValueFun3(nodes[2], nodes[4], nodes[6], nodes[8]), lambda _, nodes: mkevaluator.MkRValueFunAny(nodes[2], nodes[4]), lambda _, nodes: nodes[2], lambda _, nodes: mkevaluator.MkRValueVar(nodes[2]), lambda _, nodes: mkevaluator.MkRValueVar(nodes[2]), lambda _, nodes: mkevaluator.MkRValueSubst( nodes[2], mkevaluator.MkRValueExpr([]), nodes[4]), lambda _, nodes: mkevaluator.MkRValueSubst(nodes[2], nodes[4], nodes[6]), ], "ParamExpr": [ lambda _, nodes: mkevaluator.MkRValueExpr([nodes[0]]), lambda _, nodes: nodes[0].append_part(nodes[1]), lambda _, nodes: nodes[0].append_part(mkevaluator.MkRValueSpace()), lambda _, nodes: mkevaluator.MkRValueExpr([]), ], "ParamPart": [ lambda _, nodes: nodes[0], lambda _, nodes: nodes[0], ], "ParamPartExpr": [ lambda _, nodes: nodes[1], lambda _, nodes: mkevaluator.MkRValueFun1(nodes[2], nodes[4]), lambda _, nodes: mkevaluator.MkRValueFun2(nodes[2], nodes[4], nodes[6]), lambda _, nodes: mkevaluator.MkRValueFun3(nodes[2], nodes[4], nodes[6], nodes[8]), lambda _, nodes: mkevaluator.MkRValueFunAny(nodes[2], nodes[4]), lambda _, nodes: nodes[2], lambda _, nodes: mkevaluator.MkRValueVar(nodes[2]), lambda _, nodes: mkevaluator.MkRValueVar(nodes[2]), lambda _, nodes: mkevaluator.MkRValueSubst( nodes[2], mkevaluator.MkRValueExpr([]), nodes[4]), lambda _, nodes: mkevaluator.MkRValueSubst(nodes[2], nodes[4], nodes[6]), ], "ParamList": [ lambda _, nodes: [nodes[0]], lambda _, nodes: nodes[0] + [nodes[2]], ], "VarName": [ lambda _, nodes: mkevaluator.MkRValueVar(nodes[0]), lambda _, nodes: mkevaluator.MkRValueVar(nodes[0], nodes[1]), lambda _, nodes: mkevaluator.MkRValueVar(nodes[0], nodes[2], nodes[ 1], nodes[3]), ], "RValuePartText": [ lambda _, nodes: mkevaluator.MkRValueText(nodes[0]), lambda _, nodes: mkevaluator.MkRValueText(nodes[0]), lambda _, nodes: mkevaluator.MkRValueText(nodes[0][2]), lambda _, nodes: mkevaluator.MkRValueText(nodes[0][1]), lambda _, nodes: mkevaluator.MkRValueText(nodes[0][0] + (nodes[0][ 1] if nodes[0][1] is not None else "")), lambda _, nodes: mkevaluator.MkRValueText(nodes[0]), lambda _, nodes: mkevaluator.MkRValueText(nodes[0]), lambda _, nodes: mkevaluator.MkRValueText(nodes[0]), lambda _, nodes: mkevaluator.MkRValueText(nodes[0]), lambda _, nodes: mkevaluator.MkRValueSpace(), ], "ParamPartText": [ lambda _, nodes: mkevaluator.MkRValueText(nodes[0]), lambda _, nodes: mkevaluator.MkRValueText(nodes[0]), lambda _, nodes: mkevaluator.MkRValueText(nodes[0][2]), lambda _, nodes: mkevaluator.MkRValueText(nodes[0][1]), lambda _, nodes: mkevaluator.MkRValueText(nodes[0][0] + (nodes[0][ 1] if nodes[0][1] is not None else "")), lambda _, nodes: mkevaluator.MkRValueText(nodes[0]), lambda _, nodes: mkevaluator.MkRValueText(nodes[0]), lambda _, nodes: mkevaluator.MkRValueText(nodes[0]), lambda _, nodes: mkevaluator.MkRValueSpace(), ], "VarIdentStd": [ lambda _, nodes: nodes[0], lambda _, nodes: nodes[0] + nodes[1], lambda _, nodes: nodes[0] + nodes[1], ], "Text": [ lambda _, nodes: nodes[0], lambda _, nodes: nodes[0], lambda _, nodes: nodes[0] + nodes[1], lambda _, nodes: nodes[0] + nodes[1], ], } parser = parglare.Parser(grammar, ws='\r', actions=actions, debug=False) return parser
def getOvsParser(): g = parglare.Grammar.from_string(ovsGrammar) return parglare.Parser(g, build_tree=True)
def getAddressParser(): g = parglare.Grammar.from_string(addressGrammar) return parglare.Parser(g, build_tree=True)
def getValueParser(): g = parglare.Grammar.from_string(valGrammar) return parglare.Parser(g, build_tree=True)
@action def make_pair_dict(_, nodes): return {nodes[0].lower(): nodes[1]} @action def make_pair_dict_from_merged_dicts(_, nodes, **kwds): for x in nodes[-1]: kwds.update(x) return {nodes[0].lower(): kwds} grammar = g.Grammar.from_file('projx/grammar.pg') parser = g.Parser(grammar, actions=action.all) def parse_query(query: str) -> dict: data = parser.parse(query) return { 'extractor': { 'networkx': { 'type': data['type'] or 'subgraph', 'traversal': data['traversal'], }, }, 'transformers': data['transformers'], 'loader': { 'nx2nx': {},