0

I am currently using pyparsing module to create my own interpreter. My current code is

import pyparsing as pp

# To parse a packet with srcip,dstip,srcmac,dstmac
identifier = pp.Word(pp.alphas,pp.alphanums+'_')
ipfield = pp.Word(pp.nums,max=3)
ipAddr = pp.Combine(ipfield+"."+ipfield+"."+ipfield+"."+ipfield)
hexint = pp.Word(pp.hexnums,exact=2)
macAddr = pp.Combine(hexint+(":"+hexint)*5)
ip = pp.Combine(identifier+"="+ipAddr)
mac = pp.Combine(identifier+"="+macAddr)
pkt = ip & ip & mac & mac
arg = "createpacket<" + pp.Optional(pkt) + ">"
arg.parseString("createpacket<srcip=192.168.1.3dstip=192.168.1.4srcmac=00:FF:FF:FF:FF:00>")

While I run the last line of the code to parse an example string I get an error as follows:

File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/pyparsing.py", line 1041, in parseString
  raise exc
pyparsing.ParseException: Expected ">" (at char 13), (line:1, col:14)

Can anyone explain the reason for this error?

nymk
  • 3,323
  • 3
  • 34
  • 36
  • I **strongly** recommend that you read http://stackoverflow.com/editing-help then have another go at this... – jonrsharpe Jun 02 '15 at 20:57
  • 2
    `pkt` contains 2 `mac`, while the string you were parsing only contains one mac address. – nymk Jun 02 '15 at 21:17
  • 2
    @nymk is exactly right - since pkt expects 2 ips and 2 macs (in any order, given that they are joined by '&' operators), and your given example has only a single mac, then the grammar assumes that the optional pkt is not present, and tries to match the next item, which is the closing '>'. Not finding the '>', pyparsing then raises the exception. – PaulMcG Jun 02 '15 at 21:52
  • Yeah it seems that is the problem. Thanks – Preethi Srinivasan Jun 03 '15 at 16:27

0 Answers0