Skip to content

Modern AST for Java #490

@andreasabel

Description

@andreasabel

Java's enum, record, and sealed interface could be utilized for a compact representation of the abstract syntax, residing in a single module rather than a package.

It follows a grammar and a suggestion for a contemporary representation.
CMM.cf:

-- Grammar for small C/C++/Java-like language called C-- (C minus minus).

-- # Programs are lists of function definitions.

PDefs.     Program ::= [Def] ;

-- ## Function definition.

DFun.      Def    ::= Type Id "(" [Arg] ")" "{" [Stm] "}" ;
terminator Def "" ;

-- ## Function parameters.

ADecl.     Arg    ::= Type Id ;
separator  Arg "," ;

-- # Statements.

SDecls.    Stm    ::= Type [Id] ";" ;
SInit.     Stm    ::= Type Id "=" Exp ";" ;
SBlock.    Stm    ::= "{" [Stm] "}" ;
SExp.      Stm    ::= Exp ";" ;
SReturn.   Stm    ::= "return" Exp ";" ;
SWhile.    Stm    ::= "while" "(" Exp ")" Stm ;
SIfElse.   Stm    ::= "if" "(" Exp ")" Stm "else" Stm ;
terminator Stm "" ;

-- # Expressions.

-- ## Atomic expressions.

-- ### Literals.
EInt.      Exp6  ::= Integer ;
EDouble.   Exp6  ::= Double  ;

-- ### Identifiers and function calls.
EId.       Exp6  ::= Id      ;
EApp.      Exp6  ::= Id "(" [Exp] ")" ;  -- Restricted to Id.

-- ### Increment and decrement.
EPost.     Exp6  ::= Id IncDecOp ;        -- Restricted to Id.
EPre.      Exp6  ::= IncDecOp Id ;        -- Restricted to Id.

-- ## Compound expressions.

-- ### Multiplicative operations.
EMul.      Exp5  ::= Exp5 MulOp  Exp6 ;  -- Left assoc.

-- ### Additive operations.
EAdd.      Exp4  ::= Exp4 AddOp  Exp5 ;  -- Left assoc.

-- ### Comparison.
ECmp.      Exp3  ::= Exp4 CmpOp  Exp4 ;  -- Non-assoc.

-- ### Boolean operations.
EAnd.      Exp2  ::= Exp2  "&&" Exp3  ;  -- Left assoc.
EOr.       Exp1  ::= Exp1  "||" Exp2  ;  -- Left assoc.

-- ### Assignment.
EAss.      Exp   ::= Id    "="  Exp   ;  -- Right assoc.  Restricted to Id.

coercions  Exp 6   ;
separator  Exp "," ;  -- Function arguments are comma-separated.

-- ## Operators.

OInc.      IncDecOp ::=  "++" ;
ODec.      IncDecOp ::=  "--" ;

OTimes.    MulOp    ::=  "*"  ;
ODiv.      MulOp    ::=  "/"  ;

OPlus.     AddOp    ::=  "+"  ;
OMinus.    AddOp    ::=  "-"  ;

OLt.       CmpOp    ::=  "<"  ;
OGt.       CmpOp    ::=  ">"  ;
OLtEq.     CmpOp    ::=  "<=" ;
OGtEq.     CmpOp    ::=  ">=" ;
OEq.       CmpOp    ::=  "==" ;
ONEq.      CmpOp    ::=  "!=" ;

-- # Types and identifiers

rules Type   ::= "bool" | "int" | "double" | "void" ;

token Id (letter (letter | digit | '_')*) ;

separator nonempty Id "," ;

-- # Comment syntax

comment "#" ;
comment "//" ;
comment "/*" "*/" ;

Java syntax representation:

import java.util.List;

interface CMM {

    // Programs

    record Program(List<Def> listdef_) {}

    // Function definitions

    record Def(Type type_, String id_, List<Arg> listarg_, List<Stm> liststm_) {}

    // Function parameters

    record Arg(Type type_, String id_) {}

    // Statements

    sealed interface Stm permits
        SDecls, SInit, SBlock,
        SExp, SReturn, SWhile, SIfElse                  {}

    record SDecls  (Type type_, List<String> listid_)   implements Stm {}
    record SInit   (Type type_, String id_, Exp exp_)   implements Stm {}
    record SBlock  (List<Stm> liststm_)                 implements Stm {}
    record SExp    (Exp exp_)                           implements Stm {}
    record SReturn (Exp exp_)                           implements Stm {}
    record SWhile  (Exp exp_, Stm stm_)                 implements Stm {}
    record SIfElse (Exp exp_, Stm stm_1, Stm stm_2)     implements Stm {}

    // Expressions

    sealed interface Exp permits
        EInt, EDouble, EId, EApp, EPost, EPre,
        EMul, EAdd, ECmp, EAnd, EOr, EAss               {}

    record EInt    (Integer integer_)                   implements Exp {}
    record EDouble (Double double_)                     implements Exp {}

    record EId     (String id_)                         implements Exp {}
    record EApp    (String id_, List<Exp> listexp_)     implements Exp {}

    record EPost   (String id_, IncDecOp incdecop_)     implements Exp {}
    record EPre    (IncDecOp incdecop_, String id_)     implements Exp {}

    record EMul    (Exp exp_1, MulOp mulop_, Exp exp_2) implements Exp {}
    record EAdd    (Exp exp_1, AddOp addop_, Exp exp_2) implements Exp {}
    record ECmp    (Exp exp_1, CmpOp cmpop_, Exp exp_2) implements Exp {}

    record EAnd    (Exp exp_1, Exp exp_2)               implements Exp {}
    record EOr     (Exp exp_1, Exp exp_2)               implements Exp {}

    record EAss    (String id_, Exp exp_)               implements Exp {}

    // Operators

    enum IncDecOp { OInc, ODec };
    enum MulOp    { OTimes, ODiv };
    enum AddOp    { OPlus, OMinus };
    enum CmpOp    { OLt, OGt, OLtEq, OGtEq, OEq, ONEq };

    // Types

    enum Type     { Type_bool, Type_int, Type_double, Type_void };

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    ASTConcerning the generated abstract syntaxJava

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions