simpleparse - what is wrong with my grammar?

Mike C. Fletcher mcfletch at vrplumber.com
Sun Feb 24 13:58:11 EST 2008


Laszlo Nagy wrote:
> 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 
> UTC 2008 i686 GNU/Linux
>    Python 2.5.1
>
> What is wrong with my grammar? Can it be an internal error in simpleparse?
>   
You've created an infinitely recursing grammar.  SimpleParse is a 
straightforward recursive descent parser without look-ahead (unless 
explicitly coded) or ambiguity resolution.  You are asking it to parse 
"expr" to see if "expr,binop,expr" is matched.  It will continue 
recursing down into "expr" until it runs out of ram or otherwise 
encounters a system-level fault.

You need to disambiguate your grammar to have it work with SimpleParse.  
How you do that will depend on what you really meant by expr,binop,expr:

    a&b&c

could be parsed as either:

    ((a&b)&c)

or:

    (a&(b&c))

the following creates a parser that produces the first option (which is, 
I *think* what you wanted):

expr            := (simple_expr,no_binop)/binop_expr

binop_expr := simple_expr,binop_tail
 >simple_expr< := (paren_expr/unop_expr/word)
 >no_binop< := ?-binop/end
<end>          := EOF

paren_expr      := "(",expr,")"
unop_expr       := unop,expr
 >binop_tail<      := binop,expr
unop            := ("+"/"-")
binop           := ("|"/"&"/"@")
word            := [a-zA-Z], [a-zA-Z0-9_]*

the ?- lookahead is reasonably inefficient, but lookahead is always 
going around discarding partial results anyway, so it's no worse than 
most other formulations.

HTH,
Mike

-- 
________________________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://www.vrplumber.com
  http://blog.vrplumber.com




More information about the Python-list mailing list