def main(debug=False): current_dir = os.path.dirname(__file__) peg_grammar = open(os.path.join(current_dir, 'peg.peg')).read() # ParserPEG will use ParserPython to parse peg_grammar definition and # create parser_model for parsing PEG based grammars # In debug mode dot (graphviz) files for parser model # and parse tree will be created for visualization. # Checkout current folder for .dot files. parser = ParserPEG(peg_grammar, 'peggrammar', debug=debug) # Now we will use created parser to parse the same peg_grammar used for # parser initialization. We can parse peg_grammar because it is specified # using PEG itself. print("PARSING") parse_tree = parser.parse(peg_grammar) # ASG should be the same as parser.parser_model because semantic # actions will create PEG parser (tree of ParsingExpressions). parser_model, comment_model = visit_parse_tree( parse_tree, PEGVisitor(root_rule_name='peggrammar', comment_rule_name='comment', ignore_case=False, debug=debug)) if debug: # This graph should be the same as peg_peg_parser_model.dot because # they define the same parser. PMDOTExporter().exportFile(parser_model, "peg_peg_new_parser_model.dot") # If we replace parser_mode with ASG constructed parser it will still # parse PEG grammars parser.parser_model = parser_model parser.parse(peg_grammar)
def test_construct_parser(): parser = ParserPEG(grammar, 'calc') assert parser.parser_model.rule_name == 'calc' assert isinstance(parser.parser_model, Sequence) assert parser.parser_model.nodes[0].name == 'OneOrMore'
def get_parser(debug=False, root='query_specification'): """returns a PEG parser for the local ADQL grammar. This patches the grammar literal a bit to make it palatable to Arpeggio. NOTE: This is *not* a general PEG-to-arpeggio translator; it just happens to work on the ADQL grammar (and might do the trick of other simple cases). """ with open("adql2.1.peg") as f: peg_rules = f.read() # insert semicolons between rules peg_rules = re.sub( r"(?m)^[^#](?!(=\<\-)).+\s+" r"((?!=(\<\-|)(?=\#)).+\n)+", lambda match: match.group().strip() + ";\n", peg_rules) # replace comments (TODO: make sure the # isn't in a literal) peg_rules = re.sub('#', '// ', peg_rules) # adapt character range syntax peg_rules = re.sub("'\\[", "r'[", peg_rules) return ParserPEG(peg_rules, root, ignore_case=True, skipws=False, debug=False, reduce_tree=True, memoization=True)
def convert_semantic_to_medusa(arch, insn): isabelle_grammar = open( os.path.join(os.path.dirname(__file__), 'isabelle.peg'), 'r').read() parser = ParserPEG(isabelle_grammar, 'code', debug=False) parse_tree = parser.parse(insn['semantic']) prm = { 'cpu_info': 'm_CpuInfo', } return visit_parse_tree( parse_tree, IsabelleVisitor(arch, insn, None, prm, debug=False)) + "\n"
def convert_function_to_medusa(arch, func, var): isabelle_grammar = open( os.path.join(os.path.dirname(__file__), 'isabelle.peg'), 'r').read() parser = ParserPEG(isabelle_grammar, 'code', debug=False) parse_tree = parser.parse(func) prm = { 'cpu_info': 'rCpuInfo', } return visit_parse_tree( parse_tree, IsabelleVisitor(arch, None, var, prm, debug=False)) + "\n"
def test_parse_input(): parser = ParserPEG(grammar, 'calc') input = "4+5*7/3.45*-45*(2.56+32)/-56*(2-1.34)" result = parser.parse(input) assert isinstance(result, NonTerminal) assert str( result ) == "4 | + | 5 | * | 7 | / | 3.45 | * | - | 45 | * | ( | 2.56 | + | 32 | ) | / | - | 56 | * | ( | 2 | - | 1.34 | ) | " assert repr( result ) == "[ [ [ [ number '4' [0] ] ], '+' [1], [ [ number '5' [2] ], '*' [3], [ number '7' [4] ], '/' [5], [ number '3.45' [6] ], '*' [10], [ '-' [11], number '45' [12] ], '*' [14], [ '(' [15], [ [ [ number '2.56' [16] ] ], '+' [20], [ [ number '32' [21] ] ] ], ')' [23] ], '/' [24], [ '-' [25], number '56' [26] ], '*' [28], [ '(' [29], [ [ [ number '2' [30] ] ], '-' [31], [ [ number '1.34' [32] ] ] ], ')' [36] ] ] ], EOF [37] ]"
def __call__(self): self.MakeTest(ParserPEG(self.Case1(), "S"), 1) self.MakeTest(ParserPEG(self.Case2(), "S"), 2) #self.MakeTest(ParserPEG(self.Case3(), "S"), 3) self.MakeTest(ParserPEG(self.Case4(), "S"), 4) self.MakeTest(ParserPEG(self.Case5(), "S"), 5) self.MakeTest(ParserPEG(self.Case6(), "S"), 6) self.MakeTest(ParserPEG(self.Case7(), "S"), 7)
def test_unordered_group(): grammar = """ g <- (("a" "b") "c" )#; """ parser = ParserPEG(grammar, 'g', reduce_tree=True) r = parser.parse("c a b") assert isinstance(r, NonTerminal) r = parser.parse("a b c") assert isinstance(r, NonTerminal) with pytest.raises(NoMatch): parser.parse("a c b")
def main(debug=False, all=False): peg_grammar = open('liberty.peg').read() parser = ParserPEG(peg_grammar, 'liberty', 'comment') def parse_file(file): print(f"Parsing '{file}'") test = open(file).read() tree = parser.parse(test) # print(tree.tree_str()) return visit_parse_tree(tree, LibertyNodeVisitor(debug=debug)) if all: tests = glob('tests/*.lib') with concurrent.futures.ThreadPoolExecutor() as executor: results = executor.map(parse_file, tests) print(results) return results else: result = parse_file('tests/sky130_fd_sc_hvl__ff_085C_5v50_lv1v95.lib') print(result)
def main(debug=False): # First we will make a parser - an instance of the calc parser model. # Parser model is given in the form of PEG notation therefore we # are using ParserPEG class. Root rule name (parsing expression) is "calc". parser = ParserPEG(calc_grammar, "calc", debug=debug) # An expression we want to evaluate input_expr = "-(4-1)*5+(2+4.67)+5.89/(.2+7)" # Then parse tree is created out of the input_expr expression. parse_tree = parser.parse(input_expr) result = parser.getASG(sem_actions) if debug: # getASG will start semantic analysis. # In this case semantic analysis will evaluate expression and # returned value will be evaluated result of the input_expr expression. # Semantic actions are supplied to the getASG function. print("{} = {}".format(input_expr, result))
def check_parser(grammar, text): """Test that the PEG parsers correctly parse a grammar and match the given text. Test both the peg and cleanpeg parsers. Raise an exception if the grammar parse failed, and returns False if the match fails. Otherwise, return True. Parameters: grammar -- Not the full grammar, but just the PEG expression for a string literal or regex match, e.g. "'x'" to match an x. text -- The text to test against the grammar for a match. """ # test the peg parser parser = ParserPEG('top <- ' + grammar + ' EOF;', 'top', skipws=False) if parser.parse(text) is None: return False # test the cleanpeg parser parser = ParserCleanPEG('top = ' + grammar + ' EOF', 'top', skipws=False) if parser.parse(text) is None: return False return True
def main(debug=False): # Program code input = ''' begin up up left down right end ''' # First we will make a parser - an instance of the robot parser model. # Parser model is given in the form of PEG specification therefore we # are using ParserPEG class. parser = ParserPEG(robot_grammar, 'robot', debug=debug) # We create a parse tree out of textual input parse_tree = parser.parse(input) # visit_parse_tree will start semantic analysis. # In this case semantic analysis will evaluate expression and # returned value will be the final position of the robot. return visit_parse_tree(parse_tree, RobotVisitor(debug=debug))
number <- r'\d*\.\d*|\d+'; factor <- ("+" / "-")? (number / "(" expression ")"); // This is another comment term <- factor (( "*" / "/") factor)*; expression <- term (("+" / "-") term)*; calc <- expression+ EOF; // And final comment at the end of file ''' clean_grammar = grammar.replace('<-', '=').replace(';', '') @pytest.mark.parametrize( 'parser', [ParserPEG(grammar, 'calc'), ParserPEGClean(clean_grammar, 'calc')]) def test_construct_parser(parser): assert parser.parser_model.rule_name == 'calc' assert isinstance(parser.parser_model, Sequence) assert parser.parser_model.nodes[0].name == 'OneOrMore' @pytest.mark.parametrize( 'parser', [ParserPEG(grammar, 'calc'), ParserPEGClean(clean_grammar, 'calc')]) def test_parse_input(parser): input = "4+5*7/3.45*-45*(2.56+32)/-56*(2-1.34)" result = parser.parse(input)
from arpeggio.peg import ParserPEG # pylint: disable=import-error from arpeggio import PTNodeVisitor, visit_parse_tree # pylint: disable=import-error lang = "" with open("lang.txt", 'r') as f: lang = f.read() parser = ParserPEG(lang, 'program', debug=False) def get(state, name): if name in state: return state[name] else: return 0 def cint(state, val, name): s = dict(state) s[name] = val return s def cname(state, n1, n2): s = dict(state) s[n1] = get(s, n2) return s def inc(state, name): s = dict(state) s[name] = get(s, name) +1 return s def dec(state, name):
from arpeggio.peg import ParserPEG from arpeggio import visit_parse_tree from .grammar import grammar from .visitor import IthkuilVisitor wordParser = ParserPEG(grammar, "word", debug=False) def parseWord(word): return visit_parse_tree(wordParser.parse(word), IthkuilVisitor(debug=False))
def get_parser(root, debug=False): if not root in _PARSER: _PARSER[root] = ParserPEG(kusto_peg, root, debug=debug) return _PARSER[root]
def main(debug=False): isabelle_grammar = open( os.path.join(os.path.dirname(__file__), 'isabelle.peg'), 'r').read() parser = ParserPEG(isabelle_grammar, 'code', debug=False)
def parser() -> ParserPEG: path = os.path.dirname(__file__) filename = os.path.join(path, 'peg.peg') with open(filename, 'r') as file: grammar = file.read() return ParserPEG(grammar, 'peggrammar', 'comment')