1

我正在为必须支持 #include 指令的类 C 语言制作编译器(仅在文件的开头)

一个简单但不优雅的方法是创建一个子程序来查找指令的每次出现,并用新临时文件中的相应文件替换。

现在这根本不好。所以我尝试了以下方法:

lexer = parse
    | "#include \""   ( [^'"' '\n']* as filename) '"'
    { lexer (Lexing.from_channel (open_in filename)) ; lexer lexbuf }

想法如下:每当你找到一个包含时,使用给定的文件名打开一个新通道,并递归地调用该通道上的“lexer”规则。之后,继续使用 lexing-buffer 的当前状态并继续进行 lexing。

问题是,它从来没有奏效。

我还看到,当缓冲区 lexbuf 达到 eof 时,可以进行填充。但我找不到更多信息。这让我想到了将上面的代码更改为以下内容:

lexer = parse
    | "#include \""   ( [^'"' '\n']* as filename) '"'
    { addCurrentLexBufToAStack lexbuf ;lexer (Lexing.from_channel    (open_in filename)); }

在填充器中,您将从堆栈的顶部继续

但工作起来似乎很有野心。

有任何想法吗?

Ps 词法分析器(以及解析器)是从另一个模块调用的(我们称之为 Main.ml)

4

1 回答 1

4

好吧,你对词法分析和解析有点困惑吗?

我看到的是:

如果我的词位是#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 中);-)

于 2016-04-26T12:44:00.567 回答