Section 3 with Remik (rz33@cornell.edu) Meeting #2 Notes ******************************************************** Administrivia: Assignment #1 due next week Grading scale: to be determined Review: Grammar CS211In interface and class Writing a compiler with CS211In Grammars for language specification *********************************** We use BNF to specify the language. BNF means Backus Naur Form, named after John Backus and Peter Naur. The 'language' resulted from private communication over the ALGOL programming language in 1958. They found it difficult to express their own interpretations of ALGOL, so they decided to create a general framework to describe a language. Toy Language Example: Exp ::= (Exp Op Exp) | INT | Op "ctr" Op ::= "+" | "++" INT ::= "0" | "-1" | "+1" ... All the symbols to the left of ::= are called categories, while literals are enclosed by quotes. The last item is a prefix incrementer. Basically it has the side effect of incrementing the variable by 1 and returns the new value. CS211In Interface ***************** constants: INTEGER, WORD, OPERATOR methods: int peekAtKind() int getInt() String getWord() bool check(char) bool check(String) Compiler writing with CS211In ************************************************************* Here is a compiler for the language above (no error handling). String getExp(CS211In f) { switch( f.peekAtKind() ) { case OPERATOR: { if (f.check("+")) { // check for prefix operator if (f.check("+")) { // look ahead for our 1 variable ctr if (f.check("ctr")) { // increment, store and make avaible for assignment return "PUSHOFF 1\n PUSHIMM 1\n" + "ADD\n STOREOFF 1\n PUSHOFF 1\n"; } else { // otherwise it is just an expression String s1 = f.getExp(f); return s1 + "PUSHIMM 1\n ADD\n"; } } else { // addition operator String s1 = f.getExp(f); return s1 + "ADD\n"; } } case WORD: // handle variables ... } }