不明确的语法和可能的修复

Ambiguous grammar and possible fixes

所以我有这种语言的语法,该语法肯定包含一些歧义,但是我发现修复它有点异常困难。下面是该语言的 BNF 语法,下面是我的快乐解析器文件的那部分。

建议语言的 BNF:

<program> ::=                                   Skel program
      "program" <id> ":" <pars> "."

--> <pars> ::=
      <par> [";" <pars>]                        parallel statements

<par> ::=
    "func" <id> <structs>                       structured expression
--> |   <pars> "||" <pars>                          parallel pipeline
|   "farm" <int> <pars>                         task farm

--> <structs> ::=
    <struct> [";" <structs>]                    statements

<struct> ::=
      <exprs>                                   expression
--> |     <structs> "•" <structs>                   composition
|     "iter" <int> <structs>                    iteration

--> <exprs> ::=
      <expr> ["," <exprs>]                      expressions

<expr> ::=
    <int>                                       integer value
    |  <string>                                 string value
    |  <bool>                                   boolean value
    |  <id> [ "=" <exprs> ]                     identifier/assignment
    |  "raise" <id> "=" <exprs>                 raise exception
    |  <exprs> "catch" <id> <id> ":" <exprs>    catch exception
    |  <exprs> <op> <exprs>                     binary operator
    |  "(" <exprs> ")"                          grouping

<op> ::=                        operators
    "+" | "*" | "-" | "div"| "<"| "<=" | "==" | "!="

Parser.y

TProgram: program ID ':' TPars '.'    { Program   }

TPars   : TPar ';'                    {   []  }
        | TPars TPar                  {  :  }

TPar    : func ID TStructs            { Function     }
      --| "||" TPars                  { Parall         }
        | farm DIGIT TPars            { Farm         }

TStructs: TStruct ';'                 {  []  }
        | TStructs TStruct            {  :  }

TStruct : TExprs                      { ExprList    }
      --| '•' TStructs                { CompOp      }
        | iter DIGIT TStructs         { Iter      }

TExprs  : TExpr                       {   []    }
        | TExprs ',' TExpr            {   :   }

BinExpr : Term                        {    }
        | BinExpr Op BinExpr          { BinOp     }

Op      : '/'                         { Divide }
        | '*'                         { Times  }
        | '-'                         { Minus  }
        | '+'                         { Plus   }

Term    : ID                          { Var  }
        | DIGIT                       { Digit  }
        | FLOAT                       { Float  }

TExpr   : '(' TExprs ')'              { ParenExpr  }
        | true                        { Bool   }
        | false                       { Bool   }
        | ID '=' TExprs               { Assign    }
        | raise ID '=' TExprs         { Raise     }
        | BinExpr                     {    }

编辑 : 我在 BNF 格式中添加了箭头,显示我认为导致语法歧义的原因。

那么,你想要怎样

a = true, false

进行解析?可以是

(a=true), false

a = (true, false)

如果这是 yacc,我建议通过为 %right %left%nonassoc 编译指示提供 '=' 和 ',' 关联性和优先级来解决冲突,也许开心支持这样的。