Hey guys, i am facing a small problem here, perhaps you could give me any hints with it.

In one of my courses (Program analysis) we are using a language called "The While language" , it is simplified and I think only for education purposes. I am asked to make an abstract syntax out of the concrete one, during it in a language of my choice. This abstract syntax will be used later on to generate the language parser(I think?), Anyway here is the concrete syntax of the language

a   ::= n | x | A[a] | a1  opa a2 | -a | (a)
b   ::= true | false | a1 opr a2 | b1 opb b2 | !b | (b)
S   ::= x := a; | skip; | A[a1] := a2; | read x; | read A[a]; | write a; | 
S1  S2 | if b then  S1 else S2 fi; | while b do S od;
D   ::= int x; | int A[n]; | ∊ | D1 D2
P   ::=     program D S end



opa ∈   {+, -, *, /}        int × int → int
opr ∈   {<, >, <=, >=, =, !=} int × int → bool
opb ∈   {&, |}          bool × bool → bool

Soo, so far so good, I started creating the abstract syntax but I got stucked in a way after talking to the TA. Here is what I have right now

public abstract class Exp {}
public abstract class Statement {}

public class Plus extends Exp{
    public Exp e1,e2;
    public Plus(Exp ae1, Exp ae2){
        e1 = ae1; 
        e2 = ae2;
    }
}


public class Substract extends Exp{
    public Exp e1,e2;
    public Substract(Exp ae1, Exp ae2){
        e1 = ae1; 
        e2 = ae2;
    }
}

public class While extends Statement{
    public Exp e1;
    public Statement s1;
    public While(Exp ae1, Statement as1){
        e1 = ae1;
        s1 = as1;
    }
}

public class If extends Statement{
    public Exp e1;
    public Statement s1;
    public Statement s2;
    public If(Exp ae1, Statement as1, Statement as2){
        e1 = ae1;
        s1 = as1;
        s2 = as2;
    }
}

I did a lot of digging on this for minijava to get some ideas of how to do it, anyway, the TA said I was going to the right direction but for the arithmetic expressions I could just define all in 1 class and it is possible to use + ,- ,/ etc instead, which got me a bit confused, would be nice if someone could add stuff on this ...

Another thing is I have to also define Boolean expressions, I'd assume new abstract class BoolExp and then should be similiar to the arithmetic one

Any ideas are more than welcome

I don't think it's a good idea to define only one class for all arithmetic expressions and I don't think that's what your TA meant either. What you can do is to define one class that corresponds to the a1 opa a2. That class would look like your Add and Sub classes except it would also contain a member for the opa, which you could define as an enum.

Hey Sepp!, thanks for replying!
I see, I'll try to fix it a bit :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.