所以我正在尝试用 F# 编写一个编译器,并且一直在研究 F# powerpack 附带的 Fslex 和 Fsyacc 工具。有一个示例项目负责处理我一直试图理解的外部构建工具。可以在这里下载。该示例为我编译并运行,但我认为语法中有一个细微的错误。我说微妙,因为语法看起来类似于我在 Dragon 书中看到的用于解析表达式的语法,而且我没有发现它的经验。
输入“4*5+3”正确评估为 23。
但是,输入 4*5-3 会产生解析错误。这是 Fsyacc 生成的代码中的错误。
感谢您帮助我更好地了解问题所在,以便我能更好地了解情况并对 Fsyacc 更有信心。我在下面发布了 *.fsy 文件。
// This is the type of the data produced by a successful reduction of the 'start'
// symbol:
%type < Ast.Equation > start
%%
// These are the rules of the grammar along with the F# code of the
// actions executed as rules are reduced. In this case the actions
// produce data using F# data construction terms.
start: Prog { Equation($1) }
Prog:
| Expr EOF { $1 }
Expr:
| Expr PLUS Term { Plus($1, $3) }
| Expr MINUS Term { Minus($1, $3) }
| Term { Term($1) }
Term:
| Term ASTER Factor { Times($1, $3) }
| Term SLASH Factor { Divide($1, $3) }
| Factor { Factor($1) }
Factor:
| FLOAT { Float($1) }
| INT32 { Integer($1) }
| LPAREN Expr RPAREN { ParenEx($2) }
这是AST数据类型的定义
namespace Ast
open System
type Factor =
| Float of Double
| Integer of Int32
| ParenEx of Expr
and Term =
| Times of Term * Factor
| Divide of Term * Factor
| Factor of Factor
and Expr =
| Plus of Expr * Term
| Minus of Expr * Term
| Term of Term
and Equation =
| Equation of Expr
编辑
我已经发布了词法分析器定义和驱动解析器的代码,以帮助理解错误。
{
module Lexer
open System
open Parser
open Microsoft.FSharp.Text.Lexing
let lexeme lexbuf =
LexBuffer<char>.LexemeString lexbuf
}
// These are some regular expression definitions
let digit = ['0'-'9']
let whitespace = [' ' '\t' ]
let
newline = ('\n' | '\r' '\n')
rule tokenize = parse
| whitespace { tokenize lexbuf }
| newline { tokenize lexbuf }
// Operators
| "+" { PLUS }
| "-" { MINUS }
| "*" { ASTER }
| "/" { SLASH }
// Misc
| "(" { LPAREN }
| ")" { RPAREN }
// Numberic constants
| ['-']?digit+ { INT32 (Int32.Parse(lexeme lexbuf)) }
| ['-']?digit+('.'digit+)?(['e''E']digit+)? { FLOAT (Double.Parse(lexeme lexbuf)) }
// EOF
| eof { EOF }
最后,驱动解析器的代码。
// This project type requires the F# PowerPack at http://fsharppowerpack.codeplex.com/releases
// Learn more about F# at http://fsharp.net
// Original project template by Jomo Fisher based on work of Brian McNamara, Don Syme and Matt Valerio
// This posting is provided "AS IS" with no warranties, and confers no rights.
open System
open Microsoft.FSharp.Text.Lexing
open Ast
open Lexer
open Parser
/// Evaluate a factor
let rec evalFactor factor =
match factor with
| Float x -> x
| Integer x -> float x
| ParenEx x -> evalExpr x
/// Evaluate a term
and evalTerm term =
match term with
| Times (term1, term2) -> (evalTerm term1) * (evalTerm term2)
| Divide (term1, term2) -> (evalTerm term1) / (evalTerm term2)
| Factor fact -> evalFactor fact
/// Evaluate an expression
and evalExpr expr =
match expr with
| Plus (expr1, expr2) -> (evalExpr expr1) + (evalExpr expr2)
| Minus (expr1, expr2) -> (evalExpr expr1) - (evalExpr expr2)
| Term term -> evalTerm term
/// Evaluate an equation
and evalEquation eq =
match eq with
| Equation expr -> evalExpr expr
printfn "Calculator"
let rec readAndProcess() =
printf ":"
match Console.ReadLine() with
| "quit" -> ()
| expr ->
try
printfn "Lexing [%s]" expr
let lexbuff = LexBuffer<char>.FromString(expr)
printfn "Parsing..."
let equation = Parser.start Lexer.tokenize lexbuff
printfn "Evaluating Equation..."
let result = evalEquation equation
printfn "
Result: %s" (result.ToString())
with ex ->
printfn "Unhandled Exception: %s" ex.Message
readAndProcess()
readAndProcess()
编辑:词法分析器中的可选减号是问题所在。删除它后,样本按预期工作。