我有一个带有 Java 前端的 DSL,我想以某种易于解析的格式序列化我在前端部分获得的 AST,以便更轻松地编写使用不同编程语言生成代码的后端部分。为此,有什么比 XML 更好的方法吗?
我有一个 python 程序的 AST,想手动评估 if 语句的条件。
cond = node.test
b = eval(compile(cond,"<string>","eval"))
print b
Run Code Online (Sandbox Code Playgroud)
其中 node 是 If-Node,给我TypeError: expected Expression node, got Compare,即使 Compare 是根据 ast 的 python 文档中的语法的表达式。
有任何想法吗?
假设我有一个文件example.py:import example
VVV = 2
DictionaryNameB = {
'a' : VVV,
'bb' : 'SomethingB',
'c' : False,
'ccc' : None,
'dddd' : 'true',
'eeeee' : 0.123456,
'f' : 2,
'h' : [1,2,3]
}
Run Code Online (Sandbox Code Playgroud)
我写了一个使用的函数ast.literal_eval():
def getDicFromFile(self, dic_name):
with open( 'example.py' ) as f:
file_data = f.read()
match = re.findall('%s[^{]+\{[^\}]+\}' % dic_name, file_data, re.MULTILINE)[0]
# print(match)
dicObject = ast.literal_eval(match[len(dic_name)+3:])
return dicObject
Run Code Online (Sandbox Code Playgroud)
我得到错误引发ValueError('malformed string'); ValueError:格式错误的字符串
我明白ast.literal_eval()无法解码变量VVV,还有另外一种方法吗?
我想使用Roslyn加载C#源并将其写入另一个文件,用替换替换关键字.样品:
for (int i=0; i<10; i++){}
Run Code Online (Sandbox Code Playgroud)
翻译成
foobar (int i=0; i<10; i++){}
Run Code Online (Sandbox Code Playgroud)
这种操作的语法可能是什么样的?
我想遍历一个具有一个成员变量和一个方法的简单类的 AST。我发现该类表示为 CXXRecordDecl。
CXXREcordDecl 中用于获取表示为 FieldDecl 的成员变量列表的 api 是什么?
我正在编写一个Clang工具来静态分析源文件,并匹配和重命名类的所有私有成员.
考虑一个例子:
class AClass { // problem: my matcher modifies AST node here too
private:
int a; // <- I know how to rename this 'a' using other matcher
public:
AClass() {
AClass cl;
this->a = 1; // <- rename this 'a'
cl.a = 2; // <- rename this 'a'
}
};
void bar(AClass);
void foo() {
//bar(AClass());
}
Run Code Online (Sandbox Code Playgroud)
我使用以下匹配器来访问我想要修改的AST节点.它像我期望的那样工作.
clang-query> match memberExpr(hasDeclaration(namedDecl(isPrivate())))
Match #1:
sum.cpp:7:9: note: "root" binds here
this->a = 1;
^~~~~~~
Match #2:
sum.cpp:8:9: note: "root" binds …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 boost 的精神气解析器创建解析器。它正在解析包含三种类型值的字符串。常量、变量或函数。这些函数可以相互嵌套。测试字符串是f(a, b) = f(g(z, x), g(x, h(x)), c),其中a-e是常量,f-r是函数,s-z是变量。我成功创建了一个可以正确解析表达式的规则。当我将解析规则的函数更改为语法时出现了问题. 有几个错误我可以修复。我几乎掌握了解析表达式的语法,并将其转换为我创建的抽象语法树。但是,我收到了关于包含在 boost 库中的文件的这个错误,我无法弄清楚它来自哪里,因为我不理解编译器消息。我正在按照网站上的示例使用员工示例将数据从解析器放入结构:http : //www.boost.org/doc/libs/1_41_0/libs/spirit/example/qi/employee。 cp
主程序
#include "Parser.h"
#include "Term.h"
#include <boost/spirit/include/qi.hpp>
#include <string>
#include <iostream>
#include <list>
using std::string;
using std::cout;
using std::endl;
int main()
{
cout << "Unification Algorithm" << endl << endl;
string phrase = "f(a, b) = f(g(z, x), g(x, h(x)), c)";
string::const_iterator itr = phrase.begin();
string::const_iterator last = phrase.end();
cout << phrase << …Run Code Online (Sandbox Code Playgroud) 我又回到探索 pegjs 了,显然还没有掌握核心概念。我正在尝试解析以谓词开头的“查询语言”,然后是操作数列表(其中可能包含另一个谓词)。所以一个简单的例子是:
OR(
"string1"
"string2"
)
Run Code Online (Sandbox Code Playgroud)
我希望将上述内容转换为:
{
predicate: "OR",
operands: [
{
type: "STRING",
value: "string1"
},
{
type: "STRING",
value: "string2"
}
]
}
Run Code Online (Sandbox Code Playgroud)
这个查询:
OR(
"string1"
"string2"
AND (
"string4"
"string5"
)
"string3"
)
Run Code Online (Sandbox Code Playgroud)
将成为这个 AST:
{
predicate: "OR",
operands: [
{
type: "STRING",
value: "string1"
},
{
type: "STRING",
value: "string2"
},
{
predicate: "AND"
operands: [
{
type: "STRING",
value: "string4"
},
{
type: "STRING",
value: "string5"
}
]
},
{
type: "STRING",
value: …Run Code Online (Sandbox Code Playgroud) 我正在学习 AST,它似乎是一个强大的东西,但我很困惑代码的去向以及它为什么消失了。说我想重写
example = """def fake(x):\n
y = ['useless list']\n
return x
"""
Run Code Online (Sandbox Code Playgroud)
作为
example = """def fake(x):\n
return x
"""
Run Code Online (Sandbox Code Playgroud)
我看不到任何以这种方式重写的方法。我什至找不到获取该行文本的方法:
In [1]: example = """def fake(x):\n
...: y = ['useless list']\n
...: return x
...: """
In [3]: import ast
In [4]: p = ast.parse(example)
In [5]: p
Out[5]: <_ast.Module at 0x7f22f7274a10>
In [6]: p.body
Out[6]: [<_ast.FunctionDef at 0x7f22f7274a50>]
In [7]: p.body
Out[7]: [<_ast.FunctionDef at 0x7f22f7274a50>]
In [8]: f = p.body[0]
In [9]: f
Out[9]: <_ast.FunctionDef at 0x7f22f7274a50> …Run Code Online (Sandbox Code Playgroud) 我一直在研究是否可以制作一个由操作和叶节点组成的非常简单的AST。但更具体地说,我希望能够将任何类型用作叶节点,而不是像这样在AST数据类型本身中明确指定它。
-- Instead of this
data Tree = Number Int | Word String | Operation Tree (Tree -> Tree -> Tree) Tree
-- I'd like something along the lines of this
data Tree a = Leaf a | Operation Tree (Tree -> Tree -> Tree) Tree
Run Code Online (Sandbox Code Playgroud)
这不一定具有很大的实用性,但是我想看看是否有可能。到目前为止,我所管理的最接近的产品要求我对GADT的概念进行摸索:
{-# LANGUAGE GADTs #-}
data Tree l where
Leaf :: l -> Tree l
Operation :: Tree a -> (a -> b -> c) -> Tree b -> Tree c
let fivePlus2 = …Run Code Online (Sandbox Code Playgroud) tree haskell functional-programming abstract-syntax-tree gadt