Re: pretty-printing with yacc and lex (Stavros Macrakis)
27 Mar 1996 00:10:17 -0500

          From comp.compilers

Related articles
Source for compiler using yacc/lex wanted platon! (1996-03-05)
Re: Source for compiler using yacc/lex wanted (1996-03-22)
Re: pretty-printing with yacc and lex (Scott Stanchfield) (1996-03-22)
Re: pretty-printing with yacc and lex (1996-03-27)
| List of all articles for this month |

From: (Stavros Macrakis)
Newsgroups: comp.compilers
Date: 27 Mar 1996 00:10:17 -0500
Organization: OSF Research Institute
References: <96-03-042@comp.compilers >96-03-145 96-03-161
Keywords: tools, parse

Scott Stanchfield <> writes:

      As long as people use _reasonable_ pre-processor definitions, there
      should be no problem pretty-printing code...

Although I dislike "crazy" preprocessor definitions, pretty-printing
in their presence is not that hard. See Emacs Lisp code below
demonstrating how (of course, this isn't a complete implementation).

My code will accept the example given, and even worse cases, e.g.

#define begin {
#define end }

      x := x + 1;
#if foobar
#elif barfoo /* resume #if-level indentation */
            z := z + 1
      end end

Of course, it's not perfect....




Here's the code:

;;; Bare-bones pretty printer, demonstrating pretty-printing with
;;; C-like token macros.

;;; #define xxx ... defines a macro.

;;; { and } are only indentation depth changers

(defconst indent_tab 3)

(defun do-indent (depth)
    (insert-char ? (* indent_tab depth)))

(defun do-indent-buf ()
    (goto-char (point-min))
    (let ((depth 0) (depth-stack ()))
        (while (not (eobp))
            (cond ((looking-at "^#define \\([a-z]+\\)") ;handle macro defn
(put (intern (match-string 1))
(progn (goto-char (match-end 0))
((looking-at "^#if\\b") (forward-line 1)
(setq depth-stack (cons depth depth-stack)))
((looking-at "^#else\\b") (forward-line 1)
(setq depth (car depth-stack)))
((looking-at "^#endif\\b") (forward-line 1)
(setq depth (car depth-stack)
depth-stack (cdr depth-stack)))
(do-indent depth)
(let ((line (tokens-to-eol))
(closersonlyp t))
(while line
(setq carline (car line) line (cdr line))
(cond ((eq carline '\{)
(setq depth (1+ depth) closersonlyp nil))
((eq carline '\}) (setq depth (1- depth)))
((get carline 'cmacro)
(setq line (append (get carline 'cmacro) line)))
(t (setq closersonlyp nil))))
(if closersonlyp
(save-excursion (beginning-of-line 0)
(do-indent depth)))))))))

(defun tokens-to-eol ()
    (let (line)
        (while (looking-at "[, ]*\\([^, {}\n]+\\|[{}]\\)[, ]*")
            (setq line (cons (intern (match-string 1)) line))
            (goto-char (match-end 0)))
        (beginning-of-line 2)
        (nreverse line)))


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.