Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
compilerLaLog
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
JAUJAY Augustin
compilerLaLog
Commits
33646397
Commit
33646397
authored
3 years ago
by
Augustin Jaujay
Browse files
Options
Downloads
Patches
Plain Diff
Question 8
parent
47ca7fe2
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
pfx/basic/lexer.mll
+3
-42
3 additions, 42 deletions
pfx/basic/lexer.mll
pfx/basic/parser.mly
+17
-8
17 additions, 8 deletions
pfx/basic/parser.mly
pfx/basic/tests/ok_prog.pfx
+1
-1
1 addition, 1 deletion
pfx/basic/tests/ok_prog.pfx
pfx/dune
+3
-0
3 additions, 0 deletions
pfx/dune
with
24 additions
and
51 deletions
pfx/basic/lexer.mll
+
3
−
42
View file @
33646397
(* 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
This diff is collapsed.
Click to expand it.
pfx/basic/parser.mly
+
17
−
8
View file @
33646397
%
{
(* 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
{
[]
}
%%
This diff is collapsed.
Click to expand it.
pfx/basic/tests/ok_prog.pfx
+
1
−
1
View file @
33646397
0 push 2 push
7 push 3 add div -- -5
0 push 2 push
4 swap
This diff is collapsed.
Click to expand it.
pfx/dune
0 → 100644
+
3
−
0
View file @
33646397
(executables
(names pfxVM)
(libraries utils basicPfx))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment