好吧,你对词法分析和解析有点困惑吗?
我看到的是:
如果我的词位是#include ident,我想解析ident指向的文件中的内容 并添加它。
然后你会混淆解析和词法分析
你可以写这样的东西:(这是一个小程序,但它可以工作;-))
ast.mli
type operation =
| Plus of operation * operation
| Minus of operation * operation
| Int of int
type prog = string list * operation list
词法分析器
{
open Parser
open Lexing
open Ast
let current_pos b =
lexeme_start_p b,
lexeme_end_p b
}
let newline = '\n'
let space = [' ' '\t' '\r']
let digit = ['0' - '9']
let integer = digit+
rule token = parse
| newline { token lexbuf}
| space+ { token lexbuf}
| "#include \"" ( [^'"' '\n']* as filename) '"' { INCLUDE filename }
| integer as i { INTEGER (int_of_string i) }
| "+" { PLUSI }
| "-" { MINUSI }
| ";" { SC }
| "main" { MAIN }
| eof
{ EOF }
解析器
%{
open Ast
%}
%token <string> INCLUDE
%token EOF SC
%token PLUSI
%token MINUSI
%token MAIN
%token <int> INTEGER
%left PLUSI MINUSI
%start <Ast.prog> prog
%%
prog:
include_list MAIN operations EOF { ($1, $3) }
include_list:
| { [] }
| INCLUDE include_list { $1 :: $2 }
operation:
| operation PLUSI operation { Plus ($1, $3) }
| operation MINUSI operation { Minus ($1, $3) }
| INTEGER { Int $1 }
operations:
| operation { [$1] }
| operation SC operations { $1 :: $3 }
所以,正如你所看到的,当我解析时,我记得我必须解析的文件名和
主文件
open Lexing
open Ast
let rec print_op fmt op =
match op with
| Plus (op1, op2) ->
Format.fprintf fmt "(%a + %a)"
print_op op1 print_op op2
| Minus (op1, op2) ->
Format.fprintf fmt "(%a - %a)"
print_op op1 print_op op2
| Int i -> Format.fprintf fmt "%d" i
let rec read_includes fl =
List.fold_left (fun acc f ->
let c = open_in f in
let lb = Lexing.from_channel c in
let fl, p = Parser.prog Lexer.token lb in
close_in c;
let acc' = read_includes fl in
acc' @ p
) [] fl
let () =
try
let p = read_includes [Sys.argv.(1)] in
List.iter (Format.eprintf "%a@." print_op) p
with _ -> Format.eprintf "Bad Boy !@."
这意味着当我完成第一个文件的解析时,我会解析包含的文件。
最重要的是您对词法分析的困惑(这是编译器中最愚蠢的事情,您只需问“您看到的下一个标记是什么?”他回答“我看到#include "filename"
了”,而解析器并不那么愚蠢并说“嘿,词法分析器看到了,#include "filename"
所以我会记住这个文件名,因为我可能需要它,我会继续前进。
如果我有这三个文件:
文件 1
#include "file2"
main
6; 7
文件2
#include "file3"
main
4; 5
文件 3
main
1; 2; 3
如果我打电话./compile file1
,我有1 2 3 4 5 6
我想要的输出。;-)
[编辑]
新版本的词法分析器处理包括:
ast.mli
type operation =
| Plus of operation * operation
| Minus of operation * operation
| Int of int
type prog = operation list
词法分析器
{
open Parser
let fset = Hashtbl.create 17
(* set keeping all the filenames *)
}
let newline = '\n'
let space = [' ' '\t' '\r']
let digit = ['0' - '9']
let integer = digit+
rule token = parse
| newline { token lexbuf}
| space+ { token lexbuf}
| "#include \"" ( [^'"' '\n']* as filename) '"'
{ if Hashtbl.mem fset filename then
raise Exit
else
let c = open_in filename in
Hashtbl.add fset filename ();
let lb = Lexing.from_channel c in
let p = Parser.prog token lb in
INCLUDE p
}
| integer as i { INTEGER (int_of_string i) }
| "+" { PLUSI }
| "-" { MINUSI }
| ";" { SC }
| "main" { MAIN }
| eof
{ EOF }
解析器
%{
open Ast
%}
%token <Ast.prog> INCLUDE
%token EOF SC
%token PLUSI
%token MINUSI
%token MAIN
%token <int> INTEGER
%left PLUSI MINUSI
%start <Ast.prog> prog
%%
prog:
include_list MAIN operations EOF { List.rev_append (List.rev $1) $3 }
include_list:
| { [] }
| INCLUDE include_list { List.rev_append (List.rev $1) $2 }
operation:
| operation PLUSI operation { Plus ($1, $3) }
| operation MINUSI operation { Minus ($1, $3) }
| INTEGER { Int $1 }
operations:
| operation { [$1] }
| operation SC operations { $1 :: $3 }
主文件
open Lexing
open Ast
let rec print_op fmt op =
match op with
| Plus (op1, op2) ->
Format.fprintf fmt "(%a + %a)"
print_op op1 print_op op2
| Minus (op1, op2) ->
Format.fprintf fmt "(%a - %a)"
print_op op1 print_op op2
| Int i -> Format.fprintf fmt "%d" i
let () =
try
let c = open_in Sys.argv.(1) in
let lb = Lexing.from_channel c in
let p = Parser.prog Lexer.token lb in
close_in c;
List.iter (Format.eprintf "%a@." print_op) p
with _ -> Format.eprintf "Bad Boy !@."
因此,在词法分析器中,当我看到 a 时,#include filename
我立即调用链接的文件上的 Parser,并将解析后的内容filename
返回Ast.prog
到之前的解析调用。
我希望这一切对你来说都很清楚;-)
[第二次编辑]
我不能让这段代码像这样,我编辑它以避免包含循环(在 lexer.mll 中);-)