How to use token_value method in Gherkin-python

Best Python code snippet using gherkin-python

Run Gherkin-python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

block.py

Source: block.py Github

copy
1import re
2
3#import ply.lex as lex
4def loadSymbolTable():
5	
6	symbolTable["keyword"] = keyword
7	symbolTable["dataType"] = dataType
8	symbolTable["preDefRoutine"] = preDefRoutine
9lb = 0
10fp = 1
11def validLexeme(string):
12	
13	res = False
14	if(string in keyword):
15		#print("key " + string + "\n")
16		res = "keyword"
17	elif(string in dataType):
18		#print("dataType " + string + "\n")
19		res = "dataType"
20	elif(string in preDefRoutine):
21		res = "preDefRoutine"
22	elif(re.match(identifier, string)):
23		#print("id " + string + "\n")
24		res = "identifier"
25	elif(re.match(punctuator, string)):
26		#print("punc " + string)
27		res = "punctuator"
28	elif(re.match(number, string)):
29		res = "number"
30	elif(re.match(aritmeticOperator, string)):
31		res = "arithmeticOperator"
32	elif(re.match(assignmentOperator, string)):
33		res = "assignmentOperator"
34	elif(string in relationalOperator):
35		res = "relationalOperator"
36	elif(string in logicalOperator):
37		res = "logicalOperator"
38	elif(string == "#"):
39		res = "hashOperator"
40	elif(string == ".h"):
41		res = "headerExtension"
42	elif(string == "true" or string == "false"):
43		res = "boolean"
44	elif(string == "++"):
45		res = "incrementOperator"
46	elif(string == "--"):
47		res = "decrementOperator"
48	return res
49
50top = 0;
51i_ = 1;
52tmp = "";
53li = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
54def push(val):
55	global top,li
56	top = top+1
57	li[top]=val;
58	
59
60
61def codegen():
62	global tmp,i_,top,li
63	tmp = "t"
64	tmp+=str(i_)
65	print(tmp +" = "+str(li[top-2]), str(li[top-1]), str(li[top]));
66	top-=2;
67	li[top]=tmp
68	i_=i_+1;
69
70
71def codegen_umin():
72	global tmp,i_,top,li
73	tmp = "t"
74	tmp+=str(i_)
75	print(tmp+" = -"+str(li[top]));
76	top=top-1;
77	li[top]=tmp;
78	i_=i_+1;
79
80def codegen_assign():
81	global tmp,i_,top,li
82	print(str(li[top-1])+" = "+str(li[top]));
83	top=top-2;
84label = 1
85
86def lab1():
87	global label
88	print("L"+str(label)+":")
89	label = label+1
90	
91def lab2():
92	global tmp,i_,top,li,label
93	tmp = "t"
94	tmp+=str(i_)
95	print(tmp+" =  "+li[top-2],li[top-1],li[top]);
96	print("if "+tmp+" goto L"+str(label-1));
97	i_=i_+1;
98	label = label-1;
99	top = top-3;
100
101
102def lexer():
103	global lb
104	global fp
105	
106	lexeme = prg[lb:fp]
107	
108	while(re.match(spaces, lexeme)):
109		#print("x " + lexeme + "\n")
110		lb = lb + 1
111		fp = fp + 1
112		lexeme = prg[lb:fp]
113	
114	#if(re.match(spaces, prg[
115	#print("lexeme: " + lexeme + " type: " + str(type(lexeme)) + "\n");
116	res = validLexeme(lexeme)
117	while((not res) and (fp <= len(prg))):
118		#print("lexeme1: " + lexeme + "\n")
119		fp = fp + 1
120		lexeme = prg[lb:fp]
121		res = validLexeme(lexeme)
122	
123	#print(lexeme + "\n")
124	tokenType = res
125	res = validLexeme(lexeme)
126	while((res) and (fp <= len(prg))):
127		#print("lexeme2: " + lexeme + "\n")
128		fp = fp + 1
129		lexeme = prg[lb:fp]
130		tokenType = res
131		res = validLexeme(lexeme)
132	
133	lexeme = prg[lb:fp - 1]
134	lb = fp - 1
135	
136	if((tokenType != False) and (tokenType not in symbolTable)):
137		symbolTable[tokenType] = list()
138		
139	if((tokenType != False) and lexeme not in symbolTable[tokenType]):
140		symbolTable[tokenType].append(lexeme.strip())
141	
142	#print("TOKEN: " + str(lexeme) + " TYPE: " + str(tokenType) + "\n");
143	#print(str(lb) + " " + str(fp) + "\n")
144	#print(str(len(prg)))
145	return dict({tokenType:lexeme})
146
147def parse_start():
148	status = program()
149	
150	print("SUCCESSFUL PARSING\n") if(status == 0) else print("FAILED PARSING\n")
151	
152def program():
153
154	status = preProcessorDirective()
155	
156	if(status == 0):
157		status = externDeclaration()
158		
159		if(status == 0):
160			status = mainFunction()
161	
162	return status
163
164def preProcessorDirective():
165
166	status = 0
167	token = lexer()
168	
169	token_type = list(token.keys())[0]
170	token_value = list(token.values())[0]
171	
172	if(token_type == "hashOperator"):
173		
174		token = lexer()
175		token_type = list(token.keys())[0]
176		token_value = list(token.values())[0]
177		
178		if(token_type == "keyword" and token_value == "include"):
179				
180			token = lexer()
181			token_type = list(token.keys())[0]
182			token_value = list(token.values())[0]
183			
184			if(token_type == "relationalOperator" and token_value == "<"):
185				
186				token = lexer()
187				token_type = list(token.keys())[0]
188				token_value = list(token.values())[0]
189				
190				if(token_type == "identifier"):
191					
192					token = lexer()
193					token_type = list(token.keys())[0]
194					token_value = list(token.values())[0]
195					
196					
197					if(token_type == "headerExtension"):
198					
199						token = lexer()
200						token_type = list(token.keys())[0]
201						token_value = list(token.values())[0]	
202					
203						if(token_type == "relationalOperator" and token_value == ">"):
204					
205								status = preProcessorDirective()
206								#print(str(status) + " after return\n")
207							
208						else:
209							print("Syntax error: expected '>' but received " + str(token_value) + "\n")
210							status = 1
211					else:
212						print("Syntax error: expected 'Header Extension' but received " + str(token_value) + "\n")
213						status = 1
214						
215				else:
216					print("Syntax error: expected 'Identifer' but received " + str(token_value) + "\n")
217					status = 1
218			else:	
219				print("Syntax error: expected '<' but received " + str(token_value) + "\n")
220				status = 1
221				
222		elif(token_type == "keyword" and token_value == "define"):
223			
224			
225			token = lexer()
226			token_type = list(token.keys())[0]
227			token_value = list(token.values())[0]
228			
229			if(token_type == "identifier"):
230				
231				variableName = token_value
232				token = lexer()
233				token_type = list(token.keys())[0]
234				token_value = list(token.values())[0]
235				
236				if(token_type == "number"):
237					
238					variableValue = int(token_value.strip())
239					symbolTable[variableName] = variableValue
240					status = preProcessorDirective()
241					
242					
243				else:
244					print("Syntax error: expected 'Number' but received " + str(token_value) + "\n")
245					status = 1
246			else:
247				print("Syntax error: expected 'Identifier' but received " + str(token_value) + "\n")
248				status = 1
249					
250		else:
251			print("Syntax error: expected 'Keyword include/define' but received " + str(token_value) + "\n")
252			status = 1
253	else:
254		#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
255		global lb, fp
256		lb = lb - len(token_value)
257		fp = fp - len(token_value)
258		
259	return status
260	#print("Token key: " + str((token_type) + " values: " + str(token_value) + "\n"))	
261
262def externDeclaration():
263	
264	
265	status = 0
266	token = lexer()
267	token_type = list(token.keys())[0]
268	token_value = list(token.values())[0]
269
270	if(token_type == "keyword" and token_value == "extern"):
271
272		status = declarationStatement()
273		if(status == 0):
274		
275			token = lexer()
276			token_type = list(token.keys())[0]
277			token_value = list(token.values())[0].strip()
278
279			if(not (token_type == "punctuator" and token_value == ";")):
280				print("Syntax error: expected 'Punctuator Semicolon1' but received " + str(token_value) + "\n")
281				status = 1
282	else:
283		#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
284		global lb, fp
285		lb = lb - len(token_value)
286		fp = fp - len(token_value)	
287	return status
288
289def declarationStatement():
290	
291	status = 0
292	token = lexer()
293	token_type = list(token.keys())[0]
294	token_value = list(token.values())[0]
295
296	if(token_type == 'dataType'):
297		
298		dataType = token_value.strip()
299		status = variable(dataType)
300		
301	else:
302		print("Syntax error: expected 'Data Type' but received " + str(token_value) + "\n")
303		status = 1
304	
305	return status
306	
307def optionalDeclarationStatement():
308	
309	#print("IN OPTDECL")
310	status = 0
311	token = lexer()
312	token_type = list(token.keys())[0]
313	token_value = list(token.values())[0]
314	#print("before reset: " + str(token_value))
315
316	if(token_type == 'dataType'):
317	
318		
319		dataType = token_value.strip()
320		status = variable(dataType)
321		
322	else:
323	
324		#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
325		#print("resetting")
326		global lb, fp
327		lb = lb - len(token_value)
328		fp = fp - len(token_value)
329		status = 2
330		"""
331		if(token_value != "do"):
332			token = lexer()
333			token_type = list(token.keys())[0]
334			token_value = list(token.values())[0]
335		"""
336		#print("after reset: " + str(token_value))
337	return status
338	
339	
340def variable(dataType):
341
342	status = 0
343	token = lexer()
344	token_type = list(token.keys())[0]
345	token_value = list(token.values())[0]
346	
347	if(token_type == 'identifier'):
348		
349		#print("received identifier, " + str(token_value))
350		variableName = token_value.strip()
351		
352		if(dataType not in externalVariables):
353			externalVariables[dataType] = list()
354		
355		if(variableName not in externalVariables[dataType]):
356			externalVariables[dataType].append(variableName)
357		else:
358			print("Syntax error: The variable "+str(token_value)+" of type "+token_type+" has already been initiliazed.\n")
359			status = 1
360		#externalVariables.append([variableName, dataType])
361		if(status==0):
362			status = variableDash(dataType)
363	else:
364		print("Syntax error: expected 'Identifier' but received " + str(token_value) + "\n")
365		status = 1
366	
367	return status
368
369def variableDash(dataType):
370
371	status = 0
372	token = lexer()
373	token_type = list(token.keys())[0]
374	token_value = list(token.values())[0]
375	
376	if(token_type == 'punctuator' and token_value == ','):
377		
378		token = lexer()
379		token_type = list(token.keys())[0]
380		token_value = list(token.values())[0]
381	
382		if(token_type == 'identifier'):
383			
384			variableName = token_value.strip()
385			if(dataType not in externalVariables):
386				externalVariables[dataType] = list() 
387		
388			if(variableName not in externalVariables[dataType]):
389				externalVariables[dataType].append(variableName)
390			else:
391				print("Syntax error: The variable "+str(token_value)+" of type "+token_type+" has already been initiliazed.\n")
392				status = 1
393			if(status==0):
394				variableDash(dataType)
395		
396		else:
397			print("Syntax error: expected 'Identifier' but received " + str(token_value) + "\n")
398			status = 1
399	else:
400		#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
401		global lb, fp
402		#print(token_value)
403		#print(str(lb) + " " + str(fp))
404		lb = lb - len(token_value)
405		fp = fp - len(token_value)
406		#print(str(lb) + " " + str(fp))
407
408	return status
409	
410def mainFunction():
411	status = 0
412	token = lexer()
413	token_type = list(token.keys())[0]
414	token_value = list(token.values())[0]
415	
416	if(token_type == "dataType" and token_value == "int"):
417		
418		status = mainDash()
419		
420	else:
421		print("Syntax error: expected 'Return Type Integer' but received " + str(token_value) + "\n")
422		status = 1
423	
424	return status
425	
426	
427def mainDash():
428
429	status = 0
430	token = lexer()
431	token_type = list(token.keys())[0]
432	token_value = list(token.values())[0].strip()
433	
434	#print(str(token_type) + " " + str(token_value))
435	
436	if(token_type == "identifier" and token_value == "main"):
437	
438		token = lexer()
439		token_type = list(token.keys())[0]
440		token_value = list(token.values())[0].strip()
441		
442		if(token_type == "punctuator" and token_value == "("):
443		
444			token = lexer()
445			token_type = list(token.keys())[0]
446			token_value = list(token.values())[0].strip()
447			
448			if(token_type == "punctuator" and token_value == ")"):
449			
450				token = lexer()
451				token_type = list(token.keys())[0]
452				token_value = list(token.values())[0].strip()
453				
454				if(token_type == "punctuator" and token_value == "{"):
455				
456					status = statements()
457					
458					if(status == 0):
459						
460						token = lexer()
461						token_type = list(token.keys())[0]
462						token_value = list(token.values())[0].strip()
463						#print(token_value + str(len(token_value)))
464						if(not(token_type == "punctuator" and token_value == "}")):
465							print("Syntax error: expected 'Punctuator1 close curly bracket' but received " + str(token_value) + "\n")
466							status = 1
467				else:
468					print("Syntax error: expected 'Punctuator open curly bracket' but received " + str(token_value) + "\n")
469					status = 1
470						
471				
472			
473			elif(token_type == "dataType" and token_value == "int"):
474			
475				token = lexer()
476				token_type = list(token.keys())[0]
477				token_value = list(token.values())[0].strip()
478				
479				if(token_type == "identifier" and token_value == "argc"):
480				
481					token = lexer()
482					token_type = list(token.keys())[0].strip()
483					token_value = list(token.values())[0].strip()
484					
485					if(token_type == "punctuator" and token_value == ","):
486				
487						token = lexer()
488						token_type = list(token.keys())[0]
489						token_value = list(token.values())[0].strip()
490						
491						if(token_type == "dataType" and token_value == "char"):
492				
493							token = lexer()
494							token_type = list(token.keys())[0]
495							token_value = list(token.values())[0].strip()
496							
497							if(token_type == "arithmeticOperator" and token_value == "*"):
498				
499								token = lexer()
500								token_type = list(token.keys())[0]
501								token_value = list(token.values())[0]	.strip()
502								
503								if(token_type == "identifier" and token_value == "argv"):
504				
505									token = lexer()
506									token_type = list(token.keys())[0]
507									token_value = list(token.values())[0].strip()
508									
509									if(token_type == "punctuator" and token_value == "["):
510				
511										token = lexer()
512										token_type = list(token.keys())[0]
513										token_value = list(token.values())[0].strip()
514										
515										if(token_type == "punctuator" and token_value == "]"):
516				
517											token = lexer()
518											token_type = list(token.keys())[0]
519											token_value = list(token.values())[0].strip()
520											
521											if(token_type == "punctuator" and token_value == ")"):
522				
523												token = lexer()
524												token_type = list(token.keys())[0]
525												token_value = list(token.values())[0].strip()
526											
527												if(token_type == "punctuator" and token_value == "{"):
528				
529													status = statements()
530					
531													if(status == 0):
532						
533														token = lexer()
534														token_type = list(token.keys())[0]
535														token_value = list(token.values())[0].strip()
536				
537														if(not(token_type == "punctuator" and token_value == "}")):
538															print("Syntax error: expected 'Punctuator2 close curly bracket' ", end = "")
539															print("but received " + str(token_value) + "\n")
540															status = 1
541												else:
542													print("Syntax error: expected 'Punctuator open curly bracket'  ", end = "")
543													print("but received " + str(token_value) + "\n")
544													status = 1
545											
546											else:
547												print("Syntax error: expected 'Punctuator close round bracket' but received ", end = "")
548												print(str(token_value) + "\n")
549												status = 1
550											
551										else:
552											print("Syntax error: expected 'Punctuator close square bracket' but received ", end = "")
553											print(str(token_value) + "\n")
554											status = 1
555									else:
556										print("Syntax error: expected 'Punctuator open square bracket' but received ", end = "")
557										print(str(token_value) + "\n")
558										status = 1
559									
560								else:
561									print("Syntax error: expected 'Identifier argv' but received " + str(token_value) + "\n")
562									status = 1
563									
564							else:
565								print("Syntax error: expected 'Pointer operator *' but received " + str(token_value) + "\n")
566								status = 1
567							
568						else:
569							print("Syntax error: expected 'Data type character' but received " + str(token_value) + "\n")
570							status = 1
571						
572					else:
573						print("Syntax error: expected 'Punctuator comma' but received " + str(token_value) + "\n")
574						status = 1	
575				
576				else:
577					print("Syntax error: expected 'Identifier argc' but received " + str(token_value) + "\n")
578					status = 1
579				
580			
581			else:
582				print("Syntax error: expected 'Punctuator close round bracket' but received " + str(token_value) + "\n")
583				status = 1
584				
585		else:
586			print("Syntax error: expected 'Punctuator open round bracket' but received " + str(token_value) + "\n")
587			status = 1
588	
589	else:
590		print("Syntax error: expected 'Identifier main' but received " + str(token_value) + "\n")
591		status = 1
592		
593	return status
594data = {}
595def statements():
596	
597	
598	#print("top of statements\n")
599	status = 0
600	status = initializationStatement()
601	
602	if(status == 0):
603		#print("init success")
604		token = lexer()
605		token_type = list(token.keys())[0]
606		token_value = list(token.values())[0]
607		#print(token_value +" new value")
608		tv = token_value.strip()
609		if(token_type == "punctuator" and tv == ";"):
610
611			status = statements()
612		else:
613			print("Syntax error: expected 'Punctuator semicolon2' but received " + str(token_value) + "\n")
614			status = 1
615			
616			
617	else:
618		'''	token = lexer()
619		token_type = list(token.keys())[0]
620		token_value = list(token.values())[0]
621		tv = token_value.strip()'''
622		#print("dc" + " " + tv)
623		
624		
625		status = optionalDeclarationStatement()
626		#print(status)
627		if(status == 0):	
628			#print("decl success")
629			
630			token = lexer()
631			token_type = list(token.keys())[0]
632			token_value = list(token.values())[0]
633			tv = token_value.strip()
634			if(token_type == "punctuator" and tv == ";"):
635				
636				status = statements()
637			else:
638				print("Syntax error: expected 'Punctuator semicolon3' but received " + str(token_value) + "\n")
639				status = 1
640		else:
641			
642			status = assignmentStatement()
643			if(status == 0):
644				#print("assgn success")
645				
646				token = lexer()
647				token_type = list(token.keys())[0]
648				token_value = list(token.values())[0]
649				tv = token_value.strip()
650				if(token_type == "punctuator" and tv == ";"):
651					status = statements()
652				else:
653					print("Syntax error: expected 'Punctuator semicolon4' but received " + str(token_value) + "\n")
654					status = 1
655			else:
656				
657				status = 0
658				token = lexer()
659				token_type = list(token.keys())[0]
660				token_value = list(token.values())[0]
661				#print("IN statements: " + token_value)
662				if(token_type == "keyword" and token_value == "do"):
663					#print("Do")
664					token = lexer()
665					token_type = list(token.keys())[0]
666					token_value = list(token.values())[0].strip()
667					lab1()
668					if(token_type == "punctuator" and token_value == "{"):
669						#print("{")
670						status = statements()
671					
672
673						#print("status: " + str(status))
674						if(status == 0):
675					
676							token = lexer()
677							token_type = list(token.keys())[0]
678							token_value = list(token.values())[0].strip()
679							#print(token_value)
680							if(token_type == "punctuator" and token_value == "}"):
681								#print("}")
682								token = lexer()
683								token_type = list(token.keys())[0]
684								token_value = list(token.values())[0].strip()
685		
686								if(token_type == "keyword" and token_value == "while"):
687									#print("while")
688									token = lexer()
689									token_type = list(token.keys())[0]
690									token_value = list(token.values())[0].strip()
691		
692									if(token_type == "punctuator" and token_value == "("):
693										#print("(")
694										status = condition()
695										lab2()
696										if(status == 0):
697					
698											token = lexer()
699											token_type = list(token.keys())[0]
700											token_value = list(token.values())[0].strip()
701		
702											if(token_type == "punctuator" and token_value == ")"):
703												#print(")")
704												token = lexer()
705												token_type = list(token.keys())[0]
706												token_value = list(token.values())[0].strip()
707		
708												if(token_type == "punctuator" and token_value == ";"):
709													#print("in statements: " + token_value + "\n")
710													status = statements()
711					
712												else:
713													print("Syntax error: expected 'Punctuator semicolon5' ", end = "")
714													print("but received " + str(token_value) + "\n")
715													status = 1
716					
717											else:
718												print("Syntax error: expected 'Punctuator close round bracket' ", end = "")
719												print("but received " + str(token_value) + "\n")
720												status = 1
721					
722									else:
723										print("Syntax error: expected 'Punctuator open round bracket' ", end = "") 
724										print("but received " + str(token_value) + "\n")
725										status = 1
726					
727								else:
728									print("Syntax error: expected 'Keyword while' but received " + str(token_value) + "\n")
729									status = 1
730					
731							else:
732								print("Syntax error: expected 'Punctuator10 close curly bracket' but received " + str(token_value) + "\n")
733								status = 1
734
735					elif(token_type == "identifier" or token_type == "datatype"):
736						global lb, fp
737						#print(token_value)
738						#print(str(lb) + " " + str(fp))
739						lb = lb - len(token_value)
740						fp = fp - len(token_value)
741						status = statement1()
742
743				
744					else:
745						print("Syntax error: expected 'Punctuator open curly bracket' but received " + str(token_value) + "\n")
746						status = 1
747		
748				
749				else:
750					status = 0
751					tv = token_value.strip()
752					#print("IN statements: " + token_value)
753					if(tv == "{"):
754							status = statements()
755							
756							#print("status: " + str(status))
757							if(status == 0):
758						
759								token = lexer()
760								token_type = list(token.keys())[0]
761								token_value = list(token.values())[0].strip()
762								#print(token_value)
763								if(token_type == "punctuator" and token_value == "}"):
764									status = statements()
765								else:
766									print("Error")
767					else:
768			
769						#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
770						#global lb, fp
771						#print(token_value)
772						#print(str(lb) + " " + str(fp))
773						lb = lb - len(token_value)
774						fp = fp - len(token_value)
775									
776	
777	return status
778
779def statement1():
780	status = 0
781	status = initializationStatement()
782	
783	if(status == 0):
784		#print("init success")
785		token = lexer()
786		token_type = list(token.keys())[0]
787		token_value = list(token.values())[0]
788		#print(token_value +" new value")
789		tv = token_value.strip()
790		if(token_type == "punctuator" and tv == ";"):
791			status = 0
792		else:
793			status = 1
794			print("Error")
795	else:
796			
797			status = assignmentStatement()
798			if(status == 0):
799				#print("assgn success")
800				
801				token = lexer()
802				token_type = list(token.keys())[0]
803				token_value = list(token.values())[0]
804				tv = token_value.strip()
805				if(token_type == "punctuator" and tv == ";"):
806					status = 0
807				else:
808					status = 1
809					print("Error")
810	if(status ==0):
811		token = lexer()
812		token_type = list(token.keys())[0]
813		token_value = list(token.values())[0].strip()
814		
815		if(token_type == "keyword" and token_value == "while"):
816				#print("while")
817			token = lexer()
818			token_type = list(token.keys())[0]
819			token_value = list(token.values())[0].strip()
820
821			if(token_type == "punctuator" and token_value == "("):
822				#print("(")
823				status = condition()
824				lab2()
825				if(status == 0):
826
827					token = lexer()
828					token_type = list(token.keys())[0]
829					token_value = list(token.values())[0].strip()
830
831					if(token_type == "punctuator" and token_value == ")"):
832						#print(")")
833						token = lexer()
834						token_type = list(token.keys())[0]
835						token_value = list(token.values())[0].strip()
836
837						if(token_type == "punctuator" and token_value == ";"):
838							#print("in statements: " + token_value + "\n")
839							status = statements()
840
841						else:
842							print("Syntax error: expected 'Punctuator semicolon5' ", end = "")
843							print("but received " + str(token_value) + "\n")
844							status = 1
845
846					else:
847						print("Syntax error: expected 'Punctuator close round bracket' ", end = "")
848						print("but received " + str(token_value) + "\n")
849						status = 1
850
851			else:
852				print("Syntax error: expected 'Punctuator open round bracket' ", end = "") 
853				print("but received " + str(token_value) + "\n")
854				status = 1
855
856		else:
857			print("Syntax error: expected 'Keyword while' but received " + str(token_value) + "\n")
858			status = 1
859
860	else:
861		print("Syntax error: expected 'Punctuator10 close curly bracket' but received " + str(token_value) + "\n")
862		status = 1
863	return status
864
865
866def initializationStatement():
867
868	status = 0
869	
870	global lb, fp
871		
872	token = lexer()
873	token_type = list(token.keys())[0]
874	token_value = list(token.values())[0]
875
876	if(token_type == "dataType"):
877		if(token_value not in data):
878			data[token_value] = {};
879			#print(token_value)
880		
881		status = initStat(token_value)
882		
883		
884	else:
885		
886		#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
887		#print(token_value)
888		#print(str(lb) + " " + str(fp))
889		lb = lb - len(token_value)
890		fp = fp - len(token_value)
891		status = 2
892	#print('returning' + str(status))	
893	return status
894	
895	
896def initStat(dt):
897
898
899	status = multipleInitialization(dt)
900	#print(status)
901	
902	
903	
904	'''if(status != 0 and status != 2):
905
906		status = 0
907		token = lexer()
908		token_type = list(token.keys())[0]
909		token_value = list(token.values())[0]
910		tk = token_value
911		if(token_type == "identifier"):
912		
913			if(token_value not in data[dt]):
914				data[dt][token_value]=0
915			else:
916				print("Syntax Error: The variable has already been initialized\n")
917				return 1
918			token = lexer()
919			token_type = list(token.keys())[0]
920			token_value = list(token.values())[0]
921	
922			if(token_type == "assignmentOperator" and token_value == "="):
923				
924				status = E(dt,tk)
925				"""
926				print(status)
927				status = 0
928				token = lexer()
929				token_type = list(token.keys())[0]
930				token_value = list(token.values())[0]
931				print(token_value)
932				"""
933			
934			elif(token_type == "punctuator" and token_value == ","):
935			
936				global lb, fp
937				#print(token_value)
938				#print(str(lb) + " " + str(fp))
939				lb = lb - len(token_value)
940				fp = fp - len(token_value)
941				status = 2
942			
943			else:
944				
945				print("Syntax error: expected 'Assignment1 Operator' but received " + str(token_value) + "\n")
946				status = 1 '''
947		
948	
949	return status
950		
951def multipleInitialization(dt):
952	global data
953	status = 0
954	token = lexer()
955	token_type = list(token.keys())[0]
956	token_value = list(token.values())[0]
957	tk = token_value
958	if(token_type == "identifier"):
959		push(tk)
960		#print(tk)
961		if(token_value not in data[dt]):
962				if(dt=="int"):
963					data[dt][token_value]=int(0)
964				elif(dt=="char"):
965					data[dt][token_value]=string(0)
966				elif(dt=="float"):
967					data[dt][token_value]=float(0)
968				elif(dt=="double"):
969					data[dt][token_value]=float(0)
970				else:
971					data[dt][token_value]=0
972				#print(" "+token_value +":)")
973		else:
974				print("Syntax Error: The variable has already been initialized\n")
975				return 1
976		
977		token = lexer()
978		token_type = list(token.keys())[0]
979		token_value = list(token.values())[0]
980		tv = token_value.strip()
981
982		#print(token_value+" macha")
983		if(tv == ";"):
984			#print("; la")
985			global lb, fp
986			#print(token_value)
987			#print(str(lb) + " " + str(fp))
988			lb = lb - len(token_value)
989			fp = fp - len(token_value)
990			return 0;
991		elif(token_type == "assignmentOperator" and tv == "="):
992				
993			status = E(dt,tk)
994			codegen_assign()
995			#print(status)
996			
997			if(status == 0):
998				
999				status = multinit(dt)
1000				if(status == 2):
1001					status = 0
1002				#print(status)
1003		elif(token_type == "punctuator" and tv == ","):
1004			#print(",")
1005			status = multipleInitialization(dt)
1006			'''global lb, fp
1007			#print(token_value)
1008			#print(str(lb) + " " + str(fp))
1009			lb = lb - len(token_value)
1010			fp = fp - len(token_value)
1011			status = 2 '''
1012			
1013		else:
1014				
1015			print("Syntax error: expected 'Assignment2 Operator' but received " + str(tv) + "\n")
1016			status = 1
1017	else:
1018			
1019		print("Syntax error: expected 'Identifier' but received " + str(tv) + "\n")
1020		status = 1
1021	
1022	return status
1023	
1024def multinit(dt):
1025
1026	status = 0
1027	
1028	token = lexer()
1029	token_type = list(token.keys())[0]
1030	token_value = list(token.values())[0]
1031	tv = token_value.strip()
1032	
1033	if(token_type == "punctuator" and tv == ","):
1034	
1035		#print("got comma")
1036		status = multipleInitialization(dt)
1037	
1038	else:
1039		
1040		#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
1041		global lb, fp
1042		#print(token_value)
1043		#print(str(lb) + " " + str(fp))
1044		lb = lb - len(token_value)
1045		fp = fp - len(token_value)
1046		status = 2
1047		
1048	return status
1049
1050def assignmentStatement():
1051	global data
1052	dty =''
1053	status = 0
1054	token = lexer()
1055	token_type = list(token.keys())[0]
1056	token_value = list(token.values())[0]
1057	tk = token_value
1058	#print("asgn")
1059	if(token_type == "identifier"):
1060		push(tk)
1061		#print(tk)
1062		for i in data:
1063			for j in data[i]:
1064				if(j==token_value):
1065					dty = i
1066		if(dty==''):
1067			print("The variable "+token_value+" has not been initialized.")
1068			return 1
1069		token = lexer()
1070		token_type = list(token.keys())[0]
1071		token_value = list(token.values())[0]
1072	
1073		if(token_type == "assignmentOperator" and token_value == "="):
1074				
1075			status = E(dty,tk)
1076			codegen_assign()
1077			
1078		else:
1079			
1080			print("Syntax error: expected 'Assignment3 Operator' but received " + str(token_value) + "\n")
1081			status = 1
1082	else:
1083			
1084		#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
1085		global lb, fp
1086		#print(token_value)
1087		#print(str(lb) + " " + str(fp))
1088		lb = lb - len(token_value)
1089		fp = fp - len(token_value)
1090		status = 2
1091	
1092	return status
1093
1094def condition():
1095
1096	status = 0
1097	
1098	status = C()
1099			
1100	return status
1101
1102def C():
1103	status = 0
1104	token = lexer()
1105	token_type = list(token.keys())[0]
1106	token_value = list(token.values())[0]
1107	tv = token_value.strip()
1108	if(token_type == "identifier" or token_type=="number"):
1109		push(tv)
1110		token = lexer()
1111		token_type = list(token.keys())[0]
1112		token_value = list(token.values())[0]
1113		tk = token_value.strip()
1114		if(token_type == "relationalOperator" or token_type == "logicalOperator"):
1115			push(tk)
1116			status = C() 
1117		elif(token_value == ")"):
1118			global lb, fp
1119			#print(token_value)
1120			#print(str(lb) + " " + str(fp))
1121			lb = lb - len(token_value)
1122			fp = fp - len(token_value)
1123			return 0
1124		else:
1125			return 1
1126	elif(not (token_type == "boolean")):
1127		
1128			print("Syntax error: expected 'Boolean' but received " + str(token_value) + "\n")
1129			status = 1
1130	return status
1131op = ""
1132def E(dt,vn):
1133
1134	status = F(dt,vn)
1135
1136	if(status == 0):
1137	
1138		status = E1(dt,vn)
1139	
1140	return status
1141	
1142def E1(dt,vn):
1143
1144	status = 0
1145
1146	token = lexer()
1147	token_type = list(token.keys())[0]
1148	token_value = list(token.values())[0]
1149	tv = token_value.strip()
1150	global op;
1151	if(token_type == "arithmeticOperator" and tv == "+"):
1152		op = "+"
1153		push(tv)
1154		#print(tv)
1155		status = F(dt,vn)
1156		codegen()
1157		if(status == 0):
1158		
1159			status = E1(dt,vn)
1160			
1161	else:
1162	
1163		#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
1164		global lb, fp
1165		#print(token_value)
1166		#print(str(lb) + " " + str(fp))
1167		lb = lb - len(token_value)
1168		fp = fp - len(token_value)
1169
1170	return status
1171
1172
1173def F(dt,vn):
1174
1175	status = 0
1176	
1177	status = G(dt,vn)
1178	
1179	if(status == 0):
1180	
1181		status = F1(dt,vn)
1182
1183	return status
1184	
1185def F1(dt,vn):
1186
1187	status = 0
1188
1189	token = lexer()
1190	token_type = list(token.keys())[0]
1191	token_value = list(token.values())[0]
1192	tv = token_value.strip()
1193	global op;
1194	if(token_type == "arithmeticOperator" and tv == "-"):
1195		op = "-"
1196		push(tv)
1197		#print(tv)
1198		status = G(dt,vn)
1199		codegen()
1200		
1201		if(status == 0):
1202		
1203			status = F1(dt,vn)
1204			
1205	else:
1206	
1207		#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
1208		global lb, fp
1209		#print(token_value)
1210		#print(str(lb) + " " + str(fp))
1211		lb = lb - len(token_value)
1212		fp = fp - len(token_value)
1213
1214	return status
1215	
1216def G(dt,vn):
1217
1218	status = 0
1219	
1220	status = H(dt,vn)
1221
1222	if(status == 0):
1223	
1224		status = G1(dt,vn)
1225
1226	return status
1227
1228def G1(dt,vn):
1229
1230	status = 0
1231	
1232	token = lexer()
1233	token_type = list(token.keys())[0]
1234	token_value = list(token.values())[0]
1235	tv = token_value.strip()
1236	global op;
1237	if(token_type == "arithmeticOperator" and tv == "*"):
1238		push(tv)
1239		#print(tv)
1240		op = "*"
1241		status = H(dt,vn)
1242		codegen()
1243		if(status == 0):
1244		
1245			status = G1(dt,vn)
1246			
1247	else:
1248	
1249		#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
1250		global lb, fp
1251		#print(token_value)
1252		#print(str(lb) + " " + str(fp))
1253		lb = lb - len(token_value)
1254		fp = fp - len(token_value)
1255
1256
1257	return status
1258	
1259def H(dt,vn):
1260
1261	status = 0
1262	
1263	status = I(dt,vn)
1264	
1265	if(status == 0):
1266	
1267		status = H1(dt,vn)
1268
1269	return status
1270	
1271def H1(dt,vn):
1272
1273	status = 0
1274	
1275	token = lexer()
1276	token_type = list(token.keys())[0]
1277	token_value = list(token.values())[0]
1278	tv = token_value.strip()
1279	
1280	if(token_type == "arithmeticOperator" and tv == "/"):
1281		global op;
1282		op = "d";
1283		push(tv)
1284		#print(tv)
1285		status = I(dt,vn)
1286		codegen()
1287		if(status == 0):
1288		
1289			status = H1(dt,vn)
1290			
1291	else:
1292	
1293		#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
1294		global lb, fp
1295		#print(token_value + ":::" + str(len(token_value)))
1296		#print(str(lb) + " " + str(fp))
1297		
1298		lb = lb - len(token_value)
1299		fp = fp - len(token_value)
1300
1301	return status
1302	
1303def I(dt,vn):
1304	global data
1305	status = 0
1306	chk = 0
1307	token = lexer()
1308	token_type = list(token.keys())[0]
1309	token_value = list(token.values())[0]
1310	tv = token_value.strip()
1311	
1312	if(token_type == "arithmeticOperator" and tv == "-"):
1313		chk = 1
1314		push(tv)
1315		#print(tv)
1316		status = I()
1317		codegen_umin()
1318	elif(not(token_type == "identifier" or token_type == "number")):
1319		print("Syntax error: expected 'Identifier/Number' but received " + str(token_value) + "\n")
1320		status = 1
1321		return status
1322	if(token_type == "identifier" or token_type == "number"):
1323		push(tv)
1324		#print(tv)
1325	global op;
1326	g = True
1327	if(token_value == "identifier"):
1328		if(token_value not in data[dt]):
1329			print("Syntax error: The variable "+token_value+" not in "+dt)
1330			g = False
1331	elif(token_value == "number"):
1332		if(not isinstance(token_value,dt)):
1333			print("Syntax error: The variable belongs to a different type")
1334			False
1335	if(op=="" and g == True):
1336		if(token_type == "identifier"):
1337
1338			if(chk==1):
1339				data[dt][vn]=-1*data[dt][token_value]
1340				chk = 0
1341			else:
1342				#print(token_value)
1343				data[dt][vn]=data[dt][token_value]
1344			
1345		if(token_type == "number"):
1346			if(chk==1):
1347				data[dt][vn]=-1*float(token_value)
1348				chk = 0
1349			else:
1350				data[dt][vn]=float(token_value)
1351	elif(op=="d" and g == True):
1352		if(token_type == "identifier"):
1353			if(chk==1):
1354				data[dt][vn]/=-1*data[dt][token_value]
1355				chk = 0
1356				op=""
1357			else:
1358				data[dt][vn]/=data[dt][token_value]
1359				op=""
1360			
1361		if(token_type == "number"):
1362			if(chk==1):
1363				data[dt][vn]/=-1*float(token_value)
1364				chk = 0
1365				op = ""
1366			else:
1367				data[dt][vn]/=float(token_value)
1368				op = ""
1369	elif(op=="*" and g == True):
1370		if(token_type == "identifier"):
1371			if(chk==1):
1372				data[dt][vn]*=-1*data[dt][token_value]
1373				chk = 0
1374				op=""
1375			else:
1376				data[dt][vn]*=data[dt][token_value]
1377				op=""
1378			
1379		if(token_type == "number"):
1380			if(chk==1):
1381				data[dt][vn]*=-1*float(token_value)
1382				chk = 0
1383				op = ""
1384			else:
1385				data[dt][vn]*=float(token_value)
1386				op = ""
1387	elif(op=="-" and g == True):
1388		if(token_type == "identifier"):
1389			if(chk==1):
1390				data[dt][vn]-=-1*data[dt][token_value]
1391				chk = 0
1392				op=""
1393			else:
1394				data[dt][vn]-=data[dt][token_value]
1395				op=""
1396			
1397		if(token_type == "number"):
1398			if(chk==1):
1399				data[dt][vn]-=-1*float(token_value)
1400				chk = 0
1401				op = ""
1402			else:
1403				data[dt][vn]-=float(token_value)
1404				op = ""
1405	elif(op=="+" and g == True):
1406		if(token_type == "identifier"):
1407			if(chk==1):
1408				data[dt][vn]+=-1*data[dt][token_value]
1409				chk = 0
1410				op=""
1411			else:
1412				data[dt][vn]+=data[dt][token_value]
1413				op=""
1414			
1415		if(token_type == "number"):
1416			if(chk==1):
1417				data[dt][vn]+=-1*float(token_value)
1418				chk = 0
1419				op = ""
1420			else:
1421				data[dt][vn]+=float(token_value)
1422				op = ""
1423	return status
1424	
1425	
1426	
1427
1428prg = open("nocomments.c").read()
1429
1430
1431
1432symbolTable = dict()
1433externalVariables = dict()
1434localVariables = list()
1435keyword = ["include", "define", "while", "do", "for", "return", "extern"]
1436dataType = ["void", "int", "short", "long", "char", "float", "double"]
1437preDefRoutine = ["printf", "scanf"]
1438#headerFile = ["stdio.h", "stdlib.h", "math.h", "string.h"]
1439identifier = "^[^\d\W]\w*\Z"
1440punctuator = "^[()[\]{};.,]$"
1441aritmeticOperator = "^[-+*/]$"
1442assignmentOperator = "^=$"
1443relationalOperator = ["<", ">", "<=", ">=", "==", "!="]
1444logicalOperator = ["&&", "||", "!"]
1445number = "^\d+$"
1446spaces = "[' ''\n''\t']"
1447
1448loadSymbolTable()
1449parse_start()
1450'''
1451for i in data:
1452	for j in data[i]:
1453		print(i+" "+j+" "+str(data[i][j]))
1454'''
1455"""
1456while lb!=len(prg):
1457	lexer()
1458"""
1459#print(symbolTable)
1460#print(externalVariables)
1461"""
1462PARSER ERROR CODES:
1463
14640-SUCCESS
14651-FAILURE
1466"""
1467
1468
1469
1470		
1471	
1472
Full Screen

lex.py

Source: lex.py Github

copy
1import re
2
3#import ply.lex as lex
4def loadSymbolTable():
5	
6	symbolTable["keyword"] = keyword
7	symbolTable["dataType"] = dataType
8	symbolTable["preDefRoutine"] = preDefRoutine
9lb = 0
10fp = 1
11def validLexeme(string):
12	
13	res = False
14	if(string in keyword):
15		#print("key " + string + "\n")
16		res = "keyword"
17	elif(string in dataType):
18		#print("dataType " + string + "\n")
19		res = "dataType"
20	elif(string in preDefRoutine):
21		res = "preDefRoutine"
22	elif(re.match(identifier, string)):
23		#print("id " + string + "\n")
24		res = "identifier"
25	elif(re.match(punctuator, string)):
26		#print("punc " + string)
27		res = "punctuator"
28	elif(re.match(number, string)):
29		res = "number"
30	elif(re.match(aritmeticOperator, string)):
31		res = "arithmeticOperator"
32	elif(re.match(assignmentOperator, string)):
33		res = "assignmentOperator"
34	elif(string in relationalOperator):
35		res = "relationalOperator"
36	elif(string in logicalOperator):
37		res = "logicalOperator"
38	elif(string == "#"):
39		res = "hashOperator"
40	elif(string == ".h"):
41		res = "headerExtension"
42	elif(string == "true" or string == "false"):
43		res = "boolean"
44	elif(string == "++"):
45		res = "incrementOperator"
46	elif(string == "--"):
47		res = "decrementOperator"
48	return res
49
50top = 0;
51i_ = 1;
52tmp = "";
53li = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
54def push(val):
55	global top,li
56	top = top+1
57	li[top]=val;
58	
59
60
61def codegen():
62	global tmp,i_,top,li
63	tmp = "t"
64	tmp+=str(i_)
65	print(tmp +" = "+str(li[top-2]), str(li[top-1]), str(li[top]));
66	top-=2;
67	li[top]=tmp
68	i_=i_+1;
69
70
71def codegen_umin():
72	global tmp,i_,top,li
73	tmp = "t"
74	tmp+=str(i_)
75	print(tmp+" = -"+str(li[top]));
76	top=top-1;
77	li[top]=tmp;
78	i_=i_+1;
79
80def codegen_assign():
81	global tmp,i_,top,li
82	print(str(li[top-1])+" = "+str(li[top]));
83	top=top-2;
84label = 1
85
86def lab1():
87	global label
88	print("L"+str(label)+":")
89	label = label+1
90	
91def lab2():
92	global tmp,i_,top,li,label
93	tmp = "t"
94	tmp+=str(i_)
95	print(tmp+" =  "+li[top-2],li[top-1],li[top]);
96	print("if "+tmp+" goto L"+str(label-1));
97	i_=i_+1;
98	label = label-1;
99	top = top-3;
100
101
102def lexer():
103	global lb
104	global fp
105	
106	lexeme = prg[lb:fp]
107	
108	while(re.match(spaces, lexeme)):
109		#print("x " + lexeme + "\n")
110		lb = lb + 1
111		fp = fp + 1
112		lexeme = prg[lb:fp]
113	
114	#if(re.match(spaces, prg[
115	#print("lexeme: " + lexeme + " type: " + str(type(lexeme)) + "\n");
116	res = validLexeme(lexeme)
117	while((not res) and (fp <= len(prg))):
118		#print("lexeme1: " + lexeme + "\n")
119		fp = fp + 1
120		lexeme = prg[lb:fp]
121		res = validLexeme(lexeme)
122	
123	#print(lexeme + "\n")
124	tokenType = res
125	res = validLexeme(lexeme)
126	while((res) and (fp <= len(prg))):
127		#print("lexeme2: " + lexeme + "\n")
128		fp = fp + 1
129		lexeme = prg[lb:fp]
130		tokenType = res
131		res = validLexeme(lexeme)
132	
133	lexeme = prg[lb:fp - 1]
134	lb = fp - 1
135	
136	if((tokenType != False) and (tokenType not in symbolTable)):
137		symbolTable[tokenType] = list()
138		
139	if((tokenType != False) and lexeme not in symbolTable[tokenType]):
140		symbolTable[tokenType].append(lexeme.strip())
141	
142	#print("TOKEN: " + str(lexeme) + " TYPE: " + str(tokenType) + "\n");
143	#print(str(lb) + " " + str(fp) + "\n")
144	#print(str(len(prg)))
145	return dict({tokenType:lexeme})
146
147def parse_start():
148	status = program()
149	
150	print("SUCCESSFUL PARSING\n") if(status == 0) else print("FAILED PARSING\n")
151	
152def program():
153
154	status = preProcessorDirective()
155	
156	if(status == 0):
157		status = externDeclaration()
158		
159		if(status == 0):
160			status = mainFunction()
161	
162	return status
163
164def preProcessorDirective():
165
166	status = 0
167	token = lexer()
168	
169	token_type = list(token.keys())[0]
170	token_value = list(token.values())[0]
171	
172	if(token_type == "hashOperator"):
173		
174		token = lexer()
175		token_type = list(token.keys())[0]
176		token_value = list(token.values())[0]
177		
178		if(token_type == "keyword" and token_value == "include"):
179				
180			token = lexer()
181			token_type = list(token.keys())[0]
182			token_value = list(token.values())[0]
183			
184			if(token_type == "relationalOperator" and token_value == "<"):
185				
186				token = lexer()
187				token_type = list(token.keys())[0]
188				token_value = list(token.values())[0]
189				
190				if(token_type == "identifier"):
191					
192					token = lexer()
193					token_type = list(token.keys())[0]
194					token_value = list(token.values())[0]
195					
196					
197					if(token_type == "headerExtension"):
198					
199						token = lexer()
200						token_type = list(token.keys())[0]
201						token_value = list(token.values())[0]	
202					
203						if(token_type == "relationalOperator" and token_value == ">"):
204					
205								status = preProcessorDirective()
206								#print(str(status) + " after return\n")
207							
208						else:
209							print("Syntax error: expected '>' but received " + str(token_value) + "\n")
210							status = 1
211					else:
212						print("Syntax error: expected 'Header Extension' but received " + str(token_value) + "\n")
213						status = 1
214						
215				else:
216					print("Syntax error: expected 'Identifer' but received " + str(token_value) + "\n")
217					status = 1
218			else:	
219				print("Syntax error: expected '<' but received " + str(token_value) + "\n")
220				status = 1
221				
222		elif(token_type == "keyword" and token_value == "define"):
223			
224			
225			token = lexer()
226			token_type = list(token.keys())[0]
227			token_value = list(token.values())[0]
228			
229			if(token_type == "identifier"):
230				
231				variableName = token_value
232				token = lexer()
233				token_type = list(token.keys())[0]
234				token_value = list(token.values())[0]
235				
236				if(token_type == "number"):
237					
238					variableValue = int(token_value.strip())
239					symbolTable[variableName] = variableValue
240					status = preProcessorDirective()
241					
242					
243				else:
244					print("Syntax error: expected 'Number' but received " + str(token_value) + "\n")
245					status = 1
246			else:
247				print("Syntax error: expected 'Identifier' but received " + str(token_value) + "\n")
248				status = 1
249					
250		else:
251			print("Syntax error: expected 'Keyword include/define' but received " + str(token_value) + "\n")
252			status = 1
253	else:
254		#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
255		global lb, fp
256		lb = lb - len(token_value)
257		fp = fp - len(token_value)
258		
259	return status
260	#print("Token key: " + str((token_type) + " values: " + str(token_value) + "\n"))	
261
262def externDeclaration():
263	
264	
265	status = 0
266	token = lexer()
267	token_type = list(token.keys())[0]
268	token_value = list(token.values())[0]
269
270	if(token_type == "keyword" and token_value == "extern"):
271
272		status = declarationStatement()
273		if(status == 0):
274		
275			token = lexer()
276			token_type = list(token.keys())[0]
277			token_value = list(token.values())[0].strip()
278
279			if(not (token_type == "punctuator" and token_value == ";")):
280				print("Syntax error: expected 'Punctuator Semicolon1' but received " + str(token_value) + "\n")
281				status = 1
282	else:
283		#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
284		global lb, fp
285		lb = lb - len(token_value)
286		fp = fp - len(token_value)	
287	return status
288
289def declarationStatement():
290	
291	status = 0
292	token = lexer()
293	token_type = list(token.keys())[0]
294	token_value = list(token.values())[0]
295
296	if(token_type == 'dataType'):
297		
298		dataType = token_value.strip()
299		status = variable(dataType)
300		
301	else:
302		print("Syntax error: expected 'Data Type' but received " + str(token_value) + "\n")
303		status = 1
304	
305	return status
306	
307def optionalDeclarationStatement():
308	
309	#print("IN OPTDECL")
310	status = 0
311	token = lexer()
312	token_type = list(token.keys())[0]
313	token_value = list(token.values())[0]
314	#print("before reset: " + str(token_value))
315
316	if(token_type == 'dataType'):
317	
318		
319		dataType = token_value.strip()
320		status = variable(dataType)
321		
322	else:
323	
324		#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
325		#print("resetting")
326		global lb, fp
327		lb = lb - len(token_value)
328		fp = fp - len(token_value)
329		status = 2
330		"""
331		if(token_value != "do"):
332			token = lexer()
333			token_type = list(token.keys())[0]
334			token_value = list(token.values())[0]
335		"""
336		#print("after reset: " + str(token_value))
337	return status
338	
339	
340def variable(dataType):
341
342	status = 0
343	token = lexer()
344	token_type = list(token.keys())[0]
345	token_value = list(token.values())[0]
346	
347	if(token_type == 'identifier'):
348		
349		#print("received identifier, " + str(token_value))
350		variableName = token_value.strip()
351		
352		if(dataType not in externalVariables):
353			externalVariables[dataType] = list()
354		
355		if(variableName not in externalVariables[dataType]):
356			externalVariables[dataType].append(variableName)
357		else:
358			print("Syntax error: The variable "+str(token_value)+" of type "+token_type+" has already been initiliazed.\n")
359			status = 1
360		#externalVariables.append([variableName, dataType])
361		if(status==0):
362			status = variableDash(dataType)
363	else:
364		print("Syntax error: expected 'Identifier' but received " + str(token_value) + "\n")
365		status = 1
366	
367	return status
368
369def variableDash(dataType):
370
371	status = 0
372	token = lexer()
373	token_type = list(token.keys())[0]
374	token_value = list(token.values())[0]
375	
376	if(token_type == 'punctuator' and token_value == ','):
377		
378		token = lexer()
379		token_type = list(token.keys())[0]
380		token_value = list(token.values())[0]
381	
382		if(token_type == 'identifier'):
383			
384			variableName = token_value.strip()
385			if(dataType not in externalVariables):
386				externalVariables[dataType] = list() 
387		
388			if(variableName not in externalVariables[dataType]):
389				externalVariables[dataType].append(variableName)
390			else:
391				print("Syntax error: The variable "+str(token_value)+" of type "+token_type+" has already been initiliazed.\n")
392				status = 1
393			if(status==0):
394				variableDash(dataType)
395		
396		else:
397			print("Syntax error: expected 'Identifier' but received " + str(token_value) + "\n")
398			status = 1
399	else:
400		#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
401		global lb, fp
402		#print(token_value)
403		#print(str(lb) + " " + str(fp))
404		lb = lb - len(token_value)
405		fp = fp - len(token_value)
406		#print(str(lb) + " " + str(fp))
407
408	return status
409	
410def mainFunction():
411	status = 0
412	token = lexer()
413	token_type = list(token.keys())[0]
414	token_value = list(token.values())[0]
415	
416	if(token_type == "dataType" and token_value == "int"):
417		
418		status = mainDash()
419		
420	else:
421		print("Syntax error: expected 'Return Type Integer' but received " + str(token_value) + "\n")
422		status = 1
423	
424	return status
425	
426	
427def mainDash():
428
429	status = 0
430	token = lexer()
431	token_type = list(token.keys())[0]
432	token_value = list(token.values())[0].strip()
433	
434	#print(str(token_type) + " " + str(token_value))
435	
436	if(token_type == "identifier" and token_value == "main"):
437	
438		token = lexer()
439		token_type = list(token.keys())[0]
440		token_value = list(token.values())[0].strip()
441		
442		if(token_type == "punctuator" and token_value == "("):
443		
444			token = lexer()
445			token_type = list(token.keys())[0]
446			token_value = list(token.values())[0].strip()
447			
448			if(token_type == "punctuator" and token_value == ")"):
449			
450				token = lexer()
451				token_type = list(token.keys())[0]
452				token_value = list(token.values())[0].strip()
453				
454				if(token_type == "punctuator" and token_value == "{"):
455				
456					status = statements()
457					
458					if(status == 0):
459						
460						token = lexer()
461						token_type = list(token.keys())[0]
462						token_value = list(token.values())[0].strip()
463						#print(token_value + str(len(token_value)))
464						if(not(token_type == "punctuator" and token_value == "}")):
465							print("Syntax error: expected 'Punctuator1 close curly bracket' but received " + str(token_value) + "\n")
466							status = 1
467				else:
468					print("Syntax error: expected 'Punctuator open curly bracket' but received " + str(token_value) + "\n")
469					status = 1
470						
471				
472			
473			elif(token_type == "dataType" and token_value == "int"):
474			
475				token = lexer()
476				token_type = list(token.keys())[0]
477				token_value = list(token.values())[0].strip()
478				
479				if(token_type == "identifier" and token_value == "argc"):
480				
481					token = lexer()
482					token_type = list(token.keys())[0].strip()
483					token_value = list(token.values())[0].strip()
484					
485					if(token_type == "punctuator" and token_value == ","):
486				
487						token = lexer()
488						token_type = list(token.keys())[0]
489						token_value = list(token.values())[0].strip()
490						
491						if(token_type == "dataType" and token_value == "char"):
492				
493							token = lexer()
494							token_type = list(token.keys())[0]
495							token_value = list(token.values())[0].strip()
496							
497							if(token_type == "arithmeticOperator" and token_value == "*"):
498				
499								token = lexer()
500								token_type = list(token.keys())[0]
501								token_value = list(token.values())[0]	.strip()
502								
503								if(token_type == "identifier" and token_value == "argv"):
504				
505									token = lexer()
506									token_type = list(token.keys())[0]
507									token_value = list(token.values())[0].strip()
508									
509									if(token_type == "punctuator" and token_value == "["):
510				
511										token = lexer()
512										token_type = list(token.keys())[0]
513										token_value = list(token.values())[0].strip()
514										
515										if(token_type == "punctuator" and token_value == "]"):
516				
517											token = lexer()
518											token_type = list(token.keys())[0]
519											token_value = list(token.values())[0].strip()
520											
521											if(token_type == "punctuator" and token_value == ")"):
522				
523												token = lexer()
524												token_type = list(token.keys())[0]
525												token_value = list(token.values())[0].strip()
526											
527												if(token_type == "punctuator" and token_value == "{"):
528				
529													status = statements()
530					
531													if(status == 0):
532						
533														token = lexer()
534														token_type = list(token.keys())[0]
535														token_value = list(token.values())[0].strip()
536				
537														if(not(token_type == "punctuator" and token_value == "}")):
538															print("Syntax error: expected 'Punctuator2 close curly bracket' ", end = "")
539															print("but received " + str(token_value) + "\n")
540															status = 1
541												else:
542													print("Syntax error: expected 'Punctuator open curly bracket'  ", end = "")
543													print("but received " + str(token_value) + "\n")
544													status = 1
545											
546											else:
547												print("Syntax error: expected 'Punctuator close round bracket' but received ", end = "")
548												print(str(token_value) + "\n")
549												status = 1
550											
551										else:
552											print("Syntax error: expected 'Punctuator close square bracket' but received ", end = "")
553											print(str(token_value) + "\n")
554											status = 1
555									else:
556										print("Syntax error: expected 'Punctuator open square bracket' but received ", end = "")
557										print(str(token_value) + "\n")
558										status = 1
559									
560								else:
561									print("Syntax error: expected 'Identifier argv' but received " + str(token_value) + "\n")
562									status = 1
563									
564							else:
565								print("Syntax error: expected 'Pointer operator *' but received " + str(token_value) + "\n")
566								status = 1
567							
568						else:
569							print("Syntax error: expected 'Data type character' but received " + str(token_value) + "\n")
570							status = 1
571						
572					else:
573						print("Syntax error: expected 'Punctuator comma' but received " + str(token_value) + "\n")
574						status = 1	
575				
576				else:
577					print("Syntax error: expected 'Identifier argc' but received " + str(token_value) + "\n")
578					status = 1
579				
580			
581			else:
582				print("Syntax error: expected 'Punctuator close round bracket' but received " + str(token_value) + "\n")
583				status = 1
584				
585		else:
586			print("Syntax error: expected 'Punctuator open round bracket' but received " + str(token_value) + "\n")
587			status = 1
588	
589	else:
590		print("Syntax error: expected 'Identifier main' but received " + str(token_value) + "\n")
591		status = 1
592		
593	return status
594data = {}
595def statements():
596	
597	
598	#print("top of statements\n")
599	status = 0
600	status = initializationStatement()
601	
602	if(status == 0):
603		#print("init success")
604		token = lexer()
605		token_type = list(token.keys())[0]
606		token_value = list(token.values())[0]
607		#print(token_value +" new value")
608		tv = token_value.strip()
609		if(token_type == "punctuator" and tv == ";"):
610
611			status = statements()
612		else:
613			print("Syntax error: expected 'Punctuator semicolon2' but received " + str(token_value) + "\n")
614			status = 1
615			
616			
617	else:
618		'''	token = lexer()
619		token_type = list(token.keys())[0]
620		token_value = list(token.values())[0]
621		tv = token_value.strip()'''
622		#print("dc" + " " + tv)
623		
624		
625		status = optionalDeclarationStatement()
626		#print(status)
627		if(status == 0):	
628			#print("decl success")
629			
630			token = lexer()
631			token_type = list(token.keys())[0]
632			token_value = list(token.values())[0]
633			tv = token_value.strip()
634			if(token_type == "punctuator" and tv == ";"):
635				
636				status = statements()
637			else:
638				print("Syntax error: expected 'Punctuator semicolon3' but received " + str(token_value) + "\n")
639				status = 1
640		else:
641			
642			status = assignmentStatement()
643			if(status == 0):
644				#print("assgn success")
645				
646				token = lexer()
647				token_type = list(token.keys())[0]
648				token_value = list(token.values())[0]
649				tv = token_value.strip()
650				if(token_type == "punctuator" and tv == ";"):
651					status = statements()
652				else:
653					print("Syntax error: expected 'Punctuator semicolon4' but received " + str(token_value) + "\n")
654					status = 1
655			else:
656				
657				status = 0
658				token = lexer()
659				token_type = list(token.keys())[0]
660				token_value = list(token.values())[0]
661				#print("IN statements: " + token_value)
662				if(token_type == "keyword" and token_value == "do"):
663					#print("Do")
664					token = lexer()
665					token_type = list(token.keys())[0]
666					token_value = list(token.values())[0].strip()
667					lab1()
668					if(token_type == "punctuator" and token_value == "{"):
669						#print("{")
670						status = statements()
671						
672						#print("status: " + str(status))
673						if(status == 0):
674					
675							token = lexer()
676							token_type = list(token.keys())[0]
677							token_value = list(token.values())[0].strip()
678							#print(token_value)
679							if(token_type == "punctuator" and token_value == "}"):
680								#print("}")
681								token = lexer()
682								token_type = list(token.keys())[0]
683								token_value = list(token.values())[0].strip()
684		
685								if(token_type == "keyword" and token_value == "while"):
686									#print("while")
687									token = lexer()
688									token_type = list(token.keys())[0]
689									token_value = list(token.values())[0].strip()
690		
691									if(token_type == "punctuator" and token_value == "("):
692										#print("(")
693										status = condition()
694										lab2()
695										if(status == 0):
696					
697											token = lexer()
698											token_type = list(token.keys())[0]
699											token_value = list(token.values())[0].strip()
700		
701											if(token_type == "punctuator" and token_value == ")"):
702												#print(")")
703												token = lexer()
704												token_type = list(token.keys())[0]
705												token_value = list(token.values())[0].strip()
706		
707												if(token_type == "punctuator" and token_value == ";"):
708													#print("in statements: " + token_value + "\n")
709													status = statements()
710					
711												else:
712													print("Syntax error: expected 'Punctuator semicolon5' ", end = "")
713													print("but received " + str(token_value) + "\n")
714													status = 1
715					
716											else:
717												print("Syntax error: expected 'Punctuator close round bracket' ", end = "")
718												print("but received " + str(token_value) + "\n")
719												status = 1
720					
721									else:
722										print("Syntax error: expected 'Punctuator open round bracket' ", end = "") 
723										print("but received " + str(token_value) + "\n")
724										status = 1
725					
726								else:
727									print("Syntax error: expected 'Keyword while' but received " + str(token_value) + "\n")
728									status = 1
729					
730							else:
731								print("Syntax error: expected 'Punctuator10 close curly bracket' but received " + str(token_value) + "\n")
732								status = 1
733				
734					else:
735						print("Syntax error: expected 'Punctuator open curly bracket' but received " + str(token_value) + "\n")
736						status = 1
737		
738				else:
739		
740					#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
741					global lb, fp
742					#print(token_value)
743					#print(str(lb) + " " + str(fp))
744					lb = lb - len(token_value)
745					fp = fp - len(token_value)
746	
747	return status
748
749def initializationStatement():
750
751	status = 0
752	
753	global lb, fp
754		
755	token = lexer()
756	token_type = list(token.keys())[0]
757	token_value = list(token.values())[0]
758
759	if(token_type == "dataType"):
760		if(token_value not in data):
761			data[token_value] = {};
762			#print(token_value)
763		
764		status = initStat(token_value)
765		
766		
767	else:
768		
769		#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
770		#print(token_value)
771		#print(str(lb) + " " + str(fp))
772		lb = lb - len(token_value)
773		fp = fp - len(token_value)
774		status = 2
775	#print('returning' + str(status))	
776	return status
777	
778	
779def initStat(dt):
780
781
782	status = multipleInitialization(dt)
783	#print(status)
784	
785	
786	
787	'''if(status != 0 and status != 2):
788
789		status = 0
790		token = lexer()
791		token_type = list(token.keys())[0]
792		token_value = list(token.values())[0]
793		tk = token_value
794		if(token_type == "identifier"):
795		
796			if(token_value not in data[dt]):
797				data[dt][token_value]=0
798			else:
799				print("Syntax Error: The variable has already been initialized\n")
800				return 1
801			token = lexer()
802			token_type = list(token.keys())[0]
803			token_value = list(token.values())[0]
804	
805			if(token_type == "assignmentOperator" and token_value == "="):
806				
807				status = E(dt,tk)
808				"""
809				print(status)
810				status = 0
811				token = lexer()
812				token_type = list(token.keys())[0]
813				token_value = list(token.values())[0]
814				print(token_value)
815				"""
816			
817			elif(token_type == "punctuator" and token_value == ","):
818			
819				global lb, fp
820				#print(token_value)
821				#print(str(lb) + " " + str(fp))
822				lb = lb - len(token_value)
823				fp = fp - len(token_value)
824				status = 2
825			
826			else:
827				
828				print("Syntax error: expected 'Assignment1 Operator' but received " + str(token_value) + "\n")
829				status = 1 '''
830		
831	
832	return status
833		
834def multipleInitialization(dt):
835	global data
836	status = 0
837	token = lexer()
838	token_type = list(token.keys())[0]
839	token_value = list(token.values())[0]
840	tk = token_value
841	if(token_type == "identifier"):
842		push(tk)
843		#print(tk)
844		if(token_value not in data[dt]):
845				if(dt=="int"):
846					data[dt][token_value]=int(0)
847				elif(dt=="char"):
848					data[dt][token_value]=string(0)
849				elif(dt=="float"):
850					data[dt][token_value]=float(0)
851				elif(dt=="double"):
852					data[dt][token_value]=float(0)
853				else:
854					data[dt][token_value]=0
855				#print(" "+token_value +":)")
856		else:
857				print("Syntax Error: The variable has already been initialized\n")
858				return 1
859		
860		token = lexer()
861		token_type = list(token.keys())[0]
862		token_value = list(token.values())[0]
863		tv = token_value.strip()
864
865		#print(token_value+" macha")
866		if(tv == ";"):
867			#print("; la")
868			global lb, fp
869			#print(token_value)
870			#print(str(lb) + " " + str(fp))
871			lb = lb - len(token_value)
872			fp = fp - len(token_value)
873			return 0;
874		elif(token_type == "assignmentOperator" and tv == "="):
875				
876			status = E(dt,tk)
877			codegen_assign()
878			#print(status)
879			
880			if(status == 0):
881				
882				status = multinit(dt)
883				if(status == 2):
884					status = 0
885				#print(status)
886		elif(token_type == "punctuator" and tv == ","):
887			#print(",")
888			status = multipleInitialization(dt)
889			'''global lb, fp
890			#print(token_value)
891			#print(str(lb) + " " + str(fp))
892			lb = lb - len(token_value)
893			fp = fp - len(token_value)
894			status = 2 '''
895			
896		else:
897				
898			print("Syntax error: expected 'Assignment2 Operator' but received " + str(tv) + "\n")
899			status = 1
900	else:
901			
902		print("Syntax error: expected 'Identifier' but received " + str(tv) + "\n")
903		status = 1
904	
905	return status
906	
907def multinit(dt):
908
909	status = 0
910	
911	token = lexer()
912	token_type = list(token.keys())[0]
913	token_value = list(token.values())[0]
914	tv = token_value.strip()
915	
916	if(token_type == "punctuator" and tv == ","):
917	
918		#print("got comma")
919		status = multipleInitialization(dt)
920	
921	else:
922		
923		#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
924		global lb, fp
925		#print(token_value)
926		#print(str(lb) + " " + str(fp))
927		lb = lb - len(token_value)
928		fp = fp - len(token_value)
929		status = 2
930		
931	return status
932
933def assignmentStatement():
934	global data
935	dty =''
936	status = 0
937	token = lexer()
938	token_type = list(token.keys())[0]
939	token_value = list(token.values())[0]
940	tk = token_value
941	#print("asgn")
942	if(token_type == "identifier"):
943		push(tk)
944		#print(tk)
945		for i in data:
946			for j in data[i]:
947				if(j==token_value):
948					dty = i
949		if(dty==''):
950			print("The variable "+token_value+" has not been initialized.")
951			return 1
952		token = lexer()
953		token_type = list(token.keys())[0]
954		token_value = list(token.values())[0]
955	
956		if(token_type == "assignmentOperator" and token_value == "="):
957				
958			status = E(dty,tk)
959			codegen_assign()
960			
961		else:
962			
963			print("Syntax error: expected 'Assignment3 Operator' but received " + str(token_value) + "\n")
964			status = 1
965	else:
966			
967		#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
968		global lb, fp
969		#print(token_value)
970		#print(str(lb) + " " + str(fp))
971		lb = lb - len(token_value)
972		fp = fp - len(token_value)
973		status = 2
974	
975	return status
976
977def condition():
978
979	status = 0
980	
981	status = C()
982			
983	return status
984
985def C():
986	status = 0
987	token = lexer()
988	token_type = list(token.keys())[0]
989	token_value = list(token.values())[0]
990	tv = token_value.strip()
991	if(token_type == "identifier" or token_type=="number"):
992		push(tv)
993		token = lexer()
994		token_type = list(token.keys())[0]
995		token_value = list(token.values())[0]
996		tk = token_value.strip()
997		if(token_type == "relationalOperator" or token_type == "logicalOperator"):
998			push(tk)
999			status = C() 
1000		elif(token_value == ")"):
1001			global lb, fp
1002			#print(token_value)
1003			#print(str(lb) + " " + str(fp))
1004			lb = lb - len(token_value)
1005			fp = fp - len(token_value)
1006			return 0
1007		else:
1008			return 1
1009	elif(not (token_type == "boolean")):
1010		
1011			print("Syntax error: expected 'Boolean' but received " + str(token_value) + "\n")
1012			status = 1
1013	return status
1014op = ""
1015def E(dt,vn):
1016
1017	status = F(dt,vn)
1018
1019	if(status == 0):
1020	
1021		status = E1(dt,vn)
1022	
1023	return status
1024	
1025def E1(dt,vn):
1026
1027	status = 0
1028
1029	token = lexer()
1030	token_type = list(token.keys())[0]
1031	token_value = list(token.values())[0]
1032	tv = token_value.strip()
1033	global op;
1034	if(token_type == "arithmeticOperator" and tv == "+"):
1035		op = "+"
1036		push(tv)
1037		#print(tv)
1038		status = F(dt,vn)
1039		codegen()
1040		if(status == 0):
1041		
1042			status = E1(dt,vn)
1043			
1044	else:
1045	
1046		#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
1047		global lb, fp
1048		#print(token_value)
1049		#print(str(lb) + " " + str(fp))
1050		lb = lb - len(token_value)
1051		fp = fp - len(token_value)
1052
1053	return status
1054
1055
1056def F(dt,vn):
1057
1058	status = 0
1059	
1060	status = G(dt,vn)
1061	
1062	if(status == 0):
1063	
1064		status = F1(dt,vn)
1065
1066	return status
1067	
1068def F1(dt,vn):
1069
1070	status = 0
1071
1072	token = lexer()
1073	token_type = list(token.keys())[0]
1074	token_value = list(token.values())[0]
1075	tv = token_value.strip()
1076	global op;
1077	if(token_type == "arithmeticOperator" and tv == "-"):
1078		op = "-"
1079		push(tv)
1080		#print(tv)
1081		status = G(dt,vn)
1082		codegen()
1083		
1084		if(status == 0):
1085		
1086			status = F1(dt,vn)
1087			
1088	else:
1089	
1090		#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
1091		global lb, fp
1092		#print(token_value)
1093		#print(str(lb) + " " + str(fp))
1094		lb = lb - len(token_value)
1095		fp = fp - len(token_value)
1096
1097	return status
1098	
1099def G(dt,vn):
1100
1101	status = 0
1102	
1103	status = H(dt,vn)
1104
1105	if(status == 0):
1106	
1107		status = G1(dt,vn)
1108
1109	return status
1110
1111def G1(dt,vn):
1112
1113	status = 0
1114	
1115	token = lexer()
1116	token_type = list(token.keys())[0]
1117	token_value = list(token.values())[0]
1118	tv = token_value.strip()
1119	global op;
1120	if(token_type == "arithmeticOperator" and tv == "*"):
1121		push(tv)
1122		#print(tv)
1123		op = "*"
1124		status = H(dt,vn)
1125		codegen()
1126		if(status == 0):
1127		
1128			status = G1(dt,vn)
1129			
1130	else:
1131	
1132		#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
1133		global lb, fp
1134		#print(token_value)
1135		#print(str(lb) + " " + str(fp))
1136		lb = lb - len(token_value)
1137		fp = fp - len(token_value)
1138
1139
1140	return status
1141	
1142def H(dt,vn):
1143
1144	status = 0
1145	
1146	status = I(dt,vn)
1147	
1148	if(status == 0):
1149	
1150		status = H1(dt,vn)
1151
1152	return status
1153	
1154def H1(dt,vn):
1155
1156	status = 0
1157	
1158	token = lexer()
1159	token_type = list(token.keys())[0]
1160	token_value = list(token.values())[0]
1161	tv = token_value.strip()
1162	
1163	if(token_type == "arithmeticOperator" and tv == "/"):
1164		global op;
1165		op = "d";
1166		push(tv)
1167		#print(tv)
1168		status = I(dt,vn)
1169		codegen()
1170		if(status == 0):
1171		
1172			status = H1(dt,vn)
1173			
1174	else:
1175	
1176		#RESET POINTERS SINCE A WRONG TOKEN WAS OBTAINED
1177		global lb, fp
1178		#print(token_value + ":::" + str(len(token_value)))
1179		#print(str(lb) + " " + str(fp))
1180		
1181		lb = lb - len(token_value)
1182		fp = fp - len(token_value)
1183
1184	return status
1185	
1186def I(dt,vn):
1187	global data
1188	status = 0
1189	chk = 0
1190	token = lexer()
1191	token_type = list(token.keys())[0]
1192	token_value = list(token.values())[0]
1193	tv = token_value.strip()
1194	
1195	if(token_type == "arithmeticOperator" and tv == "-"):
1196		chk = 1
1197		push(tv)
1198		#print(tv)
1199		status = I()
1200		codegen_umin()
1201	elif(not(token_type == "identifier" or token_type == "number")):
1202		print("Syntax error: expected 'Identifier/Number' but received " + str(token_value) + "\n")
1203		status = 1
1204		return status
1205	if(token_type == "identifier" or token_type == "number"):
1206		push(tv)
1207		#print(tv)
1208	global op;
1209	g = True
1210	if(token_value == "identifier"):
1211		if(token_value not in data[dt]):
1212			print("Syntax error: The variable "+token_value+" not in "+dt)
1213			g = False
1214	elif(token_value == "number"):
1215		if(not isinstance(token_value,dt)):
1216			print("Syntax error: The variable belongs to a different type")
1217			False
1218	if(op=="" and g == True):
1219		if(token_type == "identifier"):
1220
1221			if(chk==1):
1222				data[dt][vn]=-1*data[dt][token_value]
1223				chk = 0
1224			else:
1225				#print(token_value)
1226				data[dt][vn]=data[dt][token_value]
1227			
1228		if(token_type == "number"):
1229			if(chk==1):
1230				data[dt][vn]=-1*float(token_value)
1231				chk = 0
1232			else:
1233				data[dt][vn]=float(token_value)
1234	elif(op=="d" and g == True):
1235		if(token_type == "identifier"):
1236			if(chk==1):
1237				data[dt][vn]/=-1*data[dt][token_value]
1238				chk = 0
1239				op=""
1240			else:
1241				data[dt][vn]/=data[dt][token_value]
1242				op=""
1243			
1244		if(token_type == "number"):
1245			if(chk==1):
1246				data[dt][vn]/=-1*float(token_value)
1247				chk = 0
1248				op = ""
1249			else:
1250				data[dt][vn]/=float(token_value)
1251				op = ""
1252	elif(op=="*" and g == True):
1253		if(token_type == "identifier"):
1254			if(chk==1):
1255				data[dt][vn]*=-1*data[dt][token_value]
1256				chk = 0
1257				op=""
1258			else:
1259				data[dt][vn]*=data[dt][token_value]
1260				op=""
1261			
1262		if(token_type == "number"):
1263			if(chk==1):
1264				data[dt][vn]*=-1*float(token_value)
1265				chk = 0
1266				op = ""
1267			else:
1268				data[dt][vn]*=float(token_value)
1269				op = ""
1270	elif(op=="-" and g == True):
1271		if(token_type == "identifier"):
1272			if(chk==1):
1273				data[dt][vn]-=-1*data[dt][token_value]
1274				chk = 0
1275				op=""
1276			else:
1277				data[dt][vn]-=data[dt][token_value]
1278				op=""
1279			
1280		if(token_type == "number"):
1281			if(chk==1):
1282				data[dt][vn]-=-1*float(token_value)
1283				chk = 0
1284				op = ""
1285			else:
1286				data[dt][vn]-=float(token_value)
1287				op = ""
1288	elif(op=="+" and g == True):
1289		if(token_type == "identifier"):
1290			if(chk==1):
1291				data[dt][vn]+=-1*data[dt][token_value]
1292				chk = 0
1293				op=""
1294			else:
1295				data[dt][vn]+=data[dt][token_value]
1296				op=""
1297			
1298		if(token_type == "number"):
1299			if(chk==1):
1300				data[dt][vn]+=-1*float(token_value)
1301				chk = 0
1302				op = ""
1303			else:
1304				data[dt][vn]+=float(token_value)
1305				op = ""
1306	return status
1307	
1308	
1309	
1310
1311prg = open("nocomments.c").read()
1312
1313
1314
1315symbolTable = dict()
1316externalVariables = dict()
1317localVariables = list()
1318keyword = ["include", "define", "while", "do", "for", "return", "extern"]
1319dataType = ["void", "int", "short", "long", "char", "float", "double"]
1320preDefRoutine = ["printf", "scanf"]
1321#headerFile = ["stdio.h", "stdlib.h", "math.h", "string.h"]
1322identifier = "^[^\d\W]\w*\Z"
1323punctuator = "^[()[\]{};.,]$"
1324aritmeticOperator = "^[-+*/]$"
1325assignmentOperator = "^=$"
1326relationalOperator = ["<", ">", "<=", ">=", "==", "!="]
1327logicalOperator = ["&&", "||", "!"]
1328number = "^\d+$"
1329spaces = "[' ''\n''\t']"
1330
1331loadSymbolTable()
1332parse_start()
1333'''
1334for i in data:
1335	for j in data[i]:
1336		print(i+" "+j+" "+str(data[i][j]))
1337'''
1338"""
1339while lb!=len(prg):
1340	lexer()
1341"""
1342#print(symbolTable)
1343#print(externalVariables)
1344"""
1345PARSER ERROR CODES:
1346
13470-SUCCESS
13481-FAILURE
1349"""
1350
1351
1352
1353		
1354	
1355
Full Screen

syn.py

Source: syn.py Github

copy
1import re
2
3#import ply.lex as lex
4def loadSymbolTable():
5	
6	symbolTable["keyword"] = keyword
7	symbolTable["dataType"] = dataType
8	symbolTable["preDefRoutine"] = preDefRoutine
9lb = 0
10fp = 1
11def validLexeme(string):
12	
13	res = False
14	if(string in keyword):
15		#print("key " + string + "\n")
16		res = "keyword"
17	elif(string in dataType):
18		#print("dataType " + string + "\n")
19		res = "dataType"
20	elif(string in preDefRoutine):
21		res = "preDefRoutine"
22	elif(re.match(identifier, string)):
23		#print("id " + string + "\n")
24		res = "identifier"
25	elif(re.match(punctuator, string)):
26		#print("punc " + string)
27		res = "punctuator"
28	elif(re.match(number, string)):
29		res = "number"
30	elif(re.match(aritmeticOperator, string)):
31		res = "arithmeticOperator"
32	elif(re.match(assignmentOperator, string)):
33		res = "assignmentOperator"
34	elif(string in relationalOperator):
35		res = "relationalOperator"
36	elif(string in logicalOperator):
37		res = "logicalOperator"
38	elif(string == "#"):
39		res = "hashOperator"
40	elif(string == ".h"):
41		res = "headerExtension"
42	elif(string == "true" or string == "false"):
43		res = "boolean"
44	elif(string == "++"):
45		res = "incrementOperator"
46	elif(string == "--"):
47		res = "decrementOperator"
48	return res
49
50top = 0;
51i_ = 1;
52tmp = "";
53do = 0
54li = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
55def push(val):
56	global top,li
57	top = top+1
58	li[top]=val;
59	
60
61
62def codegen():
63	global tmp,i_,top,li
64	tmp = "N"
65	tmp+=str(i_)
66	print("NODE "+tmp +" -> "+str(li[top-2])+" <-- "+ str(li[top-1])+" --> "+ str(li[top]));
67	#if(do==1):
68		#print("do --> "+tmp)
69	top-=2;
70	li[top]=tmp
71	i_=i_+1;
72
73
74def codegen_umin():
75	global tmp,i_,top,li
76	tmp = "t"
77	tmp+=str(i_)
78	print(tmp+" = -"+str(li[top]));
79	top=top-1;
80	li[top]=tmp;
81	i_=i_+1;
82
83def codegen_assign():
84	global tmp,i_,top,li
85	print(str(li[top-1])+" <-- = --> "+str(li[top]));
86	if(do!=0):
87		print("do --> =")
88	else:
89		print("main --> =")
90	top=top-2;
91label = 1
92
93def lab1():
94	global label
95	#print("L"+str(label)+":")
96	label = label+1
97wh = ["dd"]	
98def lab2():
99	global tmp,i_,top,li,label,wh
100	tmp = "N"
101	tmp+=str(i_)
102	print("NODE "+tmp +" -> "+str(li[top-2])+" <-- "+ str(li[top-1])+" --> "+ str(li[top]));
103	#print("if "+tmp+" goto L"+str(label-1));
104	i_=i_+1;
105	wh[0]=tmp
106	label = label-1;
107	top = top-3;
108
109
110def lexer():
111	global lb
112	global fp
113	
114	lexeme = prg[lb:fp]
115	
116	while(re.match(spaces, lexeme)):
117		#print("x " + lexeme + "\n")
118		lb = lb + 1
119		fp = fp + 1
120		lexeme = prg[lb:fp]
121	
122	#if(re.match(spaces, prg[
123	#print("lexeme: " + lexeme + " type: " + str(type(lexeme)) + "\n");
124	res = validLexeme(lexeme)
125	while((not res) and (fp <= len(prg))):
126		#print("lexeme1: " + lexeme + "\n")
127		fp = fp + 1
128		lexeme = prg[lb:fp]
129		res = validLexeme(lexeme)
130	
131	#print(lexeme + "\n")
132	tokenType = res
133	res = validLexeme(lexeme)
134	while((res) and (fp <= len(prg))):
135		#print("lexeme2: " + lexeme + "\n")