
Contents |
This site contains examples demonstrating how syntax may be composed by our composition system. The examples contain syntax only, we do not provide tool implementations.
Tokens
Normal Tokens
VAR = (['A'..'Z']|'_')(['a'..'z','A'..'Z','0'..'9']|'_')*;
NUMBER = (('-')?['0'..'9'])+;
NAME = (['a'..'z'])(['a'..'z','A'..'Z','0'..'9']|'_')*;
IMPLICATION = ':-';
COMMA = ',';
POINT = '.';
CUT = '!';
LBRACK = '(';
RBRACK = ')';
Ignored Tokens
WS = ' '|'\t'|'\n'|'\r'|'\f';
Productions
ClauseList = (Clause)*
Clause = Head (T.IMPLICATION Tail)? T.POINT
Head = Predicate
Tail = Term (T.COMMA Tail)?
Term = (Predicate|T.CUT)
PredicateSymbol = T . NAME
Atom = T.NAME|T.VAR|T.NUMBER
Predicate = PredicateSymbol T.LBRACK ((Atom)(T.COMMA(Atom))*)? T.RBRACK
TOKEN : {
<IDENTIFIER:(["a"-"z","A"-"Z"]|"_")(["a"-"z","A"-"Z","0"-"9"]|"_")*>|
<INTEGER:(["1"-"9"](["0"-"9"])*)|"0">|
<WHITESPACE:" "|"\t"|"\n"|"\r"|"\f">
}
REUSING syntaxdefinitionmodel.SyntaxDefinition IN /prolog.jj AS prolog;
REFACTOR prolog<ClauseList()->StatementList()>;
EXTEND prolog{
TOKEN:{ MERGE BEFORE VAR <EVAL: "eval">|
MERGE BEFORE VAR <SOLVE: "solve">|
<STRING: "\"" (~["\""])* "\"">
}
OVERRIDE void StatementList():{}{
( Clause() | Query() )*
}
void Query():{}{
<IMPLICATION> (<EVAL>|<SOLVE>)<LBRACK>Predicate() <RBRACK><POINT>
}
void JavaCall():{}{
InstanceJavaCall() | StaticJavaCall()
}
void StaticJavaCall():{}{
QualifiedName() Predicate()
}
void InstanceJavaCall():{}{
<VAR> <POINT> Predicate()
}
void QualifiedName():{}{
( ( <VAR>|<NAME> ) <POINT> )+
}
void MethodName():{}{
<NAME>
}
REFINE void Term():{}{
JavaCall()
}
REFINE void Atom():{}{
<STRING>
}
}
void [NonTerminalSlot : expression]: {} {
[NonTerminalSlot : term] ( [GrammarSymbolSlot : op] [NonTerminalSlot : expression] )?
}
REUSING syntaxdefinitionmodel.LexerProduction IN /lexicalexpressionlibary.jj AS regex;
{
TOKEN:{
<PARAM:"@"(IMPORT regex.definitions.rules[terminal.name='IDENTIFIER'].regex)>|
<TEXT:(~["@","*"])+>
}
SKIP:{
<WS:"*">
}
void DocletStatement():{}{
( SimpleComment() | DocletComment() )+
}
void SimpleComment():{}{
( <TEXT> )+
}
void DocletComment():{}{
<PARAM> SimpleComment()
}
}
REUSING syntaxdefinitionmodel.SyntaxDefinition IN /doclet.jj AS doclet;
REUSING reusesyntaxdefinitionmodel.ReuseGrammarProduction IN /expressiontemplate.ejj AS expression;
REFACTOR doclet<<WS>-><WSD>>;
{
TOKEN:{
<INT:"int">|
<CHAR:"char">|
<VOID:"void">|
<CHAR_VALUE:"'"~[]"'">|
<PUBLIC:"public">|
<PRIVATE:"private">|
<CLASS:"class">|
<PACKAGE:"package">|
<EXTEND_:"extends">|
<IMPORT_:"import">|
<IMPLEMENT:"implements">|
<SEMICOLON:";">|
<LBRACK:"(">|
<RBRACK:")">|
<LSBRACK:"{">|
<RSBRACK:"}">|
<EQ:"=">|
<GT:">">|
<LT:"<">|
<EQOP:"==">|
<AND:"&&">|
<PLUS:"+">|
<MINUS:"-">|
<MUL:"*">|
<DIV:"/">|
<POINT:".">|
<COMMA:",">|
<RETURN:"return">|
<THROW:"throws">|
<WHILE:"while">|
<IF:"if">|
<ELSE:"else">|
<FINAL:"final">|
<IDENT:(["a"-"z","A"-"Z"]|"_")(["a"-"z","A"-"Z","0"-"9"]|"_")*>|
<INTEGER_VALUE:(["1"-"9"](["0"-"9"])*)|"0">|
<STRING_VALUE:"\"" (~["\""])* "\"">
}
SKIP:{
<WS:" "|"\t"|"\n"|"\r"|"\f">
}
void CompilationUnit():{}{
( PackageDeclaration() )? ( ImportDeclaration()|Comment() )* ( ClassDeclaration ())
}
@ Comment(){
"/**" : doclet : "*/"
}
void PackageDeclaration():{}{
<PACKAGE> QualifiedName() <SEMICOLON>
}
void ImportDeclaration():{}{
<IMPORT_> QualifiedName() <SEMICOLON>
}
void ClassDeclaration():{}{
( Modifier() )? <CLASS> <IDENT> ( ExtendList() )?( ImplementsList() )? ClassBody ()
}
void ExtendList():{}{
<EXTEND_> ( ComplexType () )+
}
void ImplementsList():{}{
<IMPLEMENT> ( ComplexType() )+
}
void ClassBody():{}{
<LSBRACK> ( FieldDeclaration()|Comment() )* <RSBRACK>
}
void Modifier():{}{
<PUBLIC> | <PRIVATE>
}
void FieldDeclaration():{}{
( Modifier() )* Type() <IDENT> ( VariableAssignment()|MethodDeclaration()|<SEMICOLON> )
}
void Type():{}{
( ComplexType() | SimpleType () )
}
void MethodDeclaration():{}{
MethodDeclarationParameters() ( <THROW> QualifiedNameList () )? StatementBlock()
}
void SimpleType():{}{
<INT> | <CHAR> | <VOID>
}
void ComplexType():{}{
QualifiedName()
}
void QualifiedName():{}{
<IDENT> ( <POINT> <IDENT> )*
}
void QualifiedNameList():{}{
QualifiedName() ( <COMMA> QualifiedName() )*
}
void MethodDeclarationParameters():{}{
<LBRACK>( MethodDeclarationParameter()( <COMMA> MethodDeclarationParameter() )* )? <RBRACK>
}
void MethodDeclarationParameter():{}{
( <FINAL> )? Type() <IDENT>
}
void StatementBlock():{}{
<LSBRACK> ( Statement() )* <RSBRACK>
}
void Statement():{}{
( ExpressionStatement() | WhileStatement() | IfStatement() | ReturnStatement() )
}
void ExpressionStatement():{}{
( ( Modifier() )? Type() QualifiedName() | Expression() ) ( VariableAssignment())? <SEMICOLON>
}
void VariableAssignment():{}{
<EQ> Expression()
}
void WhileStatement():{}{
<WHILE> <LBRACK> Expression() <RBRACK> StatementBlock ()
}
void IfStatement():{}{
<IF> <LBRACK> Expression() <RBRACK> StatementBlock() ( <ELSE> StatementBlock() )?
}
void ReturnStatement():{}{
<RETURN> Expression() <SEMICOLON>
}
IMPORT expression<expression->'Expression()'.jj,op->'Operator1()'.jj,term->'Expression2()'.jj>
IMPORT expression<expression->'Expression2()'.jj,op->'Operator2()'.jj,term->'Expression3()'.jj>
IMPORT expression<expression->'Expression3()'.jj,op->'Operator3()'.jj,term->'Term()'.jj>
void Term():{}{
Atom() | <LBRACK> Expression() <RBRACK>
}
void Atom():{}{
FieldRef() | <INTEGER_VALUE> | <CHAR_VALUE> | <STRING_VALUE>
}
void FieldRef():{}{
QualifiedName() ( MethodArguments() )?
}
void Operator1():{}{
<LT> | <GT> | <EQOP>
}
void Operator2():{}{
<PLUS> | <MINUS>
}
void Operator3():{}{
<MUL> | <DIV>
}
void MethodArguments():{}{
<LBRACK> ( Expression() ( <COMMA> Expression() )* )? <RBRACK>
}
}