| 1 | //Copyright 2013 Vastech SA (PTY) LTD |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | /* |
| 16 | Package items implements dotted items for FSA generation during the lexer generation process. |
| 17 | |
| 18 | # GENERALISED SUBSET ALGORITHM |
| 19 | |
| 20 | The problem: |
| 21 | Lexers for some applications, e.g.: email header parsing have a number of char range input symbols, which overlap to a large extent. |
| 22 | |
| 23 | The lexer symbol space can be very large, e.g.: UTF-8, so it is impractical to create items for every possible value of the char ranges. |
| 24 | |
| 25 | Possible solutions: |
| 26 | |
| 27 | 1. Automatic conflict handling |
| 28 | Impractical because of overlapping char ranges for each state. |
| 29 | |
| 30 | 2. Separate symbol classification functions for each state |
| 31 | Better, but overlaps can still occur |
| 32 | |
| 33 | 3. Create a set of intersections of the valid char ranges of each set. Use the intersections as input symbols to the lexer. |
| 34 | This allows the full state space to be generated. |
| 35 | |
| 36 | DEFINITION: LexPart = (Tokens, RegDefs, TermSym) |
| 37 | |
| 38 | ALGORITHM: Generate lexer sets |
| 39 | |
| 40 | S0 := CreateSet0() |
| 41 | itemSets = {S0} |
| 42 | transitions = {} |
| 43 | repeat { |
| 44 | for set in itemSets { |
| 45 | for symRange in set.SymRanges() { |
| 46 | if nextSet := set.Next(symRange); nextSet not in itemSets { |
| 47 | add nextSet to itemSets |
| 48 | add (set, symRange, nextSet) to transitions |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | } until no more sets added to itemSets |
| 53 | return itemSets, transitions |
| 54 | |
| 55 | ALGORITHM: CreateSet0() |
| 56 | |
| 57 | S0 = {} |
| 58 | for all T : x in Tokens { |
| 59 | add T : •x to S0 |
| 60 | } |
| 61 | return S0.Closure() |
| 62 | |
| 63 | DEFINITION: Let R be the id of a regular definition. |
| 64 | |
| 65 | ALGORITHM: set.Next() |
| 66 | |
| 67 | Output: the set containing all items in set that can move over symRange. |
| 68 | |
| 69 | repeat |
| 70 | nextSet = {} |
| 71 | for I : x •c y in set and c is in symRange{ |
| 72 | add I : x c •y to nextSet |
| 73 | } |
| 74 | for R : •z in nextSet and I : x •R y in set { |
| 75 | add I : x •R y to nextSet |
| 76 | } |
| 77 | for R : z• in nextSet and I : x •R y in set { |
| 78 | add I : x R• y to nextSet |
| 79 | } |
| 80 | until no items were added to nextSet |
| 81 | |
| 82 | return nextSet.Closure() |
| 83 | |
| 84 | ALGORITHM: set.Closure() |
| 85 | |
| 86 | closure = {X : x •y | X : x •y in set and y not null} |
| 87 | repeat |
| 88 | for item I : x •R y in set and R : •z in RegDefs { |
| 89 | add R : •z to closure |
| 90 | } |
| 91 | until no more items were added to closure |
| 92 | |
| 93 | # Disjunct character ranges |
| 94 | |
| 95 | DEFINITIONS |
| 96 | |
| 97 | The alphabet, S = [s[1],s[n]], of a regular grammar is an ordered set of n symbols. Given any s[i] != s[n] of S the next symbol in S |
| 98 | can be found by s[i] + 1. s[n] + 1 is undefined. For any s[i] != s[1] the previous symbol can be found by s[i] - 1. |S| is the |
| 99 | size of S. |
| 100 | |
| 101 | Closed intervals of the alphabet, S, may be declared as symbol ranges, r = [r[1],r[k]] where the size of r, |r| = k. r is an ordered |
| 102 | subset of S with n elements without gaps, i.e.: r[i] + 1 = r[i+1]. |
| 103 | |
| 104 | X : x •a y is an item of a regular grammar, with a the expected symbol and x, y possibly empty strings of symbols from S. |
| 105 | |
| 106 | In the algorithm symbols may be elements or ranges of S. If s is an element of S, |s| = 1. If |s| = 1, s = [s,s] |
| 107 | |
| 108 | ALGORITHM: set.SymRanges() - Create the lexer symbol ranges of an item set, S: |
| 109 | |
| 110 | Input: An item set of the grammar. |
| 111 | |
| 112 | Output: A set of non-overlapping symbol ranges. |
| 113 | |
| 114 | Let SR = {} be a initially empty set of symbol ranges. |
| 115 | |
| 116 | for all items i in S { |
| 117 | let s be the expected symbol of i |
| 118 | if { |
| 119 | // s outside all r in SR |
| 120 | case for all r in I, s[|s|] < r[1] or s[1] > r[|r|]: |
| 121 | add [s[1],s[|s|] to I |
| 122 | |
| 123 | // s equal to some r in SR |
| 124 | case r in SR and s[1] = r[1] and s[|s|] = r[|r|]: |
| 125 | do nothing |
| 126 | |
| 127 | //s inside some r in SR |
| 128 | case r in SR and r[1] = s[1] and s[|s|] < r[|r|]: |
| 129 | replace r in SR by {[r[1],s[|s|]], [s[|s|]+1,r[|r|]]} |
| 130 | case r in SR and r[1] < s[1] and s[|s|] < r[|r|]: |
| 131 | replace r in SR by {[r[1],s[1]-1], [s[s[1],s[|s|]], s[|s]+1,r[|r|]]} |
| 132 | case r in SR and r[1] < s[1] and s[|s|] = r[|r|]: |
| 133 | repace r in SR by {[r[1],s[1]-1], [s[1],r[|r|]]} |
| 134 | |
| 135 | // overlap to left |
| 136 | case r in SR and s[1] < r[1] <= s[|s|] < r[|r|]: |
| 137 | replace r in SR by {[s[1],r[1]-1], [r[1],s[|s|]], [s[|s|]+1,r[|r|]]} |
| 138 | case r in SR and s[1] < r[1] and s[|s|] = r[|r|]: |
| 139 | add [s[1],r[1]-1] to SR |
| 140 | } |
| 141 | |
| 142 | // overlap to right |
| 143 | case r in SR and s[1] = r[1] and s[|s|] > r[|r|]: |
| 144 | add [r[|r|]+1,s[|s|]] to SR |
| 145 | case r in SR and [s1] > r[1] and s[|s|] > r[|r|]: |
| 146 | replace r in SR by {[r[1],s[1]-1], [s[1],r[|r|]], [r[|r|]+1,s[|s|]]} |
| 147 | } |
| 148 | */ |
| 149 | package items |