使用ocamllex进行Lexing和include指令

时间:2016-04-26 10:39:49

标签: ocaml lex ocamllex

我正在编写一个类C语言的编译器,它必须支持#include指令(仅在文件的开头)

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

现在这一点都不好看。所以我尝试了以下内容:

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

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

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

我还看到,当缓冲区lexbuf达到eof时,可以制作一个refiller。但我没有找到更多信息。这让我想到了将上面的代码更改为以下内容:

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

并且在补给者中,你将从堆叠的头部继续

但似乎很有野心。

有什么想法吗?

P.S。词法分析器(以及解析也是)从另一个模块调用(让我们称之为Main.ml)

1 个答案:

答案 0 :(得分:3)

嗯,你对lexing和解析有点困惑吗?

我看到的是:

  

如果我的lexeme是#include ident 我想解析    ident 指向的文件并添加它。

然后您会混淆解析 lexing

你可以写下这样的东西:(它是一个小程序,但它有效; - ))

ast.mli

type operation = 
 | Plus of operation * operation 
 | Minus of operation * operation
 | Int of int

type prog = string list * operation list

lexer.mll

{
  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 }   

parser.mly

%{

  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 }

所以,正如你所看到的,当我解析时,我记得我必须解析的文件名和

main.ml

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 !@."

这意味着当我完成解析第一个文件时,我会解析所包含的文件。

最重要的是你对lexing的困惑(这是编译器中最愚蠢的事情,你只是问&#34;你看到的下一个标记是什么?&#34;他回答&#34;我看到#include "filename"&#34;和那个不那么愚蠢的解析器说&#34;嘿,词法分析器看到了#include "filename"所以我会记住这个文件名,因为我可能需要它,我会继续。

如果我有这三个文件:

文件1

#include "file2"
main 
6; 7

file2的

#include "file3"
main 
4; 5

file3的

main 
1; 2; 3

如果我致电./compile file1,我的输出1 2 3 4 5 6就是我想要的。 ; - )

[编辑]

lexer处理包含的新版本:

ast.mli

type operation = 
  | Plus of operation * operation 
  | Minus of operation * operation
  | Int of int

type prog = operation list

lexer.mll

{
  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 }   

parser.mly

%{

  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 }

main.ml

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 !@."

所以,在词法分析器中,当我看到#include filename时,我立即调用由filename链接的文件上的Parser,并将解析后的Ast.prog返回到先前的解析调用。 / p>

我希望你一切都清楚; - )

[第二编辑]

我不能让这个代码像这样,我编辑它以避免包含循环(在lexer.mll中); - )