Commit 11f01fa4 authored by matthew-eads's avatar matthew-eads
Browse files

added sample lexer

parent 4f153095
......@@ -7,11 +7,10 @@ data AST = Expression Exp
| TopLevlDec Dec
data Exp = SET ID Exp
| IF SimpleExp Exp Exp
| IF SimpleExp Exp (Maybe Exp)
| WHILE SimpleExp Exp
| BEGIN (List1 Exp)
| TUPY (Exp, Exp) (Maybe Potato)
| LET [(String, Exp)]
| LET [(String, Exp)] Exp
data BinOp = Plus SimpleExp SimpleExp
| Mult SimpleExp SimpleExp
......@@ -24,8 +23,8 @@ data SimpleExp =
| Var ID
| Apply ID (List0 Exp)
| BinOp BinOp
| AGet SimpleExp SimpleExp --(arr[i])
| ASet SimpleExp SimpleExp SimpleExp --(set arr[i] j)
| AMake ID SimpleExp --(new arr size)
-- | AGet SimpleExp SimpleExp --(arr[i])
-- | ASet SimpleExp SimpleExp SimpleExp --(set arr[i] j)
-- | AMake ID SimpleExp --(new arr size)
data Dec = Val ID Exp
This diff is collapsed.
{
module Main where
import Lexer
}
%name imp
%tokentype {Token}
%token
new { TokenBuiltin $$}
"check-expect" { TokenOp $$ }
"-" { TokenOp $$ }
"/" { TokenOp $$ }
"*" { TokenOp $$ }
"+" { TokenOp $$ }
if { TokenIf }
while { TokenWhile }
begin { TokenBegin }
set { TokenSet }
"(" { TokenLParen }
")" { TokenRParen }
int { TokenInt $$ }
var { TokenVar $$ }
String {TokenVar $$ }
Int {TokenInt $$}
ID {TokenVar $$ }
"@" { TokenOp $$ }
"!" { TokenOp $$ }
"$" { TokenOp $$ }
"%" { TokenOp $$ }
"^" { TokenOp $$ }
"&" { TokenOp $$ }
"_" { TokenOp $$ }
"`" { TokenOp $$ }
"~" { TokenOp $$ }
"=" { TokenOp $$ }
"[" { TokenOp $$ }
"]" { TokenOp $$ }
"{" { TokenOp $$ }
"}" { TokenOp $$ }
":" { TokenOp $$ }
";" { TokenOp $$ }
"<" { TokenOp $$ }
">" { TokenOp $$ }
"Plus" { TokenOp $$ }
"Mult" { TokenOp $$ }
"Div" { TokenOp $$ }
"Minus" { TokenOp $$ }
"CheckExpect" { TokenOp $$ }
Potato { TokenOp "potato" }
%error {parseError}
%%
Multi0AST: AST Multi0AST {$1:$2}
| {[]}
Multi1Exp: Exp Multi1Exp {$1:$2}
| Exp {[$1]}
ExpExp: Exp Exp {($1, $2)}
MaybePotato: Potato {Just $1}
| {Nothing}
StringExp: String Exp {($1, $2)}
Multi0StringExp: StringExp Multi0StringExp {$1:$2}
| {[]}
Multi0Exp: Exp Multi0Exp {$1:$2}
| {[]}
Prog: Multi0AST {Prog $1}
AST: Exp {Expression $1}
| Dec {TopLevlDec $1}
Exp: ID Exp {SET $1 $2}
| SimpleExp Exp Exp {IF $1 $2 $3}
| SimpleExp Exp {WHILE $1 $2}
| Multi1Exp {BEGIN $1}
| ExpExp MaybePotato {TUPY $1 $2}
| Multi0StringExp {LET $1}
BinOp: "Plus" SimpleExp SimpleExp {Plus $1 $2}
| "Mult" SimpleExp SimpleExp {Mult $1 $2}
| "Div" SimpleExp SimpleExp {Div $1 $2}
| "Minus" SimpleExp SimpleExp {Minus $1 $2}
| "CheckExpect" SimpleExp SimpleExp {CheckExpect $1 $2}
SimpleExp: Int {Lit $1}
| ID {Var $1}
| ID Multi0Exp {Apply $1 $2}
| BinOp {BinOp $1}
| SimpleExp SimpleExp {AGet $1 $2}
| SimpleExp SimpleExp SimpleExp {ASet $1 $2 $3}
| ID SimpleExp {AMake $1 $2}
Dec: ID Exp {Val $1 $2}
{
type ID = String
type Potato = String
data Prog = Prog [AST]
data AST = Expression Exp
| TopLevlDec Dec
data Exp = SET ID Exp
| IF SimpleExp Exp Exp
| WHILE SimpleExp Exp
| BEGIN (List1 Exp)
| TUPY (Exp, Exp) (Maybe Potato)
| LET [(String, Exp)]
data BinOp = Plus SimpleExp SimpleExp
| Mult SimpleExp SimpleExp
| Div SimpleExp SimpleExp
| Minus SimpleExp SimpleExp
| CheckExpect SimpleExp SimpleExp
data SimpleExp =
Lit Int
| Var ID
| Apply ID (List0 Exp)
| BinOp BinOp
| AGet SimpleExp SimpleExp --(arr[i])
| ASet SimpleExp SimpleExp SimpleExp --(set arr[i] j)
| AMake ID SimpleExp --(new arr size)
data Dec = Val ID Exp
main = do {
putStrLn "hello";
}
}
......@@ -13,8 +13,8 @@ executable gg-proto
main-is: AST.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall
build-depends: base, array, haskell-src-meta, haskell-src-exts,
haskell-src, template-haskell, regex-compat, containers, Unique,
split
haskell-src, template-haskell, regex-compat, containers,
Unique, split
default-language: Haskell2010
source-repository head
......
{
module Lexer where
}
%wrapper "basic"
$digit = 0-9
$alpha = [a-z]
$Alpha = [A-Z]
tokens :-
$white+ ;
set { \s -> TokenSet }
if { \s -> TokenIf }
while { \s -> TokenWhile }
begin { \s -> TokenBegin }
"new" { \s -> TokenBuiltin s }
"check-expect" { \s -> TokenOp s }
"-" { \s -> TokenOp s }
"/" { \s -> TokenOp s }
"*" { \s -> TokenOp s }
"+" { \s -> TokenOp s }
[\+\-\/\*] { \s -> TokenOp s}
[\(] { \s -> TokenLParen }
[\)] { \s -> TokenRParen }
$digit+ { \s -> TokenInt (read s) }
$alpha [$alpha $Alpha \_]* { \s -> TokenVar s}
. { \s -> TokenSym (head s)}
{
data Token = TokenSet
| TokenIf
| TokenWhile
| TokenBegin
| TokenSym Char
| TokenOp String
| TokenBuiltin String
| TokenPlus
| TokenMult
| TokenDiv
| TokenMinus
| TokenLParen
| TokenRParen
| TokenInt Int
| TokenVar String
deriving (Eq, Show)
}
# For more information, see: http://docs.haskellstack.org/en/stable/yaml_configuration.html
# Specifies the GHC version and set of packages available (e.g., lts-3.5, nightly-2015-09-21, ghc-7.10.2)
resolver: lts-5.4
# Local packages, usually specified by relative directory name
packages:
- '.'
- location:
git: https://github.com/matthew-eads/Unique.git
commit: f70dc0cecd5d0d2fad6b694aa378211fcc828a44
# Packages to be pulled from upstream that are not in the resolver (e.g., acme-missiles-0.3)
extra-deps: []
# Override default flag values for local packages and extra-deps
flags: {}
# Extra package databases containing global packages
extra-package-dbs: []
# Control whether we use the GHC we find on the path
# system-ghc: true
# Require a specific version of stack, using version ranges
# require-stack-version: -any # Default
# require-stack-version: >= 1.0.0
# Override the architecture used by stack, especially useful on Windows
# arch: i386
# arch: x86_64
# Extra directories used by stack for building
# extra-include-dirs: [/path/to/dir]
# extra-lib-dirs: [/path/to/dir]
# Allow a newer minor version of GHC than the snapshot specifies
# compiler-check: newer-minor
packages:
- '.'
extra-deps:
- Unique-0.4.4
- text-1.2.2.0
resolver: nightly-2016-03-11
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment