Example #1
0
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)
Example #2
0
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))
Example #3
0
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)
Example #4
0
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] ]"
Example #5
0
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"
Example #6
0
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"
Example #7
0
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] ]"
Example #8
0
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 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")
Example #10
0
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'
Example #11
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, "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))
Example #12
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, "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))
Example #13
0
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)
Example #14
0
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 __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)
Example #16
0
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))
Example #17
0
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)
Example #18
0
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")
Example #19
0
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"
Example #20
0
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))
Example #21
0
        # 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, 'program')

        # Then we export it to a dot file in order to visualize it. This is
        # particularly handy for debugging purposes.
        # We can make a jpg out of it using dot (part of graphviz) like this
        # dot -O -Tjpg robot_peg_parse_tree_model.dot
        PMDOTExport().exportFile(parser.parser_model,
                        "robot_peg_parse_tree_model.dot")
                
        # We create a parse tree out of textual input
        parse_tree = parser.parse(input)
        
        # Then we export it to a dot file in order to visualize it.
        # dot -O -Tjpg robot_peg_parse_tree.dot
        PTDOTExport().exportFile(parse_tree,
                        "robot_peg_parse_tree.dot")
Example #22
0
 QUESTION <- '?';
 STAR <- '*';
 PLUS <- '+';
 OPEN <- '(';
 CLOSE <- ')';
 DOT <- '.';
 comment <- '//' r'.*\n';
"""
    

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).
Example #23
0
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"
Example #24
0
def get_parser(root, debug=False):
    if not root in _PARSER:
        _PARSER[root] = ParserPEG(kusto_peg, root, debug=debug)

    return _PARSER[root]
Example #25
0
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):
Example #26
0
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)
Example #27
0
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')
Example #28
0
# Rules are mapped to semantic actions    
sem_actions = {
    "number" : ToFloat(),
    "factor" : Factor(),
    "term"   : Term(),
    "expression" : Expr(),
    "calc"   : Calc()
}

try:    
    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")
Example #29
0
    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)