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
3
votes
2 answers

LALR grammar, trailing comma and multiline list assignment

I'm trying to produce a LALR grammar for a very simple language composed of assignments. For example: foo = "bar" bar = 42 The language should also handle list of values, for example: foo = 1, 2, 3 But I also want to handle list on multiple…
Antoine
  • 43
  • 4
3
votes
4 answers

Tokenizing left over data with lex/yacc

Forgive me, I'm completely new to parsing and lex/yacc, and I'm probably in way over my head, but nonetheless: I'm writing a pretty basic calculator with PLY, but it's input might not always be an equation, and I need to determine if it is or not…
bck
  • 45
  • 4
3
votes
3 answers

lexer error-handling PLY Python

The t_error() function is used to handle lexing errors that occur when illegal characters are detected. My question is: How can I use this function to get more specific information on errors? Like error type, in which rule or section the error…
Academia
  • 3,984
  • 6
  • 32
  • 49
3
votes
2 answers

capture in a string everything that is not a token

Context: I am dealing with a mix of boolean and arithmetic expressions that may look like in the following example: b_1 /\ (0 <= x_1) /\ (x_2 <= 2 \/ (b_3 /\ ((/ 1 3) <= x_4)))) I want to match and extract any constraint of the shape A <= B…
user13201049
3
votes
3 answers

dynamically generate regex from the keys of the dictionary python

def t_FUNC_(self, t): r'(?i)I|(?i)J|(?i)K|(?i)L|(?i)M|(?i)N|(?i)Y' return t In above function I'm returning a regex which means FUNC can be I or J or K or L or M or N or Y. Now, i have a dictionary like : dic = { 'k1':'v1',…
zubug55
  • 729
  • 7
  • 27
3
votes
0 answers

form elastic search queries after parsing expressions through ply(python library)

I have written a parser using python PLY library. Elastic search mapping schema looks like below: { "settings": { "index": { "number_of_shards": "5", "number_of_replicas": "1" } }, "mappings": { "type1": { …
3
votes
1 answer

Python PLY issue with if-else and while statements

if statements and while statement keep throwing syntax errors from p_error(p), and PLY tells me that there's a conflict at runtime. The issues are from the if-else and while statements because it was fine before adding them. Any help would be…
bbpgrs
  • 33
  • 1
  • 4
3
votes
1 answer

WARNING: yacc table file version is out of date

I have the following issue in our PythonQL project: When I'm running from Jupyter notebook, this message pops up all the time: WARNING: yacc table file version is out of date Generating LALR tables However, when using from command line, this doesn't…
randomsurfer_123
  • 315
  • 2
  • 15
3
votes
1 answer

How to set the precedence of yacc of ply

I need to make a AST from a regular expression using ply. For example, if the RE is (a|b*)abc, I want to make a pared tuple as (':', (':', (':', ('|', 'a', ('*', 'b')), 'a'), 'b'), 'c') <-- ':' means just split a string in two parts. Here is my…
auto_auto
  • 31
  • 1
  • 2
3
votes
1 answer

pycparser.plyparser.ParseError on complex struct

I'm trying to use pycparser to parse this C code: https://github.com/nbeaver/mx-trunk/blob/0b80678773582babcd56fe959d5cfbb776cc0004/libMx/d_adsc_two_theta.c A repo with a minimal example and Makefile is…
3
votes
2 answers

PLY YACC pythonic syntax for accumulating list of comma-separated values

I'm using YACC for the first time and getting used to using BNF grammar. I'm currently building a list of types from a comma separated list (eg. int, float, string): def p_type(p): '''type : primitive_type | array |…
Colin Basnett
  • 4,052
  • 2
  • 30
  • 49
3
votes
0 answers

My ply lex recognizes int numbers incorrectly

My lex needs to identify if the input is a Bin, Hex, Float, Int or ID. It identifies all correctly but if the input is, for example, 2i, it says that it is an Int and doesn't say its an error. Can I have some advice, please? Here is my code: #…
Doddy
  • 61
  • 1
  • 4
3
votes
1 answer

How to do an IF statement in PLY?

I'm doing a compiler using PLY. I have successfully implemented arithmetic and logical operations, but I'm having trouble with the 'if statement'. This is my current code: -Lexer: tokens = ( 'NAME','INT', 'DOUBLE', 'GREATER', 'LESS', …
Doddy
  • 61
  • 1
  • 4
3
votes
3 answers

python regex to find multiline C comment spanning multiple lines

I m trying to get a regex which will work on multi-line C comments. Managed to make it work for /* comments here */ but does not work if the comment goes to the next line. How do I make a regex which spans over multiple lines? Using this as my…
Thapelo
  • 31
  • 4
3
votes
1 answer

Python: Trouble with YACC

I'm using PLY to parse sentences like: "CS 2310 or equivalent experience" The desired output: [[("CS", 2310)], ["equivalent experience"]] YACC tokenizer symbols: tokens = [ 'DEPT_CODE', 'COURSE_NUMBER', 'OR_CONJ', …
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
1 2
3
24 25