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 @@
## 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
`.messages` files. (I hope I got it right, this time.)
......
......@@ -91,14 +91,14 @@ let names (producers : producers) : StringSet.t =
that can be inlined. *)
let inline grammar =
(* This function returns a fresh name beginning with [prefix] and
that is not in the set of names [names]. *)
let rec fresh ?(c=0) names prefix =
let name = prefix^string_of_int c in
if StringSet.mem name names then
fresh ~c:(c+1) names prefix
else
name
(* This function returns a fresh name that begins with [prefix] (although
this is not essential!) and that is not in the set [names]. *)
let rec fresh names prefix =
if StringSet.mem prefix names then
let prefix = prefix ^ "'" in
fresh names prefix
else
prefix
in
let use_inline = ref false in
......@@ -153,26 +153,27 @@ let inline grammar =
let prefix, nt, p, psym, suffix = chop_inline ([], b.producers) in
prefix, expand_rule nt p, nt, psym, suffix
(* We have to rename producers' names of the inlined production
if they clash with the producers' names of the branch into
which we do the inlining. *)
(* We have to rename the producers [producers] of the inlined production
if they clash with the names of the producers of the host branch [b]. *)
and rename_if_necessary b producers =
(* First we compute the set of names already in use. *)
let producers_names = names (b.producers @ producers) in
(* Compute the set of the names already in use in the host branch. *)
let used = names b.producers in
(* Compute a renaming and the new inlined producers' names. *)
let phi, producers' =
List.fold_left (fun (phi, producers) producer ->
(* Compute a renaming and the new names of the inlined producers. *)
let phi, _used, producers' =
List.fold_left (fun (phi, used, producers) producer ->
let x = producer_identifier producer in
if StringSet.mem x producers_names then
let x' = fresh producers_names x in
((x, x') :: phi, { producer with producer_identifier = x' } :: producers)
if StringSet.mem x used then
let x' = fresh used x in
(x, x') :: phi,
StringSet.add x' used,
{ producer with producer_identifier = x' } :: producers
else
(phi, producer :: producers)
) ([], []) producers
(phi, used, producer :: producers)
) ([], used, []) producers
in
phi, List.rev producers'
phi, List.rev producers'
(* Inline the non terminals that can be inlined in [b]. We use the
ListMonad to combine the results. *)
......
......@@ -16,21 +16,19 @@ bar:
{ ( 2 )}
phrase:
x000 = foo x00 = foo _3 = EOF
x' = foo x'' = foo _3 = EOF
{let t =
let x0 = x00 in
let x' = x'' in
let x =
let x = x0 in
let x = x' in
( x )
in
( x )
in
let y =
let x00 = x000 in
let x =
let x0 = x00 in
let x =
let x = x0 in
let x = x' in
( x )
in
( x )
......@@ -38,15 +36,11 @@ let y =
( x )
in
( y + t )}
| x000 = foo z0000 = bar _3 = EOF
| x' = foo z = bar _3 = EOF
{let t =
let z000 = z0000 in
let x =
let z00 = z000 in
let _1 =
let z0 = z00 in
let x =
let z = z0 in
( z )
in
( x )
......@@ -56,11 +50,9 @@ in
( x )
in
let y =
let x00 = x000 in
let x =
let x0 = x00 in
let x =
let x = x0 in
let x = x' in
( x )
in
( x )
......@@ -68,21 +60,17 @@ let y =
( x )
in
( y + t )}
| z000 = bar x00 = foo _3 = EOF
| z = bar x' = foo _3 = EOF
{let t =
let x0 = x00 in
let x =
let x = x0 in
let x = x' in
( x )
in
( x )
in
let y =
let z00 = z000 in
let x =
let z0 = z00 in
let x =
let z = z0 in
( z )
in
( x )
......@@ -90,15 +78,12 @@ let y =
( x )
in
( y + t )}
| z000 = bar z0000 = bar _3 = EOF
| z = bar z' = bar _3 = EOF
{let t =
let z000 = z0000 in
let z = z' in
let x =
let z00 = z000 in
let _1 =
let z0 = z00 in
let x =
let z = z0 in
( z )
in
( x )
......@@ -108,11 +93,8 @@ in
( x )
in
let y =
let z00 = z000 in
let x =
let z0 = z00 in
let x =
let z = z0 in
( z )
in
( x )
......
......@@ -11,15 +11,13 @@
%%
midrule___anonymous_1_:
_10 = C
_1 = C
{let x =
let _1 = _10 in
()
in
( x )}
| _10 = D
| _1 = D
{let x =
let _1 = _10 in
()
in
( x )}
......@@ -27,29 +25,26 @@ in
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 = x0 in
let x = x' in
( Left x )
in
( x :: xs )}
| y0 = B xs = list___anonymous_0_A_B__
| y = B xs = list___anonymous_0_A_B__
{let x =
let y = y0 in
( Right y )
in
( x :: xs )}
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 _2 = _21 in
let _1 = _11 in
let _2 = _2' in
let _1 = _1' in
( _1 )
in
let xs =
let _2 = _20 in
let _1 = _10 in
( _1 )
in
( xs @ ys )}
......
......@@ -11,15 +11,14 @@
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 = x0 in
let x = x' in
( Left x )
in
( x :: xs )}
| y0 = B xs = list___anonymous_0_A_B__
| y = B xs = list___anonymous_0_A_B__
{let x =
let y = y0 in
( Right y )
in
( x :: xs )}
......@@ -27,15 +26,14 @@ in
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 = x0 in
let x = x' in
( Left x )
in
( x :: xs )}
| y0 = D xs = list___anonymous_0_C_D__
| y = D xs = list___anonymous_0_C_D__
{let x =
let y = y0 in
( Right y )
in
( x :: xs )}
......
......@@ -10,21 +10,21 @@
list___anonymous_0_:
{ ( [] )}
| x0 = A xs = list___anonymous_0_
| x' = A xs = list___anonymous_0_
{let x =
let x = x0 in
let x = x' in
( x )
in
( x :: xs )}
| x0 = B xs = list___anonymous_0_
| x' = B xs = list___anonymous_0_
{let x =
let x = x0 in
let x = x' in
( x )
in
( x :: xs )}
| x0 = C xs = list___anonymous_0_
| x' = C xs = list___anonymous_0_
{let x =
let x = x0 in
let x = x' in
( x )
in
( x :: xs )}
......@@ -32,10 +32,9 @@ in
list___anonymous_1_:
{ ( [] )}
| x0 = D y0 = D xs = list___anonymous_1_
| x' = D y = D xs = list___anonymous_1_
{let x =
let y = y0 in
let x = x0 in
let x = x' in
( x + y )
in
( x :: xs )}
......
......@@ -44,17 +44,15 @@ separated_nonempty_list_DOT_clause_:
{ ( x :: xs )}
clauses:
xs0 = loption_separated_nonempty_list_DOT_clause__ _2 = EOF
xs = loption_separated_nonempty_list_DOT_clause__ _2 = EOF
{let clauses =
let xs = xs0 in
( xs )
in
( clauses )}
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 xs = xs0 in
( xs )
in
( (tm, ts) )}
......@@ -71,13 +69,9 @@ term:
{ ( Var (0, v) )}
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 _3 = _30 in
let xs0 = xs00 in
let _1 = _10 in
let x =
let xs = xs0 in
( xs )
in
( x )
......
......@@ -22,41 +22,33 @@ expr:
{ ( i )}
| _1 = LPAREN e = expr _3 = RPAREN
{ ( e )}
| e1 = expr _100 = PLUS e2 = expr
| e1 = expr _1 = PLUS e2 = expr
{let op =
let _10 = _100 in
let x =
let _1 = _10 in
( (+) )
in
( x )
in
( op e1 e2 )}
| e1 = expr _100 = MINUS e2 = expr
| e1 = expr _1 = MINUS e2 = expr
{let op =
let _10 = _100 in
let x =
let _1 = _10 in
( (-) )
in
( x )
in
( op e1 e2 )}
| e1 = expr _100 = TIMES e2 = expr
| e1 = expr _1 = TIMES e2 = expr
{let op =
let _10 = _100 in
let x =
let _1 = _10 in
( ( * ) )
in
( x )
in
( op e1 e2 )}
| e1 = expr _100 = DIV e2 = expr
| e1 = expr _1 = DIV e2 = expr
{let op =
let _10 = _100 in
let x =
let _1 = _10 in
( (/) )
in
( x )
......
......@@ -1608,9 +1608,8 @@ objectDefn:
{ ()}
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 xs = xs0 in
( xs )
in
()}
......@@ -1734,9 +1733,8 @@ generalConstraint:
{ ()}
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 xs = xs0 in
( xs )
in
()}
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -931,9 +931,8 @@ special:
{ ( Documentation.Index )}
item:
_1 = ITEM elems0 = list_text_element_ _3 = CLOSE
_1 = ITEM elems = list_text_element_ _3 = CLOSE
{let text =
let elems = elems0 in
( elems )
in
( text )}
......@@ -946,9 +945,8 @@ text_element:
( "" )
in
( Documentation.Code str )}
| _1 = CODE data0 = Data _3 = CLOSE
| _1 = CODE data = Data _3 = CLOSE
{let str =
let data = data0 in
( data)
in
( Documentation.Code str )}
......@@ -957,9 +955,8 @@ in
( "" )
in
( Documentation.PreCode str )}
| _1 = PRECODE data0 = Data _3 = CLOSE
| _1 = PRECODE data = Data _3 = CLOSE
{let str =
let data = data0 in
( data)
in
( Documentation.PreCode str )}
......@@ -968,63 +965,53 @@ in
( "" )
in
( Documentation.Verbatim str )}
| _1 = VERBATIM data0 = Data _3 = CLOSE
| _1 = VERBATIM data = Data _3 = CLOSE
{let str =
let data = data0 in
( data)
in
( Documentation.Verbatim str )}
| _1 = BOLD elems0 = list_text_element_ _3 = CLOSE
| _1 = BOLD elems = list_text_element_ _3 = CLOSE
{let text =
let elems = elems0 in
( elems )
in
( Documentation.(Style(Bold, text)) )}
| _1 = ITALIC elems0 = list_text_element_ _3 = CLOSE
| _1 = ITALIC elems = list_text_element_ _3 = CLOSE
{let text =
let elems = elems0 in
( elems )
in
( Documentation.(Style(Italic, text)) )}
| _1 = EMPHASIZE elems0 = list_text_element_ _3 = CLOSE
| _1 = EMPHASIZE elems = list_text_element_ _3 = CLOSE
{let text =
let elems = elems0 in
( elems )
in
( Documentation.(Style(Emphasize, text)) )}
| _1 = CENTER elems0 = list_text_element_ _3 = CLOSE
| _1 = CENTER elems = list_text_element_ _3 = CLOSE
{let text =
let elems = elems0 in
( elems )
in
( Documentation.(Style(Center, text)) )}
| _1 = LEFT elems0 = list_text_element_ _3 = CLOSE
| _1 = LEFT elems = list_text_element_ _3 = CLOSE
{let text =
let elems = elems0 in
( elems )
in
( Documentation.(Style(Left, text)) )}
| _1 = RIGHT elems0 = list_text_element_ _3 = CLOSE
| _1 = RIGHT elems = list_text_element_ _3 = CLOSE
{let text =
let elems = elems0 in
( elems )
in
( Documentation.(Style(Right, text)) )}
| _1 = SUPERSCRIPT elems0 = list_text_element_ _3 = CLOSE
| _1 = SUPERSCRIPT elems = list_text_element_ _3 = CLOSE
{let text =
let elems = elems0 in
( elems )
in
( Documentation.(Style(Superscript, text)) )}
| _1 = SUBSCRIPT elems0 = list_text_element_ _3 = CLOSE
| _1 = SUBSCRIPT elems = list_text_element_ _3 = CLOSE
{let text =
let elems = elems0 in
( elems )
in
( Documentation.(Style(Subscript, text)) )}
| tag = Custom elems0 = list_text_element_ _3 = CLOSE
| tag = Custom elems = list_text_element_ _3 = CLOSE
{let text =
let elems = elems0 in
( elems )
in
( Documentation.(Style(Custom tag, text)) )}
......@@ -1034,9 +1021,8 @@ in
{ ( Documentation.Enum i )}
| _1 = NEWLINE _2 = CLOSE
{ ( Documentation.Newline )}
| level = Title label = option_label_identifier_ elems0 = list_text_element_ _4 = CLOSE
| level = Title label = option_label_identifier_ elems = list_text_element_ _4 = CLOSE
{let text =
let elems = elems0 in
( elems )
in
( Documentation.Title(level, label, text) )}
......@@ -1045,9 +1031,8 @@ in
( None )
in
( Documentation.Reference(rf, text) )}
| _1 = REFERENCE rf = reference elems0 = nonempty_list_text_element_ _4 = CLOSE
| _1 = REFERENCE rf = reference elems = nonempty_list_text_element_ _4 = CLOSE
{let text =
let elems = elems0 in
( Some elems )
in
( Documentation.Reference(rf, text) )}
......@@ -1056,9 +1041,8 @@ in
( "" )
in
( Documentation.Target(target, str) )}
| target = Target data0 = Data _3 = CLOSE
| target = Target data = Data _3 = CLOSE
{let str =
let data = data0 in
( data)
in
( Documentation.Target(target, str) )}
......@@ -1078,47 +1062,40 @@ tag:
{ ( Documentation.Author data )}
| _1 = VERSION data = Data _3 = CLOSE
{ ( Documentation.Version data )}
| _1 = SEE see = see elems0 = list_text_element_ _4 = CLOSE
| _1 = SEE see = see elems = list_text_element_ _4 = CLOSE
{let text =
let elems = elems0 in
( elems )
in
( Documentation.See(see, text) )}
| _1 = SINCE data = Data _3 = CLOSE
{ ( Documentation.Since data )}
| _1 = BEFORE name = name elems0 = list_text_element_ _4 = CLOSE
| _1 = BEFORE name = name elems = list_text_element_ _4 = CLOSE
{let text =
let elems = elems0 in
( elems )
in
( Documentation.Before(name, text) )}
| _1 = DEPRECATED elems0 = list_text_element_ _3 = CLOSE
| _1 = DEPRECATED elems = list_text_element_ _3 = CLOSE
{let text =
let elems = elems0 in
( elems )
in
( Documentation.Deprecated text )}
| _1 = PARAM name = name elems0 = list_text_element_ _4 = CLOSE
| _1 = PARAM name = name elems = list_text_element_ _4 = CLOSE
{let text =
let elems = elems0 in
( elems )
in
( Documentation.Param(name, text) )}
| _1 = RAISE name = name elems0 = list_text_element_ _4 = CLOSE
| _1 = RAISE name = name elems = list_text_element_ _4 = CLOSE
{let text =
let elems = elems0 in
( elems )
in
( Documentation.Raise(name, text) )}
| _1 = RETURN elems0 = list_text_element_ _3 = CLOSE
| _1 = RETURN elems = list_text_element_ _3 = CLOSE
{let text =
let elems = elems0 in
( elems )
in
( Documentation.Return text )}
| _1 = TAG name = name elems0 = list_text_element_ _4 = CLOSE
| _1 = TAG name = name elems = list_text_element_ _4 = CLOSE
{let text =
let elems = elems0 in
( elems )
in
( Documentation.Tag(name, text) )}
......@@ -1160,13 +1137,12 @@ doc_error:
doc:
{ ( DocOckAttrs.empty )}
| _1 = DOC elems0 = list_text_element_ tags0 = list_tag_ _4 = CLOSE
| _1 = DOC elems = list_text_element_ tags' = list_tag_ _4 = CLOSE
{let tags =
let tags = tags0 in
let tags = tags' in
( tags )
in
let text =
let elems = elems0 in
( elems )
in
( Documentation.(Ok {text; tags}) )}
......@@ -1174,13 +1150,12 @@ in
{ ( Documentation.Error err )}
comment:
_1 = COMMENT elems0 = list_text_element_ tags0 = list_tag_ _4 = CLOSE
_1 = COMMENT elems = list_text_element_ tags' = list_tag_ _4 = CLOSE
{let tags =
let tags = tags0 in
let tags = tags' in
( tags )
in
let text =
let elems = elems0 in
( elems )
in
( Documentation.(Documentation (Ok {text; tags})) )}
......
......@@ -20,15 +20,11 @@ type_expr:
( None )
in
()}
| _1 = Ident _100 = Lparen x00 = type_expr _300 = Rparen %prec type_
| _1 = Ident _1' = Lparen x' = type_expr _3 = Rparen %prec type_
{let _2 =
let _30 = _300 in
let x0 = x00 in
let _10 = _100 in
let _1 = _1' in
let x =
let _3 = _30 in
let x = x0 in
let _1 = _10 in
let x = x' in
( x )
in
( Some x )
......
......@@ -4,9 +4,9 @@
%%
a:
_10 = A _2 = c
_1' = A _2 = c
{let _1 =
let _1 = _10 in
let _1 = _1' in
()
in
()}
......
This diff is collapsed.
......@@ -69,9 +69,8 @@ main:
expr_separated_list_SEMI_:
_1 = INT
{ ()}
| _1 = LBRACE xs0 = loption_separated_nonempty_list_SEMI_expr_separated_list_SEMI___ _3 = RBRACE
| _1 = LBRACE xs = loption_separated_nonempty_list_SEMI_expr_separated_list_SEMI___ _3 = RBRACE
{let _2 =
let xs = xs0 in
( xs )
in
()}
......@@ -79,9 +78,8 @@ in
weird_separated_list_COMMA_:
_1 = INT
{ ()}
| _1 = LBRACE xs0 = loption_separated_nonempty_list_COMMA_weird_separated_list_COMMA___ _3 = RBRACE
| _1 = LBRACE xs = loption_separated_nonempty_list_COMMA_weird_separated_list_COMMA___ _3 = RBRACE
{let _2 =
let xs = xs0 in
( xs )
in
()}
......@@ -89,9 +87,8 @@ in
weird_separated_list_SEMI_:
_1 = INT
{ ()}
| _1 = LBRACE xs0 = loption_separated_nonempty_list_SEMI_weird_separated_list_COMMA___ _3 = RBRACE
| _1 = LBRACE xs = loption_separated_nonempty_list_SEMI_weird_separated_list_COMMA___ _3 = RBRACE
{let _2 =
let xs = xs0 in
( xs )
in
()}
......@@ -105,9 +102,8 @@ bizarre_nonseparated_list_SEMI_:
bizarre_separated_list_SEMI_:
_1 = INT
{ ()}
| _1 = LBRACE xs0 = loption_separated_nonempty_list_SEMI_bizarre_nonseparated_list_SEMI___ _3 = RBRACE
| _1 = LBRACE xs = loption_separated_nonempty_list_SEMI_bizarre_nonseparated_list_SEMI___ _3 = RBRACE
{let _2 =
let xs = xs0 in
( xs )
in
()}
......
This diff is collapsed.
......@@ -4,25 +4,23 @@
%%
a:
x00 = d _200 = T _21 = T _2 = T x01 = d _201 = T _22 = T
x' = d _2' = T _2' = T _2 = T x'' = d _2'' = T _2''' = T
{let _3 =
let _2 = _22 in
let _20 = _201 in
let x0 = x01 in
let _2 = _2''' in
let _2' = _2'' in
let x' = x'' in
let x =
let _2 = _20 in
let x = x0 in
let _2 = _2' in
let x = x' in
( f_c x )
in
( f_b x )
in
let x =
let _2 = _21 in
let _20 = _200 in
let x0 = x00 in
let _2 = _2' in
let x =
let _2 = _20 in
let x = x0 in