lexing nested parenthesis

Kristian Ovaska kristian.ovaska at helsinki.fi
Tue Jul 30 04:08:04 EDT 2002


Dave Cinege <dcinege at psychosis.com>:
>Primarily I want to do search, evaluate, modify, and replace
>on a line by line basis. The diffculty comes with matching
>arbitrary nested constructs, IE nested parenthesis.

You can't parse those with regular languages - that's a mathematical
fact. Maybe you can play some tricks with nonregular extension, or
maybe not, but I don't think it's worth trying.

You need a stronger parsing method that handles nested constructs. The
language cathegory you're dealing with is called context-free
grammars. It's more expressive than regular languages, and it can
parse any programming language.

Yacc is a classic C tool for this. PLY is a Lex+Yacc implementation in
pure Python. SPARK is another Python tool, and there are some others.

-- 
Kristian Ovaska <kristian.ovaska at helsinki.fi>



More information about the Python-list mailing list