Skip to content

Commit 46c19e9

Browse files
author
d00mas
committed
fixed bug that happens when the tmpImage was overflowed. Now tmpImage (in DFA.cpp) is a wstring.
1 parent 1d56b42 commit 46c19e9

File tree

10 files changed

+59
-51
lines changed

10 files changed

+59
-51
lines changed

examples/simple/simple.grm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ StringLiteral = ''{String Ch}*''
1313
NumberLiteral = {Digit}+('.'{Digit}+)?
1414

1515
<Statements> ::= <Statement> <Statements>
16-
| <Statement>
16+
|
1717

1818
<Statement> ::= display <Expression>
1919
| display <Expression> read ID
@@ -38,6 +38,7 @@ NumberLiteral = {Digit}+('.'{Digit}+)?
3838
<Mult Exp> ::= <Mult Exp> '*' <Negate Exp>
3939
| <Mult Exp> '/' <Negate Exp>
4040
| <Negate Exp>
41+
4142

4243
<Negate Exp> ::= '-' <Value>
4344
| <Value>

include/CharacterSetTable.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@
2121
#include "wchar.h"
2222
#include "misc.h"
2323

24-
2524

2625
class CharacterSetTable {
2726
public:
2827
integer nbrEntries;
2928
wchar_t **characters;
3029

31-
CharacterSetTable (integer nbrEntries);
30+
CharacterSetTable (integer nbrEntries);
3231
~CharacterSetTable ();
3332

3433
};

include/DFA.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@
4343
~DFA ();
4444

4545
bool scan (wchar_t *text);
46-
bool scan (char *text);
46+
bool scan (char *text);
4747

4848
vector <Token*> &getTokens ();
4949

5050
ErrorTable *getErrors();
5151

5252
private:
53-
const DFAStateTable *stateTable;
54-
const SymbolTable *symbolTable;
55-
const CharacterSetTable *characterSetTable;
53+
const DFAStateTable *stateTable;
54+
const SymbolTable *symbolTable;
55+
const CharacterSetTable *characterSetTable;
5656
integer startState;
5757
bool caseSensitive;
5858

include/DFAStateTable.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040

4141
DFAStateTable (integer nbrEntries);
4242
~DFAStateTable ();
43-
4443
};
4544

4645
#endif

include/LALR.h

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@
3030
#include "Terminal.h"
3131
#include "NonTerminal.h"
3232

33-
/*
34-
#define REDUCTION_OK 0
35-
#define REDUCTION_ERROR 1
36-
#define REDUCTION_TEXT_ACCEPTED 2
37-
#define REDUCTION_SIMPLYFIED 3
38-
*/
39-
4033
#if defined (WIN32) && defined (_USRDLL)
4134
class __declspec(dllexport) LALR;
4235
#endif
@@ -55,15 +48,15 @@ using namespace std;
5548
const RuleTable *ruleTable, integer startState);
5649
~LALR ();
5750

58-
/*!
51+
/*!
5952
setup the parsing engine.
6053
*/
6154
void init (const vector <Token*> &tokens);
6255

6356
/*
64-
parse just until ONE reduction is performed
57+
parses just until ONE reduction is performed
6558
*/
66-
Symbol *nextReduction (bool trimReduction, bool reportOnlyOneError);
59+
Symbol *nextReduction (bool trimReductions, bool reportOnlyOneError);
6760

6861
/*!
6962
Gets the result constant for the last reduction attempt.
@@ -78,9 +71,7 @@ using namespace std;
7871
*/
7972
Symbol *parse (const vector <Token*> &tokens, bool trimReductions, bool reportOnlyOneError);
8073

81-
// void symplifyParseTree (Reduction *reduction);
82-
83-
void printRule (integer rule);
74+
void printRule (integer rule);
8475
void printReductionTree (Symbol *reduction, int deep);
8576

8677
ErrorTable *getErrors ();

include/SymbolTable.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020
#include "misc.h"
2121
#include <wchar.h>
2222

23-
2423
enum SymbolType {TERMINAL = 1, NON_TERMINAL = 0};
2524

26-
2725
typedef struct SymbolStruct {
2826
wchar_t *name;
2927
SymbolType kind;
@@ -37,7 +35,6 @@
3735

3836
SymbolTable (integer nbrEntries);
3937
~SymbolTable ();
40-
4138
};
4239

4340

src/ASTCreator.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,34 @@
5959
rdcChildren = ((NonTerminal*) reduction)->children;
6060
}
6161

62+
/*
63+
If the symbol constants are included it is possible to use a switch-case
64+
instead.
65+
switch (reduction->symbolIndex) {
66+
67+
// <If Statement> ::= if <Expression> then <StatementList> end
68+
case RULE_IF_THEN_END_STATEMENT:
69+
CREATE_NODE (IfStatement, ifStmt);
70+
71+
ifStmt->addChild (getASTNode(rdcChildren[1], ifStmt));
72+
ifStmt->addChild (getASTNode(rdcChildren[3], ifStmt));
73+
74+
return ifStmt;
75+
76+
// <If Statement> ::= if <Expression> then <StatementList> else <StatementList> end
77+
case RULE_IF_THEN_END_ELSE_STATEMENT:
78+
CREATE_NODE (IfStatement, ifStatement);
79+
80+
ifStmt->addChild (getASTNode(rdcChildren[1], ifStmt));
81+
ifStmt->addChild (getASTNode(rdcChildren[3], ifStmt));
82+
ifStmt->addChild (getASTNode(rdcChildren[5], ifStmt));
83+
return ifStmt;
84+
default: return searchEquivNode( rdcChildren, parent);
85+
}
86+
87+
*/
88+
89+
6290
/*
6391
Next is a colection of if statements that checks for the
6492
equivalency between nodes in the reduction tree and in the

src/CGTFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262

6363
cgtStream.open (filename, ifstream::in | ifstream::binary);
6464

65-
if (cgtStream.bad()) {
65+
if (((void*) cgtStream) == NULL) {
6666
return false;
6767
}
6868

src/DFA.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858
integer currentLine = 1, currentCol = 1, tokenBeginCol = 1;
5959

6060
Token *t = NULL;
61-
wchar_t tmpImage[64];
61+
62+
wstring tmpImage;
6263
wchar_t *tokenSymbol, *tokenImage;
6364
wchar_t currentChar;
6465
integer currentState = startState;
@@ -85,8 +86,9 @@
8586
errorTab->addError (ERROR_SCAN, END_COMMENT_NOT_FOUND,
8687
NULL, currentLine, tokenBeginCol);
8788
currentState = startState;
88-
imgIndex = 0;
89-
return false;
89+
90+
tmpImage.clear();
91+
return false;
9092
} else {
9193
run = false;
9294
}
@@ -96,7 +98,7 @@
9698
currentCol = 1;
9799
if (commentLine) {
98100
commentLine = false;
99-
// i++;
101+
100102
if (text[i] == 0) {
101103
break;
102104
}
@@ -111,8 +113,7 @@
111113

112114
if (!commentLine) {
113115

114-
tmpImage[imgIndex] = text[i];
115-
imgIndex++;
116+
tmpImage.append (1,text[i]);
116117

117118
// Check which is the next state
118119
integer e;
@@ -159,12 +160,12 @@
159160
// Terminal symbol
160161
case 1 :
161162
if (!commentBlock) {
162-
tmpImage[imgIndex-1] = 0;
163+
// tmpImage[imgIndex-1] = 0;
163164
tokenSymbol = new wchar_t [wcslen(symbolTable->symbols[index2].name)+1];
164-
tokenImage = new wchar_t [wcslen (tmpImage)+1];
165+
tokenImage = new wchar_t [tmpImage.length()+1];
165166

166167
wcscpy (tokenSymbol, symbolTable->symbols[index2].name);
167-
wcscpy (tokenImage, tmpImage);
168+
wcscpy (tokenImage, tmpImage.c_str());
168169

169170
t = new Token();
170171
t->symbol = tokenSymbol;
@@ -206,14 +207,16 @@
206207
break;
207208
}
208209
currentState = startState;
209-
imgIndex = 0;
210-
} else {
210+
211+
tmpImage.clear();
212+
} else {
211213
if (!commentBlock) {
212214
// Set error and return
213215
errorTab->addError (ERROR_SCAN, UNKNOWN_TOKEN, NULL, currentLine, tokenBeginCol);
214216
currentState = startState;
215-
imgIndex = 0;
216-
return false;
217+
218+
tmpImage.clear();
219+
return false;
217220
}
218221
tokenBeginCol = currentCol;
219222
}

src/LALR.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@
8787
8888
/return next reduction or NULL if error or test accepted
8989
90-
/sa getResult (), buildParseTree ()
90+
/sa getResult (), parse ()
9191
*/
9292

93-
Symbol *LALR::nextReduction (bool trimReduction, bool reportOnlyOneError) {
93+
Symbol *LALR::nextReduction (bool trimReductions, bool reportOnlyOneError) {
9494
Action *actObj;
9595
Token *tok;
9696
NonTerminal *newNonTerminal;
@@ -202,7 +202,7 @@
202202
// User can decide to simplify this by enabling the trimming
203203
if ((ruleTable->rules[target].symbols.size() == 1) &&
204204
(symbolTable->symbols[ruleTable->rules[target].symbols[0]].kind ==
205-
NON_TERMINAL) && trimReduction) {
205+
NON_TERMINAL) && trimReductions) {
206206
trim = true;
207207
newNonTerminal->trimmed = true;
208208
} else {
@@ -264,16 +264,6 @@
264264
return NULL;
265265
}
266266

267-
/*
268-
if (trim) {
269-
reductionResult = REDUCTION_SIMPLIFIED;
270-
// return NULL;
271-
return newNonTerminal;
272-
} else {
273-
reductionResult = REDUCTION_OK;
274-
return newNonTerminal;
275-
}
276-
*/
277267
return newNonTerminal;
278268
break;
279269

0 commit comments

Comments
 (0)