-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParse.fs
50 lines (40 loc) · 1.24 KB
/
Parse.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
(* Lexing and parsing of micro-C programs using fslex and fsyacc *)
module Parse
open System
open System.IO
open System.Text
open FSharp.Text
open Absyn
open Debug
(* Plain parsing from a string, with poor error reporting *)
let fromString (str: string) : program =
let lexbuf = Lexing.LexBuffer<char>.FromString (str)
try
CPar.Main CLex.Token lexbuf
with
| exn ->
let pos = lexbuf.EndPos
failwithf "%s near line %d, column %d\n" (exn.Message) (pos.Line + 1) pos.Column
// 词法分析程序,info 在调试的时候被调用,显示Token
// CLex.Token 词法分析程序入口
let token buf =
let res = CLex.Token buf
msg
<| match res with
| CPar.EOF -> sprintf "%A\n" res
| _ -> sprintf "%A, " res
res
(* Parsing from a file *)
let fromFile (filename: string) =
use reader = new StreamReader(filename)
let lexbuf = Lexing.LexBuffer<char>.FromTextReader reader
try
msg "\nToken:\n"
//CPar.Main 语法分析主程序
let ast = CPar.Main token lexbuf in
msg "\nAST:\n"
ast
with
| exn ->
let pos = lexbuf.EndPos
failwithf "%s in file %s near line %d, column %d\n" (exn.Message) filename (pos.Line + 1) pos.Column