Questions tagged [ply]

PLY is an implementation of lex and yacc parsing tools for Python. Please do not use this tag for the PLY graphic file format (use ply-file-format) nor for the plyr / dplyr R packages, which have their own tags.

#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.


Ply is no longer distributed in any package-installable form. Since it has no dependencies, it can be used in a project which uses Python v3.6 or later by simply copying two files into the project directory. To acquire the files, either clone github repository or just download the two files lex.py and yacc.py.

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:

370 questions
37
votes
6 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
  • 119,074
  • 188
  • 476
  • 699
29
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
  • 3,109
  • 5
  • 31
  • 50
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,657
  • 1
  • 17
  • 18
9
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
  • 11,509
  • 8
  • 34
  • 38
9
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
  • 343
  • 5
  • 15
9
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,306
  • 1
  • 12
  • 30
8
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
  • 9,065
  • 4
  • 62
  • 65
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
  • 58,192
  • 30
  • 175
  • 224
8
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
  • 521
  • 2
  • 10
  • 22
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
  • 59,527
  • 19
  • 156
  • 165
8
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
  • 345
  • 3
  • 11
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
  • 18,771
  • 17
  • 83
  • 114
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,238
  • 1
  • 10
  • 13
7
votes
2 answers

Parsing python with PLY, how to code the indent and dedent part

I was trying to parse the function definition for the python language with PLY. I am encountering issues related to the indentation. For instance for a for statement, I would like to be able to know when the block ends. I read the python grammar…
joetde
  • 1,556
  • 1
  • 16
  • 26
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,287
  • 1
  • 10
  • 13
1
2 3
24 25