我正在为 Ocaml 中的类做一个编译器。我需要读取带有“1”之类的命令或表达式的文件,然后它返回 Int 1。除了我和我的朋友之外,相同的代码适用于整个班级。每个人都使用相同的 ocaml 版本和 Ubuntu 13.04。错误是:Lexico.Eof
有人知道这可能是什么吗?这是 asa.ml:
type opB =
| Soma
| Sub
| Mul
| Div
type exp =
| Int of int
| Float of float
| String of string
| Char of char
| Identificador of string
| Bin of opB * exp * exp
这是 Sintatico.mly:
%{
open Asa;;
%}
%token <int> INT
%token <float> FLOAT
%token <string> STRING
%token <char> CHAR
%token <string> IDENTIFICADOR
%token APAREN FPAREN PTVIRG
%token MAIS MENOS MUL DIV
%left MAIS MENOS
%left MUL DIV
%start main
%type <Asa.exp> main
%%
main: expr { $1 }
;
expr: IDENTIFICADOR { Identificador($1) }
| INT { Int($1) }
| FLOAT { Float($1) }
| STRING { String($1) }
| CHAR { Char($1) }
| APAREN expr FPAREN { $2 }
| expr MAIS expr { Bin(Soma, $1, $3) }
| expr MENOS expr { Bin(Sub, $1, $3) }
| expr MUL expr { Bin(Mul, $1, $3) }
| expr DIV expr { Bin(Div, $1, $3) }
;
Lexico.mll:
{
open String
open Sintatico
exception Eof
}
let digito = ['0'-'9']
let caracter = [^ '\n' '\t' '\b' '\r' '\'' '\\']
let identificador = ['a'-'z' 'A'-'Z']['a'-'z' '0'-'9']*
rule token = parse
| [' ' '\t' '\n'] { token lexbuf } (* ignora os espacos *)
| digito+ as inum { print_string " int "; INT (int_of_string inum) }
| digito+'.'digito+ as fnum { print_string " float "; FLOAT (float_of_string fnum) }
| '\"' ([^ '"']* as s) '\"' { print_string " string "; STRING (s)}
| '\'' caracter '\'' as ch { print_string " char "; CHAR (String.get ch 1) }
| identificador as id { print_string " identificador "; IDENTIFICADOR (id) }
| '(' { print_string " abreparent "; APAREN }
| ')' { print_string " fechaparent "; FPAREN }
| '+' { print_string " + "; MAIS }
| '-' { print_string " - "; MENOS }
| '*' { print_string " * "; MUL }
| '/' { print_string " / "; DIV }
| ';' { print_string " ptv "; PTVIRG }
| eof { raise Eof }
调用名为 carregatudo.ml 的文件的代码是:
#load "asa.cmo"
#load "sintatico.cmo"
#load "lexico.cmo"
open Asa;;
let analisa_arquivo arquivo =
let ic = open_in arquivo in
let lexbuf = Lexing.from_channel ic in
let asa = Sintatico.main Lexico.token lexbuf in
close_in ic;
asa
对不起葡萄牙语:
arquivo 意味着文件
Lexico 意味着 Lexer
Sintatico 表示解析器
首先,我使用命令 make interpretador 运行这个 makefile:
CAMLC = ocamlc
CAMLLEX = ocamllex
CAMLYACC = ocamlyacc
interpretador: asa.cmo sintatico.cmi sintatico.cmo lexico.cmo
portugol: asa.cmo sintatico.cmi sintatico.cmo lexico.cmo principal.cmo
clean:
rm *.cmo *.cmi
# regras genericas
.SUFFIXES: .mll .mly .mli .ml .cmi .cmo .cmx
.mll.mli:
$(CAMLLEX) $<
.mll.ml:
$(CAMLLEX) $<
.mly.mli:
$(CAMLYACC) $<
.mly.ml:
$(CAMLYACC) $<
.mli.cmi:
$(CAMLC) -c $(FLAGS) $<
.ml.cmo:
$(CAMLC) -c $(FLAGS) $<
接下来是carregatudo.ml:#use "carregatudo.ml";;
接下来是函数: analisa_arquivo("teste.pt");;
输入文件 teste.pt 如下:
1
并且回报应该是
Int 1
但我不断收到错误 Lexico.Eof
谢谢!