Beispiel #1
0
def p_def(p):
	'Def : Specifier DecList SEMI'
	p[0] = Node('Def', p, [p[1], p[2], 'SEMI'], p[0])
	
	""" semantic analysis: find every symbol and put it into SymbolTable """
	""" Local Definitions !!!! """
	SemAnalysis.procDef(p[0], 'local')
Beispiel #2
0
def p_extdef_funcDec(p):
	'ExtDef : Specifier FunDec SEMI'
	specifier = SemAnalysis.procSpecifier(p[1])
	funcdec = SemAnalysis.procFunDec(p[2])

	if SemAnalysis.checkFuncRedec(specifier, funcdec):
		print('Error type 19 at Line %d: Inconsistent declaration of function \"%s\"' %(p[1].lineno, funcdec[0]))
	else:
		SemAnalysis.addInSymbolTable(['func-dec', specifier, funcdec, p[1].lineno])
Beispiel #3
0
def p_extdef_1(p):                                                # structure
	'ExtDef	:	Specifier SEMI'
	p[0] = Node('ExtDef', p, [p[1], 'SEMI'], p[0])

	struct_info = SemAnalysis.procSpecifier(p[1])

	# check if there have duplicated struct names
	for item in SemAnalysis.getSymbolTable():
		if item[0] == 'global struct' and item[1][0] == struct_info[0]:
			print('Error type 16 at Line %d: Duplicated name \"%s\"' %(p[1].lineno, struct_info[0].split(' ')[1]))
		if item[0] == 'global struct': # nested struct can also cause duplicated name !!
			for var_definiton in item[1][1]:
				if type(var_definiton[0]) == tuple and var_definiton[0][0] == struct_info[0]:
					print('Error type 16 at Line %d: Duplicated name \"%s\"' %(p[1].lineno, struct_info[0].split(' ')[1]))
	
	SemAnalysis.addInSymbolTable(['global struct', struct_info])
Beispiel #4
0
def p_extdef_2(p):                                                # function
	'ExtDef :	Specifier FunDec CompSt'
	p[0] = Node('ExtDef', p, [p[1], p[2], p[3]], p[0])
	
	fundec = SemAnalysis.procFunDec(p[2]) # [func_name, var_list]
	func_name = fundec[0]
	func_var_list = fundec[1]

	return_type = SemAnalysis.procSpecifier(p[1], 'func')
		
	# check if there has redefined function
	if func_name in SemAnalysis.getFuncName():
		print('Error type 4 at Line %d: Redefined function \"%s\"' %(p[1].lineno, func_name))

	compst = SemAnalysis.procCompSt(p[3], func_var_list, func_name, return_type)

	SemAnalysis.addInSymbolTable(['func', return_type, SemAnalysis.procFunDec(p[2]), compst])
Beispiel #5
0
def p_extdef_3(p):                                                # global variable, it may also has a definition of a structure
	'ExtDef	:	Specifier ExtDecList SEMI'
	p[0] = Node('ExtDef', p, [p[1], p[2], 'SEMI'], p[0])

	Specifier = SemAnalysis.procSpecifier(p[1])
	extdeclist = SemAnalysis.procExtDecList(p[2], [])

	structType = None   # use this variable to allow add one struct type in a declist

	for item in extdeclist:
		specifier = Specifier
		if '[' in item:         # it's an array, need to reshape it
			item = item.replace(']', '').split('[')

			if type(specifier) == str:      # basic type
				for dim in item[1:]:
					specifier += '-' + dim
				item = item[0]
				SemAnalysis.addInSymbolTable(['global variable', specifier, item])

			elif 'struct' in specifier[0]:  # struct type
				if specifier[0] != 'struct UNknownType' and structType == None:   # it's a definition of a struct as well
					SemAnalysis.addInSymbolTable(['global struct', specifier])
					structType = specifier[0]	
				
				var_list = specifier[1]
				specifier = specifier[0]
				
				for dim in item[1:]:
					specifier += '-' + dim
				item = item[0]
				SemAnalysis.addInSymbolTable(['global variable', (specifier, var_list), item])

		else:                   # add it to SymbolTable directly
			if type(specifier) == tuple and specifier[0] != 'struct UNknownType': # it also has a definition of a structure
				SemAnalysis.addInSymbolTable(['global struct', specifier])	

			SemAnalysis.addInSymbolTable(['global variable', specifier, item])
Beispiel #6
0
	Lib version:	ply 3.10
"""

import sys
import ply.lex as lex
import ply.yacc as yacc

from semanticAnalysis import SemAnalysis

from trans2MIPS import Translate2MIPS

# global variable, if some error occurs, it will be set to 1
errorflag = 0

# new a SemAnalysis object
SemAnalysis = SemAnalysis()

# ------------------------------
# lexical analysis
# ------------------------------

# list of token names

# handle reserved words!! very important
reserved = {
	'if' : 'IF',
	'else' : 'ELSE',
	'while': 'WHILE',
	'return' : 'RETURN',
	'struct' : 'STRUCT',
	'int' : 'TYPE',
Beispiel #7
0
	
	Python version: 3.5.2
	Lib version:	ply 3.10
"""

import sys
import ply.lex as lex
import ply.yacc as yacc

from semanticAnalysis import SemAnalysis

# global variable, if some error occurs, it will be set to 1
errorflag = 0

# new a SemAnalysis object
SemAnalysis = SemAnalysis()

# ------------------------------
# lexical analysis
# ------------------------------

# list of token names

# handle reserved words!! very important
reserved = {
    'if': 'IF',
    'else': 'ELSE',
    'while': 'WHILE',
    'return': 'RETURN',
    'struct': 'STRUCT',
    'int': 'TYPE',