some improvements
This commit is contained in:
		
							parent
							
								
									0d63470af3
								
							
						
					
					
						commit
						0e39681621
					
				
					 5 changed files with 94 additions and 94 deletions
				
			
		|  | @ -10,37 +10,97 @@ | ||||||
| #include "LLlex.h" | #include "LLlex.h" | ||||||
| 
 | 
 | ||||||
| long str2long(); | long str2long(); | ||||||
| char *GetString(); |  | ||||||
| 
 | 
 | ||||||
| struct token dot, aside; | struct token dot, aside; | ||||||
| 
 | 
 | ||||||
| static char *RcsId = "$Header$"; | static char *RcsId = "$Header$"; | ||||||
| 
 | 
 | ||||||
| int | /*	Skip Modula-2 like comment (* ... *).
 | ||||||
| LLlex() | 	Note that comment may be nested. | ||||||
|  | */ | ||||||
|  | static | ||||||
|  | SkipComment() | ||||||
| { | { | ||||||
|  | 	register int ch; | ||||||
|  | 	register int NestLevel = 0; | ||||||
|  | 
 | ||||||
|  | 	LoadChar(ch); | ||||||
|  | 	for (;;) { | ||||||
|  | 		if (class(ch) == STNL) { | ||||||
|  | 			LineNumber++; | ||||||
|  | 		} | ||||||
|  | 		else | ||||||
|  | 		if (ch == '(') { | ||||||
|  | 			LoadChar(ch); | ||||||
|  | 			if (ch == '*') { | ||||||
|  | 				++NestLevel; | ||||||
|  | 			} | ||||||
|  | 			else { | ||||||
|  | 				continue; | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		else | ||||||
|  | 		if (ch == '*') { | ||||||
|  | 			LoadChar(ch); | ||||||
|  | 			if (ch == ')') { | ||||||
|  | 				if (NestLevel-- == 0) { | ||||||
|  | 					return; | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 			else { | ||||||
|  | 				continue; | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		LoadChar(ch); | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | static char * | ||||||
|  | GetString(upto) | ||||||
|  | { | ||||||
|  | 	register int ch; | ||||||
|  | 	int str_size; | ||||||
|  | 	char *str = Malloc(str_size = 32); | ||||||
|  | 	register int pos = 0; | ||||||
|  | 	 | ||||||
|  | 	LoadChar(ch); | ||||||
|  | 	while (ch != upto)	{ | ||||||
|  | 		if (class(ch) == STNL)	{ | ||||||
|  | 			lexerror("newline in string"); | ||||||
|  | 			LineNumber++; | ||||||
|  | 			break; | ||||||
|  | 		} | ||||||
|  | 		if (ch == EOI) { | ||||||
|  | 			lexerror("end-of-file in string"); | ||||||
|  | 			break; | ||||||
|  | 		} | ||||||
|  | 		str[pos++] = ch; | ||||||
|  | 		if (pos == str_size)	{ | ||||||
|  | 			str = Srealloc(str, str_size += 8); | ||||||
|  | 		} | ||||||
|  | 		LoadChar(ch); | ||||||
|  | 	} | ||||||
|  | 	str[pos] = '\0'; | ||||||
|  | 	return str; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| /*	LLlex() plays the role of Lexical Analyzer for the parser.
 | /*	LLlex() plays the role of Lexical Analyzer for the parser.
 | ||||||
| 	The putting aside of tokens is taken into account. | 	The putting aside of tokens is taken into account. | ||||||
| */ | */ | ||||||
| 	if (ASIDE)	{	/* a token is put aside		*/ |  | ||||||
| 		dot = aside; |  | ||||||
| 		ASIDE = 0; |  | ||||||
| 	} |  | ||||||
| 	else	{ |  | ||||||
| 		GetToken(&dot); |  | ||||||
| 		if (DOT == EOI) DOT = -1; |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	return DOT; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| int | int | ||||||
| GetToken(tk) | LLlex() | ||||||
| 	register struct token *tk; |  | ||||||
| { | { | ||||||
|  | 	register struct token *tk = ˙ | ||||||
| 	char buf[(IDFSIZE > NUMSIZE ? IDFSIZE : NUMSIZE) + 1]; | 	char buf[(IDFSIZE > NUMSIZE ? IDFSIZE : NUMSIZE) + 1]; | ||||||
| 	register int ch, nch; | 	register int ch, nch; | ||||||
| 
 | 
 | ||||||
|  | 	if (ASIDE)	{	/* a token is put aside		*/ | ||||||
|  | 		*tk = aside; | ||||||
|  | 		ASIDE = 0; | ||||||
|  | 		return tk->tk_symb; | ||||||
|  | 	} | ||||||
|  | 	tk->tk_lineno = LineNumber; | ||||||
|  | 
 | ||||||
| again: | again: | ||||||
| 	LoadChar(ch); | 	LoadChar(ch); | ||||||
| 	if ((ch & 0200) && ch != EOI) { | 	if ((ch & 0200) && ch != EOI) { | ||||||
|  | @ -54,6 +114,7 @@ again: | ||||||
| 
 | 
 | ||||||
| 	case STNL: | 	case STNL: | ||||||
| 		LineNumber++; | 		LineNumber++; | ||||||
|  | 		tk->tk_lineno++; | ||||||
| 		goto again; | 		goto again; | ||||||
| 
 | 
 | ||||||
| 	case STGARB: | 	case STGARB: | ||||||
|  | @ -305,79 +366,12 @@ Sdec: | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	case STEOI: | 	case STEOI: | ||||||
| 		return tk->tk_symb = EOI; | 		return tk->tk_symb = -1; | ||||||
| 
 | 
 | ||||||
| 	case STCHAR: | 	case STCHAR: | ||||||
| 	default: | 	default: | ||||||
| 		crash("bad character class %d", class(ch)); | 		crash("bad character class %d", class(ch)); | ||||||
| 	} | 	} | ||||||
|  | 	/*NOTREACHED*/ | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| char * |  | ||||||
| GetString(upto) |  | ||||||
| { |  | ||||||
| 	register int ch; |  | ||||||
| 	int str_size; |  | ||||||
| 	char *str = Malloc(str_size = 32); |  | ||||||
| 	register int pos = 0; |  | ||||||
| 	 |  | ||||||
| 	LoadChar(ch); |  | ||||||
| 	while (ch != upto)	{ |  | ||||||
| 		if (class(ch) == STNL)	{ |  | ||||||
| 			lexerror("newline in string"); |  | ||||||
| 			LineNumber++; |  | ||||||
| 			break; |  | ||||||
| 		} |  | ||||||
| 		if (ch == EOI) { |  | ||||||
| 			lexerror("end-of-file in string"); |  | ||||||
| 			break; |  | ||||||
| 		} |  | ||||||
| 		str[pos++] = ch; |  | ||||||
| 		if (pos == str_size)	{ |  | ||||||
| 			str = Srealloc(str, str_size += 8); |  | ||||||
| 		} |  | ||||||
| 		LoadChar(ch); |  | ||||||
| 	} |  | ||||||
| 	str[pos] = '\0'; |  | ||||||
| 	return str; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| SkipComment() |  | ||||||
| { |  | ||||||
| 	/*	Skip Modula-2 like comment (* ... *).
 |  | ||||||
| 		Note that comment may be nested. |  | ||||||
| 	*/ |  | ||||||
| 
 |  | ||||||
| 	register int ch; |  | ||||||
| 	register int NestLevel = 0; |  | ||||||
| 
 |  | ||||||
| 	LoadChar(ch); |  | ||||||
| 	for (;;) { |  | ||||||
| 		if (class(ch) == STNL) { |  | ||||||
| 			LineNumber++; |  | ||||||
| 		} |  | ||||||
| 		else |  | ||||||
| 		if (ch == '(') { |  | ||||||
| 			LoadChar(ch); |  | ||||||
| 			if (ch == '*') { |  | ||||||
| 				++NestLevel; |  | ||||||
| 			} |  | ||||||
| 			else { |  | ||||||
| 				continue; |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 		else |  | ||||||
| 		if (ch == '*') { |  | ||||||
| 			LoadChar(ch); |  | ||||||
| 			if (ch == ')') { |  | ||||||
| 				if (NestLevel-- == 0) { |  | ||||||
| 					return; |  | ||||||
| 				} |  | ||||||
| 			} |  | ||||||
| 			else { |  | ||||||
| 				continue; |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 		LoadChar(ch); |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -4,6 +4,7 @@ | ||||||
| 
 | 
 | ||||||
| struct token	{ | struct token	{ | ||||||
| 	int tk_symb;		/* token itself	*/ | 	int tk_symb;		/* token itself	*/ | ||||||
|  | 	int tk_lineno;		/* linenumber on which it occurred */ | ||||||
| 	union { | 	union { | ||||||
| 		struct idf *tk_idf;	/* IDENT	*/ | 		struct idf *tk_idf;	/* IDENT	*/ | ||||||
| 		char *tk_str;		/* STRING	*/ | 		char *tk_str;		/* STRING	*/ | ||||||
|  |  | ||||||
|  | @ -4,12 +4,13 @@ | ||||||
| HDIR =	../../em/h | HDIR =	../../em/h | ||||||
| PKGDIR =	../../em/pkg | PKGDIR =	../../em/pkg | ||||||
| LIBDIR =	../../em/lib | LIBDIR =	../../em/lib | ||||||
| INCLUDES = -I$(HDIR) -I$(PKGDIR) -I/user1/erikb/h | INCLUDES = -I$(HDIR) -I$(PKGDIR) -I/user1/erikb/em/h | ||||||
| LSRC =	tokenfile.g program.g declar.g expression.g statement.g | LSRC =	tokenfile.g program.g declar.g expression.g statement.g | ||||||
| CC =	cc | CC =	cc | ||||||
| GEN =	LLgen | GEN =	LLgen | ||||||
| GENOPTIONS = | GENOPTIONS = | ||||||
| CFLAGS =	-DDEBUG -O $(INCLUDES) | CFLAGS =	-DDEBUG -p $(INCLUDES) | ||||||
|  | LFLAGS =	-p | ||||||
| LOBJ =	tokenfile.o program.o declar.o expression.o statement.o | LOBJ =	tokenfile.o program.o declar.o expression.o statement.o | ||||||
| COBJ =	LLlex.o LLmessage.o char.o error.o main.o \
 | COBJ =	LLlex.o LLmessage.o char.o error.o main.o \
 | ||||||
| 	symbol2str.o tokenname.o idf.o input.o idlist.o | 	symbol2str.o tokenname.o idf.o input.o idlist.o | ||||||
|  | @ -27,7 +28,7 @@ LLfiles:	$(LSRC) | ||||||
| 	@touch LLfiles | 	@touch LLfiles | ||||||
| 
 | 
 | ||||||
| main:	$(OBJ) Makefile | main:	$(OBJ) Makefile | ||||||
| 	$(CC) $(LFLAGS) $(OBJ) $(LIBDIR)/libcomp.a /user1/erikb/em/lib/libstr.a /user1/erikb/lib/libsystem.a -o main | 	$(CC) $(LFLAGS) $(OBJ) $(LIBDIR)/libcomp.a $(LIBDIR)/malloc.o /user1/erikb/em/lib/libstr.a /user1/erikb/em/lib/libsystem.a -o main | ||||||
| 	size main | 	size main | ||||||
| 
 | 
 | ||||||
| clean: | clean: | ||||||
|  |  | ||||||
|  | @ -177,5 +177,9 @@ VariableDeclaration | ||||||
| { | { | ||||||
| 	struct id_list *VarList; | 	struct id_list *VarList; | ||||||
| } : | } : | ||||||
| 	IdentList(&VarList) ':' type | 	IdentList(&VarList) | ||||||
|  | 	[ | ||||||
|  | 		ConstExpression | ||||||
|  | 	]? | ||||||
|  | 	':' type | ||||||
| ; | ; | ||||||
|  |  | ||||||
|  | @ -9,7 +9,7 @@ | ||||||
| #include "LLlex.h" | #include "LLlex.h" | ||||||
| #include "Lpars.h" | #include "Lpars.h" | ||||||
| 
 | 
 | ||||||
| static char *RcsId = "$Header:"; | static char *RcsId = "$Header$"; | ||||||
| 
 | 
 | ||||||
| char options[128]; | char options[128]; | ||||||
| char *ProgName; | char *ProgName; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue