Cannot deepcopy AST in Python

50 views
Skip to first unread message

Charl Linssen

unread,
Apr 3, 2020, 9:46:34 AM4/3/20
to antlr-discussion
Hi everyone,

I originally posted on the GitHub issue tracker (https://github.com/antlr/antlr4/issues/2793) but was advised that the question might fit better here on the discussions group.


First, my textfile is parsed to yield an AST. I then want to make several copies of this AST, and perform different manipulations on each individual copy. I tried to use


import copy; copy.deepcopy(ast)


but this yields an error:


TypeError
: cannot serialize '_io.TextIOWrapper' object


because _output is set to wrap stdout:


 '_output': <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>


This comes from trying to copy a antlr4.Token.CommonToken instance inside my AST node class(es). stdout is assigned to _output in antlr4.Lexer.


Perhaps I am missing a simple solution for making a copy of my AST, containing generated Tokens and Context etc. instances?


Thanks!

he geoffrey

unread,
Aug 9, 2020, 10:41:13 AM8/9/20
to antlr-discussion
hello, can you tell how to build a AST.  ANTLR seams does provide Class for building AST. But, when i read the book The Definitive ANTLR 4 Reference, it doesn't teach me how to build a AST. What is more, i want to find the DefUse pair in my program which i don't know how to do by by using ANTLR.

在 2020年4月3日星期五 UTC+8下午9:46:34,Charl Linssen写道:
Reply all
Reply to author
Forward
0 new messages