Giter Club home page Giter Club logo

compilerproject3's Introduction

CompilerProject3 : Semantic Analyzer

Strategy

Decl -> Type Descs ( Descs.type = Type)

Descs -> Desc ( Desc.type = Descs.type)

Descs -> Desc , Descs' ( Desc.type = Descs.type Descs'.type = Descs.type)

Desc -> Id ( Id.type = Desc.type [ERROR]{ Id.name exists } )

Desc -> Id = Expr ( Id.type = Desc.type Id.val = Expr.val [ERROR]{ Id.type != Expr.type Id.name exists } )

Expr -> Factor ( Expr.val = Factor.val Expr.type = Factor.type)

Expr -> Expr' BinOp(+ - * /) Expr'' ( Expr.val = BinOp(Expr'.val, Expr''.val) Expr.type=Expr'.type [ERROR]{ Expr'.type != Expr''.type } )

Expr -> Expr' BinOp(> < >= <= ==) Expr'' ( Expr.val = BinOp(Expr'.val, Expr''.val) Expr.type = BOOL [ERROR]{ Expr'.type != Expr''.type } )

Factor -> Decimal_Number ( Factor.type = INT Factor.val = Number.val)

Factor -> Float_Number ( Factor.type = FLOAT Factor.val = Number.val)

Factor -> Id ( Factor.type = Id.type Factor.val = Id.val [ERROR]{ Id.name doesn't exist } )

Factor -> Id SingOp(++ --) ( Factor.val = Id.val Id.val=SingOp(Id.val) [ERROR]{ Id.type == BOOL Id.name doesn't exist } )

Factor -> SingOp(++ --) Id ( Id.val=SingOp(Id.val) Factor.val = Id.val [ERROR]{ Id.type == BOOL Id.name doesn't exist } )

Factor -> TF ( Factor.type = BOOL Factor.val = TF.val)

Factor -> (Expr) ( Factor.type = Expr.type Factor.val = Expr.val)

Asig_E -> Id = Expr ( Id.val = Expr.val [ERROR]{ Id.type != Expr.type Id.name doesn't exist } )

IF -> if ( Expr ) Block ( [ERROR]{ Expr.type == Empty } )

ELSE -> else if ( Expr ) Block ( [ERROR]{ Expr.type == Empty } )

WHILE -> while ( Expr ) Block ( [ERROR]{ Expr.type == Empty } )

FunDef -> Type FunName ( ParasDef ) Block ( FunDef.return_type = Type FunDef.parameters = ParasDef.paramters [ERROR]{ FunaName exists } )

ParasDef -> ParaDef ( ParasDef.parameters.add(ParaDef.paramter) )

ParasDef -> ParaDef , ParasDef' ( ParasDef.parameters.add(ParaDef.parameter) ParasDef.parameters.add(ParasDef'.parameters) )

ParasDef -> Empty ( ParaDef.paramater = Empty )

ParaDef -> Type Id ( ParaDef.parameter.name = Id.name ParaDef.parameter.type = Type )

Functions

a. Declaration of variables in differnt scope

// global variables : a, x
int a = 11+99;
float x = 1.19;

int main(){
    // you can still create the variable 'a' in another scope(MAIN)
    int a = 9900;
  	if(true){
      	// variable 'a' in the new scope(IF)
      	int a = 9991;
    }
}

b. Assignment of variables && Calculation between variables or constants

// step into another scope
    if(true){
        // declare variable b, assign it as the result of RHS(based on precedence of different operators)
        int b = a-10000+(98*2+1);
    }
    // bool calculation
    bool h = 1<(2*3+4+a);
    
    // '--a' makes a = 9900 - 1 = 9989, but in calculation of 't' serves as 9990(new value)
    int t = --a;

c. Definition of functions

// define function without parameters
void g(){
    int x= 1;
}

//define function with two parameters
int f(int a, int b){
    int t = a*b;
    return t+a+b;
}

Errors

a. Variables redefined in the same scope

int a = 9900;
    
//error : redefinition of variable 'a'
int a = 0;

b. Variables assigned as a differnt type

// error : cannot assign variable 'i'(int) as float
int i = 1.11;

c. Variables not defined yet in use

//error : variable 'c' is not defined
c = a;

d. Calculation between differnt types

//error : calculation between 'bool' and 'int' is illegal
h = 1+true;

e. Function redefined

// error : redefinition of function 'f'
int f(){
    return 0;
}

Testing

a. Full Testing code

// define function without parameters
void g(){
    int x= 1;
}

//define function with two parameters
int f(int a, int b){
    int t = a*b;
    return t+a+b;
}

// error : redefinition of function 'f'
int f(){
    return 0;
}

// global variables : a, x
int a = 11+99;
float x = 1.19;



int main(){
    // you can still create the variable 'a' in another scope
    int a = 9900;
    
    //error : redefinition of variable 'a'
    int a = 0;
    
    // error : cannot assign variable 'a'(int) as float
    a = 1.11;
    
    // 'x--' makes x = 1.19 - 1 = 0.19, but in calculation of 'y' serves as 1.19(old value)
    float y = (x--)+11.9;
    
    // step into another scope
    if(true){
        // declare variable b, assign it as the result of RHS(based on precedence of different operators)
        int b = a-10000+(98*2+1);
    }
    // bool calculation
    bool h = 1<(2*3+4+a);
    
    //error : calculation between 'bool' and 'int' is illegal
    h = 1+true;
    
    // '--a' makes a = 9900 - 1 = 9989, but in calculation of 't' serves as 9990(new value)
    int t = --a;
    
    //error : variable 'c' is not defined
    c = a;
}

b. Results

compilerproject3's People

Contributors

patrickddj avatar

Stargazers

 avatar 蛋蛋 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.