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 main(debug=False): # 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. 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 main(debug=False): # Grammar is defined using textual specification based on PEG language. # Load grammar form file. calc_grammar = open(os.path.join(os.path.dirname(__file__), 'calc.peg'), 'r').read() # 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) # The result is obtained by semantic evaluation using visitor class. # visit_parse_tree will start semantic analysis. # In this case semantic analysis will evaluate expression and # returned value will be evaluated result of the input_expr expression. result = visit_parse_tree(parse_tree, CalcVisitor(debug=debug)) # Check that result is valid assert (result - -7.51194444444) < 0.0001 print("{} = {}".format(input_expr, result))
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 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): # 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) # getASG will start semantic analysis. # In this case semantic analysis will evaluate expression and # returned value will be the final position of the robot. return parser.getASG(sem_actions=semantic_actions)
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 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 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 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 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 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 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 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))
return s def jnz(state, name, val): s = dict(state) return s class LV(PTNodeVisitor): reg = {} def visit_cint(self, node, children): print(children) def visit_cname(self, node, children): print(children) def visit_inc(self, node, children): print(children) def visit_dec(self, node, children): print(children) def visit_jnz(self, node, children): print(children) st = "" with open("in.txt", 'r') as f: st = f.read() t = LV() visit_parse_tree(parser.parse(st), t)
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"
logging.basicConfig(level=logging.DEBUG) # 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") # Then we export it to a dot file. PMDOTExport().exportFile(parser.parser_model, "calc_peg_parser_model.dot") # An expression we want to evaluate input = "-(4-1)*5+(2+4.67)+5.89/(.2+7)" # Then parse tree is created out of the input expression. parse_tree = parser.parse(input) # We save it to dot file in order to visualise it. PTDOTExport().exportFile(parse_tree, "calc_peg_parse_tree.dot") # getASG will start semantic analysis. # In this case semantic analysis will evaluate expression and # returned value will be evaluated result of the input expression. # Semantic actions are supplied to the getASG function. print "%s = %f" % (input, parser.getASG(sem_actions)) except NoMatch, e: print "Expected %s at position %s." % (e.value, str(e.parser.pos_to_linecol(e.position)))
try: logging.basicConfig(level=logging.DEBUG) # ParserPEG will use ParserPython to parse peg_grammar definition and # create parser_model for parsing PEG based grammars parser = ParserPEG(peg_grammar, 'grammar') # Exporting parser model to dot file in order to visualise. PMDOTExport().exportFile(parser.parser_model, "peg_peg_parser_model.dot") # 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. parser.parse(peg_grammar) PTDOTExport().exportFile(parser.parse_tree, "peg_peg_parse_tree.dot") # ASG should be the same as parser.parser_model because semantic # actions will create PEG parser (tree of ParsingExpressions). asg = parser.getASG(sem_actions) # This graph should be the same as peg_peg_parser_model.dot because # they define the same parser. PMDOTExport().exportFile(asg, "peg_peg_asg.dot") # If we replace parser_mode with ASG constructed parser it will still # parse PEG grammars