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
0
votes
1 answer

Precedence for sublanguages

I'm writing a parser in PLY for a language that consists of two sublanguages: the "normal" expression language, and the language of type annotations. The problem is that they share some tokens, and that precedence differs between the two…
Jasmijn
  • 9,370
  • 2
  • 29
  • 43
0
votes
2 answers

Lexer for Parsing to the end of a line

If I have a keyword, how can I get it to, once it encounters a keyword, to just grab the rest of the line and return it as a string? Once it encounters an end of line, return everything on that line. Here is the line I'm looking at: description…
KingFish
  • 8,773
  • 12
  • 53
  • 81
0
votes
1 answer

Parsing: Why a rule I defined isn't matched?

I'm writing a trans-compiler whith PLY, in python, which aims to translate mythryl into neko. The thing is when parsing code like: fun not 1 => 0; not 0 => 1; end; This example is at:…
narke
  • 103
  • 1
  • 1
  • 4
-1
votes
1 answer

No module named 'encodings' (Python3.5 - Raspbian)

i was coding with ply library for python3 in my Raspberry Pi 3 Model B with Raspbian, so i changed my default python version from 2.7 to 3.5 by using: alias python='/usr/bin/python3.5' and then $ . ~/.bashrc it works good the first time, but with…
Brian Nieto
  • 273
  • 7
  • 17
-1
votes
2 answers

Python for creating a set operations Calculator

I'm trying to create a calculator, not for numbers, but for set operations. To illustrate the concept lets say you have a file with two columns. keyword, userid hello , john hello , alice world , alice world , john mars , john pluto ,…
broccoli
  • 4,738
  • 10
  • 42
  • 54
-2
votes
1 answer

How to use flags with python's regular expressions in string notation?

I need to use PLY for a parser, and PLY forces you to write regular expressions in string ntoation inside a token definition. For example (from the docs): # A regular expression rule with some action code def t_NUMBER(t): r'\d+' t.value =…
-2
votes
1 answer

Parsing python with PLY

I'm trying to write a python parser, and in my opiniion it could parse an "if statement" but it doesn't. It shows me a "syntax error" message. Can someone tell me what I'm doing wrong? Thanks in advance. The code is here:…
narke
  • 103
  • 1
  • 1
  • 4
-2
votes
2 answers

regex to match the argument of specific functions

how to write a regular expression to match the below string : name(abc1) or number(param9) or listget(12jtxgh) I want to match the string enclosed in brackets only if it is prepended by name or number or listget. I tried to this : r'((.*?))' and if…
zubug55
  • 729
  • 7
  • 27
-2
votes
1 answer

Python generating different files from one file

What I am trying to do is to use Python to parse script bob.ps and output bob.py and bob.cpp depending on user input. lets say we had bob.ps which is python-like simple language #comment use ShowBase # Load the environment model. environ = loadModel…
frainfreeze
  • 567
  • 6
  • 20
-2
votes
2 answers

NoneType error, using lex.lex from ply

i am trying to parse a list of words/phrases using ply and using lex.lex from that library. i have used lex.lex on a list of words before and it worked fine, just using a for loop to input into the lexer. but i keep getting the following…
tanky
  • 115
  • 1
  • 2
  • 10
1 2 3
24
25