472,345 Members | 1,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,345 software developers and data experts.

simpleparse parsing problem

Anyone out there use simpleparse? If so, I have a problem that I can't
seem to solve...I need to be able to parse this line:

"""Cen2 = Cen(OUT, "Cep", "ies", wh, 544, (wh/ht));"""

with this grammar:

grammar = r'''
declaration := ws, line, (ws, line)*, ws
line := (statement / assignment), ';', ws
assignment := identifier, ws, '=', ws, statement
statement := identifier, '(', arglist?, ')', chars?
identifier := ([a-zA-Z0-9_.:])+
arglist := arg, (',', ws, arg)*
arg := expr/ statement / identifier / num / str /
curve / spline / union / conditional / definition
definition := typedef?, ws, identifier, ws, '=', ws, arg
typedef := ([a-zA-Z0-9_])+
expr := termlist, ( operator, termlist )+
termlist := ( '(', expr, ')' ) / term
term := call / identifier / num
call := identifier, '(', arglist?, ')'
union := '{{', ws, (arg, ws, ';', ws)*, arg, ws, '}}'
operator := ( '+' / '-' / '/' / '*' /
'==' / '>=' / '<=' / '>' / '<' )
conditional := termlist, ws, '?', ws, termlist, ws, ':', ws, termlist
curve := (list / num), '@', num
spline := (cv, ',')*, cv
cv := identifier, '@', num
list := '[', arg, (',', ws, arg)*, ']'
str := '"', ([;] / chars)*, '"'
num := ( scinot / float / int )
<chars := ('-' / '/' / '?' / [a-zA-Z0-9_.!@#$%^&\*\+=<:])+
<int := ([-+]?, [0-9]+)
<float := ([-+]?, [0-9\.]+)
<scinot := (float, 'e', int)
<ws := [ \t\n]*
'''

But it fails. The problem is with how arglist/arg/expr are defined,
which makes it unable to handle the parenthesized expression at the end
of the line:

(wh/ht)

But everything I've tried to correct that problem fails. In the end, it
needs to be able to parse that line with those parentheses around wh/ht,
or without them.
Recursive parsing of expressions just seems hard to do in simpleparse,
and is beyond my parsing knowledge.

Here's the code to get the parser going:

from simpleparse.parser import Parser
p = Parser(grammar, 'line')
import pprint
bad_line = """Cen2 = Cen(OUT, "Cep", "ies", wh, 544, (wh/ht));"""

pprint.pprint(p.parse(bad_line))
Any help greatly appreciated, thanks,
-Dave
--
Presenting:
mediocre nebula.

Sep 2 '06 #1
1 1801
"David Hirschfield" <da****@ilm.comwrote in message
news:ma****************************************@py thon.org...
Anyone out there use simpleparse? If so, I have a problem that I can't
seem to solve...I need to be able to parse this line:

"""Cen2 = Cen(OUT, "Cep", "ies", wh, 544, (wh/ht));"""

with this grammar:

grammar = r'''
declaration := ws, line, (ws, line)*, ws
line := (statement / assignment), ';', ws
assignment := identifier, ws, '=', ws, statement
statement := identifier, '(', arglist?, ')', chars?
identifier := ([a-zA-Z0-9_.:])+
arglist := arg, (',', ws, arg)*
arg := expr/ statement / identifier / num / str /
curve / spline / union / conditional / definition
definition := typedef?, ws, identifier, ws, '=', ws, arg
typedef := ([a-zA-Z0-9_])+
expr := termlist, ( operator, termlist )+
termlist := ( '(', expr, ')' ) / term
term := call / identifier / num
call := identifier, '(', arglist?, ')'
union := '{{', ws, (arg, ws, ';', ws)*, arg, ws, '}}'
operator := ( '+' / '-' / '/' / '*' /
'==' / '>=' / '<=' / '>' / '<' )
conditional := termlist, ws, '?', ws, termlist, ws, ':', ws, termlist
curve := (list / num), '@', num
spline := (cv, ',')*, cv
cv := identifier, '@', num
list := '[', arg, (',', ws, arg)*, ']'
str := '"', ([;] / chars)*, '"'
num := ( scinot / float / int )
<chars := ('-' / '/' / '?' / [a-zA-Z0-9_.!@#$%^&\*\+=<:])+
<int := ([-+]?, [0-9]+)
<float := ([-+]?, [0-9\.]+)
<scinot := (float, 'e', int)
<ws := [ \t\n]*
'''
<snip>
>
David -

I converted your simpleparse grammar to pyparsing, which I could then
troubleshoot. Here is a working pyparsing grammar, perhaps you can convert
it back to simpleparse form and see if you make any better progress.

-- Paul
test = """Cen2 = Cen(OUT, "Cep", "ies", wh, 544, (wh/ht));"""
from pyparsing import *

# recursive items need forward decl - assign contents later using '<<'
operator
arg = Forward()
expr = Forward()
statement = Forward()

float_ = Regex (r"[-+]?[0-9]+\.[0-9]*")
int_ = Regex (r"[-+]?[0-9]+")
scinot = Combine(float_ + oneOf(list("eE")) + int_)
num = scinot | float_ | int_
str_ = dblQuotedString
list_ = "[" + delimitedList(arg) + "]"
identifier = Word(alphas, srange("[a-zA-Z0-9_.:]"))
cv = identifier + "@" + num
spline = delimitedList(cv)
curve = (list_ | num) + "@" + num
conditional = expr + "?" + expr + ":" + expr
operator = oneOf( ('+', '-', '/', '*', '==', '>=', '<=', '>', '<') )
union = "{{" + delimitedList( arg, delim=";" ) + "}}"
call = identifier + "(" + delimitedList(arg) + ")"
term = call | identifier | num | Group( "(" + expr + ")" )
expr << (term + ZeroOrMore( operator+term ) )
typedef = Word( alphas, alphanums )
definition = ( (typedef + identifier) | identifier ) + "=" + arg
arg << (expr | statement | identifier | num | str_ | "." |
curve | spline | union | conditional | definition)
assignment = identifier + "=" + statement
statement << ( call | assignment )
line_ = statement + ';'
declaration = OneOrMore(line_)

print declaration.parseString(test)

Prints:
['Cen2', '=', 'Cen', '(', 'OUT', '"Cep"', '"ies"', 'wh', '544', ['(', 'wh',
'/', 'ht', ')'], ')', ';']

Sep 2 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Fortepianissimo | last post by:
A while ago I decided to use simpleparse to write a parser for a kind of formula representation that I created. This representation is typed, so...
303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a....
16
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a...
5
by: gamehack | last post by:
Hi all, I was thinking about parsing equations but I can't think of any generic approach. Basically I have a struct called math_term which is...
9
by: ankitdesai | last post by:
I would like to parse a couple of tables within an individual player's SHTML page. For example, I would like to get the "Actual Pitching...
3
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing...
3
by: Anup Daware | last post by:
Hi Group, I am facing a strange problem here: I am trying to read xml response from a servlet using XmlTextWriter. I am able to read the read...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to...
0
by: Laszlo Nagy | last post by:
The program below gives me "segmentation fault (core dumped)". Environment: Linux gandalf-desktop 2.6.20-16-generic #2 SMP Tue Feb 12 05:41:34...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.