You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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::=TypeId"(" [Arg] ")""{" [Stm] "}";terminatorDef"";
-- ## Function parameters.
ADecl. Arg::=TypeId;separatorArg",";
-- # Statements.
SDecls. Stm::=Type [Id] ";";SInit. Stm::=TypeId"="Exp";";SBlock. Stm::="{" [Stm] "}";SExp. Stm::=Exp";";SReturn. Stm::="return"Exp";";SWhile. Stm::="while""("Exp")"Stm;SIfElse. Stm::="if""("Exp")"Stm"else"Stm;terminatorStm"";
-- # 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::=IdIncDecOp; -- Restricted to Id.EPre. Exp6::=IncDecOpId; -- Restricted to Id.
-- ## Compound expressions.
-- ### Multiplicative operations.
EMul. Exp5::=Exp5MulOpExp6; -- Left assoc.
-- ### Additive operations.
EAdd. Exp4::=Exp4AddOpExp5; -- Left assoc.
-- ### Comparison.
ECmp. Exp3::=Exp4CmpOpExp4; -- 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.coercionsExp 6 ;separatorExp","; -- 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
rulesType::="bool" | "int" | "double" | "void";tokenId (letter (letter | digit | '_')*) ;separatornonemptyId",";
-- # Comment syntax
comment"#";comment"//";comment"/*""*/";
Java syntax representation:
importjava.util.List;
interfaceCMM {
// Programs
record Program(List<Def> listdef_) {}
// Function definitions
record Def(Typetype_, Stringid_, List<Arg> listarg_, List<Stm> liststm_) {}
// Function parameters
record Arg(Typetype_, Stringid_) {}
// StatementssealedinterfaceStm permits
SDecls, SInit, SBlock,
SExp, SReturn, SWhile, SIfElse {}
record SDecls (Typetype_, List<String> listid_) implementsStm {}
record SInit (Typetype_, Stringid_, Expexp_) implementsStm {}
record SBlock (List<Stm> liststm_) implementsStm {}
record SExp (Expexp_) implementsStm {}
record SReturn (Expexp_) implementsStm {}
record SWhile (Expexp_, Stmstm_) implementsStm {}
record SIfElse (Expexp_, Stmstm_1, Stmstm_2) implementsStm {}
// ExpressionssealedinterfaceExp permits
EInt, EDouble, EId, EApp, EPost, EPre,
EMul, EAdd, ECmp, EAnd, EOr, EAss {}
record EInt (Integerinteger_) implementsExp {}
record EDouble (Doubledouble_) implementsExp {}
record EId (Stringid_) implementsExp {}
record EApp (Stringid_, List<Exp> listexp_) implementsExp {}
record EPost (Stringid_, IncDecOpincdecop_) implementsExp {}
record EPre (IncDecOpincdecop_, Stringid_) implementsExp {}
record EMul (Expexp_1, MulOpmulop_, Expexp_2) implementsExp {}
record EAdd (Expexp_1, AddOpaddop_, Expexp_2) implementsExp {}
record ECmp (Expexp_1, CmpOpcmpop_, Expexp_2) implementsExp {}
record EAnd (Expexp_1, Expexp_2) implementsExp {}
record EOr (Expexp_1, Expexp_2) implementsExp {}
record EAss (Stringid_, Expexp_) implementsExp {}
// OperatorsenumIncDecOp { OInc, ODec };
enumMulOp { OTimes, ODiv };
enumAddOp { OPlus, OMinus };
enumCmpOp { OLt, OGt, OLtEq, OGtEq, OEq, ONEq };
// TypesenumType { Type_bool, Type_int, Type_double, Type_void };
}
The text was updated successfully, but these errors were encountered:
Java's
enum
,record
, andsealed 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
:Java syntax representation:
The text was updated successfully, but these errors were encountered: