You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
// 调用 ANTLR 生成的 Lexer/Parser, 返回 parse tree。
# include "frontend/AntlrDriver.h"
# include <fstream>
# include <sstream>
# include <stdexcept>
# include "SysYLexer.h"
# include "SysYParser.h"
# include "antlr4-runtime.h"
AntlrResult ParseFileWithAntlr ( const std : : string & path ) {
std : : ifstream fin ( path ) ;
if ( ! fin . is_open ( ) ) {
throw std : : runtime_error ( " 无法打开输入文件: " + path ) ;
}
std : : ostringstream ss ;
ss < < fin . rdbuf ( ) ;
auto input = std : : make_unique < antlr4 : : ANTLRInputStream > ( ss . str ( ) ) ;
auto lexer = std : : make_unique < SysYLexer > ( input . get ( ) ) ;
auto tokens = std : : make_unique < antlr4 : : CommonTokenStream > ( lexer . get ( ) ) ;
auto parser = std : : make_unique < SysYParser > ( tokens . get ( ) ) ;
parser - > removeErrorListeners ( ) ;
auto tree = parser - > compUnit ( ) ;
AntlrResult result ;
result . input = std : : move ( input ) ;
result . lexer = std : : move ( lexer ) ;
result . tokens = std : : move ( tokens ) ;
result . parser = std : : move ( parser ) ;
result . tree = tree ;
return result ;
}