Since Microsoft does not publish the complete BNF for the High Level Shading Language, we specify what we think that is a BNF. Based on books and MSDN library, we create the BNF below. Although it parses a lot of shaders, we do not validate it completely. Thus, please, do not consider it as a complete HLSL BNF.

Tokens:

IDENTIFIER    = [A-Za-z_][A-Za-z0-9_]*

QUOTED_STRING = "([^"]|"")*+"
COMMENT       = /\*([^*]|\*[^/])*\*/
COMMENTCPP    = //[^\n\r]*

NUMBER        = [0-9]+|[0-9]*\.[0-9]+(E[0-9]+)?(f)?
NUMBER_2_4    = [2-4]
NUMBER_1_4    = [1-4]
WHITESPACE    = [ \t\n\r]+
STRING        = (".*[^\\]")|('.*[^\\]')
RGBA          = [rgba]+
XYZW          = [xyzw]+


Productions:

File = 
    ( Function_Declaration
     | Variable_Declaration
     | Struct_Declaration
     | Technique_Declaration
     | Property_Specification
    )+

Technique_Declaration = 
    "technique" IDENTIFIER "{" 
          (  
             "pass"  IDENTIFIER "{" 
                   (
                        IDENTIFIER "=" ["compile" IDENTIFIER] (IDENTIFIER "()" | Expression) ";"
                   )*
             "}"
          )* 
    "}"

Struct_Declaration = 
    "struct" IDENTIFIER "{" 
          (
               Type IDENTIFIER [ "[" NUMBER "]" ] [":" SemanticalParameters ] [Assignment_Operator Initializers] ";"
          )+ 
    "}" ";"  

Variable Declaration = 
    Type IDENTIFIER [ "[" NUMBER "]" ] [":" SemanticalParameters ] [Assignment_Operator Initializers] ";"

Property_Specification = 
    Type IDENTIFIER [ "[" NUMBER "]" ] [":" SemanticalParameters ] [Assignment_Operator Initializers] "{" 
        (Variable_Assignment)+ 
    "}" ";"

Function_Declaration = 
    [Storage_Class] [Type_Modifier] Type IDENTIFIER Parameters [":" SemanticalParameters] "{" 
         Function_Body 
    "}" 

Parameters = "(" [List_Of_Params] ")"

List_Of_Params = [In_out_inout] Variable_Declaration ( "," [In_out_inout] Variable_Declaration )*

Function_Body = (Statement)+

Statement = Return_Statement
           | Variable_Assignment
           | Flow_Control_Words
           | While_Statement
           | Do_Statement
           | IF_Statement
           | For_Statement
           | Switch_Statement

IF_Statement = 
    [IF_Attributes] "if" "(" Condition ")" 
        [Statement_Scope]
    ["else" Statement_Scope]

Switch_Statement = 
    [Switch_Attributes] "switch" "(" Expression ")" "{"
        (
            ("case" NUMBER | "default") ":" [Statement_Scope]
        )+ 
    "}";

While_Statement = 
    [While_Attributes] "while" "(" Condition ")"
        Statement_Scope

Do_Statement = 
    "do"
         Statement_Scope;
    "while" "(" Condition ")" ";"

For_Statement = 
    [For_Attributes] "for" "(" 
       (Variable_Assignment | Expression)* ";" 
        [Condition] ";" 
        [
            Expression 
            | Identifier_Composed [ "[" NUMBER "]" ] Assignment_Operator Initializers
        ] 
       ")"
        Statement_Scope

Statement_Scope = "{" Function_Body "}" | Function_Body

Variable_Declaration = 
    [Storage_Class] [Type_Modifier] Type IDENTIFIER [ "[" NUMBER "]" ] 
        [":" SemanticalParameters] [Assignment_Operator Initializers]

Return_Statement = "return" Expression ";"

Variable_Assignment = Identifier_Composed [ "[" NUMBER "]" ] Assignment_Operator Initializers ";"

Condition = Expression Comparison_Operators Expression;

Initializers = Expression; 

Expression = Term [ExpressionTail]

ExpressionTail = "+" Expression | "-" Expression 

Term = Factor [TermTail]
     
TermTail = "*" Term | "/" Term | "%" Term

Factor = Atom | "(" Expression ")"     

Atom = 
      NUMBER
    | [Prefix_Postfix_Operators] Identifier_Composed [Prefix_Postfix_Operators]
    | Constructor_Call
    | Variable_Declaration_Atom					   

Constructor_Call = 
    Type "(" Expression ("," Expression)* ")" 
        ["." XYZW | "." RGBA | "." IDENTIFIER]

Variable_Declaration_Atom = 
    [Storage_Class] [Type_Modifier] Type IDENTIFIER [ "[" NUMBER "]" ] 
         [":" SemanticalParameters ] [Assignment_Operator Initializers]

Storage_Class = "extern" | "static" | "nointerpolation" | "shared" | "uniform" | "volatile"

Type_Modifier = "const" | "row_major" | "column_major"

Type = 
      "float"([2-4](x[2-4])?)?  
    | "int"([2-4](x[2-4])?)? 
    | "half"([2-4](x[2-4])?)?
    | "double"([2-4](x[2-4])?)?
    | "bool"([2-4](x[2-4])?)?
    | "vector" [ "<" Basic_Type ">" | "<" [2-4] ">" | "<" Basic_Type "," NUMBER ">" ]
    | "matrix" "<" Basic_Type "," [1-4] "," [1-4] ">"
    | "sampler" | "sampler1D" | "sampler2D" | "sampler3D" | "samplerCUBE" | "sampler_state"
    | "texture" | "texture1D" | "texture2D" | "texture3D" | "textureCUBE"
    | "void"
    | IDENTIFIER

Basic_Type = "float" | "int" | "half" | "double" | "bool"

SemanticalParameters = 
      InputSemanticalParameters 
    | OutputSemanticalParameters 
    | InOutSemanticalParameters 
    | SemanticMatrixes

InputSemanticalParameters = 
      "BINORMAL" | "BLENDINDICES" | "BLENDWEIGHT" | "NORMAL" 
    | "POSITIONT" | "TANGENT" | "VFACE" | "VPOS" 
                          
OutputSemanticalParameters =  "FOG" | "TESSFACTOR" | "DEPTH"

InOutSemanticalParameters = 
      "POSITION"([0-9])?
    | "TEXCOORD"([0-9])?
    | "TEXUNIT"([0-9])? 
    | "COLOR"([0-9])? 
    | "PSIZE"

Assignment_Operator = "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "<<=" | ">>=" | "&=" | "|=" | "^="

Prefix_Postfix_Operators = "++" | "--"

Comparison_Operators = "<" | ">" | "==" | "!=" | "<=" | ">="

Boolean_Operators = "&&" | "||"

In_out_inout = "in" | "out" | "inout"

Identifier_Composed = IDENTIFIER ["." XYZW | "." RGBA | "." IDENTIFIER]

Flow_Control_Words = ("stop" | "continue" | "break" | "discard")";"

IF_Attributes = "flatten" | "branch"

While_Attributes = "unroll" "(" NUMBER ")" | "loop"

For_Attributes = "unroll" "(" NUMBER ")" | "loop"

Switch_Attributes = "call" | "forcecase" | "branch" | "flatten"  

SemanticMatrixes = 
      "WORLD"("I")?("T")?
    | "VIEW"("I")?("T")?
    | "PROJ"("I")?("T")?
    | "WORLDVIEW"("I")?("T")?
    | "WORLDPROJ"("I")?("T")?
    | "VIEWPROJ"("I")?("T")?
    | "WORLDVIEWPROJ"("I")?("T")?

Last edited Nov 30, 2007 at 12:51 PM by vfpamp, version 20

Comments

No comments yet.