| 
									
										
										
										
											2019-03-01 15:34:44 -08:00
										 |  |  | import itertools | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-04 07:26:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-01 15:34:44 -08:00
										 |  |  | def generate_tokens(tokens): | 
					
						
							|  |  |  |     numbers = itertools.count(0) | 
					
						
							|  |  |  |     for line in tokens: | 
					
						
							|  |  |  |         line = line.strip() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-25 02:01:19 +05:30
										 |  |  |         if not line or line.startswith('#'): | 
					
						
							| 
									
										
										
										
											2019-03-01 15:34:44 -08:00
										 |  |  |             continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         name = line.split()[0] | 
					
						
							|  |  |  |         yield (name, next(numbers)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     yield ('N_TOKENS', next(numbers)) | 
					
						
							|  |  |  |     yield ('NT_OFFSET', 256) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-04 07:26:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-01 15:34:44 -08:00
										 |  |  | def generate_opmap(tokens): | 
					
						
							|  |  |  |     for line in tokens: | 
					
						
							|  |  |  |         line = line.strip() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-25 02:01:19 +05:30
										 |  |  |         if not line or line.startswith('#'): | 
					
						
							| 
									
										
										
										
											2019-03-01 15:34:44 -08:00
										 |  |  |             continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         pieces = line.split() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if len(pieces) != 2: | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         name, op = pieces | 
					
						
							|  |  |  |         yield (op.strip("'"), name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Yield independently <>. This is needed so it does not collide | 
					
						
							|  |  |  |     # with the token generation in "generate_tokens" because if this | 
					
						
							|  |  |  |     # symbol is included in Grammar/Tokens, it will collide with != | 
					
						
							|  |  |  |     # as it has the same name (NOTEQUAL). | 
					
						
							|  |  |  |     yield ('<>', 'NOTEQUAL') |