New ideas.

2008年4月6日星期日

Parser Grammar in EBNF


Program -> ModuleDef ... +> Program

ModuleDef -> module Qualident where [Import] [ModuleMember ...] end module +> ModuleDef

Import -> import Qualident /','... ';' +> Import

ModuleMember -> TypeMember
-> TypeClassDef

TypeClassDef -> TypeDef
-> ClassDef

TypeMember -> Constant
-> Variable
-> Accessor
-> Function
-> Constructor

Attribute -> '[' ']' +> Attribute
Attributes -> Attribute... +> Attributes

Constant -> [Attributes] def [':' TypeExpr] '=' Expr ';' +> Constant

Variable -> [Attributes] var ':' TypeExpr [ '=' Expr ] ';' +> Variable

Accessor -> [Attributes] def ':' TypeExpr where get CodeBlock [set CodeBlock] end def +> Accessor
-> [Attributes] def '[' Fields ']' ':' TypeExpr where get CodeBlock [set CodeBlock] end def +> Accessor

Function -> [Attributes] def [TypeParams] ':' FunctionType ';' +> Function
-> [Attributes] def [TypeParams] ':' FunctionType where CodeBlock end def +> Function
-> [Attributes] def '(' ')' ':' FunctionType ';' +> Operator
-> [Attributes] def '(' ')' ':' FunctionType where CodeBlock end def +> Operator

Constructor -> [Attributes] def [TypeParams] TupleType ';' +> Constructor
-> [Attributes] def [TypeParams] TupleType where CodeBlock end def +> Constructor

TypeDef -> [Attributes] type '=' '{' /','... '}' ';' +> EnumType
-> [Attributes] type [TypeParams]'=' TypeExpr [TypeClass] ';' +> TypeDef
-> [Attributes] type [TypeParams]['=' TypeExpr] [TypeClass] where [TypeMember ...] end type +> TypeDef

ClassDef -> [Attributes] class [TypeParams] [extend ] where [TypeMember ...] end class +> ClassDef

TypeParams -> '{' /','...'}' +> TypeParams

TypeArgs -> '{'TypeExpr /','...'}' +> TypeArgs

TypeClass -> belongto /','... +> TypeClass

TypeExpr -> +> AtomType
-> TupleType
-> GenericType
-> FunctionType
-> TypeExpr '' TypeExpr +> UnionType

TupleType -> '(' [Fields] ')' +> TupleType

GenericType -> TypeArgs +> GenericType

Fields -> FieldType / ','... +> Fields

FieldType -> ':' TypeExpr +> FieldType

FunctionType -> TupleType '->' TupleType +> FunctionType

CodeBlock -> [Statement ...] +> CodeBlock

Statement -> SimpleStmt
-> CompoundStmt

SimpleStmt -> LocalVar
-> Assignment
-> MultiAssignment
-> CallFunction
-> ReturnStmt
-> PassStmt
-> ExitStmt
-> NextStmt
-> UntilStmt

LocalVar -> var [':' TypeExpr] '=' Expr ';' +> LocalVar

Assignment -> Assignable '=' Expr ';' +> Assignment

MultiAssignment ~> '(' Assignable/','... ')' '=' Expr ';' +> MultiAssignment

CallFunction -> FunctionCall ';' +> CallFunction

ReturnStmt -> return ';' +> ReturnStmt
PassStmt -> pass ';' +> PassStmt
ExitStmt -> exit ';' +> ExitStmt
NextStmt -> next ';' +> NextStmt

UntilStmt -> until BoolExpr ';' +> UntilStmt

CompoundStmt -> If_Stmt
-> Case_Stmt
-> For_Stmt
-> While_Stmt
-> Repeat_Stmt

If_Stmt -> if BoolExpr then CodeBlock [Elif_Blocks] [else CodeBlock] end if +> If_Stmt

Elif_Blocks -> Elif_Block... +> Elif_Blocks

Elif_Block -> elif BoolExpr then CodeBlock +> Elif_Block

Case_Stmt -> case Primary of Case_Branches [others then CodeBlock] end case +> Case_Stmt

Case_Branches -> Case_Branch... +> Case_Branches
Case_Branch -> ExprList then CodeBlock +> Case_Branch

For_Stmt -> for in Primary do CodeBlock end for +> For_Stmt

While_Stmt -> while BoolExpr do CodeBlock end while +> While_Stmt

Repeat_Stmt -> repeat [Expr times] CodeBlock end repeat +> Repeat_Stmt

BoolExpr -> Expr +> BoolExpr

Expr -> Primary
-> UnaryExpr
-> BinaryExpr
-> ConditionalExpr
-> Expr and Expr +> And
-> Expr or Expr +> Or
-> Expr xor Expr +> Xor
-> not Expr +> Not

UnaryExpr -> [] Primary +> UnaryExpr
BinaryExpr -> UnaryExpr BinaryTerm... +> BinaryExpr
BinaryTerm -> Primary +> BinaryTerm

ConditionalExpr -> if BoolExpr then Expr else Expr +> ConditionalExpr

Primary -> Literal
-> List
-> Assignable
-> FunctionCall
-> '(' Expr ')'

Literal -> +> Num
-> true +> True
-> false +> False

List -> '[' [ExprList] ']' +> List

Assignable -> Ident
-> MemberAccess
-> ElementAccess

FunctionCall -> Primary [TypeArgs] '(' [ExprList] ')' +> FunctionCall
MemberAccess -> Primary '.' +> MemberAccess
ElementAccess-> Primary '[' ExprList ']' +> ElementAccess

ExprList -> Expr / ','... +> ExprList

Ident -> +> Ident

Qualident -> /'.' ... +> Qualident

Lexer Grammar in EBNF


TokenList
-> Token
-> TokenList Token
Token
-> Number +> num
-> Ident +> ident
-> Operator +> oper
-> Blank
$Digit -> "0123456789"
$Letter -> "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
Space -> " \r\n"
$Ident
-> Letter
-> Ident Letter
-> Ident Digit
$Number -> Digit... [ '.' Digit...]
$Operator -> '+' '-' '*' '/' '^' '<' '>' '<=' '>=' '==' '!='
Blank
-> Space
-> Blank Space

My Introduction

Shanghai, China
Software Developer. Mainly work in C#.