CS360: Mini Language Michael Conway Original slides by Mark Boady March 11, 2015
Mini language A simple programming language Implemented in PLY You will modify it for PA4
Grammar At the top level, we dene a program program -> stmt_list stmt_list -> stmt SEMICOLON stmt_list stmt stmt -> assign_stmt while_stmt if_stmt define_stmt
Grammar What kind of statements can be used to write a program? assign_stmt -> INDENT ASSIGNOP expr while_stmt -> WHILE expr DO stmt_list OD if_stmt -> IF expr THEN stmt_list ELSE stmt_list FI define_stmt -> DEFINE IDENT PROC LPAREN param_list RPAREN stmt_list END
Grammar Expressions are commands that can actually be evaluated expr -> expr PLUS term term expr -> expr MINUS term term term -> term TIMES fact fact fact -> NUMBER ( expr ) IDENT func_call Basic Elements are numbers or Variable Names Number -> [0-9]+ Ident -> [a-z]+ except reserved words
Grammar Function calls and denitions need inputs func_call -> IDENT LPAREN expr_list RPAREN expr_list -> expr COMMA expr_list expr param_list -> IDENT COMMA param_list IDENT
Parsing Instead of evaluating as we parse Step 1: Build a Parse Tree Step 2: Evaluate the Parse Tree def p_add( p ) : 'expr : expr PLUS term' p[0] = Plus( p[1], p[3] ) def p_assn( p ) : 'assign_stmt : IDENT ASSIGNOP expr' p[0] = AssignStmt( p[1], p[3] )
Parse Tree Parse Tree Objects Constructor Creates a Tree eval walks the tree and evaluates the expression Display is for debugging class Plus( Expr ) : def init ( self, lhs, rhs ) : self.lhs = lhs self.rhs = rhs def eval( self, nt, ft ) : return (self.lhs.eval( nt, ft ) + self.rhs.eval( nt, ft )) def display( self, nt, ft, depth=0 ) :...
Evaluating Evaluating the Parse Tree def eval( self, nt, ft ) : return (self.lhs.eval( nt, ft ) + self.rhs.eval( nt, ft )) nt - The name table, stores the names of variables and values ft - function table, stores the dened functions
Parse Tree Parse Tree for a := 4 * 7; b := a+5
Parse Tree Parse Tree for a := 4 * 7; b := a+5
Parse Tree Parse Tree for a := 4 * 7; b := a+5
Parse Tree Parse Tree for a := 4 * 7; b := a+5
Decorating the Tree Parse Tree for a := 4 * 7; b := a+5
Decorating the Tree Parse Tree for a := 4 * 7; b := a+5
Decorating the Tree Parse Tree for a := 4 * 7; b := a+5
Decorating the Tree Parse Tree for a := 4 * 7; b := a+5
Decorating the Tree Decorating the parse tree evaluates an expression In the Mini Language, we use the eval(nt,ft) function for this The Mini Language is interpreted, it parses straight to a result We can also use the parse tree to compile Now we see how the code could be decorated with assembly code
Decorating the Tree (ver. 2) Parse Tree for a := 4 * 7; b := a+5
Decorating the Tree (ver. 2) Parse Tree for a := 4 * 7; b := a+5
Decorating the Tree (ver. 2) Parse Tree for a := 4 * 7; b := a+5
Assembly Code Note: If you used g++ it would make 4*7 a constant 28. Ltmp4:.cfi_def_cfa_register %rbp movl $0, %eax movl $4, -4(%rbp) movl $7, -8(%rbp) movl -4(%rbp), %ecx imull -8(%rbp), %ecx movl %ecx, -12(%rbp) movl $5, -16(%rbp) movl -12(%rbp), %ecx addl -16(%rbp), %ecx movl %ecx, -20(%rbp) popq %rbp ret.cfi_endproc
An Example Program A program to nd the factorial of n n := 0-5; if n then i := n else i := 0 - n fi; fact := 1; while i do fact := fact * i; i := i - 1 od For while and if, false means <=0 and true means >0
Running a Program $ python interpreterext.py < TestInput/fact.p [42] Running Program Dump of Symbol Table Name Table i -> 0 fact -> 120 n -> -5 Function Table The program has no print command We can see the nal symbol table and look for answers
PA4 Mini Language Question of PA4 Add the comparison operators: < > <= >= ==!= Currently: true means > 0 false means <= 0 You can make the operators return 0 or 1 (4 < 5) returns 1 for true (5 == 7) return 0 for false
PA4 Add Lists expr : list_stuff list_stuff : LBRACKET expr_list RBRACKET list_stuff : LBRACKET RBRACKET list_stuff : CAR expr list_stuff : CDR expr list_stuff : CONS expr COMMA expr list_stuff : NULL expr
PA4 Problem 2 Base it on your code from PA3 Problem 3 Redo Problem 3 of PA2 in SICP Query Language Read the SICP Book You can base on the address database we saw in lab 4
Lab 6 Part 1: Add Exponent to Recursive Descent Parser Part 2: Complete Recursive Descent Parse for Reg Expr Part 3: Test the Mini Language
Lab 6 def expr(self): result=self.term() while self.token == '+': self.match('+',"+ expected") result +=self.term() return result def term(self): result = self.factor() while self.token == '*': self.match('*',"* Expected") result *= self.factor() return result
Lab 6 def factor(self): if self.token=="(": self.match("(","( Expected") result = self.expr() self.match(")",") Expected") else: result = self.number() return result def number(self): mystr="" while self.token.isdigit(): mystr+=self.token self.gettoken() return int(mystr)
Concat No Character to match on! def concat(self): #concat -> closure { closure} result = self.closure() while What do I match on: Do I need to match anything? result = RegConcat(result,self.closure()) return result What can be after the concat? letters a-z or a left parenthesis What can't be after concat? *,,), newline