Repository navigation
Expand file tree
/
Copy pathparser.c
More file actions
189 lines (163 loc) · 5.67 KB
/
Copy pathparser.c
File metadata and controls
189 lines (163 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* @file parser.c
* @author Antonín Jarolím (xjarol06@stud.fit.vutbr.cz)
* @author Jakub Vlk (xvlkja07@stud.fit.vutbr.cz)
* @brief Parser
* Implementation IFJ22 compiler
*/
#include "parser.h"
void gStackPushStackToStack(genericStack *original, genericStack *toEmpty) {
while (gStackTop(toEmpty) != NULL) {
gStackPush(original, gStackPop(toEmpty));
}
}
///
/// \param memory
/// \param stackLength
/// \return Returns 0 if NULL inserted otherwise returns 1
int updateStackViewMember(ParserMemory *memory, int stackLength) {
PSAStackMember *top = (PSAStackMember *) gStackTop(memory->PSAStack);
if (top == NULL) {
memory->stackView[stackLength] = NULL;
return 0;
}
memory->stackView[stackLength] = getStringPSAMember(*top);
return 1;
}
void updateStackView(ParserMemory *memory) {
genericStack * tmpStack = gStackInit();
for (int stackLength = 0; stackLength < MAX_STACK_VIEWABLE; ++stackLength) {
if (updateStackViewMember(memory, stackLength) == 0)
break;
gStackPush(tmpStack, gStackPop(memory->PSAStack));
}
gStackPushStackToStack(memory->PSAStack, tmpStack);
free(tmpStack);
}
void PSAStackInit(ParserMemory *mem) {
mem->PSAStack = gStackInit();
gStackPush(mem->PSAStack, createPSAStackMember(0, endOfFile));
gStackPush(mem->PSAStack, createPSAStackMember(ProgramBody, nonTerminal));
updateStackView(mem);
}
void stackViewInit(ParserMemory *mem) {
for (int i = 0; i < MAX_STACK_VIEWABLE; ++i) {
mem->stackView[i] = NULL;
}
}
ParserMemory *initializeMemory() {
make_var(mem, ParserMemory*, sizeof(ParserMemory));
PSAStackInit(mem);
stackViewInit(mem);
createLLTable();
semanticActionsInit();
mem->parseBoolExpression = false;
return mem;
}
void exitUnexpectedEnd(token_t token) {
pErrSyntaxExit(token.type, token.rowNumber, token.rowPosNumber,
"Unexpected end of program.\n");
}
void exitNoRule(token_t token, nonTerminalType topOfTheStack) {
pErrSyntaxExit(token.type, token.rowNumber, token.rowPosNumber,
"No rule found.\nOn top of stack is: %s.\n", getNonTerminalName(topOfTheStack));
}
void exitWrongToken(token_t actualToken, lexType expectedLexType) {
pErrSyntaxExit(actualToken.type, actualToken.rowNumber, actualToken.rowPosNumber,
"Expected token is: '%s'\nInstead got token: ",
getTerminalName(expectedLexType));
}
void gStackPushReversed(genericStack *stack, PSAStackMember **rule) {
genericStack *tmpStack = gStackInit();
while (*rule != NULL) {
gStackPush(tmpStack, *rule++);
}
gStackPushStackToStack(stack, tmpStack);
free(tmpStack);
}
PSAStackMember *getTopStack(ParserMemory *memory) {
PSAStackMember *m = (PSAStackMember *) gStackTop(memory->PSAStack);
updateStackView(memory);
return m;
}
bool expressionParsing(PSAStackMember *topOfStack, bool parseBoolExpression) {
if (topOfStack->data == (int) Exp) {
ungetToken(stdin);
expAnal(parseBoolExpression);
ungetToken(stdin);
return 1;
}
return 0;
}
rule *findRule(token_t lastToken, PSAStackMember topOfStack) {
rule **newRule = (getLLMember((nonTerminalType) topOfStack.data, lastToken.type))->rules;
if (newRule == NULL)
exitNoRule(lastToken, (nonTerminalType) topOfStack.data);
rule *firstRule = *newRule;
if (firstRule->from == Command && lastToken.type == (int) identifierVar)
;
// todo: solve LL1 problem
return firstRule;
}
void deriveNonTerminal(ParserMemory *memory, const PSAStackMember *topOfStack, token_t lastToken) {
rule *newRule = findRule(lastToken, *topOfStack);
gStackPop(memory->PSAStack);
if (newRule->epsRule == false) {
gStackPushReversed(memory->PSAStack, newRule->to);
}
semanticActionInfo info;
info.lastToken = lastToken;
callSemanticAction(newRule, info);
}
void checkTopAndLastMatch(const PSAStackMember *topOfStack, token_t *lastToken) {
if ((*lastToken).type != topOfStack->data) {
exitWrongToken((*lastToken), (lexType) topOfStack->data);
}
}
bool parserEnding(token_t lastToken) {
if (lastToken.type == ending)
return 1;
exitUnexpectedEnd(lastToken);
return 0;
}
void setBoolParsing(ParserMemory *parserMemory, lexType type) {
if (type == curlyBraceLeft)
parserMemory->parseBoolExpression = false;
if (type == ifKey || type == whileKey)
parserMemory->parseBoolExpression = true;
}
int parser() {
ParserMemory *memory = initializeMemory();
bool success = false;
PSAStackMember *topOfStack;
token_t lastToken;
lastToken = getToken(stdin);
while (success == false) {
topOfStack = getTopStack(memory);
switch (topOfStack->type) {
case endOfFile:
success = parserEnding(lastToken);
break;
case terminal:
checkTopAndLastMatch(topOfStack, &lastToken);
if(lastToken.type == semicolon)
SA_EndOfCommand();
if(lastToken.type == curlyBraceRight)
SA_EndOfBlock();
setBoolParsing(memory, lastToken.type);
gStackPop(memory->PSAStack);
lastToken = getToken(stdin);
break;
case nonTerminal:;
if (expressionParsing(topOfStack, memory->parseBoolExpression)) {
gStackPop(memory->PSAStack);
lastToken = getToken(stdin);
continue;
}
deriveNonTerminal(memory, topOfStack, lastToken);
break;
}
}
SA_EndOfParsing();
return 0;
}