All pastes #30741 Raw Edit

tormen

public text v1 · immutable
#30741 ·published 2005-11-23 22:19 UTC
rendered paste body

%wrapper "posn"

tokens :-
  $white+                               ;
  "program"                             { tok (\p s -> Program     p) }
  "var"                                 { tok (\p s -> Var         p) }
  "print"                               { tok (\p s -> Print       p) }
  "if"                                  { tok (\p s -> If          p) }
  "then"                                { tok (\p s -> Then        p) }
  "else"                                { tok (\p s -> Else        p) }
  "do"                                  { tok (\p s -> Do          p) }
  "while"                               { tok (\p s -> While       p) }

  "int" | "bool"                        { tok (\p s -> Type        p s) }
  "True" | "False"                      { tok (\p s -> BoolVal     p s) }

  [a-zA-Z] ( [a-zA-Z0-9] )*             { tok (\p s -> Variable    p s) }
  [ \+ \- ]? ( 0 | [1-9] [0-9]* )       { tok (\p s -> NumVal      p s) }
  [ \+ \- \* \/ ]                       { tok (\p s -> ArithOp     p s) }
  \= | \!\= | \< | \<\= | \> | \>\=     { tok (\p s -> RelOp       p s) }

  [ \{ ]                                { tok (\p s -> CurlyBeg    p) } 
  [ \} ]                                { tok (\p s -> CurlyEnd    p) }
  [ \( ]                                { tok (\p s -> ParBeg      p) }
  [ \) ]                                { tok (\p s -> ParEnd      p) }

  ":="                                  { tok (\p s -> Becomes     p) }
  [ \; ]                                { tok (\p s -> Sem         p) }
  [ \: ]                                { tok (\p s -> Col         p) }
  [ \, ]                                { tok (\p s -> Comma       p) }

{

-- Some action helpers:
tok f (AlexPn posInStr line column) s = f line s

data Token =
  Program     Int |
  Var         Int |
  Print       Int |
  If          Int |
  Then        Int |
  Else        Int |
  Do          Int |
  While       Int |
  Type        Int String |
  BoolVal     Int String |
  Variable    Int String |
  NumVal      Int String |
  ArithOp     Int String |
  RelOp       Int String |
  CurlyBeg    Int |
  CurlyEnd    Int |
  ParBeg      Int |
  ParEnd      Int |
  Becomes     Int |
  Sem         Int |
  Col         Int |
  Comma       Int
    deriving ( Eq , Show )

scan :: String -> [Token]
scan str = alexScanTokens str

testprg :: String
testprg =
  "program Fakultaet {" ++
  "   var x,y:int; bool=bool ;" ++
  "   bool:=False;" ++
  "   x:=3; y:=1;" ++
  "   while ( x > 0) do " ++
  "       y := y*x;" ++
  "       bool:=True;" ++
  "       x:=x+1" ++
  "   ;" ++
  "   if bool = False then y = 0 ;" ++
  "   print y" ++
  "}"

}