标签: pyparsing

Python - pyparsing unicode字符

:)我尝试使用w = Word(printables),但它无法正常工作.我应该如何给出这个规范.'w'用于处理印地语字符(UTF-8)

代码指定语法并相应地解析.

671.assess  :: ?????  ::2
x=number + "." + src + "::" + w + "::" + number + "." + number
Run Code Online (Sandbox Code Playgroud)

如果它只有英文字符,那么代码对于ascii格式是正确的,但代码不适用于unicode格式.

我的意思是当我们有671.assess :: ahsaas :: 2形式的代码时代码可以工作

即它解析英文格式的单词,但我不知道如何解析然后以unicode格式打印字符.我需要这个用于英语印地语单词对齐的目的.

python代码如下所示:

# -*- coding: utf-8 -*-
from pyparsing import Literal, Word, Optional, nums, alphas, ZeroOrMore, printables , Group , alphas8bit , 
# grammar 
src = Word(printables)
trans =  Word(printables)
number = Word(nums)
x=number + "." + src + "::" + trans + "::" + number + "." + …
Run Code Online (Sandbox Code Playgroud)

python unicode nlp pyparsing

12
推荐指数
2
解决办法
6006
查看次数

通过pyparsing解析逻辑句子非常慢

我尝试使用pyparsing来解析这些逻辑表达式

x
FALSE
NOT x
(x + y <= 5) AND (y >= 10) OR NOT (z < 100 OR w)

(A=True OR NOT (G < 8) => S = J) => ((P = A) AND not (P = 1) AND (B = O)) => (S = T)

((P = T) AND NOT (K =J) AND (B = F)) => (S = O) AND
 ((P = T) OR (k and b => (8 + z <= 10)) AND NOT (a …
Run Code Online (Sandbox Code Playgroud)

python pyparsing

12
推荐指数
2
解决办法
1717
查看次数

pyparsing - 加载ABNF?

pyparsing可以从文件中读取ABNF而不必根据python对象定义它吗?

如果没有,是否有类似的东西(将ABNF文件加载到解析器对象)

python parsing pyparsing

11
推荐指数
2
解决办法
1920
查看次数

PyParsing前瞻和贪婪的表达

我正在使用PyParsing编写一个查询语言解析器,而且我已经陷入了(我相信是)一个前瞻问题.查询中的一个子句类型旨在将字符串拆分为3个部分(fieldname,operator,value),使得fieldname是一个单词,operator是一个或多个单词,value是单词,带引号的字符串或带括号的列表这些.

我的数据看起来像

author is william
author is 'william shakespeare'
author is not shakespeare
author is in (william,'the bard',shakespeare)
Run Code Online (Sandbox Code Playgroud)

我对此子句的当前解析器编写为:

fieldname = Word(alphas)

operator = OneOrMore(Word(alphas))

single_value = Word(alphas) ^ QuotedString(quoteChar="'")
list_value = Literal("(") + Group(delimitedList(single_value)) + Literal(")")
value = single_value ^ list_value

clause = fieldname + originalTextFor(operator) + value
Run Code Online (Sandbox Code Playgroud)

显然这会失败,因为operator元素是贪婪的,并且value如果它可以吞噬它.从阅读其他类似的问题和文档,我已经收集到我需要用一个NotAny或那个来管理这个前瞻FollowedBy,但是我还没有弄清楚如何使这个工作.

python pyparsing

11
推荐指数
1
解决办法
2452
查看次数

使用pyparsing改进错误消息

编辑:我做了第一个版本,Eike帮我推进了很多.我现在坚持一个更具体的问题,我将在下面描述.您可以查看历史记录中的原始问题


我正在使用pyparsing来解析用于从数据库请求特定数据的小语言.它具有许多关键字,运算符和数据类型以及布尔逻辑.

我正在尝试改进发出语法错误时发送给用户的错误消息,因为当前的错误消息不是很有用.我设计了一个小例子,类似于我正在用上述语言做的但是要小得多:

#!/usr/bin/env python                            

from pyparsing import *

def validate_number(s, loc, tokens):
    if int(tokens[0]) != 0:
        raise ParseFatalException(s, loc, "number musth be 0")

def fail(s, loc, tokens):
    raise ParseFatalException(s, loc, "Unknown token %s" % tokens[0])

def fail_value(s, loc, expr, err):
    raise ParseFatalException(s, loc, "Wrong value")

number =  Word(nums).setParseAction(validate_number).setFailAction(fail_value)
operator = Literal("=")

error = Word(alphas).setParseAction(fail)
rules = MatchFirst([
    Literal('x') + operator + number,
])

rules = operatorPrecedence(rules | error , [
    (Literal("and"), 2, opAssoc.RIGHT),
])

def try_parse(expression): …
Run Code Online (Sandbox Code Playgroud)

python pyparsing

11
推荐指数
1
解决办法
3342
查看次数

如何用pyparsing解析缩进和dedents?

这是Python语法的一个子集:

single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE

stmt: simple_stmt | compound_stmt
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE

small_stmt: pass_stmt
pass_stmt: 'pass'

compound_stmt: if_stmt
if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]

suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
Run Code Online (Sandbox Code Playgroud)

(您可以在Python SVN存储库中阅读完整语法:http://svn.python.org/.../Grammar)

我试图用这个语法在Python中生成Python的解析器.我遇到的问题是如何将这些INDENTDEDENT令牌表达为pyparsing对象.

以下是我实现其他终端的方法:

import pyparsing as p

string_start = (p.Literal('"""') | "'''" | '"' | "'")
string_token = ('\\' + p.CharsNotIn("",exact=1) | p.CharsNotIn('\\',exact=1))
string_end = p.matchPreviousExpr(string_start)

terminals = …
Run Code Online (Sandbox Code Playgroud)

python indentation parser-generator pyparsing

10
推荐指数
1
解决办法
3416
查看次数

pyparsing,forward和recursion

我正在使用pyparsing来解析vcd(值更改转储)文件.本质上,我想读取文件,将其解析为内部字典,并操纵值.

没有详细介绍结构,我的问题发生在识别嵌套类别.

在vcd文件中,您有"范围",其中包括连线和可能更深(嵌套)的范围.把它们想象成水平.

所以在我的文件中,我有:

$scope module toplevel $end
$scope module midlevel $end
$var wire a $end
$var wire b $end
$upscope $end
$var wire c $end
$var wire d $end
$var wire e $end
$scope module extralevel $end
$var wire f $end
$var wire g $end
$upscope $end
$var wire h $end
$var wire i $end
$upscope $end
Run Code Online (Sandbox Code Playgroud)

所以'toplevel'包含所有(a - i),'midlevel'包含(a - b),'extralevel'包含(f - g)等.

这是我解析此部分的代码(代码段):

scope_header = Group(Literal('$scope') + Word(alphas) + Word(alphas) + \
                     Literal('$end'))

wire_map = Group(Literal('$var') + …
Run Code Online (Sandbox Code Playgroud)

python recursion forward pyparsing

9
推荐指数
2
解决办法
3449
查看次数

扩展matplotlib mathtext解析器

对于我使用matplotlib的大多数交互式绘图,我不想使用数学的乳胶处理.(主要是因为它太慢了,但也因为它经常使用IMHO只是有点太模糊.)但是我在编写乳胶时也一直使用我自己的宏.仅作为一个例子,而不是做$M_{\odot}$我定义的事情$\Msun$.因此,当使用matplotlib时,我倾向于自动编写后者,然后得到错误并且必须修复它.这只是一个特别简单的例子,我希望能够灵活地在我的论文和我的情节中重新定义一个宏,而不需要太多的工作.

那么,是否有任何合理的方法可以扩展mathtext解析器以理解类似的东西$\Msun$?或者我是否必须破解mathtext.py或其他什么?

(我的后备是定义Msun为字符串,r'M_{\odot}'所以我可以写出类似的东西r'$M = 10\,' + Msun + '$',但这是令人不愉快的,当然对我来说不会更自动.)

latex matplotlib pyparsing

9
推荐指数
1
解决办法
748
查看次数

pyparsing捕获具有给定标题的任意文本组作为嵌套列表

我有一个类似于的文本文件;

节标题1:
有些单词可以是任何
更多单词可以是任何东西
等等lala

其他一些标题:
像以前一样可能是任何
嘿,这不是很有趣

我正在尝试用pyparser构造一个语法,当要求将解析结果作为列表时,这将导致以下列表结构; (IE;在遍历parsed.asList()元素时应该打印以下内容)

['节目标题1:',[['某些单词可以是任何内容'],['更多单词可以是任何内容'],['etc etc lala']]]
['其他标题:',[[ '就像之前可能是任何事情',['嘿,这不是很有趣']]]

标题名称都是事先已知的,并且可能会出现或不出现各个标题.如果它们确实出现,则始终至少有一行内容.

我遇到的问题是,我无法解析解析器以识别'section header 1:'ands和'some other header:'的开始位置.我最终看到了一个parsed.asList();

['节目标题1:',[[''某些单词可以是任何'],['更多单词可以是任何东西'],['etc etc lala'],['其他标题'],[' '就像之前可能是任何事情',['嘿,这不是很有趣']]]

(IE:节标题1:正确看到,但是跟随它的每一个都被添加到节标题1中,包括更多标题行等等.)

我尝试了各种各样的东西,以各种方式使用leaveWhitespace()和LineEnd(),但我无法弄清楚.

我正在讨厌的基本解析器是(人为的例子 - 实际上这是一个类定义等等.).

header_1_line=Literal('section header 1:')

text_line=Group(OneOrMore(Word(printables)))

header_1_block=Group(header_1_line+Group(OneOrMore(text_line)))

header_2_line=Literal('some other header:')

header_2_block=Group(header_2_line+Group(OneOrMore(text_line)))

overall_structure=ZeroOrMore(header_1_block|header_2_block)
Run Code Online (Sandbox Code Playgroud)

正在被召唤

parsed=overall_structure.parseFile()
Run Code Online (Sandbox Code Playgroud)

干杯,马特.

python pyparsing

9
推荐指数
1
解决办法
2540
查看次数

使用pyparsing解析嵌套函数调用

我正在尝试使用pyparsing以下形式解析函数调用:

f(x, y)
Run Code Online (Sandbox Code Playgroud)

这很简单.但由于它是一个递归下降的解析器,它也应该很容易解析:

f(g(x), y)
Run Code Online (Sandbox Code Playgroud)

这是我无法得到的.这是一个煮沸的例子:

from pyparsing import Forward, Word, alphas, alphanums, nums, ZeroOrMore, Literal

lparen = Literal("(")
rparen = Literal(")")

identifier = Word(alphas, alphanums + "_")
integer  = Word( nums )

functor = identifier

# allow expression to be used recursively
expression = Forward()

arg = identifier | integer | expression
args = arg + ZeroOrMore("," + arg)

expression << functor + lparen + args + rparen

print expression.parseString("f(x, y)")
print expression.parseString("f(g(x), y)")
Run Code Online (Sandbox Code Playgroud)

这是输出:

['f', …
Run Code Online (Sandbox Code Playgroud)

python parsing pyparsing

9
推荐指数
2
解决办法
2304
查看次数