| Date | 09/12/2022 |
| Topic | Fluent APIs |
| Professor | Dr. Hasker |
| Week | 2 |
turnstile = new Fsm();
State s = new State();
s.setStateId("locked");
s.setInitial(true);
turnstile.getStates().add(s);
...
turnstile = new Fsm();
turnstile.getStates().add(new State("locked", true));
turnstile.getStates().add(new State("unlocked"));
...
Fluent API
- Using factory methods rather than constructors to build objects.
- Using method chaining: each step returns object that can be applied, which is an application of the Builder pattern.
- Implicit parameters: in this case, a "current state" so no need to repeat.
- Provide useful conventions to eliminate boilerplate code.
turnstile = fsm()
.addState("locked")
.addTransition("ticket", "collect", "unlocked")
.addTransition("pass", "alarm", "exception")
.addState("unlocked")
.addTransition("ticket", "eject", "unlocked")
...
Internal DSL
Domain specific language - solution just using existing programming tools
External DSL
Building a new language for the domain
- Allows for non-developers to build, verify models
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
- No two states have the same ID
- One initial state
- Each state pair has at most one transition
- Each transition goes to a declared state
- No state not reachable from the initial state
fsm : {state}* ;
state : {'initial'}? 'state' stateid '{' {transition}* '}' ;
transition : event {'/' action}? {'->' stateid}? ';' ;
stateid : name ;
event : name ;
action : name ;
| Date | 09/13/2022 |
| Topic | External DSL |
| Professor | Dr. Hasker |
| Week | 2 |
Domain specific languages in general
Examples
Next: Regular Expressions
Problem: how would I search for all of the uses of "int" in a program?
Parenthesis groups items
Star has highest precedence
Concatentation (xy) has next precedence
alternation (x|y) has the lowest precedence
Strings of 0's and 1's with at least one 1
Strings of a's and b's with at least 3 consecutive a's in them
strings of a's and b's with at least 3 a's in them (anywhere)
strings of a's and b's with no doubled a's or b's
| Date | 09/14/2022 |
| Topic | RegEx |
| Professor | Dr. Hasker |
| Week | 2 |
x+ | xx* = 1 or more,
x* = 0 or more
[uvw] instead of (u | v | w)[k-n][0, 1, 2, 3, 4, 5, 6, 7, 8, 9][a-zA-Z0-9]egrep \[[0-9a-zA-Z_]*\] *.cpp
^ forces a match at the beginning$ forces a match at the endRegular 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]
| Date | 09/15/2022 |
| Topic | Grammar |
| Professor | Dr. Hasker |
| Week | 2 |
3 + 4 * 5
Broadly speaking, a grammar describes how programs or "sentences" are structured
Parsing:
Programs: sentences that capture computer instructions
Utterances in domain-specific languages (DSLs):
Grammars give us parse trees, parse trees can be interpreted
Expr -> true
Expr -> false
Expr -> zero
Expr -> succ( Expr )
Expr -> pred( Expr )
Expr -> iszero( Expr )
Expr -> if(Expr, Expr, Expr)
SimpleSentence -> Noun Verb Object
Noun -> zoe | sam
Verb -> climb | push | throw
Object -> car | ladder | ball
Where:
lower case: terminal
upper case: non-terminal (variable)
Extend BTL with x = Expr, sequence
Simplifying: make both into expressions, adding:
Expr -> variable
Expr -> variable = Expr
Expr -> Expr ; Expr