Commit 27acef5c authored by POTTIER Francois's avatar POTTIER Francois

Modified both back-ends to avoid an explicit initial call to the lexer.

The first token is now read during the first call to [run].
This exchanges the order of the first two messages in the trace,
but is actually more regular.
In the code back-end, this fixes a "bug" in the case where the grammar
accepts only the empty word. In the table back-end, the bug is not yet fixed.
parent 75dd6ff6
......@@ -10,7 +10,15 @@
* Voir si ça coûte cher de stocker la taille de la pile dans
chaque cellule
* Could we avoid a special case for the first call to read()?
* engine.ml: initial call to [run env true] should be [run env false]
if the initial state has a default reduction on #
i.e. if only the empty word is accepted!
I think currently the code back-end is OK
and the table back-end is wrong (void.mly)
in theory, [entry] in [engine.ml]
should be parameterized by [please_discard]
which should be [false] iff the initial state
has a default reduction on #
* Changes that could be applied to the code back-end:
[action] could now be inlined into [run]
......
Lookahead token is now INT (-1--1)
State 0:
Lookahead token is now INT (-1--1)
Shifting (INT) to state 4
State 4:
Lookahead token is now PLUS (-1--1)
......
Lookahead token is now INT (0-3)
State 0:
Lookahead token is now INT (0-3)
Shifting (INT) to state 4
State 4:
Lookahead token is now PLUS (4-5)
......
Lookahead token is now LPAREN (-1--1)
State 0:
Lookahead token is now LPAREN (-1--1)
Shifting (LPAREN) to state 2
State 2:
Lookahead token is now INT (-1--1)
......
Lookahead token is now LPAREN (0-1)
State 0:
Lookahead token is now LPAREN (0-1)
Shifting (LPAREN) to state 2
State 2:
Lookahead token is now INT (1-4)
......
Lookahead token is now LPAREN (-1--1)
State 0:
Lookahead token is now LPAREN (-1--1)
Shifting (LPAREN) to state 2
State 2:
Lookahead token is now INT (-1--1)
......
Lookahead token is now LPAREN (0-1)
State 0:
Lookahead token is now LPAREN (0-1)
Shifting (LPAREN) to state 2
State 2:
Lookahead token is now INT (1-4)
......
Lookahead token is now LPAREN (-1--1)
State 0:
Lookahead token is now LPAREN (-1--1)
Shifting (LPAREN) to state 2
State 2:
Lookahead token is now INT (-1--1)
......
Lookahead token is now LPAREN (0-1)
State 0:
Lookahead token is now LPAREN (0-1)
Shifting (LPAREN) to state 2
State 2:
Lookahead token is now INT (1-4)
......
Lookahead token is now INT (0-3)
State 0:
Lookahead token is now INT (0-3)
Shifting (INT) to state 4
State 4:
Lookahead token is now PLUS (4-5)
......
Lookahead token is now LPAREN (0-1)
State 0:
Lookahead token is now LPAREN (0-1)
Shifting (LPAREN) to state 2
State 2:
Lookahead token is now INT (1-4)
......
......@@ -953,10 +953,17 @@ let runpushcellunless shiftreduce s e =
The parameter [defred] tells which default reduction, if any, we
are about to perform. *)
(* 2014/12/06 New convention regarding initial states (i.e., states
which have no incoming symbol). The function [initenv] does not
invoke the lexer, so the [run] function for an initial state must
do it. (Except in the very special case where the initial state
has a default reduction on [#] -- this means the grammar recognizes
only the empty word.) *)
let gettoken s defred e =
match Lr1.incoming_symbol s, defred with
| Some (Symbol.T _), Some (_, toks)
| (Some (Symbol.T _) | None), Some (_, toks)
when TerminalSet.mem Terminal.sharp toks ->
assert (TerminalSet.cardinal toks = 1);
......@@ -967,28 +974,28 @@ let gettoken s defred e =
e
| Some (Symbol.T _), Some _ ->
| (Some (Symbol.T _) | None), Some _ ->
(* There is some other default reduction. Discard the first
input token. *)
blet ([ PWildcard, EApp (EVar discard, [ EVar env ]) ], e)
| Some (Symbol.T _), None ->
| (Some (Symbol.T _) | None), None ->
(* There is no default reduction. Discard the first input token
and peek at the next one. *)
blet ([ PVar token, EApp (EVar discard, [ EVar env ]) ], e)
| (Some (Symbol.N _) | None), Some _ ->
| Some (Symbol.N _), Some _ ->
(* There is some default reduction. Do not peek at the input
token. *)
e
| (Some (Symbol.N _) | None), None ->
| Some (Symbol.N _), None ->
(* There is no default reduction. Peek at the first input token,
without taking it off the input stream. This is normally done
......@@ -1628,8 +1635,10 @@ let discarddef = {
}
(* This is [initenv], used to allocate a fresh parser environment.
It performs the very first call to the lexer, and fills in all
fields in a straightforward way. *)
It fills in all fields in a straightforward way. The [token]
field receives a dummy value. It will be overwritten by the
first call to [run], which will invoke [discard]. This allows
us to invoke the lexer in just one place. *)
let initenvdef =
let lexer = "lexer"
......@@ -1641,17 +1650,15 @@ let initenvdef =
EAnnot (
EFun ( [ PVar lexer; PVar lexbuf ],
blet (
[ PVar token, EApp (EVar lexer, [ EVar lexbuf ]) ] @
trace "Lookahead token is now %s (%d-%d)"
[ EApp (EVar print_token, [ EVar token ]);
ERecordAccess (ERecordAccess (EVar lexbuf, "Lexing.lex_start_p"), "Lexing.pos_cnum");
ERecordAccess (ERecordAccess (EVar lexbuf, "Lexing.lex_curr_p"), "Lexing.pos_cnum") ],
(* We do not have a dummy token at hand, so we forge one. *)
(* It will be overwritten by the first call to the lexer. *)
[ PVar token, EMagic EUnit ],
ERecord ([
(flexer, EVar lexer);
(flexbuf, EVar lexbuf);
(ftoken, EVar token);
(fstartp, ERecordAccess (EVar lexbuf, "Lexing.lex_start_p"));
(fendp, ERecordAccess (EVar lexbuf, "Lexing.lex_curr_p"));
(fstartp, EVar "Lexing.dummy_pos");
(fendp, EVar "Lexing.dummy_pos");
(fshifted, EMaxInt)
]
)
......
......@@ -327,19 +327,10 @@ module Make (T : TABLE) = struct
next = empty;
} in
(* Perform an initial call to the lexer. *)
let triple = read() in
(* Log our first lookahead token. *)
let (token, startp, endp) = triple in
Log.lookahead_token (T.token2terminal token) startp endp;
(* Build an initial environment. *)
let env = {
triple;
triple = (error_token, Lexing.dummy_pos, Lexing.dummy_pos); (* dummy *)
stack = empty;
current = s;
} in
......@@ -358,7 +349,7 @@ module Make (T : TABLE) = struct
(* Catch [Accept], which represents normal termination. Let [Error] escape. *)
try
loop (run env false)
loop (run env true)
with
| Accept v ->
v
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment