12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138 |
- /*++
- Copyright (c) 2016 Minoca Corp.
- This file is licensed under the terms of the GNU General Public License
- version 3. Alternative licensing terms are available. Contact
- info@minocacorp.com for details. See the LICENSE file at the root of this
- project for complete licensing information.
- Module Name:
- lr0.c
- Abstract:
- This module implements support for generating an LR(0) grammar from a
- description of productions.
- Author:
- Evan Green 9-Apr-2016
- Environment:
- Build
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include "yygenp.h"
- #include <assert.h>
- #include <string.h>
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- /*++
- Structure Description:
- This structure contains the working state for the LR(0) state generator.
- Members:
- StateSet - Stores the hash table of states, based on the item index of
- their first right hand side.
- ShiftSet - Stores the set of states to shift to for each shift symbol.
- ShiftSymbol - Stores the array of possible shift symbols out of the
- current state.
- ReduceSet - Stores the set of reductions being built for the current state.
- KernelBase - Stores an array of arrays of item indices, indexed by shift
- symbol. For the currently operated on state, if symbol I were shifted,
- what new item sets would be in the kernel. The kernel is the part of
- a state added by the grammar (or previous state), rather than the
- closure.
- KernelEnd - Stores an array indicating the end of the kernel base arrays,
- again indexed by shift symbol.
- KernelItems - Stores a big flattened array of all the kernel item sets.
- This is the storage area for kernel base.
- LastState - Stores a pointer to the last LR(0) state processed.
- CurrentState - Stores a pointer to the current LR(0) state being operated
- on.
- ShiftCount - Stores the number of shifts.
- --*/
- typedef struct _YYGEN_STATE_CONTEXT {
- PYYGEN_STATE *StateSet;
- PYY_STATE_INDEX ShiftSet;
- PYY_VALUE ShiftSymbol;
- PYY_RULE_INDEX ReduceSet;
- PYY_ITEM_INDEX *KernelBase;
- PYY_ITEM_INDEX *KernelEnd;
- PYY_ITEM_INDEX KernelItems;
- PYYGEN_STATE LastState;
- PYYGEN_STATE CurrentState;
- YY_VALUE ShiftCount;
- } YYGEN_STATE_CONTEXT, *PYYGEN_STATE_CONTEXT;
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- YY_STATUS
- YypGenerateDerives (
- PYYGEN_CONTEXT Context
- );
- YY_STATUS
- YypGenerateNullable (
- PYYGEN_CONTEXT Context
- );
- YY_STATUS
- YypGenerateStates (
- PYYGEN_CONTEXT Context
- );
- YY_STATUS
- YypInitializeStateContext (
- PYYGEN_CONTEXT Context,
- PYYGEN_STATE_CONTEXT StateContext
- );
- VOID
- YypDestroyStateContext (
- PYYGEN_CONTEXT Context,
- PYYGEN_STATE_CONTEXT StateContext
- );
- YY_STATUS
- YypGenerateFirstDerives (
- PYYGEN_CONTEXT Context
- );
- PULONG
- YypGenerateEpsilonFreeFirstSet (
- PYYGEN_CONTEXT Context
- );
- VOID
- YypGenerateReflexiveTransitiveClosure (
- PULONG Bitmap,
- ULONG BitCount
- );
- VOID
- YypGenerateTransitiveClosure (
- PULONG Bitmap,
- ULONG BitCount
- );
- YY_STATUS
- YypInitializeStates (
- PYYGEN_CONTEXT Context,
- PYYGEN_STATE_CONTEXT StateContext
- );
- YY_STATUS
- YypSaveReductions (
- PYYGEN_CONTEXT Context,
- PYYGEN_STATE_CONTEXT StateContext
- );
- VOID
- YypAdvanceItemSets (
- PYYGEN_CONTEXT Context,
- PYYGEN_STATE_CONTEXT StateContext
- );
- VOID
- YypAddNewStates (
- PYYGEN_CONTEXT Context,
- PYYGEN_STATE_CONTEXT StateContext
- );
- YY_STATE_INDEX
- YypGetState (
- PYYGEN_CONTEXT Context,
- PYYGEN_STATE_CONTEXT StateContext,
- YY_VALUE Symbol
- );
- PYYGEN_STATE
- YypCreateState (
- PYYGEN_CONTEXT Context,
- PYYGEN_STATE_CONTEXT StateContext,
- YY_VALUE Symbol
- );
- YY_STATUS
- YypSaveShifts (
- PYYGEN_CONTEXT Context,
- PYYGEN_STATE_CONTEXT StateContext
- );
- VOID
- YypPrintItems (
- PYYGEN_CONTEXT Context
- );
- VOID
- YypPrintDerives (
- PYYGEN_CONTEXT Context
- );
- VOID
- YypPrintEpsilonFreeFirsts (
- PYYGEN_CONTEXT Context,
- PULONG EpsilonFreeFirsts
- );
- VOID
- YypPrintFirstDerives (
- PYYGEN_CONTEXT Context
- );
- VOID
- YypPrintClosure (
- PYYGEN_CONTEXT Context,
- YY_VALUE ItemsCount
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // ------------------------------------------------------------------ Functions
- //
- YY_STATUS
- YypGenerateLr0Grammar (
- PYYGEN_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine generates an LR(0) grammar based on the description.
- Arguments:
- Context - Supplies a pointer to the generator context.
- Return Value:
- YY status.
- --*/
- {
- YY_STATUS YyStatus;
- YyStatus = YypGenerateDerives(Context);
- if (YyStatus != YyStatusSuccess) {
- goto GenerateLr0GrammarEnd;
- }
- YyStatus = YypGenerateNullable(Context);
- if (YyStatus != YyStatusSuccess) {
- goto GenerateLr0GrammarEnd;
- }
- YyStatus = YypGenerateStates(Context);
- if (YyStatus != YyStatusSuccess) {
- goto GenerateLr0GrammarEnd;
- }
- GenerateLr0GrammarEnd:
- return YyStatus;
- }
- VOID
- YypEstablishClosure (
- PYYGEN_CONTEXT Context,
- PYYGEN_STATE State
- )
- /*++
- Routine Description:
- This routine creates a closure on the itemset of the current state.
- Arguments:
- Context - Supplies a pointer to the generator context.
- State - Supplies a pointer to the state to create the closure of.
- Return Value:
- None.
- --*/
- {
- ULONG BitIndex;
- PULONG CurrentRuleSet;
- ULONG CurrentWord;
- PULONG FirstDerives;
- YY_ITEM_INDEX ItemIndex;
- PYY_ITEM_INDEX ItemsPointer;
- PYY_ITEM_INDEX Nucleus;
- ULONG NucleusCount;
- PYY_ITEM_INDEX NucleusEnd;
- YY_RULE_INDEX RuleNumber;
- PULONG RuleSetEnd;
- ULONG RuleSetSize;
- YY_VALUE StartSymbol;
- YY_VALUE Symbol;
- Nucleus = State->Items;
- NucleusCount = State->ItemsCount;
- NucleusEnd = Nucleus + NucleusCount;
- RuleSetSize = YYGEN_BITMAP_WORD_COUNT(Context->RuleCount);
- RuleSetEnd = Context->RuleSet + RuleSetSize;
- StartSymbol = Context->StartSymbol;
- CurrentRuleSet = Context->RuleSet;
- while (CurrentRuleSet < RuleSetEnd) {
- *CurrentRuleSet = 0;
- CurrentRuleSet += 1;
- }
- //
- // Loop through all the right hand sides. OR into the ruleset all of the
- // first derives from the first element if it's a non-terminal.
- //
- ItemsPointer = Nucleus;
- while (ItemsPointer < NucleusEnd) {
- Symbol = Context->Items[*ItemsPointer];
- if (Symbol >= Context->TokenCount) {
- FirstDerives = Context->FirstDerives +
- ((Symbol - StartSymbol) * RuleSetSize);
- CurrentRuleSet = Context->RuleSet;
- while (CurrentRuleSet < RuleSetEnd) {
- *CurrentRuleSet |= *FirstDerives;
- CurrentRuleSet += 1;
- FirstDerives += 1;
- }
- }
- ItemsPointer += 1;
- }
- //
- // Merge the itemsets from the rules indicated by the ruleset into the
- // nucleus, keeping them in global item array order and avoiding duplicates.
- //
- RuleNumber = 0;
- Context->ItemSetEnd = Context->ItemSet;
- ItemsPointer = Nucleus;
- CurrentRuleSet = Context->RuleSet;
- while (CurrentRuleSet < RuleSetEnd) {
- CurrentWord = *CurrentRuleSet;
- if (CurrentWord != 0) {
- for (BitIndex = 0; BitIndex < YYGEN_BITS_PER_WORD; BitIndex += 1) {
- if ((CurrentWord & (1 << BitIndex)) != 0) {
- ItemIndex = Context->Rules[RuleNumber + BitIndex].RightSide;
- while ((ItemsPointer < NucleusEnd) &&
- (*ItemsPointer < ItemIndex)) {
- *(Context->ItemSetEnd) = *ItemsPointer;
- Context->ItemSetEnd += 1;
- ItemsPointer += 1;
- }
- *(Context->ItemSetEnd) = ItemIndex;
- Context->ItemSetEnd += 1;
- while ((ItemsPointer < NucleusEnd) &&
- (*ItemsPointer == ItemIndex)) {
- ItemsPointer += 1;
- }
- }
- }
- }
- RuleNumber += YYGEN_BITS_PER_WORD;
- CurrentRuleSet += 1;
- }
- while (ItemsPointer < NucleusEnd) {
- *(Context->ItemSetEnd) = *ItemsPointer;
- Context->ItemSetEnd += 1;
- ItemsPointer += 1;
- }
- if ((Context->Flags & YYGEN_FLAG_DEBUG) != 0) {
- YypPrintClosure(Context, State->ItemsCount);
- }
- return;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
- YY_STATUS
- YypGenerateDerives (
- PYYGEN_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine generates the derives array, which is an array of unique rules
- and points to runs of rules.
- Arguments:
- Context - Supplies a pointer to the generator context.
- Return Value:
- YY status.
- --*/
- {
- PYY_VALUE Components;
- PYYGEN_RULE CurrentRule;
- PYY_RULE_INDEX Derives;
- ULONG Flags;
- YY_ITEM_INDEX ItemIndex;
- PYY_VALUE Items;
- YY_VALUE LastTerminal;
- YY_VALUE LeftSide;
- YY_RULE_INDEX RuleIndex;
- PYYGEN_RULE Rules;
- YY_STATUS YyStatus;
- YyStatus = YyStatusNoMemory;
- Derives = YypAllocate(Context->SymbolCount * sizeof(YY_RULE_INDEX));
- if (Derives == NULL) {
- goto GenerateDerivesEnd;
- }
- Context->Derives = Derives;
- //
- // There's an extra rule on the end to terminate the last run of
- // rules while iterating.
- //
- Rules = YypAllocate(sizeof(YYGEN_RULE) * (Context->RuleCount + 1));
- if (Rules == NULL) {
- YyStatus = YyStatusNoMemory;
- goto GenerateDerivesEnd;
- }
- Context->Rules = Rules;
- Items = YypAllocate(sizeof(YY_VALUE) * Context->ItemCount);
- if (Items == NULL) {
- YyStatus = YyStatusNoMemory;
- goto GenerateDerivesEnd;
- }
- Context->Items = Items;
- //
- // The first item corresponds to rule 1 and it's empty.
- //
- Items[0] = -1;
- //
- // The next three items correspond to the right hand side of the start
- // rule, which is to produce the production marked start and then EOF.
- //
- Items[1] = Context->StartSymbol;
- Context->StartSymbol = Context->TokenCount;
- Items[2] = 0;
- Items[3] = -2;
- ItemIndex = 4;
- //
- // The first rule is invalid since it cannot be negated.
- // The second rule is empty.
- // The third rule is the start rule.
- //
- CurrentRule = Rules + 2;
- CurrentRule->LeftSide = Context->StartSymbol;
- CurrentRule->RightSide = 1;
- Derives[Context->StartSymbol] = 2;
- CurrentRule += 1;
- RuleIndex = 3;
- //
- // Loop over converting productions to derives + rules.
- //
- for (LeftSide = Context->StartSymbol + 1;
- LeftSide < Context->SymbolCount;
- LeftSide += 1) {
- Components = Context->Elements[LeftSide].Components;
- Derives[LeftSide] = RuleIndex;
- assert((Components != NULL) && (*Components >= 0));
- while (*Components != 0) {
- CurrentRule->LeftSide = LeftSide;
- CurrentRule->RightSide = ItemIndex;
- LastTerminal = -1;
- while (*Components > 0) {
- Items[ItemIndex] = *Components;
- //
- // Keep track of the last terminal specified in the rule.
- //
- if (*Components < Context->TokenCount) {
- LastTerminal = *Components;
- }
- Components += 1;
- ItemIndex += 1;
- }
- Items[ItemIndex] = -RuleIndex;
- ItemIndex += 1;
- //
- // The precedence for the rule is specified in the terminator. -1
- // corresponds to precedence 0 (none). If no precedence or
- // associativity is describe for the rule, then it is taken from
- // the last terminal in the rule.
- //
- assert(*Components != 0);
- CurrentRule->Precedence = -(*Components) - 1;
- if ((CurrentRule->Precedence == 0) && (LastTerminal > 0)) {
- CurrentRule->Precedence =
- Context->Elements[LastTerminal].Precedence;
- }
- Flags = Context->Elements[LeftSide].Flags;
- if (Flags == 0) {
- Flags = Context->Elements[LastTerminal].Flags;
- }
- if (Flags != 0) {
- if ((Flags & YY_ELEMENT_LEFT_ASSOCIATIVE) != 0) {
- CurrentRule->Associativity = YyLeftAssociative;
- } else if ((Flags & YY_ELEMENT_RIGHT_ASSOCIATIVE) != 0) {
- CurrentRule->Associativity = YyRightAssociative;
- } else if ((Flags & YY_ELEMENT_NON_ASSOCIATIVE) != 0) {
- CurrentRule->Associativity = YyNonAssociative;
- }
- }
- Components += 1;
- CurrentRule += 1;
- RuleIndex += 1;
- }
- }
- assert(Rules + Context->RuleCount == CurrentRule);
- CurrentRule->LeftSide = 0;
- CurrentRule->RightSide = ItemIndex;
- if ((Context->Flags & YYGEN_FLAG_DEBUG) != 0) {
- YypPrintItems(Context);
- YypPrintDerives(Context);
- }
- YyStatus = YyStatusSuccess;
- GenerateDerivesEnd:
- return YyStatus;
- }
- YY_STATUS
- YypGenerateNullable (
- PYYGEN_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine generates the array of booleans indicating which rules are
- empty or are made up of other rules that are empty.
- Arguments:
- Context - Supplies a pointer to the generator context.
- Return Value:
- YY status.
- --*/
- {
- BOOL Empty;
- BOOL FoundOne;
- YY_ITEM_INDEX ItemIndex;
- YY_VALUE LeftSide;
- PBOOL Nullable;
- YY_RULE_INDEX RuleIndex;
- YY_ITEM_INDEX Search;
- Nullable = YypAllocate(Context->SymbolCount * sizeof(BOOL));
- if (Nullable == NULL) {
- return YyStatusNoMemory;
- }
- //
- // The idea is to find which productions are empty, then go back and mark
- // any productions that are just made up of those productions as empty also,
- // and so on, until no new empty ones are found.
- //
- do {
- FoundOne = FALSE;
- for (ItemIndex = 1; ItemIndex < Context->ItemCount; ItemIndex += 1) {
- Empty = TRUE;
- //
- // Loop over each element in the rule. If it consists of something
- // that's not nullable (including a token), then it's also not
- // nullable.
- //
- Search = Context->Items[ItemIndex];
- while (Search >= 0) {
- if (Nullable[Search] == FALSE) {
- Empty = FALSE;
- }
- ItemIndex += 1;
- Search = Context->Items[ItemIndex];
- }
- //
- // If it's empty or is only made up of other things that are
- // empty, mark it as nullable. This means everything will have
- // to be checked again.
- //
- if (Empty != FALSE) {
- RuleIndex = -Search;
- LeftSide = Context->Rules[RuleIndex].LeftSide;
- if (Nullable[LeftSide] == FALSE) {
- Nullable[LeftSide] = TRUE;
- FoundOne = TRUE;
- }
- }
- }
- } while (FoundOne != FALSE);
- if ((Context->Flags & YYGEN_FLAG_DEBUG) != 0) {
- for (ItemIndex = 0; ItemIndex < Context->SymbolCount; ItemIndex += 1) {
- if (Nullable[ItemIndex] != FALSE) {
- printf("%s is nullable\n", Context->Elements[ItemIndex].Name);
- } else {
- printf("%s is not nullable\n",
- Context->Elements[ItemIndex].Name);
- }
- }
- }
- Context->Nullable = Nullable;
- return YyStatusSuccess;
- }
- YY_STATUS
- YypGenerateStates (
- PYYGEN_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine generates the LR(0) grammar states.
- Arguments:
- Context - Supplies a pointer to the generator context.
- Return Value:
- YY status.
- --*/
- {
- UINTN AllocationSize;
- YYGEN_STATE_CONTEXT StateContext;
- YY_STATUS YyStatus;
- memset(&StateContext, 0, sizeof(YYGEN_STATE_CONTEXT));
- YyStatus = YypInitializeStateContext(Context, &StateContext);
- if (YyStatus != YyStatusSuccess) {
- goto GenerateStatesEnd;
- }
- Context->ItemSet = YypAllocate(Context->ItemCount * sizeof(YY_VALUE));
- if (Context->ItemSet == NULL) {
- YyStatus = YyStatusNoMemory;
- goto GenerateStatesEnd;
- }
- AllocationSize = YYGEN_BITMAP_WORD_COUNT(Context->RuleCount) *
- sizeof(ULONG);
- Context->RuleSet = YypAllocate(AllocationSize);
- if (Context->RuleSet == NULL) {
- YyStatus = YyStatusNoMemory;
- goto GenerateStatesEnd;
- }
- YyStatus = YypGenerateFirstDerives(Context);
- if (YyStatus != YyStatusSuccess) {
- goto GenerateStatesEnd;
- }
- YyStatus = YypInitializeStates(Context, &StateContext);
- if (YyStatus != YyStatusSuccess) {
- goto GenerateStatesEnd;
- }
- while (StateContext.CurrentState != NULL) {
- if ((Context->Flags & YYGEN_FLAG_DEBUG) != 0) {
- printf("State %d:\n", StateContext.CurrentState->Number);
- }
- YypEstablishClosure(Context, StateContext.CurrentState);
- YypSaveReductions(Context, &StateContext);
- YypAdvanceItemSets(Context, &StateContext);
- YypAddNewStates(Context, &StateContext);
- if (StateContext.ShiftCount != 0) {
- YypSaveShifts(Context, &StateContext);
- }
- StateContext.CurrentState = StateContext.CurrentState->Next;
- }
- YyStatus = YyStatusSuccess;
- GenerateStatesEnd:
- YypDestroyStateContext(Context, &StateContext);
- return YyStatus;
- }
- YY_STATUS
- YypInitializeStateContext (
- PYYGEN_CONTEXT Context,
- PYYGEN_STATE_CONTEXT StateContext
- )
- /*++
- Routine Description:
- This routine allocates arrays needed for LR(0) state generation.
- Arguments:
- Context - Supplies a pointer to the generator context.
- StateContext - Supplies a pointer to the zeroed out state context.
- Return Value:
- YY status.
- --*/
- {
- YY_VALUE Count;
- PYY_VALUE End;
- PYY_VALUE Item;
- YY_VALUE Symbol;
- PYY_VALUE SymbolCounts;
- YY_STATUS YyStatus;
- YyStatus = YyStatusNoMemory;
- SymbolCounts = YypAllocate(Context->SymbolCount * sizeof(YY_VALUE));
- if (SymbolCounts == NULL) {
- goto InitializeStateContextEnd;
- }
- //
- // Count the number of times a symbol is referenced, and the total number
- // of symbols in all the rules.
- //
- End = Context->Items + Context->ItemCount;
- Count = 0;
- Item = Context->Items;
- while (Item < End) {
- Symbol = *Item;
- if (Symbol >= 0) {
- Count += 1;
- SymbolCounts[Symbol] += 1;
- }
- Item += 1;
- }
- //
- // Allocate the kernel base and kernel item arrays.
- //
- StateContext->KernelBase =
- YypAllocate(Context->SymbolCount * sizeof(PYY_ITEM_INDEX));
- if (StateContext->KernelBase == NULL) {
- goto InitializeStateContextEnd;
- }
- StateContext->KernelEnd =
- YypAllocate(Context->SymbolCount * sizeof(PYY_ITEM_INDEX));
- if (StateContext->KernelEnd == NULL) {
- goto InitializeStateContextEnd;
- }
- StateContext->KernelItems = YypAllocate(Count * sizeof(YY_ITEM_INDEX));
- if (StateContext->KernelItems == NULL) {
- goto InitializeStateContextEnd;
- }
- //
- // Initialize the indices for ther kernel base array, knowing how large
- // each kernel base array will be but not initializing it yet.
- //
- Count = 0;
- for (Symbol = 0; Symbol < Context->SymbolCount; Symbol += 1) {
- StateContext->KernelBase[Symbol] = StateContext->KernelItems + Count;
- Count += SymbolCounts[Symbol];
- }
- StateContext->ShiftSymbol = SymbolCounts;
- SymbolCounts = NULL;
- StateContext->ShiftSet =
- YypAllocate(Context->SymbolCount * sizeof(YY_STATE_INDEX));
- if (StateContext->ShiftSet == NULL) {
- goto InitializeStateContextEnd;
- }
- StateContext->ReduceSet =
- YypAllocate((Context->SymbolCount + 1) * sizeof(YY_RULE_INDEX));
- if (StateContext->ReduceSet == NULL) {
- goto InitializeStateContextEnd;
- }
- StateContext->StateSet =
- YypAllocate(Context->ItemCount * sizeof(PYYGEN_STATE));
- if (StateContext->StateSet == NULL) {
- goto InitializeStateContextEnd;
- }
- YyStatus = YyStatusSuccess;
- InitializeStateContextEnd:
- if (SymbolCounts != NULL) {
- YypFree(SymbolCounts);
- }
- return YyStatus;
- }
- VOID
- YypDestroyStateContext (
- PYYGEN_CONTEXT Context,
- PYYGEN_STATE_CONTEXT StateContext
- )
- /*++
- Routine Description:
- This routine frees memory allocated in the given state context.
- Arguments:
- Context - Supplies a pointer to the application context.
- StateContext - Supplies a pointer to the state context.
- Return Value:
- None.
- --*/
- {
- if (StateContext->StateSet != NULL) {
- YypFree(StateContext->StateSet);
- }
- if (StateContext->ShiftSet != NULL) {
- YypFree(StateContext->ShiftSet);
- }
- if (StateContext->ReduceSet != NULL) {
- YypFree(StateContext->ReduceSet);
- }
- if (StateContext->ShiftSymbol != NULL) {
- YypFree(StateContext->ShiftSymbol);
- }
- if (StateContext->KernelBase != NULL) {
- YypFree(StateContext->KernelBase);
- }
- if (StateContext->KernelEnd != NULL) {
- YypFree(StateContext->KernelEnd);
- }
- if (StateContext->KernelItems != NULL) {
- YypFree(StateContext->KernelItems);
- }
- return;
- }
- YY_STATUS
- YypGenerateFirstDerives (
- PYYGEN_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine creates the array of FIRST bitmaps, indicating which rules are
- involved in the FIRST set of each non-terminal. The FIRST set is the set
- of terminals that can appear first in a given itemset.
- Arguments:
- Context - Supplies a pointer to the generator context.
- Return Value:
- YY status.
- --*/
- {
- ULONG BitIndex;
- ULONG CurrentWord;
- PULONG EffRow;
- PULONG EpsilonFreeFirsts;
- PULONG FirstRow;
- YY_VALUE LeftSide;
- ULONG NonTerminalSetSize;
- YY_VALUE RowIndex;
- PYYGEN_RULE Rule;
- YY_RULE_INDEX RuleIndex;
- ULONG RuleSetSize;
- YY_VALUE StartSymbol;
- YY_VALUE SymbolIndex;
- YY_STATUS YyStatus;
- EpsilonFreeFirsts = NULL;
- RuleSetSize = YYGEN_BITMAP_WORD_COUNT(Context->RuleCount);
- NonTerminalSetSize = YYGEN_BITMAP_WORD_COUNT(Context->NonTerminalCount);
- StartSymbol = Context->StartSymbol;
- YyStatus = YyStatusNoMemory;
- Context->FirstDerives =
- YypAllocate(Context->NonTerminalCount * (RuleSetSize * sizeof(ULONG)));
- if (Context->FirstDerives == NULL) {
- goto GenerateFirstSetEnd;
- }
- //
- // Get the closure of first non-terminals for each non-terminal.
- //
- EpsilonFreeFirsts = YypGenerateEpsilonFreeFirstSet(Context);
- if (EpsilonFreeFirsts == NULL) {
- goto GenerateFirstSetEnd;
- }
- //
- // Loop through each row (non-terminal) of the first set.
- //
- FirstRow = Context->FirstDerives;
- for (RowIndex = StartSymbol;
- RowIndex < Context->SymbolCount;
- RowIndex += 1) {
- //
- // Get the equivalent row in the EFF bitmap.
- //
- EffRow = EpsilonFreeFirsts +
- ((RowIndex - StartSymbol) * NonTerminalSetSize);
- BitIndex = 0;
- CurrentWord = *EffRow;
- //
- // Loop over every bit in the bitmap.
- //
- for (SymbolIndex = StartSymbol;
- SymbolIndex < Context->SymbolCount;
- SymbolIndex += 1) {
- //
- // If the bit is set in the EFF bitmap, then set the bits for all
- // of that non-terminal's rules.
- //
- if ((CurrentWord & (1 << BitIndex)) != 0) {
- RuleIndex = Context->Derives[SymbolIndex];
- assert(RuleIndex >= 0);
- Rule = &(Context->Rules[RuleIndex]);
- LeftSide = Rule->LeftSide;
- do {
- YYGEN_BITMAP_SET(FirstRow, RuleIndex);
- Rule += 1;
- RuleIndex += 1;
- } while (Rule->LeftSide == LeftSide);
- }
- BitIndex += 1;
- if (BitIndex == YYGEN_BITS_PER_WORD) {
- BitIndex = 0;
- EffRow += 1;
- CurrentWord = *EffRow;
- }
- }
- FirstRow += RuleSetSize;
- }
- if ((Context->Flags & YYGEN_FLAG_DEBUG) != 0) {
- YypPrintFirstDerives(Context);
- }
- YyStatus = YyStatusSuccess;
- GenerateFirstSetEnd:
- if (EpsilonFreeFirsts != NULL) {
- YypFree(EpsilonFreeFirsts);
- }
- return YyStatus;
- }
- PULONG
- YypGenerateEpsilonFreeFirstSet (
- PYYGEN_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine creates the grid of bits that is the Epsilon Free First set.
- This is for every non-terminal (row), the set of terminals that appear
- first in that item set. The epsilon free part means that the non-terminals
- that consist of the empty set are not included.
- Arguments:
- Context - Supplies a pointer to the generator context.
- Return Value:
- Returns a pointer to the 2 dimensional bitmap containing the Epsilon Free
- First sets.
- NULL on allocation failure.
- --*/
- {
- UINTN AllocationSize;
- PULONG EpsilonFreeFirsts;
- YY_VALUE LeftSide;
- PULONG Row;
- ULONG RowSize;
- PYYGEN_RULE Rule;
- YY_RULE_INDEX RuleIndex;
- YY_VALUE Symbol;
- YY_VALUE SymbolIndex;
- RowSize = YYGEN_BITMAP_WORD_COUNT(Context->NonTerminalCount);
- AllocationSize = Context->NonTerminalCount * RowSize * sizeof(ULONG);
- EpsilonFreeFirsts = YypAllocate(AllocationSize);
- if (EpsilonFreeFirsts == NULL) {
- return NULL;
- }
- Row = EpsilonFreeFirsts;
- //
- // Loop through all the productions.
- //
- for (SymbolIndex = Context->StartSymbol;
- SymbolIndex < Context->SymbolCount;
- SymbolIndex += 1) {
- //
- // Loop through each rule in this production.
- //
- RuleIndex = Context->Derives[SymbolIndex];
- Rule = Context->Rules + RuleIndex;
- LeftSide = Rule->LeftSide;
- do {
- //
- // If the first symbol in the right hand side is a non-terminal,
- // add it to the bitmap for this row.
- //
- Symbol = Context->Items[Rule->RightSide];
- if (Symbol >= Context->TokenCount) {
- Symbol -= Context->StartSymbol;
- YYGEN_BITMAP_SET(Row, Symbol);
- }
- Rule += 1;
- } while (Rule->LeftSide == LeftSide);
- Row += RowSize;
- }
- YypGenerateReflexiveTransitiveClosure(EpsilonFreeFirsts,
- Context->NonTerminalCount);
- if ((Context->Flags & YYGEN_FLAG_DEBUG) != 0) {
- YypPrintEpsilonFreeFirsts(Context, EpsilonFreeFirsts);
- }
- return EpsilonFreeFirsts;
- }
- VOID
- YypGenerateReflexiveTransitiveClosure (
- PULONG Bitmap,
- ULONG BitCount
- )
- /*++
- Routine Description:
- This routine generates the reflexive transitive closure of the given bitmap.
- This is just the transitive closure of the bitmap, but with each row's bit
- for itself also set.
- Arguments:
- Bitmap - Supplies a pointer to the bitmap. The reflexive transitive closure
- will be returned in this bitmap.
- BitCount - Supplies the number of bits per row (and column).
- Return Value:
- None.
- --*/
- {
- ULONG BitIndex;
- PULONG End;
- PULONG Row;
- ULONG RowSize;
- YypGenerateTransitiveClosure(Bitmap, BitCount);
- RowSize = YYGEN_BITMAP_WORD_COUNT(BitCount);
- End = Bitmap + (BitCount * RowSize);
- Row = Bitmap;
- BitIndex = 0;
- //
- // Mark the diagonal down the grid of bits to make the closure reflexive.
- //
- while (Row < End) {
- *Row |= 1 << BitIndex;
- BitIndex += 1;
- if (BitIndex == YYGEN_BITS_PER_WORD) {
- BitIndex = 0;
- Row += 1;
- }
- Row += RowSize;
- }
- return;
- }
- VOID
- YypGenerateTransitiveClosure (
- PULONG Bitmap,
- ULONG BitCount
- )
- /*++
- Routine Description:
- This routine generates the transitive closure of the given bitmap using
- Warshall's algorithm.
- Arguments:
- Bitmap - Supplies a pointer to the bitmap. The transitive closure will be
- returned in this bitmap.
- BitCount - Supplies the number of bits per row (and column).
- Return Value:
- None.
- --*/
- {
- ULONG BitIndex;
- PULONG Copy;
- PULONG CurrentColumn;
- PULONG CurrentWord;
- PULONG End;
- PULONG RowEnd;
- PULONG RowI;
- PULONG RowJ;
- ULONG RowSize;
- //
- // Warshall's algorithm for the transitive closure is basically this for
- // a grid of R[row, column]:
- // for i in 0..n:
- // for j in 0..n:
- // for k in 0..n:
- // R[j, k] |= R[j, i] && R[i, k];
- //
- RowSize = YYGEN_BITMAP_WORD_COUNT(BitCount);
- End = Bitmap + (BitCount * RowSize);
- CurrentWord = Bitmap;
- BitIndex = 0;
- RowI = Bitmap;
- //
- // Loop through one dimension of the bitmap in i. The current word and bit
- // index increment through the bits in that row.
- //
- while (RowI < End) {
- CurrentColumn = CurrentWord;
- RowJ = Bitmap;
- //
- // Loop j over every row in the bitmap.
- //
- while (RowJ < End) {
- //
- // Check to see if R[j, i] is set, and OR row I into row J if so.
- //
- if ((*CurrentColumn & (1 << BitIndex)) != 0) {
- Copy = RowI;
- RowEnd = RowJ + RowSize;
- while (RowJ < RowEnd) {
- *RowJ |= *Copy;
- RowJ += 1;
- Copy += 1;
- }
- } else {
- RowJ += RowSize;
- }
- CurrentColumn += RowSize;
- }
- BitIndex += 1;
- if (BitIndex == YYGEN_BITS_PER_WORD) {
- BitIndex = 0;
- CurrentWord += 1;
- }
- RowI += RowSize;
- }
- return;
- }
- YY_STATUS
- YypInitializeStates (
- PYYGEN_CONTEXT Context,
- PYYGEN_STATE_CONTEXT StateContext
- )
- /*++
- Routine Description:
- This routine sets up the initial states of the LR(0) state machine
- generator.
- Arguments:
- Context - Supplies a pointer to the generator context.
- StateContext - Supplies a pointer to the zeroed out state context.
- Return Value:
- YY status.
- --*/
- {
- YY_VALUE LeftSide;
- PYYGEN_RULE Rule;
- YY_RULE_INDEX RuleCount;
- YY_RULE_INDEX StartDerives;
- PYYGEN_STATE State;
- StartDerives = Context->Derives[Context->StartSymbol];
- assert(StartDerives >= 0);
- Rule = &(Context->Rules[StartDerives]);
- LeftSide = Rule->LeftSide;
- RuleCount = 0;
- do {
- Rule += 1;
- RuleCount += 1;
- } while (Rule->LeftSide == LeftSide);
- State = YypAllocate(sizeof(YYGEN_STATE) + (RuleCount * sizeof(YY_VALUE)));
- if (State == NULL) {
- return YyStatusNoMemory;
- }
- State->ItemsCount = RuleCount;
- State->Items = (PYY_VALUE)(State + 1);
- Rule = &(Context->Rules[StartDerives]);
- RuleCount = 0;
- do {
- State->Items[RuleCount] = Rule->RightSide;
- Rule += 1;
- RuleCount += 1;
- } while (Rule->LeftSide == LeftSide);
- Context->FirstState = State;
- Context->StateCount = 1;
- StateContext->LastState = State;
- StateContext->CurrentState = State;
- return YyStatusSuccess;
- }
- YY_STATUS
- YypSaveReductions (
- PYYGEN_CONTEXT Context,
- PYYGEN_STATE_CONTEXT StateContext
- )
- /*++
- Routine Description:
- This routine examines the current itemsets and converts any empty ones
- into reductions for the current state.
- Arguments:
- Context - Supplies a pointer to the generator context.
- StateContext - Supplies a pointer to the zeroed out state context.
- Return Value:
- YY status.
- --*/
- {
- UINTN AllocationSize;
- PYY_RULE_INDEX Destination;
- PYY_RULE_INDEX End;
- YY_VALUE Item;
- PYY_ITEM_INDEX ItemSet;
- YY_VALUE ReductionCount;
- PYYGEN_REDUCTIONS Reductions;
- PYY_RULE_INDEX Source;
- //
- // Loop through all the itemsets for this state. If any are currently at
- // the end, that's a reduction.
- //
- ReductionCount = 0;
- ItemSet = Context->ItemSet;
- while (ItemSet < Context->ItemSetEnd) {
- Item = Context->Items[*ItemSet];
- if (Item < 0) {
- StateContext->ReduceSet[ReductionCount] = (YY_RULE_INDEX)-Item;
- ReductionCount += 1;
- }
- ItemSet += 1;
- }
- if (ReductionCount == 0) {
- return YyStatusSuccess;
- }
- AllocationSize = sizeof(YYGEN_REDUCTIONS) +
- (ReductionCount * sizeof(YY_RULE_INDEX));
- Reductions = YypAllocate(AllocationSize);
- if (Reductions == NULL) {
- return YyStatusNoMemory;
- }
- Reductions->Number = StateContext->CurrentState->Number;
- Reductions->Count = ReductionCount;
- Reductions->Rules = (PYY_RULE_INDEX)(Reductions + 1);
- Source = StateContext->ReduceSet;
- End = Source + ReductionCount;
- Destination = Reductions->Rules;
- while (Source < End) {
- *Destination = *Source;
- Destination += 1;
- Source += 1;
- }
- //
- // Add it to the end of the list.
- //
- if (Context->LastReduction != NULL) {
- Context->LastReduction->Next = Reductions;
- Context->LastReduction = Reductions;
- } else {
- Context->FirstReduction = Reductions;
- Context->LastReduction = Reductions;
- }
- return YyStatusSuccess;
- }
- VOID
- YypAdvanceItemSets (
- PYYGEN_CONTEXT Context,
- PYYGEN_STATE_CONTEXT StateContext
- )
- /*++
- Routine Description:
- This routine creates the set of possible shift symbols and for each symbol
- determines the new item sets (kernel) in that next state.
- Arguments:
- Context - Supplies a pointer to the generator context.
- StateContext - Supplies a pointer to the zeroed out state context.
- Return Value:
- YY status.
- --*/
- {
- PYY_ITEM_INDEX CurrentKernel;
- YY_ITEM_INDEX ItemIndex;
- PYY_ITEM_INDEX ItemSet;
- YY_VALUE ShiftCount;
- YY_VALUE Symbol;
- for (Symbol = 0; Symbol < Context->SymbolCount; Symbol += 1) {
- StateContext->KernelEnd[Symbol] = NULL;
- }
- //
- // Loop across all the right hand sides for this state.
- //
- ShiftCount = 0;
- ItemSet = Context->ItemSet;
- while (ItemSet < Context->ItemSetEnd) {
- ItemIndex = *ItemSet;
- ItemSet += 1;
- //
- // If the first symbol in this right hand side is not the end and is
- // not EOF, then add it as a next item set for this symbol.
- //
- Symbol = Context->Items[ItemIndex];
- if (Symbol > 0) {
- CurrentKernel = StateContext->KernelEnd[Symbol];
- //
- // If this symbol has never been added before, then it's a new
- // shift possibility out of the current state.
- //
- if (CurrentKernel == NULL) {
- StateContext->ShiftSymbol[ShiftCount] = Symbol;
- ShiftCount += 1;
- CurrentKernel = StateContext->KernelBase[Symbol];
- }
- *CurrentKernel = ItemIndex + 1;
- CurrentKernel += 1;
- StateContext->KernelEnd[Symbol] = CurrentKernel;
- }
- }
- StateContext->ShiftCount = ShiftCount;
- return;
- }
- VOID
- YypAddNewStates (
- PYYGEN_CONTEXT Context,
- PYYGEN_STATE_CONTEXT StateContext
- )
- /*++
- Routine Description:
- This routine adds the new states spun out from advancing the item sets on
- the current state.
- Arguments:
- Context - Supplies a pointer to the generator context.
- StateContext - Supplies a pointer to the zeroed out state context.
- Return Value:
- YY status.
- --*/
- {
- YY_VALUE Search;
- YY_VALUE ShiftIndex;
- PYY_VALUE ShiftSymbol;
- YY_VALUE Symbol;
- ShiftSymbol = StateContext->ShiftSymbol;
- //
- // Sort the shift symbols.
- //
- for (ShiftIndex = 1;
- ShiftIndex < StateContext->ShiftCount;
- ShiftIndex += 1) {
- Symbol = ShiftSymbol[ShiftIndex];
- Search = ShiftIndex;
- while ((Search > 0) && (ShiftSymbol[Search - 1] > Symbol)) {
- ShiftSymbol[Search] = ShiftSymbol[Search - 1];
- Search -= 1;
- }
- ShiftSymbol[Search] = Symbol;
- }
- //
- // Find or add states for all new shift possibilities.
- //
- for (ShiftIndex = 0;
- ShiftIndex < StateContext->ShiftCount;
- ShiftIndex += 1) {
- Symbol = ShiftSymbol[ShiftIndex];
- StateContext->ShiftSet[ShiftIndex] =
- YypGetState(Context, StateContext, Symbol);
- if ((Context->Flags & YYGEN_FLAG_DEBUG) != 0) {
- printf("State %d -> %d via %s\n",
- StateContext->CurrentState->Number,
- StateContext->ShiftSet[ShiftIndex],
- Context->Elements[Symbol].Name);
- }
- }
- return;
- }
- YY_STATE_INDEX
- YypGetState (
- PYYGEN_CONTEXT Context,
- PYYGEN_STATE_CONTEXT StateContext,
- YY_VALUE Symbol
- )
- /*++
- Routine Description:
- This routine finds or creates a state based on the incoming shift symbol
- and the item sets in that state.
- Arguments:
- Context - Supplies a pointer to the generator context.
- StateContext - Supplies a pointer to the zeroed out state context.
- Symbol - Supplies a pointer to the shift symbol to get or create the
- state for.
- Return Value:
- returns the state number for the new state.
- --*/
- {
- PYY_ITEM_INDEX CurrentKernel;
- BOOL Found;
- ULONG ItemSetCount;
- PYY_ITEM_INDEX KernelEnd;
- YY_ITEM_INDEX Key;
- PYYGEN_STATE State;
- PYY_ITEM_INDEX StateItemSet;
- CurrentKernel = StateContext->KernelBase[Symbol];
- KernelEnd = StateContext->KernelEnd[Symbol];
- assert(KernelEnd != NULL);
- ItemSetCount = KernelEnd - CurrentKernel;
- assert(ItemSetCount != 0);
- //
- // The hash table of states is keyed off of the item index of the first
- // item set in the state.
- //
- Key = *CurrentKernel;
- State = StateContext->StateSet[Key];
- if (State != NULL) {
- Found = FALSE;
- do {
- //
- // Perform a quick check against the item count. If it matches, do
- // a deeper check to see if the states contain the same item sets.
- //
- if (State->ItemsCount == ItemSetCount) {
- Found = TRUE;
- CurrentKernel = StateContext->KernelBase[Symbol];
- StateItemSet = State->Items;
- while (CurrentKernel < KernelEnd) {
- if (*CurrentKernel != *StateItemSet) {
- Found = FALSE;
- break;
- }
- CurrentKernel += 1;
- StateItemSet += 1;
- }
- }
- //
- // If this state wasn't it, get the next state in the hash bucket
- // by following the link. If there are no more links, then add
- // this as a new state.
- //
- if (Found == FALSE) {
- if (State->Link != NULL) {
- State = State->Link;
- } else {
- State = YypCreateState(Context, StateContext, Symbol);
- if (State == NULL) {
- return -1;
- }
- State->Link = State;
- Found = TRUE;
- }
- }
- } while (Found == FALSE);
- } else {
- State = YypCreateState(Context, StateContext, Symbol);
- if (State == NULL) {
- return -1;
- }
- StateContext->StateSet[Key] = State;
- }
- return State->Number;
- }
- PYYGEN_STATE
- YypCreateState (
- PYYGEN_CONTEXT Context,
- PYYGEN_STATE_CONTEXT StateContext,
- YY_VALUE Symbol
- )
- /*++
- Routine Description:
- This routine creates a state for the current item set.
- Arguments:
- Context - Supplies a pointer to the generator context.
- StateContext - Supplies a pointer to the zeroed out state context.
- Symbol - Supplies a pointer to the shift symbol to create the state for.
- Return Value:
- returns a pointer to the newly created state on success.
- NULL on allocation failure.
- --*/
- {
- ULONG Count;
- PYY_ITEM_INDEX Destination;
- PYY_ITEM_INDEX End;
- PYY_ITEM_INDEX ItemSet;
- PYYGEN_STATE State;
- if (Context->StateCount >= YY_MAX_STATES) {
- return NULL;
- }
- ItemSet = StateContext->KernelBase[Symbol];
- End = StateContext->KernelEnd[Symbol];
- Count = End - ItemSet;
- State = YypAllocate(sizeof(YYGEN_STATE) + (Count * sizeof(YY_ITEM_INDEX)));
- if (State == NULL) {
- return NULL;
- }
- State->Items = (PYY_ITEM_INDEX)(State + 1);
- State->AccessingSymbol = Symbol;
- State->Number = Context->StateCount;
- Context->StateCount += 1;
- State->ItemsCount = Count;
- Destination = State->Items;
- while (ItemSet < End) {
- *Destination = *ItemSet;
- ItemSet += 1;
- Destination += 1;
- }
- StateContext->LastState->Next = State;
- StateContext->LastState = State;
- return State;
- }
- YY_STATUS
- YypSaveShifts (
- PYYGEN_CONTEXT Context,
- PYYGEN_STATE_CONTEXT StateContext
- )
- /*++
- Routine Description:
- This routine saves the shift symbols for the current state.
- Arguments:
- Context - Supplies a pointer to the generator context.
- StateContext - Supplies a pointer to the zeroed out state context.
- Return Value:
- YY Status code.
- --*/
- {
- ULONG AllocationSize;
- PYY_STATE_INDEX Destination;
- PYY_STATE_INDEX End;
- PYYGEN_SHIFTS Shifts;
- PYY_STATE_INDEX Source;
- AllocationSize = sizeof(YYGEN_SHIFTS) +
- (StateContext->ShiftCount * sizeof(YY_VALUE));
- Shifts = YypAllocate(AllocationSize);
- if (Shifts == NULL) {
- return YyStatusNoMemory;
- }
- Shifts->States = (PYY_STATE_INDEX)(Shifts + 1);
- Shifts->Number = StateContext->CurrentState->Number;
- Shifts->Count = StateContext->ShiftCount;
- Source = StateContext->ShiftSet;
- End = Source + StateContext->ShiftCount;
- Destination = Shifts->States;
- while (Source < End) {
- *Destination = *Source;
- Destination += 1;
- Source += 1;
- }
- if (Context->LastShift != NULL) {
- Context->LastShift->Next = Shifts;
- } else {
- Context->FirstShift = Shifts;
- }
- Context->LastShift = Shifts;
- return YyStatusSuccess;
- }
- VOID
- YypPrintItems (
- PYYGEN_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine prints the items array.
- Arguments:
- Context - Supplies a pointer to the generator context.
- Return Value:
- None.
- --*/
- {
- YY_ITEM_INDEX Index;
- YY_VALUE Value;
- printf("\nItems:\n");
- for (Index = 0; Index < Context->ItemCount; Index += 1) {
- Value = Context->Items[Index];
- if (Value >= 0) {
- printf(" %d: %s\n", Index, Context->Elements[Value].Name);
- } else {
- printf(" %d: Rule %d (%s)\n",
- Index,
- -Value,
- Context->Elements[Context->Rules[-Value].LeftSide].Name);
- }
- }
- return;
- }
- VOID
- YypPrintDerives (
- PYYGEN_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine prints the derives to standard out for debugging purposes.
- Arguments:
- Context - Supplies a pointer to the generator context.
- Return Value:
- None.
- --*/
- {
- YY_VALUE Index;
- YY_VALUE LeftSide;
- PYYGEN_RULE Rule;
- YY_RULE_INDEX RuleIndex;
- printf("\nDerives:\n");
- for (Index = Context->StartSymbol;
- Index < Context->SymbolCount;
- Index += 1) {
- printf("%s derives ", Context->Elements[Index].Name);
- RuleIndex = Context->Derives[Index];
- Rule = &(Context->Rules[RuleIndex]);
- LeftSide = Rule->LeftSide;
- while (Rule->LeftSide == LeftSide) {
- printf(" %d", RuleIndex);
- RuleIndex += 1;
- Rule += 1;
- }
- printf("\n");
- }
- printf("\n");
- return;
- }
- VOID
- YypPrintEpsilonFreeFirsts (
- PYYGEN_CONTEXT Context,
- PULONG EpsilonFreeFirsts
- )
- /*++
- Routine Description:
- This routine prints the set of epsilon-free FIRSTs.
- Arguments:
- Context - Supplies a pointer to the generator context.
- EpsilonFreeFirsts - Supplies a pointer to the array of bitmaps that are the
- Epsilon-Free firsts.
- Return Value:
- None.
- --*/
- {
- ULONG Bit;
- YY_VALUE BitIndex;
- YY_VALUE Index;
- PULONG Row;
- ULONG RowSize;
- ULONG Word;
- RowSize = YYGEN_BITMAP_WORD_COUNT(Context->NonTerminalCount);
- printf("\nEpsilon Free Firsts:\n");
- for (Index = Context->StartSymbol;
- Index < Context->SymbolCount;
- Index += 1) {
- printf("\n%s", Context->Elements[Index].Name);
- Row = EpsilonFreeFirsts + ((Index - Context->StartSymbol) * RowSize);
- Word = *Row;
- Bit = 0;
- for (BitIndex = 0;
- BitIndex < Context->NonTerminalCount;
- BitIndex += 1) {
- if ((Word & (1 << Bit)) != 0) {
- printf(" %s",
- Context->Elements[Context->StartSymbol + BitIndex].Name);
- }
- Bit += 1;
- if (Bit == YYGEN_BITS_PER_WORD) {
- Bit = 0;
- Row += 1;
- Word = *Row;
- }
- }
- }
- return;
- }
- VOID
- YypPrintFirstDerives (
- PYYGEN_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine prints the set of first derives.
- Arguments:
- Context - Supplies a pointer to the generator context.
- Return Value:
- None.
- --*/
- {
- ULONG Bit;
- YY_VALUE Index;
- PULONG Row;
- ULONG RowSize;
- YY_RULE_INDEX RuleIndex;
- YY_VALUE StartSymbol;
- ULONG Word;
- RowSize = YYGEN_BITMAP_WORD_COUNT(Context->RuleCount);
- StartSymbol = Context->StartSymbol;
- printf("\n\nFirst Derives:\n");
- for (Index = StartSymbol; Index < Context->SymbolCount; Index += 1) {
- printf("\n %s derives\n", Context->Elements[Index].Name);
- Row = Context->FirstDerives + ((Index - StartSymbol) * RowSize);
- Bit = 0;
- Word = *Row;
- for (RuleIndex = 0; RuleIndex <= Context->RuleCount; RuleIndex += 1) {
- if ((Word & (1 << Bit)) != 0) {
- printf(" %d\n", RuleIndex);
- }
- Bit += 1;
- if (Bit == YYGEN_BITS_PER_WORD) {
- Bit = 0;
- Row += 1;
- Word = *Row;
- }
- }
- }
- return;
- }
- VOID
- YypPrintClosure (
- PYYGEN_CONTEXT Context,
- YY_VALUE ItemsCount
- )
- /*++
- Routine Description:
- This routine prints the set of first derives.
- Arguments:
- Context - Supplies a pointer to the generator context.
- ItemsCount - Supplies the number of item sets in the state.
- Return Value:
- None.
- --*/
- {
- PYY_ITEM_INDEX ItemSet;
- printf("\nn = %d\n", ItemsCount);
- ItemSet = Context->ItemSet;
- while (ItemSet < Context->ItemSetEnd) {
- printf(" %d\n", *ItemSet);
- ItemSet += 1;
- }
- return;
- }
|