[英]How solve OutOfMemoryException when parsing large files with Antlr4 in c#?
我正在嘗試使用c#解析Antlr4的大文件(大約500MB)。 但我有一個OutOfMemoryException。
我目前的代碼描述如下:
var path = GetInput(Path.Combine(DatFilePath)); // Build the large file
var inputStream = new StreamReader(path);
var input = new UnbufferedCharStream(inputStream);
GroupGrammarLexer lexer = new GroupGrammarLexer(input);
lexer.TokenFactory = new CommonTokenFactory(true);
var tokens = new UnbufferedTokenStream(lexer);
GroupGrammarParser parser = new GroupGrammarParser(tokens);
parser.BuildParseTree = false;
GroupGrammarParser.FileContext tree = parser.file(); // here I get OutOfMemoryException
我的語法:
語法GroupGrammar;
/*
* Parser Rules
*/
file: row+;
row: group | comment | not;
group: GROUP NAME ATTACHTO NAME;
comment: '**' .*? NL;
not: .*? NL;
GROUP : '*'? G R O U P ;
ATTACHTO : '*'? A T T A C H T O ;
W : ('W'|'w') ;
E : ('E'|'e') ;
L : ('L'|'l') ;
G : ('G'|'g') ;
R : ('R'|'r') ;
O : ('O'|'o') ;
U : ('U'|'u') ;
P : ('P'|'p') ;
A : ('A'|'a') ;
T : ('T'|'t') ;
C : ('C'|'c') ;
H : ('H'|'h') ;
NAME : '\''[a-zA-Z0-9_]+'\'' ;
WS: (' ') -> skip;
NL: '\r'? '\n';
我已經完成了關於大文件的所有建議,但我仍然得到了OutOfMemoryException。 當我使用較小的文件測試此代碼時,它工作得很好。
有什么東西我不見了嗎?
我感謝任何幫助。
最好的祝福
嘗試在堆棧大小增加的線程中運行標記化和解析:
Thread thread = new Thread(delegate ()
{
// Tokenize and parse here
},
500000);
thread.Start();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.