Closed mikusp closed 10 years ago
Any questions/ideas?
program = { expression } ;
expression = constant
| variable
| "'" expression
| list
| dotted list ;
constant = boolean
| number
| character
| string ;
boolean = "#t"
| "#f" ;
number = [ sign ], ( integer | floating ) ;
integer = digit, { digit } ;
sign = "-"
| "+" ;
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
floating = digit, { digit }, ".", digit , { digit } ;
character = "#\", any character ;
any character = ? all possible characters ? ;
string = """, { string character }, """ ;
string character = "\""
| "\\"
| any character - ( """ | "\" ) ;
variable = identifier ;
identifier = ( letter | symbol ), { ( letter | digit | symbol ) } ;
letter = "a" | "b" | ... | "z" | "A" | "B" | ... | "Z" ;
symbol = "!" | "$" | "%" | "&" | "*" | "/" | ":"
| "<" | "=" | ">" | "?" | "~" | "_" | "^"
| "." | "+" | "-" ;
list = "(", { expression }, ")" ;
dotted list = "(", expression, { expression }, ".", expression, ")" ;
Done
My initial idea was to use use a BNFC. It requires a not-so-simple input file containing grammar that is basically BNF but using some extensions, labels, built-in priorities of operators and what-not. I came with something like this (based on http://www.scheme.com/tspl2d/grammar.html)
I'm afraid that for a dynamically typed language, resulting AST will be too much type-safe to do anything useful easily. Instead, building a custom parser (like in http://hackage.haskell.org/package/husk-scheme-3.17/docs/src/Language-Scheme-Parser.html#lispDef) could be better if we trade more complicated grammar for simplicity. In "Write yourself a Scheme" every type is a "subclass" of LispVal - atoms, numbers, strings, lists of LispVals, lambdas, etc. If we stick to this representation, we would only really have to implement a HUGE eval function that evaluates given LispVal.
tl;dr Should we stick to more complicated, real life grammar or just a bare minimum needed to evaluate given Scheme code?