所以我对一种语言有这种语法,语法肯定含有一些含糊之处,但是我发现解决这个问题有点异常困难。下面是该语言的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格式的箭头,显示了我认为导致语法含糊不清的内容。
答案 0 :(得分:3)
那么,你想要什么?
a = true, false
要解析?它可能是
(a=true), false
或
a = (true, false)
如果这是yacc,我建议通过提供&#39; =&#39;来解决冲突。和&#39;,&#39;与%right
%left
和%nonassoc
pragma的关联性和优先级,也许Happy支持这样的事情。