reuseLogostLogotudLogorewerseLogo modelplexLogo
gearsBG
Reuseware Composition Framework
Components, Modules, Aspects or something new?
Introduce new Composition Techniques into your Language of Choice with Reuseware!

Contents

Modular Syntax Definitions (Examples)

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.


Prolog like Syntax in scc

 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 

Regular Expression Library in jj

TOKEN : { 
	<IDENTIFIER:(["a"-"z","A"-"Z"]|"_")(["a"-"z","A"-"Z","0"-"9"]|"_")*>|
	<INTEGER:(["1"-"9"](["0"-"9"])*)|"0">|
	<WHITESPACE:" "|"\t"|"\n"|"\r"|"\f">
}


Extending Prolog with Statements in ejj

 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>
        }
 }

Expression Template in ejj

 void [NonTerminalSlot : expression]: {} {
   	 [NonTerminalSlot : term] ( [GrammarSymbolSlot : op] [NonTerminalSlot : expression] )?  
 }

Simple (Java-)Doclet Syntax in ejj

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()
  }
}


µJava Syntax in ejj

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> 
    } 
}

Retrieved from "http://www.reuseware.org/index.php/Modular_Synax_Examples"

This page has been accessed 413 times. This page was last modified 16:13, 2 February 2009.