-
Notifications
You must be signed in to change notification settings - Fork 14
/
test.py
26 lines (21 loc) · 715 Bytes
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# SPDX-License-Identifier: MIT
# Copyright (c) 2021 Advanced Micro Devices, Inc. All rights reserved.
import pyparsing as pp
# grammar
rvalue = pp.pyparsing_common.identifier
op = pp.Literal("+")
expr = rvalue + op + rvalue
# test
print(expr.parseString("a + b")) # output : ['a','+','b']
# ...
# continue from previous snippet
class RValue():
def __init__(self,tokens):
self._value = tokens
class Op():
def __init__(self,tokens):
self._op = tokens
rvalue.setParseAction(RValue)
op.setParseAction(Op)
# run test again
print(expr.parseString("a + b")) # output : [<__main__.RValue object ...>, <__main__.Op object ...>, <__main__.RValue object ...>]