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 construct( arpeg_node ) : with open( "apl_grammar.txt", "rt", encoding='utf8' ) as f_in : funpy_grammar = f_in.read() parser = ParserPEG(funpy_grammar, "program", reduce_tree=True, debug=1) parser.debug = 1 #def test( parser ) : #parser.parse("R") #result = parser.parse("⌽A") OK #result = parser.parse("⌽[1]A") # OK result = parser.parse("⊖⌽A") # OK #result = parser.parse("") #result = parser.parse("") #result = parser.parse("") #result = parser.parse("") #result = parser.parse("") #result = parser.parse("") #result = parser.parse("") #result = parser.parse("") #result = parser.parse("") result
def parse(filename, debug=False): with open(filename) as file: contents = file.read() parser = ParserPEG(calc_grammar, "start", True) parse_tree = parser.parse(contents) visitor = Visitor(debug=debug) result = visit_parse_tree(parse_tree, visitor ) #Try to get if we found anything or not if ( visitor.functions or visitor.classes): print (visitor.namespace ) return visitor print ("Empty") return None
def __init__(self, grammarPath = None, root: str = 'volpiano', strict: bool = False, **kwargs) -> None: """Initialize a Cantus Volpiano parser. Parameters ---------- grammarPath : str, optional Path to the PEG grammar file, defaults to ``grammars/cantus_volpiano.peg`` root : str, optional Root element of the parser, by default 'volpiano' strict : bool, optional Whether to parse in strict mode or not. In strict mode, exceptions are raised whenever volpiano strings deviate from the standard syntax. In non-strict mode, some deviations will be automatically corrected. See :meth:`ParserCantusVolpiano.preprocess` for details. By default True **kwargs Other keywords are passed to :class:`ParserPeg`. """ if grammarPath == None: grammarPath = GRAMMAR_PATH if not os.path.exists(grammarPath): raise Exception(f'Grammar file ({ grammarPath }) does not exist') with open(grammarPath, 'r') as handle: grammar = handle.read() self.strict = strict self.parser = ParserPEG(grammar, root, skipws=False, **kwargs)
def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('-d', '--debug', action='store_true', default=False, help='Debug Arpeggio parser') parser.add_argument('-v', '--verbose', action='store_true', default=False, help='Increase output verbosity') parser.add_argument('--no-order', action='store_true', default=False, help='Do not use OrderedDict') parser.add_argument('--no-visit', action='store_true', default=False, help='Do not visit the parsed tree') parser.add_argument('--rule', default='m_source_file', help='Root rule name') parser.add_argument('source_file', help='Source file to parse') global args args = parser.parse_args() logging.basicConfig(level=logging.DEBUG if args.verbose or args.debug else logging.WARNING, stream=sys.stdout) with open(args.source_file) as source_file: source_code = source_file.read() log.debug('Source file "{}" was read with success.'.format(args.source_file)) with open(m_grammar_file_path) as m_grammar_file: m_grammar = m_grammar_file.read() global m_parser m_parser = ParserPEG(m_grammar, args.rule, debug=args.debug, reduce_tree=False) log.debug('M language clean-PEG grammar was parsed with success.') parse_tree = m_parser.parse(source_code) log.debug('Source file "{}" was parsed with success.'.format(args.source_file)) if not args.no_visit: result = visit_parse_tree(parse_tree, MLanguageVisitor(debug=args.debug)) print(json.dumps(result)) return 0
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, "document", debug=debug) filename = sys.argv[1] with open(filename) as file: contents = file.read() # An expression we want to evaluate input_expr = "<tag>dgdg</tag>" input_expr = contents # Then parse tree is created out of the input_expr expression. parse_tree = parser.parse(input_expr) #result = parser.getASG(sem_actions) #print( result ) print( parse_tree ) 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, "document", debug=debug) filename = sys.argv[1] with open(filename) as file: contents = file.read() # An expression we want to evaluate input_expr = "<tag>dgdg</tag>" input_expr = contents # Then parse tree is created out of the input_expr expression. parse_tree = parser.parse(input_expr) #result = parser.getASG(sem_actions) #print( result ) print(parse_tree) 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 _get_parser(strict): """Ensure the module-level peg parser is created and return it.""" global _strict_parser, _permissive_parser # Each branch below only acquires the lock if the global is unset. if strict: if _strict_parser is None: with _parser_create_lock: if _strict_parser is None: _strict_parser = ParserPEG( canonical, root_rule_name='version', skipws=False ) return _strict_parser else: if _permissive_parser is None: with _parser_create_lock: if _permissive_parser is None: _permissive_parser = ParserPEG( permissive, root_rule_name='version', skipws=False, ignore_case=True, ) return _permissive_parser
def parse(input_expr, context, debug=False): with open(os.path.join(os.path.dirname(__file__), 'matrip.peg'), 'r') as f: grammar = f.read() parser = ParserPEG(grammar, "matrip", debug=debug) parse_tree = parser.parse(input_expr) visitor = MatripVisitor(debug=debug) visit_parse_tree(parse_tree, visitor) return generate_base_table(visitor.measures, visitor.expressions, context)
def runSmurf(program): parser = ParserPEG(grammar, "program", "comment", debug=False) parse_tree = parser.parse(program) ast = visit_parse_tree(parse_tree, NodeVisitor(debug=False)) ast.accept(Interpreter()) # runSmurf(""" # let a = 5, b = 4 print(a+b) # """)
def runGrammar(program): parser = ParserPEG(grammar, "program", "comment", debug=False) tree = parser.parse(program) ast = visit_parse_tree(tree, VisitorClass(debug=False)) ast.accept(Interpreter()) # runGrammar(""" # let f = fn (a) { a + 1 } # print(f(3)) # """)
def test_regex_with_empty_successful_match_in_repetition(): grammar = \ """ rule = (subexpression)+ subexpression = r'^.*$' """ parser = ParserPEG(grammar, "rule") parsed = parser.parse("something simple") assert parsed.rule_name == "rule" assert parsed.subexpression.rule_name == "subexpression"
def test_issue_16(): parser = ParserPEG(grammar, "calc", skipws=False) input_expr = """public function __construct( )""" parse_tree = parser.parse(input_expr) # Do semantic analysis. Do not use default actions. asg = parser.getASG(sem_actions=sem_actions, defaults=False) assert asg
def solve(file, verbose): parser = ParserPEG(grammar, root_rule_name='rules') parse_tree = parser.parse(file.read()) rules = visit_parse_tree(parse_tree, BagVisitor()) graph = nx.DiGraph() for rule in rules: for bag in rule.contains: graph.add_edge(rule.bag.name, bag.name, quantity=bag.quantity) print('Part 1:', len(nx.ancestors(graph, 'shiny gold'))) print('Part 2:', count_bags(graph, 'shiny gold'))
def main(debug=False): filename = sys.argv[1] #debug = bool(sys.argv[2]) with open(filename) as file: contents = file.read() #print (contents) # # An expression we want to evaluate input_expr = """class Application extends Container implements HttpKernelInterface, TerminableInterface, ResponsePreparerInterface { """ # input_expr = input input_expr = contents if not input_expr: return None # 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, "start", True) # Then parse tree is created out of the input_expr expression. parse_tree = parser.parse(input_expr) visitor = Visitor(debug=debug) result = visit_parse_tree(parse_tree, visitor ) print (visitor.namespace ) #print (visitor.classes ) #print (visitor.functions ) # analyseASG( parser, input, Visitor(), True, False ) # Then parse tree is created out of the input_expr expression. #parse_tree = parser.parse(input_expr) #result = parser.getASG(sem_actions) #print( parse_tree ) #print( result ) 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 parse(input_expr): """ @param input_expr type str @return calcVisitor type CalcVisitor """ parser = ParserPEG(calc_grammar, "calc") parse_tree = parser.parse(input_expr) calcVisitor = CalcVisitor() visit_parse_tree(parse_tree, calcVisitor) return calcVisitor
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", True) analyseASG( parser, input, Visitor(), True, False ) # filename = sys.argv[1] # with open(filename) as file: # contents = file.read() # # # An expression we want to evaluate input_expr = """public function __construct( $rtt = hh ) { }""" # input_expr = contents input_expr = input # Then parse tree is created out of the input_expr expression. #parse_tree = parser.parse(input_expr) #result = parser.getASG(sem_actions) #print( parse_tree ) #print( result ) 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))
class ParserCantusText(): def __init__(self, grammarPath: str = GRAMMAR_PATH, root: str = 'text', **kwargs) -> None: """Initialize a Cantus Text parser. Parameters ---------- grammarPath : str, optional Path to the PEG grammar file, defaults to ``cantus/cantus_text.peg`` root : str, optional Root element of the parser, by default 'text' **kwargs Other keywords are passed to :class:`ParserPeg`. """ if not os.path.exists(grammarPath): raise Exception(f'Grammar file ({ grammarPath }) does not exist') with open(grammarPath, 'r') as handle: grammar = handle.read() self.parser = ParserPEG(grammar, root, skipws=False, **kwargs) def parse(self, text: str, debug: bool = True): # TODO docstring if not type(text) == str: return None return self.parser.parse(text)
def get_rule_parser(): grammar = ''' rule = int ':' (str / seq ('|' seq)*) seq = int+ str = '"' r'[^"]+' '"' int = r'[0-9]+' ''' return ParserPEG(grammar, root_rule_name='rule')
def solve(file, verbose): parser = ParserPEG(grammar, root_rule_name='passports', skipws=False) parse_tree = parser.parse(file.read()) required = {'byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid'} valid_keys = 0 valid_strict = 0 schema = PassportSchema() for passport in visit_parse_tree(parse_tree, PassportVisitor()): if required <= passport.keys(): valid_keys += 1 if not schema.validate(passport): valid_strict += 1 print('Part 1:', valid_keys) print('Part 2:', valid_strict)
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 = visit_parse_tree(parse_tree, CalcVisitor(debug=debug)) # 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. print("{} = {}".format(input_expr, result))
def main(debug=False): # First we will make a parser - an instance of the CVS parser model. # Parser model is given in the form of clean PEG description therefore we # are using ParserPEG class from arpeggio.clenapeg. Grammar is loaded from # csv.peg file Skipping of whitespace will be done only for tabs and # spaces. Newlines have semantics in csv files. They are used to separate # records. current_dir = os.path.dirname(__file__) csv_grammar = open(os.path.join(current_dir, 'csv.peg'), 'r').read() parser = ParserPEG(csv_grammar, 'csvfile', ws='\t ', debug=debug) # Creating parse tree out of textual input test_data = open(os.path.join(current_dir, 'test_data.csv'), 'r').read() parse_tree = parser.parse(test_data) # Create list of lists using visitor csv_content = visit_parse_tree(parse_tree, CSVVisitor()) print("This is a list of lists with the data from CSV file.") pp = pprint.PrettyPrinter(indent=4) pp.pprint(csv_content)
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", True) # 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): current_dir = os.path.dirname(__file__) # Load grammar robot_grammar = open(os.path.join(current_dir, 'robot.peg')).read() # 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) # Load program code robot_program = open(os.path.join(current_dir, 'program.rbt')).read() # We create a parse tree out of textual input parse_tree = parser.parse(robot_program) # 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))
def __init__(self, grammarPath: str = GRAMMAR_PATH, root: str = 'file', **kwargs) -> None: """ Args: grammar_path (:obj:`str`, optional): path to the grammar file (default is gabc/gabc.peg) root (:obj:`str`, optional): the root element of the parser (default is 'file') """ if not os.path.exists(grammarPath): raise Exception(f'Grammar file ({ grammarPath }) does not exist') with open(grammarPath, 'r') as handle: grammar = handle.read() self.parser = ParserPEG(grammar, root, skipws=False, memoization=True, **kwargs)
class ParserDSPL: def __init__(self, grammar, root="program", dbg=False): self.parser = ParserPEG(grammar, root, comment_rule_name=None, debug=dbg, reduce_tree=True) self.parser.autokwd = True def parse(self, input: str, dbg=False): # === LEXING / PARSING ================================================= try: parse_tree = self.parser.parse(input) except NoMatch as e: print("syntax error at: (Ln {}, Col {})".format(e.line, e.col)) quit() # === SEMANTIC ANALYSIS ================================================ return visit_parse_tree(parse_tree, Visitor(debug=dbg))
def main(): with open('java2.peg', 'r') as file: data = file.read() parser = ParserPEG(data, "java") # ok_samples = [ # "class Baz extends Korv implements Runnable, Comparable {}", # "class Bar implements Runnable, Comparable {}", # "class Korv extends Fisk {}", # "class Knas {}", # "class Foo implements Runnable {}", # "class HejA { int foo() {} }", #"class HejB { int foo(String apaB) {} }", #"class HejC { int foo(String apaC, int korvC) {} }", #"class HejD { int foo(String apaD, int korvD, Object raketD) {} }", #"class HejE { int foo() {} void bar(){} }", #"class HejF extends Knas { int foo() {} void bar(){} }", #"class HejG extends Knas implements Runnable { int foo() {} void bar(){} }", # ] # fail_samples = [ # "class {}", # "class Knas extends implements Runnable {}", # "class Foo implements {}", # ] ok_samples = [ "sampleinputs/SampleD.java", "sampleinputs/SampleB.java", "sampleinputs/SampleC.java", "sampleinputs/SampleA.java", ] for input_file in ok_samples: with open(input_file, 'r') as file: input_expr = file.read() parse_tree = parser.parse(input_expr) result = visit_parse_tree(parse_tree, CalcVisitor(debug=False)) print(str(result[0]))
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 python constructs therefore we # are using ParserPython class. with open('calc.peg', 'r') as file: data = file.read() parser = ParserPEG(data, "calc") # An expression we want to evaluate input_expr = "-(4-1)*5+(2+4.67)+5.89/(.2+7)" # We create a parse tree out of textual input_expr 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 solve(file, verbose): simple_parser = ParserPEG(simple_grammar, root_rule_name='expr') advanced_parser = ParserPEG(advanced_grammar, root_rule_name='expr') simple_sum = 0 advanced_sum = 0 for line in file: parse_tree = simple_parser.parse(line) simple_sum += visit_parse_tree(parse_tree, SimpleVisitor()) parse_tree = advanced_parser.parse(line) advanced_sum += visit_parse_tree(parse_tree, AdvancedVisitor()) print('Part 1:', simple_sum) print('Part 2:', advanced_sum)
def test_issue_22(): """ Infinite recursion during resolving of a grammar given in a clean PEG notation. """ current_dir = os.path.dirname(__file__) grammar1 = open(os.path.join(current_dir, 'grammar1.peg')).read() parser1 = ParserPEG(grammar1, 'belang') parser1.parse('a [0]') parser1.parse('a (0)') grammar2 = open(os.path.join(current_dir, 'grammar2.peg')).read() parser2 = ParserPEG(grammar2, 'belang', debug=True) parser2.parse('a [0](1)[2]')
class ParserCalc(PTNodeVisitor): def __init__(self, parser=None): self.parser = ParserPEG(grammar, 'calc', skipws=False, ignore_case=True) def __call__(self, s): try: return visit_parse_tree(self.parser.parse(s), self.CalcVisitor(debug=False)) except Exception as e: # print(e) return None class CalcVisitor(PTNodeVisitor): def visit_calc(self, n, c): _expr = lambda ast: visit_parse_tree(ast, ParserCalc.ExpressionVisitor(debug=False)) if 'repeat' in c.results: repeat = int(c.results['repeat'][0]) if 1 < repeat <= 20: return [_expr(n) for _ in range(repeat)] else: return [_expr(n)] class ExpressionVisitor(PTNodeVisitor): def visit_roll(self, n, c): if self.debug: print('roll: %s'%(c.results)) res = c.results return roll.roll( int(res['sides'][0]), count=int(res.get('count', [1])[0]), take_size=int(res.get('take_size', [0])[0]), take_higest=res.get('take_high', []) != [] ) def visit_eval(self, n, c): if self.debug: print('eval: %s'%(c.results)) c = c.results if 'int' in c: i = int(c['int'][0]) return [str(i), i] elif 'float' in c: f = c['float'][0] return [f, float(f)] else: return c['roll'][0] def visit_factor(self, n, c): if self.debug: print('factor: %s'%(c.results)) c = c.results if 'eval' in c and 'expression' in c: print('Error "eval" and "expression"') if 'eval' in c: cc = c['eval'][0] if 'expression' in c: cc = c['expression'][0] cc[0] = '(%s)'%(cc[0]) return cc def visit_term(self, n, c): if self.debug: print('term: %s'%(c.results)) if len(c.results['factor']) == 0: print('Error 0 factors in term') return ['', None] c = c.results res = c['factor'][0] if 'mul' in c: fac = c['factor'][1:] mul = c['mul'] op = lambda x, y, o: None if x is None or y is None else x * y if o == '*' else float(x) / y if y != 0 else False for i in range(0, len(fac)): m = mul[i] f = fac[i] res[1] = op(res[1], f[1], m) if not res[1]: res[1] = None f[0] = bold(f[0]) res[0] += '%s%s'%(m, f[0]) return res def visit_expression(self, n, c): if self.debug: print('expression: %s'%(c.results)) if len(c.results['term']) == 0: print('Error 0 term in expr') return ['', None] c = c.results res = c['term'][0] if 'sign' in c: res = ['-%s'%(res[0]), -1 * res[1]] if 'add' in c: fac = c['term'][1:] add = c['add'] op = lambda x, y, o: None if x is None or y is None else x + y if o == '+' else x - y for i in range(0, len(fac)): a = add[i] f = fac[i] res[0] += '%s%s'%(a, f[0]) res[1] = op(res[1], f[1], a) return res def visit_calc(self, n, c): if self.debug: print('calc: %s'%(c.results)) c = c.results if 'roll' in c: return c['roll'][0] if 'expression' in c: return c['expression'][0] return [None, None]
release = int (dot int)* pre = sep? pre_tag pre_post_num? pre_tag = "c" / "rc" / "alpha" / "a" / "beta" / "b" / "preview" / "pre" post = sep? post_tag pre_post_num? post_implicit = "-" int post_tag = "post" / "rev" / "r" pre_post_num = sep? int dev = sep? "dev" int? local = "+" r'([a-zA-Z0-9]+([-_\.][a-zA-Z0-9]+)*)' sep = dot / "-" / "_" dot = "." int = r'[0-9]+' alpha = r'[a-zA-Z0-9]' ''' _strict_parser = ParserPEG(canonical, root_rule_name='version', skipws=False) _permissive_parser = ParserPEG( permissive, root_rule_name='version', skipws=False, ignore_case=True) def unwrap_token(value): if isinstance(value, Token): return value.value return value @attr.s class Token(object): value = attr.ib()
return term def visit_expression(self, node, children): """ Adds or substracts terms. Term nodes will be already evaluated. """ if self.debug: print("Expression {}".format(children)) expr = children[0] for i in range(2, len(children), 2): if i and children[i - 1] == "-": expr -= children[i] else: expr += children[i] if self.debug: print("Expression = {}".format(expr)) return expr if __name__ == '__main__': debug = False calc_grammar = open('calc.peg').read() parser = ParserPEG(calc_grammar, "calc", debug=debug) input_expr = "2*(3+4)" parse_tree = parser.parse(input_expr) result = visit_parse_tree(parse_tree, CalcVisitor(debug=debug)) print(result)
def __init__(self, parser=None): self.parser = ParserPEG(grammar, 'calc', skipws=False, ignore_case=True)
from arpeggio import visit_parse_tree from arpeggio.cleanpeg import ParserPEG from nose.tools import assert_equal from calculette_impots_m_language_parser.scripts import m_to_ast script_dir_path = os.path.dirname(os.path.abspath(__file__)) distribution_dir_path = pkg_resources.get_distribution( 'calculette_impots_m_language_parser').location m_grammar_file_path = os.path.join(distribution_dir_path, 'm_language.cleanpeg') with open(m_grammar_file_path) as m_grammar_file: m_grammar = m_grammar_file.read() m_parser = m_to_ast.m_parser = ParserPEG(m_grammar, 'm_source_file', reduce_tree=False) class args(object): debug = False no_order = False m_to_ast.args = args def test_smoke(): smoke_m_file_path = os.path.join(script_dir_path, 'valid_formulas.m') with open(smoke_m_file_path) as smoke_m_file:
relop = '==' / '!=' / '>=' / '>' / '<=' / '<' primary = integer / function_call / variable_reference / "(" arithmetic_expression ")" function_call = variable_reference "(" call_arguments ")" / "print" "(" call_arguments ")" call_arguments = (expr ("," expr)*)? function_definition = param_list brace_block param_list = "(" identifier ("," identifier)* ")" / "(" ")" brace_block = "{" code "}" """ with open(argv[1]) as file: filePath = file.read() #PARSERPG WENT ALONG BEST WITH MY SYTAX FOR THE GRAMMER #PARSERS THROUGHT THE WHOLE SYTAX parser = ParserPEG(grammer, "program", "comment") #THIS IS THE PARSING FOR THE FILE parse_tree = parser.parse(filePath) #THIS IS THE BUILDING OF ASTTREE ast = visit_parse_tree(parse_tree, Visitor()) #CALLS INTERPRETER ast.accept(Interpreter())
import os import sys from simulator import Simulator from exceptions import SimulationError MAX_REG = 15 if len(sys.argv) == 2: MAX_REG = int(sys.argv[1]) # Load the parser with the correct grammar from arpeggio.cleanpeg import ParserPEG, NoMatch, PTNodeVisitor, visit_parse_tree grammar = open(os.path.join(os.path.dirname(__file__), 'grammar.peg'), 'r').read() parser = ParserPEG(grammar, "program", "comment", debug=False) # A little visitor to insert instructions and lines informations in the tree class SetInstructionVisitor(PTNodeVisitor): instruction = -1 line = -1 def __init__(self, instruction, line): self.instruction = instruction self.line = line super().__init__(self) def visit__default__(self, node, children): node.instruction = self.instruction node.line = self.line