dlg_p.g

来自「SRI international 发布的OAA框架软件」· G 代码 · 共 615 行 · 第 1/2 页

G
615
字号
/*  This is the parser for the dlg
 *  This is a part of the Purdue Compiler Construction Tool Set
 *
 * SOFTWARE RIGHTS
 *
 * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
 * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
 * company may do whatever they wish with source code distributed with
 * PCCTS or the code generated by PCCTS, including the incorporation of
 * PCCTS, or its output, into commerical software.
 *
 * We encourage users to develop software with PCCTS.  However, we do ask
 * that credit is given to us for developing PCCTS.  By "credit",
 * we mean that if you incorporate our source code into one of your
 * programs (commercial product, research project, or otherwise) that you
 * acknowledge this fact somewhere in the documentation, research report,
 * etc...  If you like PCCTS and have developed a nice tool with the
 * output, please mention that you developed it using PCCTS.  In
 * addition, we ask that this header remain intact in our source code.
 * As long as these guidelines are kept, we expect to continue enhancing
 * this system and expect to make other tools available as they are
 * completed.
 *
 * DLG 1.33
 * Will Cohen
 * With mods by Terence Parr; AHPCRC, University of Minnesota
 * 1989-1995
 */

#header	<<
#include <ctype.h>
#include "dlg.h"
>>

<<

/* MR20 G. Hobbelt 
   Fix for Borland C++ 4.x & 5.x compiling with ALL warnings enabled
*/

#ifdef __TURBOC__
#pragma warn -aus  /* unused assignment of 'xxx' */
#endif

int	action_no = 0;	   /* keep track of actions outputed */
int	nfa_allocated = 0; /* keeps track of number of nfa nodes */
nfa_node **nfa_array = NULL;/* root of binary tree that stores nfa array */
nfa_node nfa_model_node;   /* model to initialize new nodes */
set	used_chars;	   /* used to label trans. arcs */
set	used_classes;	   /* classes or chars used to label trans. arcs */
set	normal_chars;	   /* mask to get rid elements that aren't used
			      in set */
int	flag_paren = FALSE;
int	flag_brace = FALSE;
int	mode_counter = 0;  /* keep track of number of %%names */

>>

#lexaction <<
int	func_action;		/* should actions be turned into functions?*/
int	lex_mode_counter = 0;	/* keeps track of the number of %%names */
/* MR1									    */
/* MR1  11-Apr-97	Provide mechanism for inserting code into DLG class */
/* MR1				via <<%%lexmember...>>			    */
/* MR1									    */
int	lexMember = 0;		/* <<%%lexmemeber ...>>	   		MR1 */
int	lexAction = 0;		/* <<%%lexaction ...>>			MR1 */
int	parserClass = 0;	/* <<%%parserclass ...>>        MR1 */
int	lexPrefix = 0;		/* <<%%lexprefix ...>>			MR1 */
char	theClassName[100];						     /* MR11 */
char	*pClassName=theClassName;					 /* MR11 */
int	firstLexMember=1;					             /* MR1 */

#ifdef __USE_PROTOS
void  xxputc(int c) {						/* MR1 */
#else
void xxputc(c)							/* MR1 */
  int	c;							/* MR1 */
{								/* MR1 */
#endif
  if (parserClass) {						/* MR1 */
    *pClassName++=c;						/* MR1 */
    *pClassName=0;						/* MR1 */
  } else if (lexMember || lexPrefix) {				/* MR1 */
    if (class_stream != NULL) fputc(c,class_stream);		/* MR1 */
  } else {							/* MR1 */
    fputc(c,OUT);						/* MR1 */
  };								/* MR1 */
}  								/* MR1 */

#ifdef __USE_PROTOS
void xxprintf(char *format,char *string) {			/* MR1 */
#else
void xxprintf(format,string) 					/* MR1 */
  char *format;							/* MR1 */
  char *string;							/* MR1 */
{								/* MR1 */
#endif
  if (lexMember || lexPrefix || parserClass) {			/* MR1 */
    if (class_stream != NULL)					/* MR1 */
	 fprintf(class_stream,format,string);			/* MR1 */
  } else {							/* MR1 */
    fprintf(OUT,format,string);					/* MR1 */
  };								/* MR1 */
}  								/* MR1 */
>>

#token "[\r\t\ ]+"	<< zzskip(); >>						/* Ignore white */
#token "\n"			<< zzline++; zzskip(); DAWDLE; >>	/* Track Line # */
#token L_EOF		"\@"
#token PER_PER		"\%\%"
#token NAME_PER_PER	"\%\%[a-zA-Z_][a-zA-Z0-9_]*"
		<< p_mode_def(&zzlextext[2],lex_mode_counter++); >>

#token LEXMEMBER	"\<\<\%\%lexmember"			/* MR1 */
		<<lexMember=1;					/* MR1 */
	          if (firstLexMember != 0) {			/* MR1 */
	            firstLexMember=0;				/* MR1 */
	            p_class_def1();				/* MR1 */
		  };						/* MR1 */
	          zzmode(ACT);					/* MR1 */
                >>						/* MR1 */
#token LEXACTION	"\<\<\%\%lexaction"			/* MR1 */
		<<lexAction=1;zzmode(ACT);>>			/* MR1 */
#token PARSERCLASS	"\<\<\%\%parserclass"			/* MR1 */
		<<parserClass=1;				/* MR1 */
		  zzmode(ACT);					/* MR1 */
		>>						/* MR1 */
#token LEXPREFIX	"\<\<\%\%lexprefix"			/* MR1 */
		<<lexPrefix=1;zzmode(ACT);>>			/* MR1 */

#token ACTION		"\<\<"
		<< if (func_action)
			fprintf(OUT,"\n%s %sact%d()\n{ ",
					gen_cpp?"ANTLRTokenType":"static void",
					gen_cpp?ClassName("::"):"", ++action_no);
		   zzmode(ACT); zzskip();
		>>
#token GREAT_GREAT	"\>\>"
#token L_BRACE		"\{"
#token R_BRACE		"\}"
#token L_PAR		"\("
#token R_PAR		"\)"
#token L_BRACK		"\["
#token R_BRACK		"\]"
#token ZERO_MORE	"\*"
#token ONE_MORE		"\+"
#token OR		"\|"
#token RANGE		"\-"
#token NOT		"\~"
#token OCTAL_VALUE "\\0[0-7]*"		
	<< {int t; sscanf(&zzlextext[1],"%o",&t); zzlextext[0] = t;}>>
#token HEX_VALUE   "\\0[Xx][0-9a-fA-F]+"
	<< {int t; sscanf(&zzlextext[3],"%x",&t); zzlextext[0] = t;}>>
#token DEC_VALUE   "\\[1-9][0-9]*"
	<< {int t; sscanf(&zzlextext[1],"%d",&t); zzlextext[0] = t;}>>
#token TAB		"\\t"		<< zzlextext[0] = '\t';>>
#token NL		"\\n"		<< zzlextext[0] = '\n';>>
#token CR		"\\r"		<< zzlextext[0] = '\r';>>
#token BS		"\\b"		<< zzlextext[0] = '\b';>>

/* MR1									*/
/* MR1 10-Apr-97 MR1	Allow #token regular expressions to cross lines	*/
/* MR1									*/
#token CONTINUATION	"\\ \n"		<< zzline++; zzskip();>> /* MR1 */

/* NOTE: this takes ANYTHING after the \ */
#token LIT		"\\~[tnrb]"	<< zzlextext[0] = zzlextext[1];>>

/* NOTE: this takes ANYTHING that doesn't match the other tokens */
#token REGCHAR		"~[\\]"


grammar		:   << p_head(); p_class_hdr(); func_action = FALSE;>>
		 ( {LEXACTION | LEXMEMBER | LEXPREFIX | PARSERCLASS } ACTION)* /* MR1 */
		    <<if ( gen_cpp ) p_includes();>>
		    start_states
		    << func_action = FALSE; p_tables(); p_tail(); >>
		    (ACTION)* "@"
			<< if (firstLexMember != 0) p_class_def1(); >> 		/* MR1 */
		;

start_states	: ( PER_PER do_conversion
		  | NAME_PER_PER do_conversion (NAME_PER_PER do_conversion)*)
		    PER_PER
		;

do_conversion	: <<new_automaton_mode(); func_action = TRUE;>>
			rule_list
			<<
				dfa_class_nop[mode_counter] =
					relabel($1.l,comp_level);
				if (comp_level)
					p_shift_table(mode_counter);
				dfa_basep[mode_counter] = dfa_allocated+1;
				make_dfa_model_node(dfa_class_nop[mode_counter]);
				nfa_to_dfa($1.l);
				++mode_counter;
		    		func_action = FALSE;
#ifdef HASH_STAT
				fprint_hash_stats(stderr);
#endif
			>>
		;

rule_list	: rule <<$$.l=$1.l; $$.r=$1.r;>>
			(rule
				<<{nfa_node *t1;
				   t1 = new_nfa_node();
				   (t1)->trans[0]=$$.l;
				   (t1)->trans[1]=$1.l;
				   /* all accept nodes "dead ends" */
				   $$.l=t1; $$.r=NULL;
				   }
				>>
			)*
		| /* empty */
			<<$$.l = new_nfa_node(); $$.r = NULL;
			   warning("no regular expressions", zzline);
			>>
		;

rule	: reg_expr ACTION
/* MR23 */		<< if ($1.r != NULL) {
					$$.l=$1.l; $$.r=$1.r; ($1.r)->accept=action_no;
				   }
				>>
		| ACTION
			<<$$.l = NULL; $$.r = NULL;
			  error("no expression for action  ", zzline);
			>>
		;

reg_expr	: and_expr <<$$.l=$1.l; $$.r=$1.r;>>
			(OR and_expr
				<<{nfa_node *t1, *t2;
				   t1 = new_nfa_node(); t2 = new_nfa_node();
				   (t1)->trans[0]=$$.l;
				   (t1)->trans[1]=$2.l;
/* MR23 */		   if ($$.r != NULL) ($$.r)->trans[1]=t2;
                   if ($2.r) {
    				   ($2.r)->trans[1]=t2;     /* MR20 */
                   }
				   $$.l=t1; $$.r=t2;
				  }
				>>
			)*
		;

and_expr	: repeat_expr
					<<
						$$.l=$1.l; $$.r=$1.r;
				    >>
			(repeat_expr 
/* MR23 */				<< if ($$.r != NULL) {
							($$.r)->trans[1]=$1.l;
							$$.r=$1.r;
						   }
						>>
			)*
		;

repeat_expr	: expr <<$$.l=$1.l; $$.r=$1.r;>>
			{ ZERO_MORE
			<<{	nfa_node *t1,*t2;
/* MR23 */		if ($$.r != NULL) ($$.r)->trans[0] = $$.l;
				t1 = new_nfa_node(); t2 = new_nfa_node();
				t1->trans[0]=$$.l;
				t1->trans[1]=t2;
/* MR23 */		if ($$.r != NULL) ($$.r)->trans[1]=t2;
				$$.l=t1;$$.r=t2;
			  }
			>>
			| ONE_MORE
/* MR23 */		<<if ($$.r != NULL) ($$.r)->trans[0] = $$.l;>>
			}
		| ZERO_MORE
			<< error("no expression for *", zzline);>>
		| ONE_MORE
			<< error("no expression for +", zzline);>>
		;

expr	: << $$.l = new_nfa_node();
			 $$.r = new_nfa_node();
		  >>
		  L_BRACK atom_list R_BRACK
			<<
/* MR23 */		if ($$.l != NULL) {
					($$.l)->trans[0] = $$.r;
					($$.l)->label = set_dup($2.label);
					set_orin(&used_chars,($$.l)->label);
				}
			>>
		| NOT L_BRACK atom_list R_BRACK
			<<
/* MR23 */		if ($$.l != NULL) {
					($$.l)->trans[0] = $$.r;
					($$.l)->label = set_dif(normal_chars,$3.label);
					set_orin(&used_chars,($$.l)->label);
				}
			>>
		| L_PAR reg_expr R_PAR
			<<
/* MR23 */		if ($$.l != NULL) {				
					($$.l)->trans[0] = $2.l;
					if ($2.r) {
    					($2.r)->trans[1] = $$.r;    /* MR20 */
					}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?