antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
BSD 3-Clause "New" or "Revised" License
17k stars 3.26k forks source link

Parsing [ pl/sql grammer] too slow with C++ target #2619

Open searocsun opened 5 years ago

searocsun commented 5 years ago

Background: I want to parser pl/sql with c ++target .

Grammar: https://github.com/antlr/grammars-v4/tree/master/plsql

results: ParseTree is too slow, about 3 ~4 seconds

int main(int , const char *) { std::string str; std::string str_rep = "TO_NUMBER()"; std::string str_where = "WHERE";

std::string tmp_str;
std::string tmp_str_col;

std::string::size_type position;

ANTLRInputStream input; 

PlSqlLexer lexer(&input);

CommonTokenStream tokens(&lexer);

PlSqlParser parser(&tokens);

std::cout << "Please enter PlSql sql string: " << std::endl;

while (std::getline(std::cin, str,'^')&&str != "q"){

    transform(str.begin(), str.end(), str.begin(), ::toupper);

    input.load(str);

    lexer.Lexer::setInputStream(&input);

    tokens.setTokenSource(&lexer);

    tokens.fill();

    /*
    for (auto token : tokens.getTokens()){
    std::cout << token->toString() << std::endl;
    }
    */

    parser.setInputStream(&tokens);

    tree::ParseTree* tree = parser.sql_script();

} }

Can someone tell me how to fix this problem?

Thanks

Nic30 commented 5 years ago

Step 0: download intellij + antlr4 plugin and take look at what is slow in your grammar.

You should be able to see something like this: Screenshot from 2019-08-23 15-39-13

You can sort by time and click on row to highlight the corresponding rule.

(Note that there are also other tools which does the same.)

searocsun commented 5 years ago

@Nic30 Thank you for your advice

Nic30 commented 5 years ago

Actually, no, I did not gave you a good advice. You do have an embedded C++ in your code. (Which I did not see at the first time.)

searocsun commented 5 years ago

@Nic30 thank you so much.

bernd5 commented 3 years ago

I tried the PL/SQL grammer with a CSharp target - it is slow here, too. And it reported some ambiguities...

Could you improve the grammar?

nIxedoahz commented 2 years ago

Can you tell me how to solve this problem? Thanks

kaby76 commented 2 years ago

Please take this conversation to here. This is a problem with the grammar, not Antlr. This issue should be closed here.