Design Patterns : Elements of Reusable Object-Oriented Software


Design Patterns: Elements of Reusable Object-Oriented Software



Download 4,06 Mb.
Pdf ko'rish
bet190/288
Sana07.04.2022
Hajmi4,06 Mb.
#535140
1   ...   186   187   188   189   190   191   192   193   ...   288
Bog'liq
GOF Design Patterns

Design Patterns: Elements of Reusable Object-Oriented Software 
281 
'a' repeat & 'abc' 
then matching the input "aabc" against the subexpression"'a' repeat" would yield 
two input streams, one having matchedone character of the input, and the other 
having matched twocharacters. Only the stream that has accepted one character 
willmatch the remaining "abc". 
Now we consider the definitions of match: for each classdefining the regular 
expression. The definition forSequenceExpression matches each of its 
subexpressions insequence. Usually it will eliminate input streams from 
itsinputState. 
match: inputState 
^ expression2 match: (expression1 match: inputState). 
An AlternationExpression will return a state that consistsof the union of states 
from either alternative. The definition ofmatch: for AlternationExpression is 
match: inputState 
| finalState | 
finalState := alternative1 match: inputState. 
finalState addAll: (alternative2 match: inputState). 
^ finalState 
The match: operation for RepetitionExpressiontries to find as many states that 
could match as possible: 
match: inputState 
| aState finalState | 
aState := inputState. 
finalState := inputState copy. 
[aState isEmpty] 
whileFalse: 
[aState := repetition match: aState. 
finalState addAll: aState]. 
^ finalState 
Its output state usually contains more states than its input state,because a 
RepetitionExpression can match one, two, or manyoccurrences of repetition on the 
input state. The outputstates represent all these possibilities, allowing 
subsequent elementsof the regular expression to decide which state is the correct 
one. 


Design Patterns: Elements of Reusable Object-Oriented Software 
282 
Finally, the definition of match: forLiteralExpression tries to match its 
components against eachpossible input stream. It keeps only those input streams 
that have amatch: 
match: inputState 
| finalState tStream | 
finalState := Set new. 
inputState 
do: 
[:stream | tStream := stream copy. 
(tStream nextAvailable: 
components size 
) = components 
ifTrue: [finalState add: tStream] 
]. 
^ finalState 
The nextAvailable: message advances the input stream. Thisis the only match: 
operation that advances the stream.Notice how the state that's returned contains 
a copy of the inputstream, thereby ensuring that matching a literal never changes 
theinput stream. This is important because each alternative of 
anAlternationExpression should see identical copies ofthe input stream. 
Now that we've defined the classes that make up an abstract syntaxtree, we can 
describe how to build it.Rather than write a parser for regular expressions, we'll 
definesome operations on the RegularExpression classes so thatevaluating a 
Smalltalk expression will produce an abstract syntax treefor the corresponding 
regular expression. That lets us use thebuilt-in Smalltalk compiler as if it were 
a parser for regularexpressions. 
To build the abstract syntax tree, we'll need to define"|", "repeat", and "&" 
asoperations on RegularExpression. These operations aredefined in class 
RegularExpression like this: 
& aNode 
^ SequenceExpression new
expression1: self expression2: aNode asRExp 
repeat 
^ RepetitionExpression new repetition: self 
| aNode 
^ AlternationExpression new 
alternative1: self alternative2: aNode asRExp 



Download 4,06 Mb.

Do'stlaringiz bilan baham:
1   ...   186   187   188   189   190   191   192   193   ...   288




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish