LARA

Abstract Syntax for While

Abstract Syntax = concise representation of program as a data structure

  • ignores concrete symbols (whether we use “{” or “begin” for blocks)
  • maps easily to Scala's case classes

For our While language:

program ::= statement
statement ::= print | read | assign | if | while | block
print ::= string var
assign ::= var expr
if ::= exp statement (statement)?
while ::= expr statement
block ::= statement*
expr ::= var | literal | expr binOp expr | unOp expr

Mapping abstract syntax to Scala:

  • one class for each syntactic category (program,statement,read,write,…)
  • for class with multiple alternatives:
    • make class abstract
    • create a subclass for each alternative
  • each component of the alternative becomes an argument (field) of the case class
abstract class Statement
case class IfStatement(cond : Exp, 
                       tru : Statement, 
                       els : Option[Statement])
     extends Statement
...

Next: specify concrete textual representation

  • tokens (“while”, “if”, “==”, “<”, …)
  • concrete syntax (connects these tokens and describes valid grammar)