繁体   English   中英

模棱两可的语法和可能的修复

[英]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 $2 $4 }

TPars   : TPar ';'                    {   [$1]  }
        | TPars TPar                  { $2 : $1 }

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

TStructs: TStruct ';'                 {  [$1]  }
        | TStructs TStruct            { $2 : $1 }

TStruct : TExprs                      { ExprList $1   }
      --| '•' TStructs                { CompOp $2     }
        | iter DIGIT TStructs         { Iter $2 $3    }

TExprs  : TExpr                       {   [$1]    }
        | TExprs ',' TExpr            {  $3 : $1  }

BinExpr : Term                        {  $1  }
        | BinExpr Op BinExpr          { BinOp $2 $1 $3  }

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

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

TExpr   : '(' TExprs ')'              { ParenExpr $2 }
        | true                        { Bool $1  }
        | false                       { Bool $1  }
        | ID '=' TExprs               { Assign $1 $3  }
        | raise ID '=' TExprs         { Raise $2 $4   }
        | BinExpr                     {  $1  }

编辑 :我已经添加了BNF格式的箭头,显示了我认为导致语法模糊的内容。

那么,你想要怎么样?

a = true, false

要解析? 它可能是

(a=true), false

要么

a = (true, false)

如果这是yacc,我建议通过给出'='和','关联性和优先级与%right %left%nonassoc pragma来解决冲突,也许Happy支持这样的事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM