Compiler Construction E Syntactic Sugar E

Compiler Construction E Syntactic Sugar E

Compiler Construction e Syntactic sugar E Compiler Construction Syntactic sugar 1 / 16 Syntactic sugar & Desugaring Syntactic Sugar Additions to a language to make it easier to read or write, but that do not change the expressiveness Desugaring Higher-level features that can be decomposed into language core of essential constructs ) This process is called ”desugaring”. Compiler Construction Syntactic sugar 2 / 16 Pros & Cons for syntactic sugar Pros More readable, More writable Express things more elegantly Cons Adds bloat to the languages Syntactic sugar can affect the formal structure of a language Compiler Construction Syntactic sugar 3 / 16 Syntactic Sugar in Lambda-Calculus The term ”syntactic sugar” was coined by Peter J. Landin in 1964, while describing an ALGOL-like language that was defined in term of lambda-calculus ) goal: replace λ by where Curryfication λxy:e ) λx:(λy:e) Local variables let x = e1 in e2 ) (λx:e2):e1 Compiler Construction Syntactic sugar 4 / 16 List Comprehension in Haskell qs [] = [] qs (x:xs) = qs lt_x ++ [x] ++ qs ge_x where lt_x = [y | y <- xs, y < x] ge_x = [y | y <- xs, x <= y] Compiler Construction Syntactic sugar 5 / 16 List Comprehension in Haskell Sugared [(x,y) | x <- [1 .. 6], y <- [1 .. x], x+y < 10] Desugared filter p (concat (map (\ x -> map (\ y -> (x,y)) [1..x]) [1..6] ) ) where p (x,y) = x+y < 10 Compiler Construction Syntactic sugar 6 / 16 Interferences with error messages ”true” | 42 standard input:1.1-6: type mismatch condition type: string expected type: int function _main() = ( (if "true" then 1 else (42 <> 0)); () ) Compiler Construction Syntactic sugar 7 / 16 Regular Unary - & and | Beware of ( exp ) vs. ( exps ) Extra for ?: as in GNU C (a ?: b) where Function overload Sugar in Tiger Light if then Compiler Construction Syntactic sugar 8 / 16 Extra for ?: as in GNU C (a ?: b) where Function overload Sugar in Tiger Light if then Regular Unary - & and | Beware of ( exp ) vs. ( exps ) Compiler Construction Syntactic sugar 8 / 16 Sugar in Tiger Light if then Regular Unary - & and | Beware of ( exp ) vs. ( exps ) Extra for ?: as in GNU C (a ?: b) where Function overload Compiler Construction Syntactic sugar 8 / 16 Dangerous Desugaring Suppose we want to introduce an in-bound operator α ≤ β ≤ γ Naive translation let in if α ≤ β & β ≤ γ then 1 else 0 end Compiler Construction Syntactic sugar 9 / 16 Dangerous Desugaring Another translation let var _beta := β in if α ≤ _beta & _beta ≤ γ then 1 else 0 end Compiler Construction Syntactic sugar 10 / 16 Dangerous Desugaring Another (another) translation let var _alpha := α var _beta := β var _gamma := γ in if _alpha ≤ _beta & _beta ≤ _gamma then 1 else 0 end Compiler Construction Syntactic sugar 11 / 16 Dangerous Desugaring Final (and correct) translation let var _alpha := α var _beta := β in if _alpha ≤ _beta then let var _gamma := γ in if _beta ≤ _gamma then 1 else 0 else 0 end Compiler Construction Syntactic sugar 12 / 16 Basic desugaring 1 Walk the AST using a visitor 2 Focus on the type of node to be replaced 3 Build new ub-AST 4 Replace the nodes (and associated sub-trees) by the new sub-AST Compiler Construction Syntactic sugar 13 / 16 Tweasts Text With Embedded AST Idea: Is it possible to desugar directly inside of the parser Advantages: Reduce the number of AST classes Avoid many desugaring traversals Desugaring in concrete syntax Compiler Construction Syntactic sugar 14 / 16 Desugaring in Concrete Syntax exp: exp "&" exp { $$ = parse::parse(parse::Tweast() << "if " << $1 << " then " << $3 << "<> 0 else 0"); } Desugaring Desugaring in Abstract Syntax exp: exp "&" exp { $$ = new IfExp(@$, $1, new OpExp(@$,$3, OpExp::ne, new IntExp(@2, 0)), new IntExp(@2, 0)); } Compiler Construction Syntactic sugar 15 / 16 Desugaring Desugaring in Abstract Syntax exp: exp "&" exp { $$ = new IfExp(@$, $1, new OpExp(@$,$3, OpExp::ne, new IntExp(@2, 0)), new IntExp(@2, 0)); } Desugaring in Concrete Syntax exp: exp "&" exp { $$ = parse::parse(parse::Tweast() << "if " << $1 << " then " << $3 << "<> 0 else 0"); } Compiler Construction Syntactic sugar 15 / 16 Summary Syntactic Desugaring Sugar Tweast Compiler Construction Syntactic sugar 16 / 16.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    19 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