1 / 13

Simple Language (SL)

Simple Language (SL). Start version 00. Content. SL, the language AG, attribute grammar system First version of SL compiler written in AG. SL examples. let fac :: Int -&gt; Int = <br> -&gt; if n &gt; 0 then n * fac (n-1) else 1

layne
Download Presentation

Simple Language (SL)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Simple Language (SL) Start version 00

  2. Content • SL, the language • AG, attribute grammar system • First version of SL compiler written in AG IPT - SL

  3. SL examples let fac :: Int -> Int = \n -> if n > 0 then n * fac (n-1) else 1 fi in fac 6 ni let fac :: Int -> Int = \n -> let cnt :: Ref Int = new 2 ; res :: Ref Int = new 1 in while ( cnt <= n ) do res := res * cnt ; cnt := cnt + 1 od ; res ni in fac 6 ni IPT - SL

  4. SL features • Expression evaluation • Int, Bool, Array, Product, Data, Ref • Assignment • Let (blocks), Lambda (functions) • If, While, Case • Typechecking IPT - SL

  5. SL first version • Integer constant • Generating: listing + code 55 .sl 55 stdout LDC 55 TRAP 0 HALT .ssm IPT - SL

  6. AG: SL parser • Parser combinators + scanner pRoot = sem_Root_Root <$> pExpr pExpr = (sem_Expr_Intexpr . string2int) <$> pInteger string2int = foldl (\val dig -> (10*val + ord dig -ord '0')) 0 • Semantic functions? IPT - SL

  7. AG: SL abstract syntax • Abstract tree representation DATA Root | Root Expr DATA Expr | Intexpr Int Root Root Expr Intexpr 55 tree node tree node type tree node variant IPT - SL

  8. AG: SL aspects (attributes) • Pretty printed output & stack code Root Root pp synthesized attribute copied Expr Intexpr 55 pp text.show $ int • Described by ATTR Root [ || ppexpr: PP_Doc ] SEM Root | Root LHS . ppexpr = "expr_pp" SEM Expr [ || pp: PP_Doc ] | Intexpr LHS . pp = "text.show $ int" IPT - SL

  9. AG notation inherited  inh+synth  synthesized  Attribute type (Hugs or Node) ATTR Root [ | | ppexpr: PP_Doc ] SEM Root | Root LHS . ppexpr = "expr_pp" SEM Expr [ | | pp: PP_Doc ] | Intexpr LHS . pp = "text.show $ int" Attribute name Attribution (Hugs) text for definition, specific for:Node type, Node variant, Attribute, Direction IPT - SL

  10. AG compilation • Generates Haskell • Generate SL.hs from SL.ag compile "SL" allc • Datastructures, semantics, ... data Expr = Expr_Intexpr Int deriving Show -- semantic domains type T_Expr = (SCode,PP_Doc) -- catas sem_Expr (Expr_Intexpr int) = sem_Expr_Intexpr int -- funcs sem_Expr_Intexpr ::Int -> T_Expr sem_Expr_Intexpr int = ( ((S_LDC int:)), (text.show $ int) ) IPT - SL

  11. Scanner • Transforms character sequences to tokens data TokenType = TkSymbol | TkVarid | TkConid | TkKeyword | TkOp | TkString | TkCharb| TkInteger8 | TkInteger10 | TkInteger16 | TkTextnm | TkTextln | TkError deriving (Eq, Ord) type Linenumber = Int type Filename = String newtype Token = Tok (TokenType, String, String, Linenumber, Filename) IPT - SL

  12. Code Generation • For a Simple Stack Machine SEM Root [ || ppscode: PP_Doc ] | Root LHS . ppscode = "( vlist . map (text.textOfS)" ". expr_scode" ". (S_TRAP 0:)" ". (S_HALT:)" "$ []" ")" SEM Expr [ || scode: SCode ] | Intexpr LHS . scode = "(S_LDC int:)" DATA S | HALT | TRAP Int | LDC Int IPT - SL

  13. Simple Stack Machine • Memory (Code, Stack) + Registers + Stack manipulating instructions IPT - SL

More Related