示例#1
0
    def runSemanticAnalyzer(self, text):
        from spi import Lexer, Parser, SemanticAnalyzer
        lexer = Lexer(text)
        parser = Parser(lexer)
        tree = parser.parse()

        semantic_analyzer = SemanticAnalyzer()
        semantic_analyzer.visit(tree)
        return semantic_analyzer
示例#2
0
    def makeInterpreter(self, text):
        from spi import Lexer, Parser, SemanticAnalyzer, Interpreter
        lexer = Lexer(text)
        parser = Parser(lexer)
        tree = parser.parse()

        semantic_analyzer = SemanticAnalyzer()
        semantic_analyzer.visit(tree)

        interpreter = Interpreter(tree)
        return interpreter
示例#3
0
def symbol():
    lexer = Lexer(open('text.pas', 'r', encoding='utf-8').read())

    parser = Parser(lexer)
    tree = parser.parse()
    builder = SemanticAnalyzer(spi.ScopedSymbolTable())
    builder.visit(tree)

    print(builder.symtab)
    interpreter = Interpreter()
    interpreter.visit(node=tree)
    print(interpreter.global_scope)
示例#4
0
def test_symbol_table_builder():
    text = """
    PROGRAM Part11;
    VAR
       x : INTEGER;
       y : REAL;
    
    BEGIN
    
    END.
    """

    lexer = Lexer(text)
    parser = Parser(lexer)
    tree = parser.parse()
    semantic_analyzer = SemanticAnalyzer()
    builtins_scope = semantic_analyzer.visit(tree)

    assert builtins_scope._symbols == {
        "INTEGER": integer_type,
        "REAL": real_type,
        "PART11": ProcedureSymbol("PART11"),
    }

    assert builtins_scope.global_scope._symbols == {
        "X": VarSymbol("X", integer_type),
        "Y": VarSymbol("Y", real_type),
    }
示例#5
0
def test_part12():
    text = """
    PROGRAM Part12;
    VAR
       a : INTEGER;
    
    PROCEDURE P1;
    VAR
       a : REAL;
       k : INTEGER;
    
       PROCEDURE P2;
       VAR
          a, z : INTEGER;
       BEGIN {P2}
          z := 777;
       END;  {P2}
    
    BEGIN {P1}
    
    END;  {P1}
    
    BEGIN {Part12}
       a := 10;
    END.  {Part12}
    """

    lexer = Lexer(text)
    parser = Parser(lexer)
    tree = parser.parse()
    semantic_analyzer = SemanticAnalyzer()
    builtins_scope = semantic_analyzer.visit(tree)
    global_scope = builtins_scope.global_scope
    interpreter = Interpreter(tree)
    interpreter.interpret()

    assert builtins_scope._symbols == {
        "INTEGER": integer_type,
        "REAL": real_type,
        "PART12": ProcedureSymbol("PART12"),
    }

    assert global_scope._symbols == {
        "A": VarSymbol("A", integer_type),
        "P1": ProcedureSymbol("P1"),
    }

    assert global_scope.P1_scope._symbols == {
        "A": VarSymbol("A", real_type),
        "K": VarSymbol("K", integer_type),
        "P2": ProcedureSymbol("P2"),
    }

    assert global_scope.P1_scope.P2_scope._symbols == {
        "A": VarSymbol("A", integer_type),
        "Z": VarSymbol("Z", integer_type),
    }

    assert interpreter.GLOBAL_SCOPE == {"A": 10}
示例#6
0
def interpreter(text):

    lexer = Lexer(text)
    try:
        parser = Parser(lexer)
        tree = parser.parse()
    except (LexerError, ParserError) as e:
        return (None, e.message)

    semantic_analyzer = SemanticAnalyzer()
    try:
        semantic_analyzer.visit(tree)
    except SemanticError as e:
        return (None, e.message)

    interpreter = Interpreter(tree)
    interpreter.call_stack = TestCallStack()
    return interpreter
示例#7
0
def test_duplicate_decl_error():
    text = """
    program SymTab6;
       var x, y : integer;
       var y : real;
    begin
       x := x + y;
    end.
    """

    lexer = Lexer(text)
    parser = Parser(lexer)
    tree = parser.parse()
    semantic_analyzer = SemanticAnalyzer()

    with pytest.raises(DuplicateDeclaration) as e:
        semantic_analyzer.visit(tree)

    assert e.typename == "DuplicateDeclaration"
    assert e.value.args == ("'Y'", )
示例#8
0
def test_symtab_exception1():
    text = """
    PROGRAM NameError1;
    VAR
       a : INTEGER;
    
    BEGIN
       a := 2 + b;
    END.
    """

    lexer = Lexer(text)
    parser = Parser(lexer)
    tree = parser.parse()
    semantic_analyzer = SemanticAnalyzer()

    with pytest.raises(UndeclaredVariable) as e:
        semantic_analyzer.visit(tree)

    assert e.typename == "UndeclaredVariable"
    assert e.value.args == ("'B'", )
示例#9
0
def test_formal_parameter():
    text = """
        program Main;
       var x, y: real;
    
       procedure Alpha(a : integer);
          var y : integer;
       begin
          x := a + x + y;
       end;
    
    begin { Main }
    
    end.  { Main }
    """

    lexer = Lexer(text)
    parser = Parser(lexer)
    tree = parser.parse()
    semantic_analyzer = SemanticAnalyzer()
    builtins_scope = semantic_analyzer.visit(tree)
    global_scope = builtins_scope.global_scope
    interpreter = Interpreter(tree)
    interpreter.interpret()

    assert builtins_scope._symbols == {
        "INTEGER": integer_type,
        "REAL": real_type,
        "MAIN": ProcedureSymbol("MAIN"),
    }

    assert global_scope._symbols == {
        "X": VarSymbol("X", real_type),
        "Y": VarSymbol("Y", real_type),
        "ALPHA": ProcedureSymbol("ALPHA"),
    }

    assert global_scope.ALPHA_scope._symbols == {
        "A": VarSymbol("A", integer_type),
        "Y": VarSymbol("Y", integer_type),
    }

    assert interpreter.GLOBAL_SCOPE == {}
示例#10
0
def test_part11():
    text = """
        PROGRAM Part11;
    VAR
       number : INTEGER;
       a, b   : INTEGER;
       y      : REAL;
    
    BEGIN {Part11}
       number := 2;
       a := number ;
       b := 10 * a + 10 * number DIV 4;
       y := 20 / 7 + 3.14
    END.  {Part11}
    """
    lexer = Lexer(text)
    parser = Parser(lexer)
    tree = parser.parse()
    semantic_analyzer = SemanticAnalyzer()
    builtins_scope = semantic_analyzer.visit(tree)
    global_scope = builtins_scope.global_scope
    interpreter = Interpreter(tree)
    interpreter.interpret()

    assert builtins_scope._symbols == {
        "INTEGER": integer_type,
        "REAL": real_type,
        "PART11": ProcedureSymbol("PART11"),
    }

    assert global_scope._symbols == {
        "NUMBER": VarSymbol("NUMBER", integer_type),
        "A": VarSymbol("A", integer_type),
        "B": VarSymbol("B", integer_type),
        "Y": VarSymbol("Y", real_type),
    }

    assert interpreter.GLOBAL_SCOPE == dict(A=2,
                                            B=25,
                                            NUMBER=2,
                                            Y=5.997142857142857)
示例#11
0
def test_part_14():
    text = """
    program Main;
       var b, x, y : real;
       var z : integer;
    
       procedure AlphaA(a : integer);
          var b : integer;
    
          procedure Beta(c : integer);
             var y : integer;
    
             procedure Gamma(c : integer);
                var x : integer;
             begin { Gamma }
                x := a + b + c + x + y + z;
             end;  { Gamma }
    
          begin { Beta }
    
          end;  { Beta }
    
       begin { AlphaA }
    
       end;  { AlphaA }
    
       procedure AlphaB(a : integer);
          var c : real;
       begin { AlphaB }
          c := a + b;
       end;  { AlphaB }
    
    begin { Main }
    end.  { Main }
    """

    lexer = Lexer(text)
    parser = Parser(lexer)
    tree = parser.parse()
    semantic_analyzer = SemanticAnalyzer()
    builtins_scope = semantic_analyzer.visit(tree)
    global_scope = builtins_scope.global_scope

    assert builtins_scope._symbols == {
        "INTEGER": integer_type,
        "REAL": real_type,
        "MAIN": ProcedureSymbol("MAIN"),
    }

    assert global_scope._symbols == {
        "B": VarSymbol("B", real_type),
        "X": VarSymbol("X", real_type),
        "Y": VarSymbol("Y", real_type),
        "Z": VarSymbol("Z", integer_type),
        "ALPHAA": ProcedureSymbol("ALPHAA"),
        "ALPHAB": ProcedureSymbol("ALPHAB"),
    }

    assert global_scope.ALPHAA_scope._symbols == {
        "A": VarSymbol("A", integer_type),
        "B": VarSymbol("B", integer_type),
        "BETA": ProcedureSymbol("BETA"),
    }

    assert global_scope.ALPHAA_scope.BETA_scope._symbols == {
        "C": VarSymbol("C", integer_type),
        "Y": VarSymbol("Y", integer_type),
        "GAMMA": ProcedureSymbol("GAMMA"),
    }

    assert global_scope.ALPHAA_scope.BETA_scope.GAMMA_scope._symbols == {
        "C": VarSymbol("C", integer_type),
        "X": VarSymbol("X", integer_type),
    }

    assert global_scope.ALPHAB_scope._symbols == {
        "A": VarSymbol("A", integer_type),
        "C": VarSymbol("C", real_type),
    }