我有一个 dict ex 形式的字符串输出。
{'key1':'value1','key2':'value2'}
Run Code Online (Sandbox Code Playgroud)
如何轻松地将其保存为字典而不是字符串?
是否有任何工具可以将C++代码转换为xml,或者其他一些更容易解析的格式?如果它也可以选择将xml转回C++,那就太好了.我已经知道了doxygen的xml格式......也许它只是我,但我觉得它没有特别的帮助.
我使用go/parser来解析 golang 文件并检查它的 AST。我有一个特定的问题,我想使用 go/parser 但我遇到了障碍。
考虑以下文件存在于 GOPATH/src 中
$GOPATH/src/
example.go
example_package/
example_package.go
Run Code Online (Sandbox Code Playgroud)
以下是上述文件的内容
例子.go
package main
import (
"example_package"
)
type MyObject struct {
base *example_package.BaseObject
}
func DoMyThing(arg *example_package.FirstArg) {
arg.Write(10)
}
func DoMyAnotherThing() {
}
func main() {
example_package.GetItStarted(&MyObject{})
}
Run Code Online (Sandbox Code Playgroud)
example_package.go
package example_package
func GetItStarted(obj interface{}) {
}
type FirstArg interface {
Read() int
Write(x int)
}
type BaseObject struct {
}
func (p *BaseObject) DoSomething(arg *FirstArg, a int) {
arg.Write(arg.Read() + a)
}
Run Code Online (Sandbox Code Playgroud)
我的意图是编写一个名为 …
>>> parseprint("l[1:2, 3]")
Module(body=[
Expr(value=Subscript(value=Name(id='l', ctx=Load()), slice=ExtSlice(dims=[
Slice(lower=Num(n=1), upper=Num(n=2), step=None),
Index(value=Num(n=3)),
]), ctx=Load())),
])
Run Code Online (Sandbox Code Playgroud)
但是,此语法在交互式python shell中不起作用:
>>> foo = range(10)
>>> foo[1:2,3]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not tuple
Run Code Online (Sandbox Code Playgroud)
有人知道如何使用此功能吗?
相关讨论:
我对 Haskell 和一般的函数式编程很陌生,所以如果这个问题看起来很简单或很愚蠢,请原谅我。
我有一个用于生成抽象语法树的简单语言的解析器。为了展平 AST(将 while 和 if 语句转换为跳转),我需要在树中放置标签。问题是我不知道下一个标签应该是什么(我还在急切地思考,因为如果我有状态,这些都不是问题)。
到目前为止,我拥有的功能如下:
transform :: Stmt -> FStmt
transform (Seq stmt) = FSeq (map transform stmt)
transform (Assign var val) = FAssign var val
transform (While cond stmt) = FWhile "label1" (Jumpf cond "label2") (transform stmt) (Jump "label1") "label2"
transform (If cond stmt1 stmt2) = FIf (Jumpf cond "label") (transform stmt1) "label" (transform stmt2)
Run Code Online (Sandbox Code Playgroud)
在当前状态下,该函数“展平”了 AST,但不会尝试放置正确的标签(它对每个构造使用相同的字符串)。
基本上问题在于,在顺序语句(每个程序都是顺序语句)的情况下,我想不出一种方法来传递应该在标签中使用的下一个值。
先感谢您。
我的问题是重新+定义 - 运算符用ast评估表达式.我有一个表达式列表,使用eval()很容易解决:
>>> expr = '1+2*3**4/5'
>>> print(eval(expr))
33.4
Run Code Online (Sandbox Code Playgroud)
但我喜欢重新定义+-operator(添加)列表和dict,如下所示:
expr = '[1,2,3]+[4,5,6]'
Run Code Online (Sandbox Code Playgroud)
eval的常规结果是
[1,2,3,4,5,6]
但我想拥有
[5,7,9]
喜欢用R语言.
对于这样的字典,这同样适用:
expr = "{'a':1, 'b':2} + {'a':3, 'b':4}"
Run Code Online (Sandbox Code Playgroud)
我想要
{'a':4,'b':6}
简而言之,我认为要替换普通的add函数,即当操作数是list或dict正确的动作时.
我试图用ast和NodeTransformer,但没有成功.有人可以帮帮我吗?
以下方法从文件的 AST 中提取所有公共方法调用。我需要从 CallExpr 中找出完整的包,例如:ast.Inspect() 是从“go/ast”导入的。我想将 pkgsInclude 字符串列表与导入的包名称匹配:
func functionCalls(path string, node *ast.File, pkgsInclude []string) int {
fCalls := 0
ast.Inspect(node, func(n ast.Node) bool {
switch fCall := n.(type) {
case *ast.CallExpr:
if fun, ok := fCall.Fun.(*ast.SelectorExpr); ok {
if fun.Sel.IsExported() {
fCalls += 1
}
}
}
return true
})
return fCalls
}
Run Code Online (Sandbox Code Playgroud) 我正在使用以下函数从 Julia AST 中去除行号:
function filter_lineno(ex::Expr)
filter!(ex.args) do e
isa(e, LineNumberNode) && return false
if isa(e, Expr)
(e::Expr).head === :line && return false
filter_lineno(e::Expr)
end
return true
end
return ex
end
Run Code Online (Sandbox Code Playgroud)
但是当代码中有宏时,这似乎无法正常工作。这是一个失败的例子:
expr = Meta.parse("begin run(``) end")
filter_lineno(expr)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
function filter_lineno(ex::Expr)
filter!(ex.args) do e
isa(e, LineNumberNode) && return false
if isa(e, Expr)
(e::Expr).head === :line && return false
filter_lineno(e::Expr)
end
return true
end
return ex
end
Run Code Online (Sandbox Code Playgroud)
处理文档字符串时的另一个示例:
expr = Meta.parse("begin \"Here is the doc\"\nmodule X end end")
filter_lineno(expr)
Run Code Online (Sandbox Code Playgroud)
产生以下结果:
quote …Run Code Online (Sandbox Code Playgroud) 使用 Boost.Spirit 将某些表达式转换为 AST 的正确方法是什么?
我试图构建它,但我认为它很混乱并且可以简化很多。
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace ast {
struct unary_operator;
struct binary_operator;
struct expression {
typedef boost::variant<
double,
std::string,
boost::recursive_wrapper<unary_operator>,
boost::recursive_wrapper<binary_operator>,
boost::recursive_wrapper<expression>
> type;
expression() {
}
template<typename Expr>
expression(const Expr &expr)
: expr(expr) {
}
expression &operator+=(expression rhs);
expression &operator-=(expression rhs);
expression &operator*=(expression rhs);
expression &operator/=(expression rhs);
expression &and_(expression rhs);
expression &or_(expression rhs);
expression &equals(expression rhs);
expression ¬_equals(expression rhs);
expression &less_than(expression rhs);
expression &less_equals(expression rhs);
expression &greater_than(expression rhs);
expression &greater_equals(expression rhs);
expression &factor(expression …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 F# 中创建一个递归下降解析器。我查看了http://www.fssnip.net/bM,但这种类型的解析器使用字符串而不是列表。
我正在努力解析括号,尤其是嵌套括号。有很多解析器失败的边缘情况。
我使用以下数据类型来表示令牌。
type Token =
| ParOpen
| ParClose
| Identifier of string
| IntLiteral of int
| Add
let example = [ParOpen;IntLiteral 3;Token.Add;IntLiteral 5;ParClose]
Run Code Online (Sandbox Code Playgroud)
下面的数据类型用于表示 AST 中的节点。这有点像生产规则。
type Expression =
| Add of Expression * Expression
| Par of Expression
| Constant of int
| Leaf //not used currently
Run Code Online (Sandbox Code Playgroud)
例如,下面的函数可用于解析“3 + 5”。但是在解析括号时它不适用于大多数情况。例如“(3 + 5) + 1”将失败。
let rec parse listOfTokens =
match listOfTokens with
| IntLiteral i::Token.Add::list -> Add(Constant i, parse list)
| IntLiteral …Run Code Online (Sandbox Code Playgroud)