错误Eof Ocaml

时间:2014-02-24 22:56:41

标签: compiler-construction ocaml ocamllex

我正在为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

谢谢!

1 个答案:

答案 0 :(得分:1)

解析器使用多个令牌以查看递归规则是否匹配,这很自然地导致Eof被引发。基本上你的解析器在文件的末尾运行,因为它没有任何规则告诉它何时停止查找表达式的更多部分。

一个简单的解决方法是将Eof异常更改为令牌END_OF_INPUT,并与语法中的异常匹配:

main: expr END_OF_INPUT { $1 }

或者,您可以引入明确的终止符,例如;