Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #39639

Re: How to write a language parser ?

Newsgroups comp.lang.python
Date 2013-02-22 17:25 -0800
References <51279cfe$0$293$14726298@news.sunsite.dk>
Message-ID <ee25ac81-e136-4afe-adf7-461193b3d006@googlegroups.com> (permalink)
Subject Re: How to write a language parser ?
From mbg1708@planetmail.com

Show all headers | View raw


On Friday, February 22, 2013 11:29:42 AM UTC-5, Timothy Madden wrote:
> Hello
> 
> 
> 
> I am trying to write a DBGp client in python, to be used for debugging 
> 
> mostly php scripts.
> 
> 
> 
> Currently the XDebug module for php allows me to set breakpoints on any 
> 
> line, include blank ones and lines that are not considered executable, 
> 
> resulting in breakpoints that will never be hit, even if program flow 
> 
> control appears to pass through the lines.
> 
> 
> 
> For that I would like to write a php parser, in order to detect the 
> 
> proper breakpoints line for statements spanning multiple lines.
> 
> 
> 
> Is there an (open-source) way to do to this in python code ? Most 
> 
> parsers I could see after a search are either too simple for a real 
> 
> programming language, or based on a python module written in C. My debug 
> 
> client is a Vim plugin, and I would like to distribute it as script 
> 
> files only, if that is possible. The generator itself my well be a C 
> 
> module, as I only distribute the generated output.
> 
> 
> 
> The best parser I could find is PLY, and I would like to know if it is 
> 
> good enough for the job. My attempt at a bison parser (C only) ended in 
> 
> about a hundred conflicts, most of which are difficult to understand, 
> 
> although I admit I do not know much about the subject yet.
> 
> 
> 
> Are there other parsers you have used for complete languages ?
> 
> 
> 
> Thank you,
> 
> Timothy Madden

Take a look at this whitepaper:
    http://www.cis.upenn.edu/~matuszek/General/recursive-descent-parsing.html

I needed a parser for a chunk of SQL syntax.  After trying PyBison and writing crude text analysis in Python, I found this very useful paper.  I used the advice in this paper to write my own recursive descent parser in pure Python.  The two steps were:

1.  Write a yacc syntax (without any action items).  This step allowed me to get rid of various shift and reduce conflicts in my grammar.

2.  Use the yacc grammar as a guide for the recursive descent parser.  Essentially I wrote one parser function in Python for each yacc production.

The process has the merit that the yacc syntax is known to be robust before you start coding, so the eventual Python code is based on a good design.

Good luck.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to write a language parser ? Timothy Madden <terminatorul@gmail.com> - 2013-02-22 18:29 +0200
  Re: How to write a language parser ? Chris Angelico <rosuav@gmail.com> - 2013-02-23 03:38 +1100
  Re: How to write a language parser ? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-23 00:06 +0000
  Re: How to write a language parser ? mbg1708@planetmail.com - 2013-02-22 17:25 -0800
  Re: How to write a language parser ? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-02-23 02:14 +0000
  Re: How to write a language parser ? Devin Jeanpierre <jeanpierreda@gmail.com> - 2013-02-23 05:57 -0500

csiph-web