Пример #1
0
def p_formalDecl(p):
    '''formalDecl : typeSpecifier IDENTIFIER
                  | typeSpecifier IDENTIFIER LBRACKET RBRACKET'''
    varName = p[2]
    varType = p[1].children[0].type
    for item in global_scope:
        if item[1] == varName:
            if item[0] == varType:
                print "Warning : Shadowing global variable %s near line %s.\n"  % (varName, p.lexer.lineno)
                
    match = False
    for item in local_scope:
        if item[1] == varName: #same identifier
            if item[0] == varType: #same type
                print "Syntax Error: Redefinition of local variable %s with type %s near line %s.\n" % \
                      (varName, varType, p.lexer.lineno)
            else:
                print "Syntax Error: Redefinition of local variable %s with type %s (originally type %s) near line %s.\n" % \
                      (varName, varType, item[0], p.lexer.lineno)
            global syntaxerrs
            syntaxerrs += 1
            match = True
                
    if not match:
        if DEBUG:
            print "New local variable %s %s declared near line %s.\n" % (varType, varName, p.lexer.lineno)
            
        if len(p) > 3:
            local_scope.append((varType, varName, "array"))
        else:
            local_scope.append((varType, varName))
            
    p[0] = Node("formalDecl", p[1:])
    if len(p) > 3:
        p[0].subtype = 'array'
Пример #2
0
def p_varDecl(p):
    '''varDecl  : typeSpecifier IDENTIFIER LBRACKET INT_LITERAL RBRACKET SEMICOLON
                | typeSpecifier IDENTIFIER SEMICOLON'''
    p[0] = Node("varDecl", p[1:])
    if len(p) > 4:
        p[0].subtype = "array"