OCaml Interpreter: Why my interpreter just execute only one line in my file -
i'm writing interpreter, using ocamlyacc , ocamllex compile parser , lexer.
my problem that, have file calles test, contain 2 commands defined in lexer:
print print b
but interpreter execute line print a
only! know problem in main in parser, need recursed. fixed (code below) still doesn't work.
%{ open path %} %token <int> int %token <string> string %token eol %token eof %token get_line %token print %start main %type <path.term> main %% main: | expr eol {$1} | expr eof {$1} ; str: | string { $1 } ; intger: | int {$1 } ; expr: | print str { print $2 } | print str expr { print $2 } | get_line int str { print_line_in_file ($2, $3) } | get_line int str expr { print_line_in_file ($2, $3) } ;
edit
this lexer, tried simplify as possible spot error.
(* file lexer.mll *) { open parser } rule main = parse | [' ''\t''\n'] { main lexbuf } | "print_line_in_file" { get_line } | "print" { print} | ['1' - '9']+ lxm { int(int_of_string lxm) } | ['a'-'z''a'-'z'] ['a'-'z''a'-'z''0'-'9']* lxm { string lxm } | eof {eof}
main.ml
open path let _ = try let filename = sys.argv.(1) in let lexbuf = lexing.from_channel (open_in filename) in let result = parser.main lexer.main lexbuf in command result; flush stdout parsing.parse_error -> print_string "error! check syntax"; flush stdout
to expand on gasche's answer, need change parser definition follows:
%type <path.term list> main %% main: | expr eol main {$1::$3} | expr eof {[$1]} | eof {[]} /* if want allow redundant eol @ end */ ;
with original definition, single line considered complete parse main
, that's why parser stops after that.
Comments
Post a Comment