Questions tagged [ply]

PLY is an implementation of lex and yacc parsing tools for Python.

About PLY

PLY is a parser generator tool that uses reflection to read token definitions and production rules written in pure Python. You can, for example, define tokens with a simple string attribution or with methods containing a regular expression in its docstring.

To install PLY on your machine for python2/3, follow the steps outlined below:

  1. Download the source code from here.
  2. Unzip the downloaded zip file
  3. Navigate into the unzipped ply-3.10 folder
  4. Run the following command in your terminal: python setup.py install

If you completed all the above, you should now be able to use the PLY module. You can test it out by opening a python interpreter and typing import ply.lex.

Note: Do not use pip to install PLY, it will install a broken distribution on your machine.


Examples of token definitions with code to interpret value:

def t_BOOLEAN(token):
    r'(?:true|false)'
    token.value = token.value == "true"
    return token
def t_NUMBER(token):
    r'[0-9]+'
    token.value = int(token.value)
    return token


Related tags:

317 questions
32
votes
5 answers

How best to parse a simple grammar?

Ok, so I've asked a bunch of smaller questions about this project, but I still don't have much confidence in the designs I'm coming up with, so I'm going to ask a question on a broader scale. I am parsing pre-requisite descriptions for a course…
Nick Heiner
  • 108,809
  • 177
  • 454
  • 689
19
votes
4 answers

Python - Display 3D Point Cloud

I have a .PLY file that contains a 3D Point Cloud: I want to plot it and visualize it in Python. The .PLY file contains ONLY vertex and NOT faces. Could you indicate me a simple Python library that will take care of plotting the 3D Point Cloud? It…
Employee
  • 2,434
  • 3
  • 22
  • 42
14
votes
2 answers

Lex strings with single, double, or triple quotes

My objective is to parse like Python does with strings. Question: How to write a lex to support the following: "string..." 'string...' """multi line string \n \n end""" '''multi line string \n \n end''' Some code: states = ( ('string',…
Steve Peak
  • 2,447
  • 1
  • 14
  • 18
9
votes
4 answers

How to Count Unique rows in a data frame?

I have a data frame in R which has a lot of duplicate records. I am interested in finding out how many records of each are in this data frame. For example, I have this data frame: Fake Name Fake ID Fake Status Fake Program June …
Alokin
  • 287
  • 2
  • 12
8
votes
3 answers

Implementing goto in an ast

Background: As a short project over winter break, I'm trying to implement a programming language called Axe (designed for graphing calculators) using Python and PLY. A brief note: the language allows only global variables and makes heavy use of…
Michael0x2a
  • 41,137
  • 26
  • 119
  • 175
8
votes
3 answers

PLY: quickly parsing long lists of items?

I'm working with a fairly simple parser in PLY, and one of my rules takes on the following form: def p_things(p): ''' things : thing things things : thing ''' p[0] = [p[1]] if len(p) == 3: p[0] += p[2] Input files…
Tim
  • 57,767
  • 18
  • 155
  • 161
8
votes
4 answers

How to write a regular expression to match a string literal where the escape is a doubling of the quote character?

I am writing a parser using ply that needs to identify FORTRAN string literals. These are quoted with single quotes with the escape character being doubled single quotes. i.e. 'I don''t understand what you mean' is a valid escaped FORTRAN…
Brendan
  • 17,191
  • 17
  • 78
  • 103
8
votes
5 answers

How to prevent table regeneration in PLY

I am using PLY in a command line application that I package as a Python egg to be installed via pip. Everytime I run my script from the command line, I see the following message: "Generating LALR tables" Additionally, parser.out and parsetab.py…
Michael
  • 1,206
  • 1
  • 11
  • 27
7
votes
3 answers

Controlling Python PLY lexer states from parser

I am working on a simple SQL select like query parser and I need to be able to capture subqueries that can occur at certain places literally. I found lexer states are the best solution and was able to do a POC using curly braces to mark the start…
haridsv
  • 7,212
  • 4
  • 56
  • 57
7
votes
1 answer

EOF error in parser YACC

I am trying to parse a string using the yacc parser provided in the PLY library for Python. The parser itself is very long, but the problem that i am having is that it always gives me the same error, no matter what kind of string i put. The error…
camelCase
  • 513
  • 2
  • 9
  • 21
7
votes
1 answer

Using PLY to parse SQL statements

I know there are other tools out there to parse SQL statements, but I am rolling out my own for educational purposes. I am getting stuck with my grammar right now.. If you can spot an error real quick please let me know. SELECT = r'SELECT' FROM =…
sampwing
  • 1,158
  • 1
  • 7
  • 13
7
votes
2 answers

Ply Lex parsing problem

I'm using ply as my lex parser. My specifications are the following : t_WHILE = r'while' t_THEN = r'then' t_ID = r'[a-zA-Z_][a-zA-Z0-9_]*' t_NUMBER = r'\d+' t_LESSEQUAL = r'<=' t_ASSIGN = r'=' t_ignore = r' \t' When i try to parse…
Karan
  • 10,539
  • 6
  • 32
  • 38
7
votes
1 answer

how to encapsulate the lex and yacc of PLY in two seperate class

I am writing my own parser with PLY. I want to encapsulate the lex and yacc respectively Here is the code for class Lex: class Lex: tokens = ( 'NAME', 'NUMBER', ) literals = ['=', '+', '-', '*', '/', '(', ')'] # Tokens …
Ryan
  • 315
  • 3
  • 10
7
votes
2 answers

Problems with PLY LEX and YACC

I am trying to run the first part of a simple example of the PLY but I encounter a strange error. When I run the following code, it gives me an error regarding lex.lex() Anyone knows what the problem is? import ply.lex as lex tokens = […
sabzdarsabz
  • 313
  • 3
  • 15
6
votes
2 answers

ply lexmatch regular expression has different groups than a usual re

I am using ply and have noticed a strange discrepancy between the token re match stored in t.lex.lexmatch, as compared with an sre_pattern defined in the usual way with the re module. The group(x)'s seem to be off by 1. I have defined a simple…
murftown
  • 1,127
  • 1
  • 9
  • 12
1
2 3
21 22