Skip to content
Snippets Groups Projects
Commit 33646397 authored by Augustin Jaujay's avatar Augustin Jaujay
Browse files

Question 8

parent 47ca7fe2
No related branches found
No related tags found
No related merge requests found
(* Question 6.1 et 6.2 *)
{
(* open Parser
open Ast *)
open Parser
(* open Ast *)
open Utils
type token =
| EOF | PUSH | POP | SWAP | ADD | SUB | MUL | DIV | REM | INT of int
let print_token = function
| EOF -> print_string "EOF"
| INT i -> print_int i
| PUSH -> print_string "PUSH"
| SWAP -> print_string "SWAP"
| POP -> print_string "POP"
| ADD -> print_string "ADD"
| SUB -> print_string "SUB"
| MUL -> print_string "MUL"
| DIV -> print_string "DIV"
| REM -> print_string "REM"
let mk_int nb lexbuf =
try INT (int_of_string nb)
with Failure _ -> failwith (Location.string_of (Location.curr lexbuf))
......@@ -51,27 +36,3 @@ rule token = parse
| "rem" {REM}
(* illegal characters *)
| _ { failwith (Location.string_of (Location.curr lexbuf)) }
\ No newline at end of file
{
let rec examine_all lexbuf =
let result = token lexbuf in
print_token result;
print_string " ";
match result with
| EOF -> ()
| _ -> examine_all lexbuf
let compile file =
print_string ("File "^file^" is being treated!\n");
try
let input_file = open_in file in
let lexbuf = Lexing.from_channel input_file in
Location.init lexbuf file;
examine_all lexbuf;
print_newline ();
close_in (input_file)
with Sys_error _ ->
print_endline ("Can't find file '" ^ file ^ "'")
let _ = Arg.parse [] compile ""
}
\ No newline at end of file
%{
(* Ocaml code here*)
open Ast
%}
(**************
* The tokens *
**************)
(* enter tokens here, they should begin with %token *)
%token EOF
%token EOF PUSH POP SWAP ADD SUB MUL DIV REM
%token <int> INT
......@@ -16,7 +14,6 @@
* Entry points of the parser *
******************************)
(* enter your %start clause here *)
%start <Ast.program> program
%%
......@@ -25,8 +22,20 @@
* The rules *
*************)
(* list all rules composing your grammar; obviously your entry point has to be present *)
program: i=INT EOF { i,[] }
program:
| i=INT; c=command { i, c }
| EOF { 0, [] }
command:
| PUSH command { failwith ("Argument missing for push function") }
| PUSH i=INT c=command { (Push i)::c }
| POP c=command { Pop::c }
| SWAP c=command { Swap::c }
| ADD c=command { Add::c }
| SUB c=command { Sub::c }
| MUL c=command { Mul::c }
| DIV c=command { Div::c }
| REM c=command { Rem::c }
| EOF { [] }
%%
0 push 2 push 7 push 3 add div -- -5
0 push 2 push 4 swap
(executables
(names pfxVM)
(libraries utils basicPfx))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment