(请原谅我提前解决任何格式错误,因为这是我第一次在这里提出问题.)
我正在为课堂上的While语言翻译,我认为我对它背后的想法以及我需要做的事情有很好的处理.为我们提供了一些代码(Expr类型的eval函数),我们必须实现Statement类型的eval和赋值的符号查找功能.
我目前的问题是,在尝试执行上述操作时遇到类型错误,我不确定原因.
这是相关代码(请注意,此时有意将eval设置为= 0):
data Expr = Num Integer
| Var String
| Bin Op Expr Expr deriving Show
data Statement = Assign String Expr
| If Expr Statement Statement
| While Expr Statement
| Compound [Statement] deriving Show
env = [("n",1), ("fact", 1)]
eval (Num x) _ = x
eval (Var v) e = xlookup v e
where xlookup v ((w, x):r) | v==w = x
| otherwise = xlookup v r
eval (Bin op l r) e …Run Code Online (Sandbox Code Playgroud) 所以我有一个Node对象的抽象语法树.每个节点都有任意数量的子节点以及任意数量的标签,这些标签是通过std :: map结构附加到节点的信息的花絮.现在我想以类似XML的格式打印整个语法树.为此我使用这个功能:
int __ostreamNode_indent = 0;
std::ostream & operator << ( std::ostream & ss, Node* n )
{
for( int i = 0 ; i < __ostreamNode_indent ; ++i )
ss << " ";
ss << "<" << n->getSymbolType() << " ";
for( std::map<std::string,std::string>::iterator itr = n->getTags().begin() ; itr != n->getTags().end() ; ++itr )
{
ss << itr->first << "=\"" << itr->second << "\" ";
}
ss << "numtags=" << n->getTags().size() << " ";
if( n->getChildren().size() == 0 ) …Run Code Online (Sandbox Code Playgroud) 2.10.3的Scala API文档说我可以"使用refiy生成表示给定Scala表达式的抽象语法树".因此,我可以这样做:
scala> val uni = scala.reflect.runtime.universe
uni: scala.reflect.api.JavaUniverse = scala.reflect.runtime.JavaUniverse@4e42766
scala> uni reify { 1 to 3 }
res2: uni.Expr[scala.collection.immutable.Range.Inclusive] = Expr[scala.collection.immutable.Range.Inclusive](Predef.intWrapper(1).to(3))
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,我得到了我正在寻找的东西:( Predef.intWrapper(1).to(3))一个方便的扩展表示要执行的操作).
1 + 3然而,当我试图进行具体化时,我并不表示要执行我想要的操作.
scala> uni reify { 1 + 3 }
res5: uni.Expr[Int(4)] = Expr[Int(4)](4)
Run Code Online (Sandbox Code Playgroud)
这是预期的行为吗?是+原始的操作,因此,没有具体化?
Scala文档显示了一个具体化的例子,暗示了一个更有用的表示形式:
reify{ 2 + 4 } // Apply( Select( Literal(Constant(2)), newTermName("$plus")), List( Literal(Constant(4)) ) )
Run Code Online (Sandbox Code Playgroud)
如何检查1 + 3(如果存在)适当的扩展表示,以及如何检索任何表达式的详细表示(紧接在上面)?
编辑:我现在看到Scala文档中的表示显示是由showRaw.但是,我仍然无法重现上面示例中的原始表示形式2 + 4.
我有以下代码 -
from sys import version
class ExampleClass(object):
def get_sys_version(self):
return version
x = ExampleClass()
print x.get_sys_version()
Run Code Online (Sandbox Code Playgroud)
它被这段代码解析 -
import ast
source = open("input.py")
code = source.read()
node = ast.parse(code, mode='eval')
Run Code Online (Sandbox Code Playgroud)
并导致此错误 -
Traceback (most recent call last):
File "parse.py", line 5, in <module>
node = ast.parse(code, mode='eval')
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.py", line 37, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
File "<unknown>", line 1
from sys import version
Run Code Online (Sandbox Code Playgroud)
这似乎是一个非常简单的解析文件 - 它肯定会运行 - 为什么解析器抛出这个错误?
我有一组网络协议的 XML 描述,我试图从中生成 Go 代码,因此我没有任何现有的 Go 代码可以使用。所有使用go/ast(例如go fmt)的示例都采用现有代码,对 AST 进行一些转换,然后将它们写回。因为我只有 XML 文件,所以我正在使用的 AST 必须手工编写。问题是我无法让我的手写 AST 输出。
package main
import (
"go/ast"
"go/printer"
"go/token"
"os"
)
func main() {
f := ast.File{
Name: ast.NewIdent("foo"),
Decls: []ast.Decl{
&ast.GenDecl{
Tok: token.TYPE,
Specs: []ast.Spec{
&ast.TypeSpec{
Name: ast.NewIdent("Bar"),
Type: ast.NewIdent("uint32"),
},
},
},
},
}
fset := token.NewFileSet()
printer.Fprint(os.Stdout, fset, f)
}
Run Code Online (Sandbox Code Playgroud)
预期输出:
package foo
type Bar uint32
Run Code Online (Sandbox Code Playgroud)
实际输出:无
如何让 AST 打印?
我正在编写一个基于Python的列表推导的宏@vcomp(矢量理解)和一个条件子句,以简洁的方式过滤元素.
macro vcomp(comprehension::Expr, when::Symbol, condition)
comp_head, comp_args = comprehension.head, comprehension.args
comp_head ? [:comprehension, :typed_comprehension] && error("@vcomp not a comprehension")
when ? :when && error("@vcomp expected `when`, got: `$when`")
T = comp_head == :typed_comprehension ? comp_args[1] : nothing
if VERSION < v"0.5-"
element = comp_head == :comprehension ? comp_args[1] : comp_args[2]
sequence = comp_head == :comprehension ? comp_args[2] : comp_args[3]
else
element = comp_head == :comprehension ? comp_args[1].args[1] : comp_args[2].args[1]
sequence = comp_head == :comprehension ? comp_args[1].args[2] …Run Code Online (Sandbox Code Playgroud) 我正在使用JavaParser并关注其 Wiki。问题是即使我更改了方法的名称并向其添加了参数,文件也不会更新。换句话说,不会保存更改。当我System.out.println更改时CompilationUnit,它会打印更改,但这些更改根本不会影响源文件。
有什么类似的东西CompilationUnit.update()还是我错过了什么?
我从 Wiki 中使用的示例:
files_list = FilePicker.chooseAndGetJavaFiles();
if (files_list == null || files_list.isEmpty()) {
Errors.showError(Errors.COMMENT_GENERATOR_FILELIST_NULL_OR_EMPTY);
} else {
CompilationUnit cu = null;
FileInputStream in = new FileInputStream(files_list.get(0));
try {
cu = JavaParser.parse(in);
} catch (ParseException ex) {
Logger.getLogger(CommentGenerator.class.getName()).log(Level.SEVERE, null, ex);
} finally{
in.close();
}
new MethodChangerVisitor().visit(cu,null);
System.out.println(cu.toString());
}
}
private static class MethodChangerVisitor extends VoidVisitorAdapter{
@Override
public void visit(MethodDeclaration n, Object arg) {
// change the name of the method …Run Code Online (Sandbox Code Playgroud) java visitor abstract-syntax-tree compilationunit javaparser
我正在和一个朋友一起研究一种语言的解释器,我们从一个我猜不是那么明智的决定开始:我们首先制作了所有要执行的元素(实际上是一个由不同类组成的树);但是现在查看 boost 示例,我对如何合并两者感到很困惑。我知道从什么开始(语法),我知道要达到什么(实例化的类彼此拥有),我不知道如何达到它。
我们从没有变量的表达式开始,因此我们查看了精神计算器示例;但我不明白什么时候实例化元素。
表达项示例:
namespace exp
{
class op
{
private:
public:
virtual double exec(function_scope &fs);
};
class operand : public op
{
private:
double value;
public:
operand(double value);
double exec(function_scope &fs);
};
class op_bin : public op
{
private:
public:
op * ll;
op* rr;
op_bin(op* ll, op* rr);
~op_bin();
};
namespace bin
{
class sum : public op_bin
{
public:
sum(op* ll, op* rr);
double exec(function_scope &fs);
};
}
}
Run Code Online (Sandbox Code Playgroud)
忽略 exec 函数,它在运行时使用。
例如,代码 5 + (2 …
我有 python 3.6。我正在尝试使用以下方法安装 ast 库:
sudo pip3 install ast
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误,我不知道为什么以及如何解决它。
WARNING: The directory '/home/x/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. WARNING: The directory '/home/x/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. …Run Code Online (Sandbox Code Playgroud) python ×3
c++ ×2
boost-spirit ×1
go ×1
haskell ×1
java ×1
javaparser ×1
julia ×1
parsing ×1
pip ×1
python-3.x ×1
reification ×1
scala ×1
stdmap ×1
stl ×1
visitor ×1
xml ×1