Commit bd694516 authored by POTTIER Francois's avatar POTTIER Francois

Implement a more economical renaming scheme for semantic values

during the elimination of %inlined symbols.
parent f8437b40
...@@ -2,6 +2,11 @@ ...@@ -2,6 +2,11 @@
## 2018/10/31 ## 2018/10/31
* Implement a more economical renaming scheme for OCaml variables
during the elimination of `%inline` symbols. This leads to slightly
more readable code (more reasonable variables names, fewer `let`
bindings).
* Another attempt at removing all trailing spaces in auto-generated * Another attempt at removing all trailing spaces in auto-generated
`.messages` files. (I hope I got it right, this time.) `.messages` files. (I hope I got it right, this time.)
......
...@@ -91,14 +91,14 @@ let names (producers : producers) : StringSet.t = ...@@ -91,14 +91,14 @@ let names (producers : producers) : StringSet.t =
that can be inlined. *) that can be inlined. *)
let inline grammar = let inline grammar =
(* This function returns a fresh name beginning with [prefix] and (* This function returns a fresh name that begins with [prefix] (although
that is not in the set of names [names]. *) this is not essential!) and that is not in the set [names]. *)
let rec fresh ?(c=0) names prefix = let rec fresh names prefix =
let name = prefix^string_of_int c in if StringSet.mem prefix names then
if StringSet.mem name names then let prefix = prefix ^ "'" in
fresh ~c:(c+1) names prefix fresh names prefix
else else
name prefix
in in
let use_inline = ref false in let use_inline = ref false in
...@@ -153,26 +153,27 @@ let inline grammar = ...@@ -153,26 +153,27 @@ let inline grammar =
let prefix, nt, p, psym, suffix = chop_inline ([], b.producers) in let prefix, nt, p, psym, suffix = chop_inline ([], b.producers) in
prefix, expand_rule nt p, nt, psym, suffix prefix, expand_rule nt p, nt, psym, suffix
(* We have to rename producers' names of the inlined production (* We have to rename the producers [producers] of the inlined production
if they clash with the producers' names of the branch into if they clash with the names of the producers of the host branch [b]. *)
which we do the inlining. *)
and rename_if_necessary b producers = and rename_if_necessary b producers =
(* First we compute the set of names already in use. *) (* Compute the set of the names already in use in the host branch. *)
let producers_names = names (b.producers @ producers) in let used = names b.producers in
(* Compute a renaming and the new inlined producers' names. *) (* Compute a renaming and the new names of the inlined producers. *)
let phi, producers' = let phi, _used, producers' =
List.fold_left (fun (phi, producers) producer -> List.fold_left (fun (phi, used, producers) producer ->
let x = producer_identifier producer in let x = producer_identifier producer in
if StringSet.mem x producers_names then if StringSet.mem x used then
let x' = fresh producers_names x in let x' = fresh used x in
((x, x') :: phi, { producer with producer_identifier = x' } :: producers) (x, x') :: phi,
StringSet.add x' used,
{ producer with producer_identifier = x' } :: producers
else else
(phi, producer :: producers) (phi, used, producer :: producers)
) ([], []) producers ) ([], used, []) producers
in in
phi, List.rev producers' phi, List.rev producers'
(* Inline the non terminals that can be inlined in [b]. We use the (* Inline the non terminals that can be inlined in [b]. We use the
ListMonad to combine the results. *) ListMonad to combine the results. *)
......
...@@ -16,21 +16,19 @@ bar: ...@@ -16,21 +16,19 @@ bar:
{ ( 2 )} { ( 2 )}
phrase: phrase:
x000 = foo x00 = foo _3 = EOF x' = foo x'' = foo _3 = EOF
{let t = {let t =
let x0 = x00 in let x' = x'' in
let x = let x =
let x = x0 in let x = x' in
( x ) ( x )
in in
( x ) ( x )
in in
let y = let y =
let x00 = x000 in
let x = let x =
let x0 = x00 in
let x = let x =
let x = x0 in let x = x' in
( x ) ( x )
in in
( x ) ( x )
...@@ -38,15 +36,11 @@ let y = ...@@ -38,15 +36,11 @@ let y =
( x ) ( x )
in in
( y + t )} ( y + t )}
| x000 = foo z0000 = bar _3 = EOF | x' = foo z = bar _3 = EOF
{let t = {let t =
let z000 = z0000 in
let x = let x =
let z00 = z000 in
let _1 = let _1 =
let z0 = z00 in
let x = let x =
let z = z0 in
( z ) ( z )
in in
( x ) ( x )
...@@ -56,11 +50,9 @@ in ...@@ -56,11 +50,9 @@ in
( x ) ( x )
in in
let y = let y =
let x00 = x000 in
let x = let x =
let x0 = x00 in
let x = let x =
let x = x0 in let x = x' in
( x ) ( x )
in in
( x ) ( x )
...@@ -68,21 +60,17 @@ let y = ...@@ -68,21 +60,17 @@ let y =
( x ) ( x )
in in
( y + t )} ( y + t )}
| z000 = bar x00 = foo _3 = EOF | z = bar x' = foo _3 = EOF
{let t = {let t =
let x0 = x00 in
let x = let x =
let x = x0 in let x = x' in
( x ) ( x )
in in
( x ) ( x )
in in
let y = let y =
let z00 = z000 in
let x = let x =
let z0 = z00 in
let x = let x =
let z = z0 in
( z ) ( z )
in in
( x ) ( x )
...@@ -90,15 +78,12 @@ let y = ...@@ -90,15 +78,12 @@ let y =
( x ) ( x )
in in
( y + t )} ( y + t )}
| z000 = bar z0000 = bar _3 = EOF | z = bar z' = bar _3 = EOF
{let t = {let t =
let z000 = z0000 in let z = z' in
let x = let x =
let z00 = z000 in
let _1 = let _1 =
let z0 = z00 in
let x = let x =
let z = z0 in
( z ) ( z )
in in
( x ) ( x )
...@@ -108,11 +93,8 @@ in ...@@ -108,11 +93,8 @@ in
( x ) ( x )
in in
let y = let y =
let z00 = z000 in
let x = let x =
let z0 = z00 in
let x = let x =
let z = z0 in
( z ) ( z )
in in
( x ) ( x )
......
...@@ -11,15 +11,13 @@ ...@@ -11,15 +11,13 @@
%% %%
midrule___anonymous_1_: midrule___anonymous_1_:
_10 = C _1 = C
{let x = {let x =
let _1 = _10 in
() ()
in in
( x )} ( x )}
| _10 = D | _1 = D
{let x = {let x =
let _1 = _10 in
() ()
in in
( x )} ( x )}
...@@ -27,29 +25,26 @@ in ...@@ -27,29 +25,26 @@ in
list___anonymous_0_A_B__: list___anonymous_0_A_B__:
{ ( [] )} { ( [] )}
| x0 = A xs = list___anonymous_0_A_B__ | x' = A xs = list___anonymous_0_A_B__
{let x = {let x =
let x = x0 in let x = x' in
( Left x ) ( Left x )
in in
( x :: xs )} ( x :: xs )}
| y0 = B xs = list___anonymous_0_A_B__ | y = B xs = list___anonymous_0_A_B__
{let x = {let x =
let y = y0 in
( Right y ) ( Right y )
in in
( x :: xs )} ( x :: xs )}
phrase: phrase:
_10 = C _20 = list___anonymous_0_A_B__ _11 = D _21 = list___anonymous_0_A_B__ _3 = EOF _1 = C _2 = list___anonymous_0_A_B__ _1' = D _2' = list___anonymous_0_A_B__ _3 = EOF
{let ys = {let ys =
let _2 = _21 in let _2 = _2' in
let _1 = _11 in let _1 = _1' in
( _1 ) ( _1 )
in in
let xs = let xs =
let _2 = _20 in
let _1 = _10 in
( _1 ) ( _1 )
in in
( xs @ ys )} ( xs @ ys )}
......
...@@ -11,15 +11,14 @@ ...@@ -11,15 +11,14 @@
list___anonymous_0_A_B__: list___anonymous_0_A_B__:
{ ( [] )} { ( [] )}
| x0 = A xs = list___anonymous_0_A_B__ | x' = A xs = list___anonymous_0_A_B__
{let x = {let x =
let x = x0 in let x = x' in
( Left x ) ( Left x )
in in
( x :: xs )} ( x :: xs )}
| y0 = B xs = list___anonymous_0_A_B__ | y = B xs = list___anonymous_0_A_B__
{let x = {let x =
let y = y0 in
( Right y ) ( Right y )
in in
( x :: xs )} ( x :: xs )}
...@@ -27,15 +26,14 @@ in ...@@ -27,15 +26,14 @@ in
list___anonymous_0_C_D__: list___anonymous_0_C_D__:
{ ( [] )} { ( [] )}
| x0 = C xs = list___anonymous_0_C_D__ | x' = C xs = list___anonymous_0_C_D__
{let x = {let x =
let x = x0 in let x = x' in
( Left x ) ( Left x )
in in
( x :: xs )} ( x :: xs )}
| y0 = D xs = list___anonymous_0_C_D__ | y = D xs = list___anonymous_0_C_D__
{let x = {let x =
let y = y0 in
( Right y ) ( Right y )
in in
( x :: xs )} ( x :: xs )}
......
...@@ -10,21 +10,21 @@ ...@@ -10,21 +10,21 @@
list___anonymous_0_: list___anonymous_0_:
{ ( [] )} { ( [] )}
| x0 = A xs = list___anonymous_0_ | x' = A xs = list___anonymous_0_
{let x = {let x =
let x = x0 in let x = x' in
( x ) ( x )
in in
( x :: xs )} ( x :: xs )}
| x0 = B xs = list___anonymous_0_ | x' = B xs = list___anonymous_0_
{let x = {let x =
let x = x0 in let x = x' in
( x ) ( x )
in in
( x :: xs )} ( x :: xs )}
| x0 = C xs = list___anonymous_0_ | x' = C xs = list___anonymous_0_
{let x = {let x =
let x = x0 in let x = x' in
( x ) ( x )
in in
( x :: xs )} ( x :: xs )}
...@@ -32,10 +32,9 @@ in ...@@ -32,10 +32,9 @@ in
list___anonymous_1_: list___anonymous_1_:
{ ( [] )} { ( [] )}
| x0 = D y0 = D xs = list___anonymous_1_ | x' = D y = D xs = list___anonymous_1_
{let x = {let x =
let y = y0 in let x = x' in
let x = x0 in
( x + y ) ( x + y )
in in
( x :: xs )} ( x :: xs )}
......
...@@ -44,17 +44,15 @@ separated_nonempty_list_DOT_clause_: ...@@ -44,17 +44,15 @@ separated_nonempty_list_DOT_clause_:
{ ( x :: xs )} { ( x :: xs )}
clauses: clauses:
xs0 = loption_separated_nonempty_list_DOT_clause__ _2 = EOF xs = loption_separated_nonempty_list_DOT_clause__ _2 = EOF
{let clauses = {let clauses =
let xs = xs0 in
( xs ) ( xs )
in in
( clauses )} ( clauses )}
clause: clause:
tm = term _2 = INFERS xs0 = loption_separated_nonempty_list_COMMA_term__ tm = term _2 = INFERS xs = loption_separated_nonempty_list_COMMA_term__
{let ts = {let ts =
let xs = xs0 in
( xs ) ( xs )
in in
( (tm, ts) )} ( (tm, ts) )}
...@@ -71,13 +69,9 @@ term: ...@@ -71,13 +69,9 @@ term:
{ ( Var (0, v) )} { ( Var (0, v) )}
params: params:
_10 = LPARENT xs00 = loption_separated_nonempty_list_COMMA_term__ _30 = RPARENT _1 = LPARENT xs = loption_separated_nonempty_list_COMMA_term__ _3 = RPARENT
{let terms = {let terms =
let _3 = _30 in
let xs0 = xs00 in
let _1 = _10 in
let x = let x =
let xs = xs0 in
( xs ) ( xs )
in in
( x ) ( x )
......
...@@ -22,41 +22,33 @@ expr: ...@@ -22,41 +22,33 @@ expr:
{ ( i )} { ( i )}
| _1 = LPAREN e = expr _3 = RPAREN | _1 = LPAREN e = expr _3 = RPAREN
{ ( e )} { ( e )}
| e1 = expr _100 = PLUS e2 = expr | e1 = expr _1 = PLUS e2 = expr
{let op = {let op =
let _10 = _100 in
let x = let x =
let _1 = _10 in
( (+) ) ( (+) )
in in
( x ) ( x )
in in
( op e1 e2 )} ( op e1 e2 )}
| e1 = expr _100 = MINUS e2 = expr | e1 = expr _1 = MINUS e2 = expr
{let op = {let op =
let _10 = _100 in
let x = let x =
let _1 = _10 in
( (-) ) ( (-) )
in in
( x ) ( x )
in in
( op e1 e2 )} ( op e1 e2 )}
| e1 = expr _100 = TIMES e2 = expr | e1 = expr _1 = TIMES e2 = expr
{let op = {let op =
let _10 = _100 in
let x = let x =
let _1 = _10 in
( ( * ) ) ( ( * ) )
in in
( x ) ( x )
in in
( op e1 e2 )} ( op e1 e2 )}
| e1 = expr _100 = DIV e2 = expr | e1 = expr _1 = DIV e2 = expr
{let op = {let op =
let _10 = _100 in
let x = let x =
let _1 = _10 in
( (/) ) ( (/) )
in in
( x ) ( x )
......
...@@ -1608,9 +1608,8 @@ objectDefn: ...@@ -1608,9 +1608,8 @@ objectDefn:
{ ()} { ()}
defaultSyntax: defaultSyntax:
_1 = LeftBrace xs0 = loption_separated_nonempty_list_Comma_fieldSetting__ _3 = RightBrace _1 = LeftBrace xs = loption_separated_nonempty_list_Comma_fieldSetting__ _3 = RightBrace
{let _2 = {let _2 =
let xs = xs0 in
( xs ) ( xs )
in in
()} ()}
...@@ -1734,9 +1733,8 @@ generalConstraint: ...@@ -1734,9 +1733,8 @@ generalConstraint:
{ ()} { ()}
userDefinedConstraint: userDefinedConstraint:
_1 = CONSTRAINED _2 = BY _3 = LeftBrace xs0 = loption_separated_nonempty_list_Comma_userDefinedConstraintParameter__ _5 = RightBrace _1 = CONSTRAINED _2 = BY _3 = LeftBrace xs = loption_separated_nonempty_list_Comma_userDefinedConstraintParameter__ _5 = RightBrace
{let _4 = {let _4 =
let xs = xs0 in
( xs ) ( xs )
in in
()} ()}
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -931,9 +931,8 @@ special: ...@@ -931,9 +931,8 @@ special:
{ ( Documentation.Index )} { ( Documentation.Index )}
item: item:
_1 = ITEM elems0 = list_text_element_ _3 = CLOSE _1 = ITEM elems = list_text_element_ _3 = CLOSE
{let text = {let text =
let elems = elems0 in
( elems ) ( elems )
in in
( text )} ( text )}
...@@ -946,9 +945,8 @@ text_element: ...@@ -946,9 +945,8 @@ text_element:
( "" ) ( "" )
in in
( Documentation.Code str )} ( Documentation.Code str )}
| _1 = CODE data0 = Data _3 = CLOSE | _1 = CODE data = Data _3 = CLOSE
{let str = {let str =
let data = data0 in
( data) ( data)
in in
( Documentation.Code str )} ( Documentation.Code str )}
...@@ -957,9 +955,8 @@ in ...@@ -957,9 +955,8 @@ in
( "" ) ( "" )
in in
( Documentation.PreCode str )} ( Documentation.PreCode str )}
| _1 = PRECODE data0 = Data _3 = CLOSE | _1 = PRECODE data = Data _3 = CLOSE
{let str = {let str =
let data = data0 in
( data) ( data)
in in
( Documentation.PreCode str )} ( Documentation.PreCode str )}
...@@ -968,63 +965,53 @@ in ...@@ -968,63 +965,53 @@ in
( "" ) ( "" )
in in
( Documentation.Verbatim str )} ( Documentation.Verbatim str )}
| _1 = VERBATIM data0 = Data _3 = CLOSE | _1 = VERBATIM data = Data _3 = CLOSE