Skip to content

Instantly share code, notes, and snippets.

/Banjo.sdf Secret

Created December 16, 2012 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/65339e3999325fa02e77 to your computer and use it in GitHub Desktop.
Save anonymous/65339e3999325fa02e77 to your computer and use it in GitHub Desktop.
The following grammar shows abiguities where both sides of the ambiguity are equal. Very strange.
module Banjo
hiddens
%% Start symbol
context-free start-symbols
TranslationUnit
context-free syntax
Expr -> TranslationUnit
exports
lexical syntax
-> EOF
%% [a-zA-Z\_][a-zA-Z0-9\_]*[\']* -> ID
[a-zA-Z\_\255][a-zA-Z0-9\_\255\254]*[\']* -> ID
"_" ~[\_]* "_" -> ID
[0-9]+ -> INT
"\"" StringChar* "\"" -> STRING
~[\"\n] -> StringChar
"\\\"" -> StringChar
BackSlashChar -> StringChar
"\\" -> BackSlashChar
"," ? -> OptComma
";" ? -> OptSemi
":" ? -> OptColon
"*" -> BINOP
"/" -> BINOP
"+" -> BINOP
"-" -> BINOP
"%" -> BINOP
"^" -> BINOP
"&" -> BINOP
"|" -> BINOP
"->" -> BINOP
"<" -> BINOP
">" -> BINOP
"<=" -> BINOP
">=" -> BINOP
"==" -> BINOP
"!=" -> BINOP
"~=" -> BINOP
"<=>" -> BINOP
"<:" -> BINOP
"?" -> BINOP
":" -> BINOP
"!" -> UNARYOP
"~" -> UNARYOP
[\ \t\n\r] -> LAYOUT
[\*] -> CommentChar
"/*" (~[\*] | CommentChar)* "*/" -> LAYOUT
"//" ~[\n\r]* ([\n\r] | EOF) -> LAYOUT
FloatDigits ExponentPart? [fFdD]? -> FloatLiteral
[0-9]* "." [0-9]* -> FloatDigits
[0-9]+ -> FloatDigits
[0-9]+ -> FloatLiteral {reject}
"." -> FloatDigits {reject}
[\+\-]? [0-9]+ -> SignedInteger
[eE] SignedInteger -> ExponentPart
lexical restrictions
%% Ensure greedy matching
CommentChar -/- [\/]
INT -/- [0-9]
ID -/- [a-zA-Z0-9\_\255\254\']
UNARYOP -/- [\*\/\+\-\%\|\^\<\>\=\!\:\~]
BINOP -/- [\*\/\+\-\%\|\^\<\>\=\!\:\~]
FloatLiteral -/- [0-9\.eE]
%% EOF may not be followed by any char
EOF -/- ~[]
%% Backslash chars in strings may not be followed by "
BackSlashChar -/- [\"]
%% Ensure greedy matching for comments
LAYOUT? -/- [\ \t\n\r]
LAYOUT? -/- [\/].[\/]
LAYOUT? -/- [\/].[\*]
%% Arithmetic Expressions
context-free syntax
ID -> Id{"Id"}
Id -> Expr
STRING -> Literal{"String"}
INT -> Literal{"Int"}
FloatLiteral -> Literal{"Float"}
"true" -> Literal{"True"}
"false" -> Literal{"False"}
Literal -> Expr
"(" Expr ")" -> Expr{bracket}
sequence
Expr "." ID -> Expr{"FieldRef"}
Expr "[" Expr "]" -> Expr{"Get"}
"-" Expr -> Expr{"Negate"}
"!" Expr -> Expr{"Not"}
"~" Expr -> Expr{"Complement"}
"=>" Expr -> Expr{"LazyValue"}
"->" Expr -> Expr{"NullaryArrow"}
Expr "*" Expr -> Expr{"Product", left}
Expr "/" Expr -> Expr{"Quotient", left}
Expr "%" Expr -> Expr{"Mod", left}
Expr "+" Expr -> Expr{"Sum", left}
Expr "-" Expr -> Expr{"Difference", left}
Expr "&" Expr -> Expr{"Intersection", left}
Expr "^" Expr -> Expr{"LogicalNotEq", left}
Expr "|" Expr -> Expr{"Union", left}
Expr "<=" Expr -> Expr{"LessOrEqual", non-assoc}
Expr ">=" Expr -> Expr{"GreaterOrEqual", non-assoc}
Expr "<" Expr -> Expr{"LessThan", non-assoc}
Expr ">" Expr -> Expr{"GreaterThan", non-assoc}
Expr "==" Expr -> Expr{"EqualTo", non-assoc}
Expr "!=" Expr -> Expr{"NotEqualTo", non-assoc}
Expr "<=>" Expr -> Expr{"Cmp", non-assoc}
Expr "?" Expr ":" Expr -> Expr{"Cond"}
context-free priorities
{ left: Expr "." ID -> Expr
Expr "(" { Expr "," }* ")" -> Expr } >
{ Object -> Expr } >
{ Expr "?" -> Expr } >
{ "-" Expr -> Expr
"!" Expr -> Expr
"~" Expr -> Expr
"=>" Expr -> Expr
"->" Expr -> Expr } >
{ left: Expr "*" Expr -> Expr
Expr "/" Expr -> Expr
Expr "%" Expr -> Expr } >
{ left: Expr "+" Expr -> Expr
Expr "-" Expr -> Expr } >
{ left: Expr "<" Expr -> Expr
Expr ">" Expr -> Expr
Expr "<=" Expr -> Expr
Expr ">=" Expr -> Expr
Expr "!=" Expr -> Expr
Expr "==" Expr -> Expr } >
{ left: Expr "&" Expr -> Expr } >
{ left: Expr "^" Expr -> Expr } >
{ left: Expr "|" Expr -> Expr } >
{ Expr "?" Expr ":" Expr -> Expr
"if" Expr "then" Expr "else" Expr -> Expr } >
{ ID "->" Expr -> Expr } >
{ left: Def+ Expr -> Expr }
%% Lambda
context-free syntax
ID "->" Expr -> Expr{"Lambda"}
%% Call
context-free syntax
Expr "(" { Expr "," }* ")" -> Expr{"Call"}
%% Function / method definitions
context-free syntax
-> OptDefaultValue{"NoDefaultValue"}
"=" Expr -> OptDefaultValue{"DefaultValue"}
ID OptContract OptDefaultValue -> Param{"Param"}
Def+ Expr -> Expr{"Let",left}
":" Expr -> OptContract{"SomeContract"}
":" "*" -> OptContract{"AsIs"}
"" -> OptContract{"DefaultContract"}
ID "(" {Param ","}* ")" OptContract "=" Expr -> Def{"Fun"}
ID OptContract "=" Expr -> Def{"Var"}
context-free syntax
ID OptContract "=" Expr -> ObjectElement{"FieldDef"}
ID "(" {Param ","}* ")" OptContract "=" Expr -> ObjectElement{"MethodDef"}
"{" "}" -> Object{"EmptyObject"}
"{" ObjectElement+ "}" -> Object{"Object"}
Object -> Expr
Let(
[ amb(
[ Var("seven", DefaultContract(), Int("7"))
, Var("seven", DefaultContract(), Int("7"))
]
)
]
, Id("seven")
)
seven = 7 seven
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment