/* cee2html.fg - pretzel formatted grammar file for simple C
 *
 * target formatter: HTML
 *
 * $Id: cee2html.fg,v 1.1 1996/12/16 18:53:15 gaertner Exp $
 *
 * History at end.
 */

%token BINOP
%token UNORBINOP
%token QUESTION
%token UNOP
%token LPAR
%token RPAR
%token LBRACE
%token RBRACE
%token SEMI
%token COLON
%token LPROC

%token ID

%token NUMBER
%token INT_LIKE
%token CASE_LIKE
%token IF_LIKE
%token DO_LIKE
%token ELSE_LIKE
%token TYPEDEF_LIKE
%token STRUCT_LIKE
%token SIZEOF_LIKE
%token DEFINE_LIKE
%token COMMA

%token INSERT
%token STRING

%token CHUNK
%token IGNORE



%%

final	: exp
	| stmt
;


  // get formatting of identifiers right

id	: ID
;


exp	: id	{ "<i>" $1 "</i>" }
;


  // ************************************************************
  // format expressions:

exp	: NUMBER
	| STRING
;

  // easy things:

exp	: exp UNOP		// x++
	| UNOP exp		// ++x
	| exp BINOP exp
	| exp UNORBINOP exp
	| UNORBINOP exp		// *x

	| exp LPAR decl RPAR		// f(int i)
	| exp LPAR exp RPAR	// f(a+b)
	| exp LPAR RPAR		// f()

	| LPAR exp RPAR
;

exp	: SIZEOF_LIKE exp INT_LIKE	{ $1 $2 " " $3 }	// new(z) char
;

exp	: SIZEOF_LIKE exp		{ $1 " " $2 }
	| SIZEOF_LIKE LPAR decl RPAR		// sizeof(struct s)
	| SIZEOF_LIKE INT_LIKE		{ $1 " " $2 }
	| SIZEOF_LIKE UNORBINOP		{ $1 " " $2 }
	| SIZEOF_LIKE BINOP		{ $1 " " $2 }
	| SIZEOF_LIKE UNOP		{ $1 " " $2 }
	| SIZEOF_LIKE SIZEOF_LIKE	{ $1 " " $2 }
	| SIZEOF_LIKE COMMA		{ $1 " " $2 }
;





  // **************************************************************
  // declarations of variables with initializers


decl	: INT_LIKE exp			{ $1 " " $2 }	// int a=1
	| INT_LIKE INT_LIKE exp		{ $1 " " $2 " " $3 }	// extern int a
	| exp exp			{ $1 " " $2 }	// foo bar=1
	| exp				{ $1 }		// int foo=1, bar, foobar

	| decl COMMA INT_LIKE exp	{ $1 $2  force $3 $4 }
	| decl COMMA INT_LIKE exp decl	{ $1 $2  force $3 " " $4 " " $3 }
	| decl COMMA exp exp		{ $1 $2  force $3 " " $4 }
	| decl COMMA exp		{ $1 $2  force $3 }

	| STRUCT_LIKE exp LBRACE decl RBRACE exp	// structs
					{ $1 " " $2 " "$3 indent force $4
					outdent force $5 " " $6  }
	| STRUCT_LIKE exp LBRACE stmt RBRACE exp
					{ $1 " " $2 " " $3 indent force $4
					outdent force $5 " " $6 }
	| STRUCT_LIKE exp LBRACE stmt RBRACE
					{ $1 " " $2 " " $3 indent
					force $4 outdent force $5 }
	| STRUCT_LIKE decl		{ $1 " " $2 }
;


stmt	: decl SEMI			// int i;
;

stmt	: decl stmt			//
;


  // ***************************************************************
  // typedefs

typedef	: TYPEDEF_LIKE decl		{ $1 " " $2 }
;

stmt	: typedef SEMI
;




  // ***************************************************************
  // statements


stmt	: exp SEMI
;

  // empty statement

stmt	: LBRACE RBRACE
;

  // block statement
stmt	: LBRACE stmt RBRACE	 { force $1 indent force $2 outdent force
					$3 force }
;


  // *****************************************************************
  // control structures

  // an `else_like' is something to which a statement may be appended

  // if then else

else_like	: IF_LIKE exp		{ $1 " " $2 }
	| ELSE_LIKE
;

else_like	: else_like stmt else_like	{
				force $1 $2
			        force $3 " " cancel }
 //		| else_like exp else_like
;

  // else-less if

stmt	: else_like stmt	{ $1 indent
				break_space $2
				outdent force }
;

  // else if

else_like : else_like else_like		{ $1 cancel " " $2 }
;


  // do, case etc
stmt	: DO_LIKE stmt else_like SEMI


	| CASE_LIKE SEMI	// return;
	| CASE_LIKE exp SEMI	{ $1 " " $2 $3 }
;

tag	: CASE_LIKE COLON	// default:
	| exp COLON		{ "\n@def " $1 "\n" $1 $2 }
	| CASE_LIKE exp COLON	{ $1 " " $2 $3 }
	| tag tag		{ $1 break_space $2 }
;

stmt	: tag stmt		{ force backup $1 opt9 $2 }
;

  // `for' constructs

else_like	: IF_LIKE LPAR stmt stmt exp RPAR
	| IF_LIKE LPAR SEMI stmt RPAR		// for (; a;)
	| IF_LIKE LPAR stmt stmt RPAR		// for (a; b;)
;

  // join up statements

stmt	: stmt stmt		{ $1 force $2 }
  	| stmt SEMI			// empty statemet
;


stmt	: CHUNK SEMI		{ $1 $2 force }
	| CHUNK
;

exp	: CHUNK
;




  // ***********************************************************
  // preprocessor directives


stmt	: LPROC
	| LPROC stmt
	| LPROC STRING		// #include "abc.h"
	| LPROC LPROC		// #include <abc.h>
	| INSERT
	| IGNORE
;





%%

/*
 * $Log: cee2html.fg,v $
 * Revision 1.1  1996/12/16 18:53:15  gaertner
 * Initial revision
 *
 */
