SoftArt Shader Language Syntax EBNF
经过一晚上的整理,把 SoftArt Shader Language 的 EBNF 弄出来了。
主要参照的是 ISO C++的EBNF 。
由于ISO C++并不是一个LALR(1)能够解决的问题,因此不知道这个C++ EBNF的简化版能否在YACC上比较方便的实现。
如果不能实现,我会先考虑修正语法满足需要,而不是手写编译器,我还没那个能耐。
Flex 与 Bison 的 下载地址
有点儿参差不齐,哈哈。一些Terminator我就没有写了。预处理的部分由于使用Boost.Wave,因此也没有加入到EBNF中。这些都很大程度上简化了EBNF的内容。
然后大概的根据层次归了一下类,看起来要方便一点。
但愿语法树里面没有漏掉的。
等回到自己机器上就开始自下向上的实现这个东西了,争取10月份之前能把整个Shader编译器集成到SoftArt里面去。
目前就让它在SVN的SandBox里面待着好了。
主要参照的是 ISO C++的EBNF 。
由于ISO C++并不是一个LALR(1)能够解决的问题,因此不知道这个C++ EBNF的简化版能否在YACC上比较方便的实现。
如果不能实现,我会先考虑修正语法满足需要,而不是手写编译器,我还没那个能耐。
Flex 与 Bison 的 下载地址
//SASL: SoftArt Shader Language
Syntax
Program :: = DeclSeq
//////////////////////////////////
//Declaration
//////////////////////////////////
DeclSeq :: = Decl |
DeclSeq Decl
Decl :: = BlockDecl |
FunctioDef
Declarator :: = DirectDecl [ SemanticSpec ] [ RegisterSpec ]
DirectDecl :: = DeclId |
DirectDecl '(' ParamDeclClause ')'|
DirectDecl ' [ ' [ConstExp ] ']'|
'(' Declarator ')'
CvQualifierSeq :: = CvQualifier [ CvQualifierSeq ]
CvQualifier :: = 'const' | 'uniform'
SemanticSpec :: = ':' Semantic
RegisterSpec :: = ':' 'register' '(' Register ')'
BlockDecl :: = SimpleDecl
SimpleDecl :: = [ DeclSpecSeq ] [ InitDeclList ] ' ; '
AbsDecl :: = DirectAbsDecl
DirectAbsDecl :: = [ DirectAbsDecl ] '(' ParamDeclClause ')' |
[ DirectAbsDecl ] ' [ ' [ConstExp ] ']' |
'(' AbsDecl ')'
DeclSpecSeq :: = [ DeclSpecSeq ] DeclSpec
InitDeclList :: = InitDecl |
InitDeclList ' , ' InitDecl
InitDecl :: = Declarator [ Init ]
Init :: = ' = ' InitClause |
( ExpList)
InitClause :: = AssignmentExp |
'{' InitList [ ',' ] '}' |
'{' '}'
InitList :: = InitClause |
InitList ' , ' InitClause
DeclSpec :: = TypeSpec
TypeSpec :: = SimpleTypeSpec |
ClassSpec
SimpleTypeSpec :: = 'int' | 'half' | 'float' |
'int2' | 'half2' | 'float2' |
'int3' | 'half3' | 'float3' |
'int4' | 'half4' | 'float4' |
'float4x4'
/////////////////////////////
//Function
/////////////////////////////
FunctionDef :: = [ DeclSpecSeq ] Declarator FunctionBody
FunctionBody :: = CompoundStatement
/////////////////////////////
//Statements
/////////////////////////////
CompoundStatement :: = '{' [ StatementSeq ] '}'
StatementSeq :: = Statement |
StatementSeq Statement
Statement :: = ExpStatement |
CompountStatement |
SelectionStatement |
IterationStatement |
DeclStatement
ExpStatement :: = [ Expression ] ' ; '
SelectionStatement :: = 'if' '(' Condition ')' Statement |
'if' '(' Condition ')' Statement 'else' Statement
IterationStatement :: = 'while' '(' Condition ')' Statement |
'do' Statement 'while' '(' Expression ')' ' ; '
'for' '(' ForInitStatement [ Condition ] ' ; ' [ Expression ] ')' Statement
Condition :: = Expression |
TypeSpecSeq Declarator ' = ' AssignmentExp
ForInitStatement :: = ExpStatement |
SimpleDecl
DeclStatement :: = BlockDecl
/////////////////////////////
//Parameter
/////////////////////////////
ParamDeclClause :: = [ ParamDeclList ]
ParamDeclList :: = ParamDecl |
ParamDeclList ' , ' ParamDecl
ParamDecl :: = DeclSpecSeq Delarator |
DeclSpecSeq [ AbsDecl ]
/////////////////////////////
//Class And Struct
/////////////////////////////
ClassSpec :: = ClassHead '{' [ MemberSpec ] '}'
ClassHead :: = 'struct' [ Identifier ]
MemberSpec ` :: = MemberDecl [ MemberSpec ]
MemberDeclaration :: = [ DeclSpecSeq ] [ MemberDeclList ]
MemberDeclList :: = MemberDecl |
MemberDeclList ' , ' MemberDeclarator
MemberDeclarator :: = Declarator [ PureSpec ] |
Declarator [ ConstInit ] |
[ Identifier ] ':' ConstExp
//////////////////////////////////////
//Expressions
//////////////////////////////////////
Expression :: = AssignmentExp
ConstExp :: = CondExp
AssignmentExp :: = CondExp |
LogicalOrExp AssignmentOp AssignmentExp
CondExp :: = LogicalOrExp |
LogicalOrExp '?' Expression ':' AssignmentExp
LogicalOrExp :: = LogicalAndExp |
LogicalOrExp '||' LogicalAndExp
LogicalAndExp :: = InclusiveOrExp |
LogicalAndExp '&&' InclusiveOrExp
InclusiveOrExp :: = ExclusiveOrExp |
InclusiveOrExp '|' ExclusiveOrExp
ExclusiveOrExp :: = AndExp |
ExclusiveOrExp '^' AndExp
AndExp :: = EqualExp |
AndExp '&' EqualExp
EqualExp :: = RelExp |
EqualExp ' == ' RelExp |
EqualExp '! = ' RelExp
RelExp :: = ShiftExp |
RelExp '<' ShiftExp |
RelExp '>' ShiftExp |
RelExp '< = ' ShiftExp |
RelExp '> = ' ShiftExp
ShiftExp :: = AddExp |
ShiftExp '<<' AddExp |
ShiftExp '>>' AddExp
AddExp :: = MulExp |
AddExp '+' MulExp |
AddExp '-' MulExp
MulExp :: = PmExp |
MulExp '*' PmExp |
MulExp '/' PmExp |
MulExp '%' PmExp
PmExp :: = CastExp |
PmExp '.*' CastExp
CastExp :: = UnaryExp |
'(' TypeId ')' CastExp
UnaryExp :: = PostfixExp |
'++' CastExp |
'--' CastExp |
UnaryOp CastExp
PostfixExp :: = PrimaryExp |
PostfixExp ' [ ' Expression ' ] ' |
PostfixExp '(' Expression ')' |
PostfixExp '(' [ ExpList ] ')' |
SimpleTypeSpec '(' [ ExpList ] ')' |
PostfixExp '++' |
PostfixExp '--'
PrimaryExp :: = Literal |
'(' Expression ')' |
IdExp
IdExp :: = UnqualifiedId
ExpList :: = AssigmentExp |
ExpList ' , ' AssignmentExp
//////////////////////////////
//Identifier
//////////////////////////////
DeclId :: = IdExp |
TypeName
UnqualifiedId :: = Identifier
Identifier :: = NonDigit |
Identifier NonDigit |
Identifier Digit
//////////////////////////////
//Literals
//////////////////////////////
Literal :: = IntLiteral |
FloatLiteral |
BoolLitreral
IntLiteral :: = DemicalLiteral
DemicalLiteral :: = NonZeroDigit |
DemicalLiteral | Digit
FloatLiteral :: = FracConstant [ ExponentPart ] |
DigitSeq ExponentPart
BoolLiteral :: = 'false' |
'true'
FracConstant :: = [ DigitSeq ] '.' DigitSeq |
DigitSeq '.'
ExponentPart :: = 'e' [ Sign ] DigitSeq |
'E' [ Sign ] DigitSeq
DigitSeq :: = Digit |
DigitSeq Digit
//////////////////////////
//Terminals
//////////////////////////
NonDigit :: = AlphaChar |
'_'
NonZeroDigit :: = ' 1 ' | | ' 9 '
AssignmentOp :: = ' = ' |
'* = ' | '/ = ' | '% = ' |
'+ = ' | '- = ' |
'>> = ' | '<< = ' |
'& = ' | '^ = ' | '| = '
Register
Semantic
Digit
AlphaChar
Keyword
Syntax
Program :: = DeclSeq
//////////////////////////////////
//Declaration
//////////////////////////////////
DeclSeq :: = Decl |
DeclSeq Decl
Decl :: = BlockDecl |
FunctioDef
Declarator :: = DirectDecl [ SemanticSpec ] [ RegisterSpec ]
DirectDecl :: = DeclId |
DirectDecl '(' ParamDeclClause ')'|
DirectDecl ' [ ' [ConstExp ] ']'|
'(' Declarator ')'
CvQualifierSeq :: = CvQualifier [ CvQualifierSeq ]
CvQualifier :: = 'const' | 'uniform'
SemanticSpec :: = ':' Semantic
RegisterSpec :: = ':' 'register' '(' Register ')'
BlockDecl :: = SimpleDecl
SimpleDecl :: = [ DeclSpecSeq ] [ InitDeclList ] ' ; '
AbsDecl :: = DirectAbsDecl
DirectAbsDecl :: = [ DirectAbsDecl ] '(' ParamDeclClause ')' |
[ DirectAbsDecl ] ' [ ' [ConstExp ] ']' |
'(' AbsDecl ')'
DeclSpecSeq :: = [ DeclSpecSeq ] DeclSpec
InitDeclList :: = InitDecl |
InitDeclList ' , ' InitDecl
InitDecl :: = Declarator [ Init ]
Init :: = ' = ' InitClause |
( ExpList)
InitClause :: = AssignmentExp |
'{' InitList [ ',' ] '}' |
'{' '}'
InitList :: = InitClause |
InitList ' , ' InitClause
DeclSpec :: = TypeSpec
TypeSpec :: = SimpleTypeSpec |
ClassSpec
SimpleTypeSpec :: = 'int' | 'half' | 'float' |
'int2' | 'half2' | 'float2' |
'int3' | 'half3' | 'float3' |
'int4' | 'half4' | 'float4' |
'float4x4'
/////////////////////////////
//Function
/////////////////////////////
FunctionDef :: = [ DeclSpecSeq ] Declarator FunctionBody
FunctionBody :: = CompoundStatement
/////////////////////////////
//Statements
/////////////////////////////
CompoundStatement :: = '{' [ StatementSeq ] '}'
StatementSeq :: = Statement |
StatementSeq Statement
Statement :: = ExpStatement |
CompountStatement |
SelectionStatement |
IterationStatement |
DeclStatement
ExpStatement :: = [ Expression ] ' ; '
SelectionStatement :: = 'if' '(' Condition ')' Statement |
'if' '(' Condition ')' Statement 'else' Statement
IterationStatement :: = 'while' '(' Condition ')' Statement |
'do' Statement 'while' '(' Expression ')' ' ; '
'for' '(' ForInitStatement [ Condition ] ' ; ' [ Expression ] ')' Statement
Condition :: = Expression |
TypeSpecSeq Declarator ' = ' AssignmentExp
ForInitStatement :: = ExpStatement |
SimpleDecl
DeclStatement :: = BlockDecl
/////////////////////////////
//Parameter
/////////////////////////////
ParamDeclClause :: = [ ParamDeclList ]
ParamDeclList :: = ParamDecl |
ParamDeclList ' , ' ParamDecl
ParamDecl :: = DeclSpecSeq Delarator |
DeclSpecSeq [ AbsDecl ]
/////////////////////////////
//Class And Struct
/////////////////////////////
ClassSpec :: = ClassHead '{' [ MemberSpec ] '}'
ClassHead :: = 'struct' [ Identifier ]
MemberSpec ` :: = MemberDecl [ MemberSpec ]
MemberDeclaration :: = [ DeclSpecSeq ] [ MemberDeclList ]
MemberDeclList :: = MemberDecl |
MemberDeclList ' , ' MemberDeclarator
MemberDeclarator :: = Declarator [ PureSpec ] |
Declarator [ ConstInit ] |
[ Identifier ] ':' ConstExp
//////////////////////////////////////
//Expressions
//////////////////////////////////////
Expression :: = AssignmentExp
ConstExp :: = CondExp
AssignmentExp :: = CondExp |
LogicalOrExp AssignmentOp AssignmentExp
CondExp :: = LogicalOrExp |
LogicalOrExp '?' Expression ':' AssignmentExp
LogicalOrExp :: = LogicalAndExp |
LogicalOrExp '||' LogicalAndExp
LogicalAndExp :: = InclusiveOrExp |
LogicalAndExp '&&' InclusiveOrExp
InclusiveOrExp :: = ExclusiveOrExp |
InclusiveOrExp '|' ExclusiveOrExp
ExclusiveOrExp :: = AndExp |
ExclusiveOrExp '^' AndExp
AndExp :: = EqualExp |
AndExp '&' EqualExp
EqualExp :: = RelExp |
EqualExp ' == ' RelExp |
EqualExp '! = ' RelExp
RelExp :: = ShiftExp |
RelExp '<' ShiftExp |
RelExp '>' ShiftExp |
RelExp '< = ' ShiftExp |
RelExp '> = ' ShiftExp
ShiftExp :: = AddExp |
ShiftExp '<<' AddExp |
ShiftExp '>>' AddExp
AddExp :: = MulExp |
AddExp '+' MulExp |
AddExp '-' MulExp
MulExp :: = PmExp |
MulExp '*' PmExp |
MulExp '/' PmExp |
MulExp '%' PmExp
PmExp :: = CastExp |
PmExp '.*' CastExp
CastExp :: = UnaryExp |
'(' TypeId ')' CastExp
UnaryExp :: = PostfixExp |
'++' CastExp |
'--' CastExp |
UnaryOp CastExp
PostfixExp :: = PrimaryExp |
PostfixExp ' [ ' Expression ' ] ' |
PostfixExp '(' Expression ')' |
PostfixExp '(' [ ExpList ] ')' |
SimpleTypeSpec '(' [ ExpList ] ')' |
PostfixExp '++' |
PostfixExp '--'
PrimaryExp :: = Literal |
'(' Expression ')' |
IdExp
IdExp :: = UnqualifiedId
ExpList :: = AssigmentExp |
ExpList ' , ' AssignmentExp
//////////////////////////////
//Identifier
//////////////////////////////
DeclId :: = IdExp |
TypeName
UnqualifiedId :: = Identifier
Identifier :: = NonDigit |
Identifier NonDigit |
Identifier Digit
//////////////////////////////
//Literals
//////////////////////////////
Literal :: = IntLiteral |
FloatLiteral |
BoolLitreral
IntLiteral :: = DemicalLiteral
DemicalLiteral :: = NonZeroDigit |
DemicalLiteral | Digit
FloatLiteral :: = FracConstant [ ExponentPart ] |
DigitSeq ExponentPart
BoolLiteral :: = 'false' |
'true'
FracConstant :: = [ DigitSeq ] '.' DigitSeq |
DigitSeq '.'
ExponentPart :: = 'e' [ Sign ] DigitSeq |
'E' [ Sign ] DigitSeq
DigitSeq :: = Digit |
DigitSeq Digit
//////////////////////////
//Terminals
//////////////////////////
NonDigit :: = AlphaChar |
'_'
NonZeroDigit :: = ' 1 ' | | ' 9 '
AssignmentOp :: = ' = ' |
'* = ' | '/ = ' | '% = ' |
'+ = ' | '- = ' |
'>> = ' | '<< = ' |
'& = ' | '^ = ' | '| = '
Register
Semantic
Digit
AlphaChar
Keyword
有点儿参差不齐,哈哈。一些Terminator我就没有写了。预处理的部分由于使用Boost.Wave,因此也没有加入到EBNF中。这些都很大程度上简化了EBNF的内容。
然后大概的根据层次归了一下类,看起来要方便一点。
但愿语法树里面没有漏掉的。
等回到自己机器上就开始自下向上的实现这个东西了,争取10月份之前能把整个Shader编译器集成到SoftArt里面去。
目前就让它在SVN的SandBox里面待着好了。