/**************************************************    
 * This is a prettyprinting parser implementation *    
 * file generated by pretzel. (It's Bison code!)  *    
 *                                                *    
 *************************************************/    

%{                                                     

  /* this is needed for the yyprint function below */  
#include<stdio.h>                                      

  /* include the attribute definitions */              
#include"attr.h"                                     

  /* include the output definitions */                 
#include"output.h"                                   

  /* enable parser debugging */                        
#define YYDEBUG 1                                      

  /* redefine the semantic value */                    
#define YYSTYPE Attribute*                             

  /* enable calls of YYPRINT */                        
#define YYPRINT(file, type, value) yyprint (file, type, value)

  /* this is the prototype for yyprint */              
static void                                            
yyprint(FILE *file, int type, YYSTYPE value);          

  /* make sure that our local copy of yylex is         
   * declared with name `yylex' and as a static        
   * function. This should trick the parser into       
   * calling the scanner object stored in              
   * `static_scanner' (see below). */                  

  /* undefine yylex to make sure it isn't renamed      
   * by any command line option (e.g. -P). */          
#undef yylex                                           
  /* declare our local `dummy' yylex */                
static int yylex(YYSTYPE *rvalue);                     
  /* make sure any newer versions of Bison don't       
   * redeclare it differently */                       
#define YYLEX_DECL                                     
%}                                                     

/*************************************************     
 * various user definitions start here:          *     
 *************************************************/    

/* makeWEAVE definitions for a parser for simple formatted
 * PASCAL code. (preBISON file of makeWEAVE)
 */


%token SEMI
%token BEG
%token END
%token MATH
%token SIMP
%token OPEN
%token CLOSE
%token ALPHA
%token OMEGA
%token UNTIL
%token INTRO
%token PROC
%token COLON
%token ELSE

%{
#define YYDEBUG 1
%}

/*************************************************     
 * some pretzel definitions start here:          *     
 ************************************************/     

%{                                                     

  /* include own header only in default case: */       
#ifndef PPARSE_NAME                                    
#include "Ppparse.h"                                 
#endif                                                 

  /* this is a definition of the yyerror routine */    
yyerror (const char *s)                                
{  /* ignore error messages by BISON */                
   /* trick! use s do get around warning `s not used'. */
   if (1==0) fprintf(stderr, "%s", s);               
   return 0;                                           
}                                                      

  /* enable parser debugging */                        
#define YYDEBUG 1                                      

  /* variable to hold the parsed attribute */          
static Attribute *rest=create(null);                   

  /* include the scanner only in the default case: */  
#ifndef PSCAN_NAME                                     
#define PSCAN_NAME Ppscan                              
#include "Ppscan.h"                                  
#endif                                                 

  /* trick the yyparse function to use the scan member 
   * of the scanner class: */                          
static Pscan* static_scanner = NULL;                   
static int yylex( YYSTYPE *rvalue )                    
{                                                      
    return(static_scanner->scan(rvalue));              
}                                                      

%}                                                     

%pure_parser                                           


%%

stmt_list		: stmt		{rest=$$=$1;}
		| stmt_list SEMI stmt		{ 
			rest=$$=
			join(
				join(
				join(
				$1,
				$2),
				create(force)),
				$3);
		
		}
		| SEMI		{ 
			rest=$$=
			join(
				create("Hallo"),
				create(null));
		
		}
		| stmt_list SEMI		{ 
			rest=$$=
			join(
				$1,
				$2);
		
		}
;

stmt		: BEG stmt_list END		{ 
			rest=$$=
			join(
				join(
				join(
				join(
				join(
				join(
				join(
				$1,
				create(indent)),
				create("[3]")),
				create(force)),
				$2),
				create(outdent)),
				create(force)),
				$3);
		
		}
		| math		{rest=$$=$1;}
;

math		: MATH		{rest=$$=$1;}
		| math math		{rest=$$=join($1,$2);}
		| SIMP		{rest=$$=$1;}
		| OPEN math CLOSE		{rest=$$=join($1,join($2,$3));}
;

stmt		: ALPHA math OMEGA stmt		{ 
			rest=$$=
			join(
				join(
				join(
				join(
				join(
				join(
				join(
				join(
				join(
				$1,
				create(" ")),
				$2),
				create(" ")),
				$3),
				create(indent)),
				create("[2]")),
				create(force)),
				$4),
				create(outdent));
		
		}
;

stmt		: BEG stmt_list UNTIL math		{ 
			rest=$$=
			join(
				join(
				join(
				join(
				join(
				join(
				join(
				join(
				$1,
				create(indent)),
				create("[1]")),
				create(force)),
				$2),
				create(outdent)),
				create(force)),
				$3),
				$4);
		
		}
;

proc		: proc INTRO math SEMI		{ 
			rest=$$=
			join(
				join(
				join(
				join(
				$1,
				$2),
				$3),
				$4),
				create(force));
		
		}
		| proc stmt		{ 
			rest=$$=
			join(
				join(
				$1,
				$2),
				create(force));
		
		}
		| PROC		{rest=$$=$1;}
;

stmt		: INTRO math SEMI		{ 
			rest=$$=
			join(
				join(
				join(
				$1,
				$2),
				$3),
				create(force));
		
		}
;

%%
/*************************************************     
 * additional pretzel code starts here:          *     
 *************************************************/    

#ifndef PPARSE_NAME                                    
#define PPARSE_NAME Ppparse                            
#endif                                                 

PPARSE_NAME::PPARSE_NAME()                             
{                                                      
}                                                      
PPARSE_NAME::~PPARSE_NAME()                            
{                                                      
    static_scanner = NULL;                             
}                                                      
int PPARSE_NAME::prettyprint(istream* is, ostream* os) 
{                                                      
    Latex_cweb_output ppoutput(*os);                   
    return(prettyprint(is, &ppoutput));                
}                                                      
int PPARSE_NAME::prettyprint(istream* is, Output* outp)
{                                                      
    int ret_val = 0;                                   
    PSCAN_NAME ppscanner(is);                          
    static_scanner = &ppscanner;                       
    ret_val=yyparse();                                 
    while (!(is->eof())) {                             
        char c;                                        
        is->get(c);                                    
    }                                                  
    rest->print(*outp);                                
    return(ret_val);                                   
}                                                      

void PPARSE_NAME::debug_on() { yydebug = 1; }          
void PPARSE_NAME::debug_off() { yydebug = 0; }         

  /* This is a routine to print the semantic value */
static void 
yyprint(FILE *file, int type, YYSTYPE value) {
  Latex_cweb_output os(cerr);
  value->print(os);
}
/*************************************************     
 * optional user defined C code follows here:    *     
 *************************************************/    

