Grammars R Egular Expressions Operational Semantics Revisited C a Se Study: Microc S Y N Tax S E Mantics in Terpreter S O You Want to Design a Language

Grammars R Egular Expressions Operational Semantics Revisited C a Se Study: Microc S Y N Tax S E Mantics in Terpreter S O You Want to Design a Language

A fe w n o te s o n g ra m m a rs , la n g u a g e s p e c ific a tio n s , a n d in te rp re te rs K e u n w o o L e e C S E 3 4 1 : P ro g ra m m in g L a n g u a g e s U n iv e rs ity o f W a s h in g to n W in te r 2 0 0 4 Outline Language syntax B ! grammars " egular expressions Operational semantics revisited C a se study: MicroC S y n tax S e mantics In terpreter S o you want to design a language... What do you do? Specify syntax Specify semantics r ite a prototype interpreter Goal of this lecture-and-a-half: whirlwind overview of eac! step, and conceptual tools Syntax: terminology a la n g uage is a subset of the finite-length strings over some alphabet of symbols a g r a mmar s%ecifies which strings belong in teh alphabet a p a r ser turns a string in a language into a more stru)tured representation (usually a syntax tree, relevant to that language & ackus$Naur form n amed for John &ackus * Peter (aur U sed to specify context-free grammars (correspond to context-free languages, C !-s have four %arts: 1. terminals: "atomic. textual entities, e.g. identifiers/ keywords/ #iterals 2. nonterminals: .structured. textual entities, e.g. if/then/else or val *eclarations 3. productions: rules for &uilding nonterminals 4. a unique 1start1 production Productions o nterminal on left/ sequence of terminals and nonterminals on right/ separated by ::= ifExpr ::4 if expr then expr else expr May have cases separated by bars: expr ::= binOpExpr 6 unaryOpExpr 6 ifExpr 6 000 Example constLiteral ::= &OOL_LITERAL 6 INT_LITERAL expr ::= constLiteral 6 IDENTIFIE5 6 expr 8 expr 6 not expr 6 expr 9 expr 6 let decl in expr end 6 if expr then expr else expr 6 : expr ; decl ::= val ID2(TIFIE5 9 expr & (7 1alpha%et"" & ( 7 g r a mmar alphabet = terminals 9 tokens, not individual characters C a n s p ecify format of tokens using &(7 too, but cumbersome < s u ally use regular expressions instead 5 egular expressions r e g exp 9 an expression that may "match" a string; recursively defined as: base case: a character7 a matches "a" indu)tive cases: concatenation: aa matches "aa" r epetition :>leene star;: a* matches "a", .aaaa", .aaaaaaaa", .. u n ion (alternation;: a|b matches "a" or .& Inductive cases may use parens to enforce order of operators 5 egexp examples (a*b)|c matches: 1%1, 1c", 1aaaab" a((b*c*)|d*) matches: "a", 1abb%bc", 1ac", "ab%%cccccc", "ad1 a|b|c|d matches: "a", 1%1, 1c1, 1d1 5 2 syntactic sugars [A&C627#@ = (AA&ACA6A2A7A#; [A$B@ 9 [A&C....XYB@ A " 9 1 optional A1 e.g., AB:C 4 (AC|ABC, A 8 = "one or more A1 e.g., 9;B = 99<B . 9 1 a n y character1 e.g., 0< 4(.any string/ including empty string. [^A@ = "Any character %ut A.1 5 egexps for tokens Integer literals: IN42#25 9 [0-9]+ Identifiers: ID2(TI725 9 [A-Za-z][A-Za-z0-9]+ > e ywords (easy): IF 9 if 4F2( 9 then Extended B(7 Sequences: tupleExpr ::= : expr* ; 6 e lim ited sequences: tupleExpr ::4 : expr* = , ; A t-least-one sequences: tupleExpr ::= : expr , expr;= , ; Optional sequences: valDecl ::= val I62(TI7IER 4 expr > = ? Parsing, lexing Lexer: string $> token stream Parser: token stream $G syntax tree U sually build using lexer generator and parser generator: lexer gen and parser gen can take syntax spe)ification and automatically generate code for each bison, yacc/ @ava))/ sablecc, ml-yacc Semantics revisited 5 eview: 1Language Construct X in a Nutshell" Syntax: if expr1 then expr2 else expr3 6 ynamic semantics eva# expr1 to v if v is true, eval expr2 to v2 and return else eval expr3 to v3 and return Static semantics constraints: expr1 boolean/ expr2 and expr3 agree result type: type of expr2 and expr3 7 ormal notation +*ynamic semanti)s only) F omework 3 eval... | evalExp env (8f (e/ e1/ e2,,(4 case evalExp env e of Integer i 4A raise Fai# .Bype error: non-boolean used for test." | Boolean b => if b then evalExp env e1 else evalExp env e2 Summary One way to do language design: S pecify syntax U s e BN!/ regexps S pecify semantics U s e operational semantics (in English, or in , Implement prototype interpreter You basically 1now how000 O perational semantics of language closely related to actual code for interpreterH Break: ML project overview C ase study: MicroC program ::9 d eclJ stmtJ decl ::= typeName I62(TI7I25 ?9 expr@ = typeName ::= int A flo at stmt ::= expr = A if : expr ; stmt else stmt A while : expr ; stmt A K stmt*L= M expr ::= I(43LI42RAL A 7LOAT3LI425AL A I62(4I7I25 A I62(4I7I25 9 expr A expr + expr A expr == expr A MicroC program int x 9 $,N= int y 9 N= while (x = x + 1) { y 9 y 8 x= M M icroC semantics We'll just do dynamic semantics of assignment exprs: IDEN4IFIER = expr If an expression e evaluates to a value v in an environment Env/ then the expression varName 4 e evaluates to v and %roduces the updated environment Env', where Env' is Env with +varName, v) substituted for the old binding of varName U seful insight: expressions produce both value and new environment (due to side effectsD, Interpreter HOWTO 1. Eata type for each syntactic form 2. Lexer & parser 3. Eata type for values and environments 4. 8mplement dynamic semantics for ea)h syntacti) form G0 +For statically type* #anguages) implement static semanti)s of each syntactic form Syntax datatypes datatype ex%r 4 8ntEx%r of int ex%r ::4(8 B_LITE"9L | FloatE$%r of rea# 6 FLI9BHL8BE"9L | AssignE$%r of string < e$%r 6 8DE B8!8E" | VarE$%r of string 6 8DE B8!8E" 4 e$%r 6(9**E$%r of e$%r < e$%r 6 e$%r ; e$%r | EqE$%r of e$%r < e$%r 6 e$%r 44 e$%r datatype stm t 4 EvalStmt of ex%r stm t ::= e$%r 7 6(8fS tm t of e$%r < stm t < stmt 6 if + e $%r , stmt else stmt | WhileStm t of e$%r < stm t 6 while + e$%r , stmt | Blo)kStmt of stmt lis t 6 J stm t<=7 K datatype typeName 4 TInt | TFloat datatype de)# 4 De)# of typeName * strin g < e $%r datatype program 4 Program of *e)# lis t < stmt list Values and envs. datatype value 9 IntVal of int | FloatPal of real type environment 9 :string * value) list :J returns the value bound to name in e *) fun loo'upEnv(e:environment, name:string) = ... :J returns the environment e with nam e's binding updated to newPalue J; fun update2nv(e:environment, name:string, newValue:value) = ... Evaluator (exprs) e valExpr has type: environment * expr $G value * environment fun evalExpr(env, IntExpr i) 4 +IntVal i, env) | evalExpr(env, VarExpr s,(4 #ookupEnv(env, s, | evalExpr(env, AssignExpr(id, ex%,, 4 #et val (v/ newEnv,(4 evalExpr(env, ex%, in +v, updateEnv(newEnv/ id, v,, end 6(000 Evaluator (stmts) evalStmt has type: environment * stmt $G environment fun evalStmt(env/ E va#S tm t e, 4 #et va# +H/ newEnv, 4 evalE$%r(env, e, in newEnv en* | evalStmt(env, 8fStm t(e/ s1/ s2)) 4 #et va# (v, newEnv) 4 evalEx%r(env/ e, va# v8sZero 4 case v of 8ntVa# i => i 4 0 6 FloatLa# f 4A f N4 0.0 an*a#so f A4 000 in if vIsZero then evalStmt(newEnv/ s1, else evalStmt(new Env/ s2, en* 6(000 Evaluator (decls) e v alEecl has type: environment J decl $G environment fun evalDe)#+env, Ee)#(t/ name/ ex%,, 4 let va# (v, newEnv) = evalExpr(env, exp) in updateEnv(newEnv/ name, v) en* Evaluator (program) fun eval(Program(decls, stmts)) = #et fun evalDeclList(env, nil) = env 6 evalDeclList(env, *::*s) = evalEeclList(evalDecl(env, *,/ ds) fun evalStmtList+env, nil, = env 6 evalStmtList(env, s::ss) = evalStmtList(evalStmt(env, s)/ ss) val *eclsEnv = evalDeclList+nil/ decls) in evalStmtList+*eclsEnv, stmts) end.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    29 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us