Disambiguation and Layout-Sensitive Syntax
Eelco Visser
CS4200 | Compiler Construction | September 10, 2020 Disambiguation and Layout-Sensitive Syntax
Syntax Definition Summary Derivations - Generating sentences and trees from context-free grammars Ambiguity Declarative Disambiguation Rules - Associativity and priority Grammar Transformations - Eliminating ambiguity by transformation Layout-Sensitive Syntax - Disambiguation using layout constraints Structure Syntax = Structure
module structure let inc = function(x) { x + 1 } imports Common in inc(3) context-free start-symbols Exp end context-free syntax
Exp.Var = ID
Exp.Int = INT
Exp.Add = Exp "+" Exp Let( Exp.Fun = "function" "(" {ID ","}* ")" "{" Exp "}" [ Bnd( "inc" Exp.App = Exp "(" {Exp ","}* ")" , Fun(["x"], Add(Var("x"), Int("1"))) ) Exp.Let = "let" Bnd* "in" Exp "end" ] , App(Var("inc"), [Int("3")]) Bnd.Bnd = ID "=" Exp ) Token = Character
module structure let inc = function(x) { x + 1 } imports Common in inc(3) context-free start-symbols Exp end context-free syntax
Exp.Var = ID module Common Exp.Int = INT lexical syntax Exp.Add = Exp "+" Exp ID = [a-zA-Z] [a-zA-Z0-9]* Exp.Fun = "function" "(" {ID ","}* ")" "{" Exp "}" INT = [\-]? [0-9]+ Exp.App = Exp "(" {Exp ","}* ")"
Exp.Let = "let" Bnd* "in" Exp "end" Lexical Syntax = Context-Free Syntax Bnd.Bnd = ID "=" Exp (But we don’t care about structure of lexical syntax) Literal = Non-Terminal
module structure let inc = function(x) { x + 1 } imports Common in inc(3) context-free start-symbols Exp end context-free syntax syntax Exp.Var = ID "+" = [\43] Exp.Int = INT "function" = [\102] [\117] [\110] [\99] [\116] [\105] [\111] [\110] Exp.Add = Exp "+" Exp "{" = [\123] "}" = [\125] Exp.Fun = "function" "(" {ID ","}* ")" "{" Exp "}" "(" = [\40] "," = [\44] Exp.App = Exp "(" {Exp ","}* ")" ")" = [\41] "let" = [\108] [\101] [\116] Exp.Let = "let" Bnd* "in" Exp "end" "in" = [\105] [\110] "end" = [\101] [\110] [\100] Bnd.Bnd = ID "=" Exp "=" = [\61] Layout = Whitespace & Comments
module Common let inc = function(x) { x + 1 } lexical syntax in // function application LAYOUT = [\ \t\n\r] inc /* function position */ ( 3 // argument list LAYOUT = "/*" InsideComment* "*/" ) InsideComment = ~[\*] end InsideComment = CommentChar CommentChar = [\*]
LAYOUT = "//" ~[\n\r]* NewLineEOF NewLineEOF = [\n\r] NewLineEOF = EOF Layout = (Almost) Everywhere
module Common let inc = function(x) { x + 1 } lexical syntax in // function application LAYOUT = [\ \t\n\r] inc /* function position */ ( 3 // argument list LAYOUT = "/*" InsideComment* "*/" ) InsideComment = ~[\*] end InsideComment = CommentChar CommentChar = [\*]
LAYOUT = "//" ~[\n\r]* NewLineEOF NewLineEOF = [\n\r] NewLineEOF = EOF Exp.App = Exp "(" {Exp ","}* ")"
Exp-CF.App = Exp-CF LAYOUT?-CF "(" LAYOUT?-CF {Exp ","}*-CF LAYOUT?-CF ")" Extension Language Composition = Grammar Composition
module extension let imports functional query select = 1 context-free start-symbols Exp fs = select f from A where test f = select context-free syntax in Exp = Query print fs Cond = Exp
module functional imports Common context-free syntax Exp = <(
Exp.Add = <
Let( Exp.Fun = < [ Bnd( function(<{ID ","}*>){ "inc"
Exp.Let = < let
grammar start S non-terminals E T F terminals "+" "*" "(" ")" ID productions S = E E = E "+" T E = T T = T "*" F T = F F = "(" E ")" F = ID Abbreviated Grammar
grammar start S grammar non-terminals E T F productions terminals "+" "*" "(" ")" ID S = E productions E = E "+" T S = E E = T E = E "+" T T = T "*" F E = T T = F T = T "*" F F = "(" E ")" T = F F = ID F = "(" E ")" F = ID
Nonterminals, terminals can be derived from productions First production defines start symbol Notation
A, B, C: non-terminals l: terminals a, b, c: strings of non-terminals and terminals (alpha, beta, gamma in math) w, v: strings of terminal symbols Meta: Syntax of Grammars context-free syntax / grammars context-free syntax Grammar.Grammar = < grammar Start.Start = <