语言定义的其他示例
在本章的以后部分,我们会看到有关如何解析简单的算术语言的两个扩展示例。第一个用fslex.exe 和 fsyacc.exe 实现,第二个用开源的解析库 FParsec。这种语言与前一章的非常相似,语言规范完全相同,有四则运算,只是不再使用连接符(combinator),语言本身有文字格式 *、+、/、- 来表示运算。扩展的巴氏范式(Extended Backus–Naur Form,EBNF),用下面的代码定义这种语言:
digit = "1"| "2"| "3"| "4"| "5"| "6"| "7"| "8"| "9"| "0";
numpart = digit , {digit } ;
number = [ "-" ] ,numpart , [ ".", numpart ] ;
operator = "+"| "-"| "*"| "/";
character = "A"| "B"| "C"| "D"| "E"| "F"| "G"
| "H"| "I"| "J"| "K"| "L"| "M"| "N"
| "O"| "P"| "Q"| "R"| "S"| "T"| "U"
| "V"| "W"| "X"| "Y"| "Z"
| "a"| "b"| "c"| "d"| "e"| "f"| "g"
| "h"| "i"| "j"| "k"| "l"| "m"| "n"
| "o"| "p"| "q"| "r"| "s"| "t"| "u"
| "v"| "w"| "x"| "y"| "z"| "_";
ident = character , {character } ;
ident or num =ident | number ;
expression = ident or num ;
| [ "(" ] ,ident or num , operator , ident or num , [ ")" ] ;
这种语言的表达式像这样:
1 + 2
5.87 + (8.465 / 3.243)
原文:http://blog.csdn.net/hadstj/article/details/30053617