所以我有这种语言的语法,语法肯定包含一些歧义,但是我发现修复这个语法有点异常困难。下面是该语言的 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"| "<"| "<=" | "==" | "!="
解析器
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 格式中添加了箭头,显示了我认为导致语法歧义的原因。