Eleminating left recursion in parser rule of spirit x3

前端 未结 1 1411
一生所求
一生所求 2021-01-11 15:54

I am currently stuck with a rule I am trying to parse using boost spirit x3. Here is the EBNF(using the % operator from spirit for lists) for what I am trying to parse:

相关标签:
1条回答
  • 2021-01-11 16:59

    I solved this problem and the solution was incredibly easy. The trick is to change the grammar, so I dont have a left recursion, and it parses nice into my structs.

    so I changed

    type ::= class_type | lambda_type
    
    lambda_type ::= more_arg_lambda | one_arg_lambda
    
    more_arg_lambda ::= "(", type%",", ")", "=>", type
    
    one_arg_lambda ::= type, "=>", type  <- here is the left recursion
    
    class_type ::= identifier%"::", ["<", type%",", ">"]
    

    to

    type ::= class_type | lambda_type
    
    lambda_type ::= more_arg_lambda | one_arg_lambda
    
    more_arg_lambda ::= "(", type%",", ")", "=>", type
    
    one_arg_lambda ::= class_type, "=>", type  <- here is the magic trick
    
    class_type ::= identifier%"::", ["<", type%",", ">"]
    

    this second grammar descripes the exaclty same language, but without left recursion, and without changing the structure of the grammar. This was actualy luck and doesnt work for every grammar obviously.

    0 讨论(0)
提交回复
热议问题