\begin{algo}{DuvalGeneration}{n}
  \IN{length $n$}
  \OUT{all the Lyndon words of length up to $n$, in lexorder}
  \SET{w_0}{\CALL{First}{\Sigma}}
  \SET{i}{0}
  \DOWHILE{i\neq -1}
    \DOFORI{j}{1}{n-1}
      \SET{w_{i+j}}{w_{j-1}}
    \OD
    \CALL{output}{Pref_i(w)}
    \SET{i}{n-1}
    \DOWHILE{i>-1 \AND w_i=\CALL{Last}{\Sigma}}
      \DEC{i}
    \OD
    \IF{i>-1}
      \SET{w_i}{\CALL{Next}{w_i}}
    \FI
  \OD
\end{algo}
