Week 2 Notes


Fluent APIs | CS 3040

Date 09/12/2022
Topic Fluent APIs
Professor Dr. Hasker
Week 2

Sequences | Sequential Solution

turnstile = new Fsm();
State s = new State();
s.setStateId("locked");
s.setInitial(true);
turnstile.getStates().add(s);

...

Functional solution

turnstile = new Fsm();
turnstile.getStates().add(new State("locked", true));
turnstile.getStates().add(new State("unlocked"));

...

Fluent API

turnstile = fsm()
.addState("locked")
	.addTransition("ticket", "collect", "unlocked")
	.addTransition("pass", "alarm", "exception")
.addState("unlocked")
	.addTransition("ticket", "eject", "unlocked")


...

Developing a fluent API for a language

  1. Write/select sample programs in the target language
  2. Design objects with factory methods constructing domain objects
  3. Add methods matching target language actions
  4. Identify initial states, implicit information from one to next

Internal DSL

Domain specific language - solution just using existing programming tools

External DSL

Building a new language for the domain

Run Method: Interpreter

public class FsmlInterpreter {
	public static String[] run(Fsm fsm, String[] input) {
		ArrayList<String> output = new ArrayList<>();
		String state = fsm.getInitial();
		for (String event : input) {
			ActionStatePair pair = fsm.makeTransition(state, event);
			if (pair.action != null) output.add(pair);
			state = pair.state
		}
		return output.toArray(new String[output.size()]);
	}
}

Syntax

Fluent API for internal DSL

Semantics

Interpreter describes what happens when executing syntax

Well-formedness

Checking this is resasonable

External DSL

fsm : {state}* ;
state : {'initial'}? 'state' stateid '{' {transition}* '}' ;
transition : event {'/' action}? {'->' stateid}? ';' ;
stateid : name ;
event : name ;
action : name ;

Implementing the External DSL

External DSL | CS 3040

Date 09/13/2022
Topic External DSL
Professor Dr. Hasker
Week 2

Problem: how would I search for all of the uses of "int" in a program?

Regular Expressions

Precedence

RegEx | CS 3040

Date 09/14/2022
Topic RegEx
Professor Dr. Hasker
Week 2

Shorthand

x+ | xx* = 1 or more, x* = 0 or more

Ignoring case

Ranges

Searching for array expressions in code Example:

egrep \[[0-9a-zA-Z_]*\] *.cpp

Other

Regular Expressions in ANTLR
General rules:
        VARIABLE: regular expression ;
Any text to be matched: in quotes
        SALUTATION: 'Hello';
With alternative:
        SALUTATION: 'Hello' | 'Hi' | 'Greetings,';
Dot: any character
        ANY: .;
Parens: group
Range: ..
        DIGIT: '0'..'9';
        LETTER: ('A'..'Z') | ('a'..'z');
Range with other characters:
        IDENTIFIER_CHARACTER: ('a'..'z') | ('A'..'Z') | ('0'..'9') | '_';
Repetition: +, *:
        NUMBER: ('0'..'9')+;
        ID: ('a'..'z')+('0'..'9')*;
Optional:
        MONTH: '1'?('0'..'9');
Negation:
        NON_DIGIT: ~('0'..'9');
... 
- [https://faculty-web.msoe.edu/hasker/cs3040/notes/n02-3040-re.html]

Grammar | CS 3040

Date 09/15/2022
Topic Grammar
Professor Dr. Hasker
Week 2

Expression Parsing

3 + 4 * 5

Solution: grammars

BTL definition

Expr -> true
Expr -> false
Expr -> zero
Expr -> succ( Expr )
Expr -> pred( Expr )
Expr -> iszero( Expr )
Expr -> if(Expr, Expr, Expr)

Another grammar: capturing statements in English

SimpleSentence -> Noun Verb Object
Noun -> zoe | sam
Verb -> climb | push | throw
Object -> car | ladder | ball

Where:

Expr -> variable
Expr -> variable = Expr
Expr -> Expr ; Expr

Grammars